Skip to content

Commit

Permalink
Grant access to StringEscapeUtils.escapeXml10 and StringUtils.capitalize
Browse files Browse the repository at this point in the history
This closes #690
This closes #710

Co-authored-by: Beatrice Peptenaru <[email protected]>
  • Loading branch information
kwin and Beatrice Peptenaru committed May 8, 2024
1 parent ba3c803 commit 4594184
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import jakarta.el.VariableMapper;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.el.ExpressionFactoryImpl;

Expand Down Expand Up @@ -119,6 +120,7 @@ public ElFunctionMapper() {

StringUtils.class.getMethod("upperCase", new Class<?>[] { String.class }),
StringUtils.class.getMethod("lowerCase", new Class<?>[] { String.class }),
StringUtils.class.getMethod("capitalize", new Class<?>[] { String.class }),
StringUtils.class.getMethod("substringAfter", new Class<?>[] { String.class, String.class }),
StringUtils.class.getMethod("substringBefore", new Class<?>[] { String.class, String.class }),
StringUtils.class.getMethod("substringAfterLast", new Class<?>[] { String.class, String.class }),
Expand All @@ -129,6 +131,7 @@ public ElFunctionMapper() {
StringUtils.class.getMethod("replace", new Class<?>[] { String.class, String.class, String.class }),
StringUtils.class.getMethod("length", new Class<?>[] { CharSequence.class }),
StringUtils.class.getMethod("defaultIfEmpty", new Class<?>[] { CharSequence.class, CharSequence.class }),
StringEscapeUtils.class.getMethod("escapeXml10", new Class<?>[] { String.class }),

YamlMacroElEvaluator.ElFunctionMapper.class.getMethod("containsItem", new Class<?>[] { List.class, String.class }),
YamlMacroElEvaluator.ElFunctionMapper.class.getMethod("containsAllItems", new Class<?>[] { List.class, List.class }),
Expand All @@ -141,7 +144,7 @@ public ElFunctionMapper() {
}

} catch (NoSuchMethodException e) {
throw new IllegalStateException("Class StringUtils/ArrayUtils is missing expected methods", e);
throw new IllegalStateException("Class StringUtils/StringEscapeUtils/ArrayUtils is missing expected methods", e);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package biz.netcentric.cq.tools.actool.configreader;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.Collections;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import jakarta.el.ELException;

class YamlMacroElEvaluatorTest {
private YamlMacroElEvaluator elEvaluator;

@BeforeEach
public void setUp() {
elEvaluator = new YamlMacroElEvaluator();
}

@Test
void testFunctions() {
assertEquals("bread&amp;butter", evaluateSimpleExpression("escapeXml10(\"bread&butter\")"));
assertEquals("Test", evaluateSimpleExpression("capitalize(\"test\")"));
assertEquals("item1,item2", evaluateSimpleExpression("join(var1, \",\")", Collections.singletonMap("var1", new Object[] {"item1", "item2"})));
}

@Test
void testNonExistingFunction() {
assertThrows(ELException.class, () -> evaluateSimpleExpression("invalid(\"test\")"));
}

@Test
void testSyntaxErrpr() {
assertThrows(ELException.class, () -> evaluateSimpleExpression("invalid(\"test\""));
}

private Object evaluateSimpleExpression(String expression) {
return evaluateSimpleExpression(expression, Collections.emptyMap());
}

private Object evaluateSimpleExpression(String expression, Map<String, Object> variables) {
return elEvaluator.evaluateEl("${" + expression + "}", Object.class, variables);
}
}
2 changes: 2 additions & 0 deletions docs/AdvancedFeatures.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Function Signature | Description
`subarray(String array, startIndexInclusive,endIndexExclusive)`| [`ArrayUtils.subarray(...)`](https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/ArrayUtils.html#subarray-T:A-int-int-)
`upperCase(String str)`|[`StringUtils.upperCase(...)`](https://commons.apache.org/proper/commons-lang/javadocs/api-3.3/org/apache/commons/lang3/StringUtils.html#upperCase(java.lang.String))
`lowerCase(String str)`|[`StringUtils.lowerCase(...)`](https://commons.apache.org/proper/commons-lang/javadocs/api-3.3/org/apache/commons/lang3/StringUtils.html#lowerCase(java.lang.String))
`capitalize(String str)`|[`StringUtils.capitalize(...)`](https://commons.apache.org/proper/commons-lang/javadocs/api-3.3/org/apache/commons/lang3/StringUtils.html#capitalize(java.lang.String))
`replace(String text, String searchString, String replacement)`|[`StringUtils.replace(...)`](https://commons.apache.org/proper/commons-lang/javadocs/api-3.3/org/apache/commons/lang3/StringUtils.html#replace(java.lang.String,%20java.lang.String,%20java.lang.String))
`substringAfter(String str, String separator)`|[`StringUtils.substringAfter(...)`](https://commons.apache.org/proper/commons-lang/javadocs/api-3.3/org/apache/commons/lang3/StringUtils.html#substringAfter(java.lang.String,%20java.lang.String))
`substringBefore(String str, String separator)`|[`StringUtils.substringBefore(...)`](https://commons.apache.org/proper/commons-lang/javadocs/api-3.3/org/apache/commons/lang3/StringUtils.html#substringBefore(java.lang.String,%20java.lang.String))
Expand All @@ -24,6 +25,7 @@ Function Signature | Description
`startsWith(String str, String prefix)`| [`StringUtils.startsWith(...)`](https://commons.apache.org/proper/commons-lang/javadocs/api-3.3/org/apache/commons/lang3/StringUtils.html#startsWith(java.lang.CharSequence,%20java.lang.CharSequence))
`length(String string)`| [`StringUtils.length(...)`](https://commons.apache.org/proper/commons-lang/javadocs/api-3.3/org/apache/commons/lang3/StringUtils.html#length(java.lang.CharSequence))
`defaultIfEmpty(String str, String default)` | [`StringUtils.defaultIfEmpty(...)`](https://commons.apache.org/proper/commons-lang/javadocs/api-3.3/org/apache/commons/lang3/StringUtils.html#defaultIfEmpty(T,%20T))
`escapeXml10(String str)` | [`StringEscapeUtils.escapeXml10(...)`](https://commons.apache.org/proper/commons-lang/javadocs/api-3.3/org/apache/commons/lang3/StringEscapeUtils.html#escapeXml10(java.lang.String)), useful for escaping `initialContent` which uses enhanced JCR DocView syntax
`containsItem(List<String> list, String item)`| Returns `true` if the item is contained in the given list.
`containsAnyItem(List<String> list, List<String> items)`| Returns `true` if any of the items is contained in the given list.
`containsAllItems(List<String> list, List<String> items)`| Returns `true` if all of the items are contained in the given list (independent of their order).
Expand Down

0 comments on commit 4594184

Please sign in to comment.