use of org.apache.nifi.attribute.expression.language.Query.Range in project nifi by apache.
the class ExpressionCompiler method newStringLiteralEvaluator.
private Evaluator<String> newStringLiteralEvaluator(final String literalValue) {
if (literalValue == null || literalValue.length() < 2) {
return addToken(new StringLiteralEvaluator(literalValue), literalValue);
}
final List<Range> ranges = Query.extractExpressionRanges(literalValue);
if (ranges.isEmpty()) {
return addToken(new StringLiteralEvaluator(literalValue), literalValue);
}
final List<Evaluator<?>> evaluators = new ArrayList<>();
int lastIndex = 0;
for (final Range range : ranges) {
if (range.getStart() > lastIndex) {
evaluators.add(newStringLiteralEvaluator(literalValue.substring(lastIndex, range.getStart())));
}
final String treeText = literalValue.substring(range.getStart(), range.getEnd() + 1);
evaluators.add(buildEvaluator(compileTree(treeText)));
lastIndex = range.getEnd() + 1;
}
final Range lastRange = ranges.get(ranges.size() - 1);
if (lastRange.getEnd() + 1 < literalValue.length()) {
final String treeText = literalValue.substring(lastRange.getEnd() + 1);
evaluators.add(newStringLiteralEvaluator(treeText));
}
if (evaluators.size() == 1) {
return toStringEvaluator(evaluators.get(0));
}
Evaluator<String> lastEvaluator = toStringEvaluator(evaluators.get(0));
for (int i = 1; i < evaluators.size(); i++) {
lastEvaluator = new AppendEvaluator(lastEvaluator, toStringEvaluator(evaluators.get(i)));
}
this.evaluators.addAll(evaluators);
return lastEvaluator;
}
use of org.apache.nifi.attribute.expression.language.Query.Range in project nifi by apache.
the class TestQuery method testExtractExpressionRanges.
@Test
public void testExtractExpressionRanges() {
assertEquals(29, Query.extractExpressionRanges("${hello:equals( $${goodbye} )}").get(0).getEnd());
List<Range> ranges = Query.extractExpressionRanges("hello");
assertTrue(ranges.isEmpty());
ranges = Query.extractExpressionRanges("${hello");
assertTrue(ranges.isEmpty());
ranges = Query.extractExpressionRanges("hello}");
assertTrue(ranges.isEmpty());
ranges = Query.extractExpressionRanges("$${hello");
assertTrue(ranges.isEmpty());
ranges = Query.extractExpressionRanges("$he{ll}o");
assertTrue(ranges.isEmpty());
ranges = Query.extractExpressionRanges("${hello}");
assertEquals(1, ranges.size());
Range range = ranges.get(0);
assertEquals(0, range.getStart());
assertEquals(7, range.getEnd());
ranges = Query.extractExpressionRanges("${hello:equals( ${goodbye} )}");
assertEquals(1, ranges.size());
range = ranges.get(0);
assertEquals(0, range.getStart());
assertEquals(28, range.getEnd());
ranges = Query.extractExpressionRanges("${hello:equals( $${goodbye} )}");
assertEquals(1, ranges.size());
range = ranges.get(0);
assertEquals(0, range.getStart());
assertEquals(29, range.getEnd());
ranges = Query.extractExpressionRanges("${hello:equals( $${goodbye} )} or just hi, ${bob:or(${jerry})}");
assertEquals(2, ranges.size());
range = ranges.get(0);
assertEquals(0, range.getStart());
assertEquals(29, range.getEnd());
range = ranges.get(1);
assertEquals(43, range.getStart());
assertEquals(61, range.getEnd());
ranges = Query.extractExpressionRanges("${hello:equals( ${goodbye} )} or just hi, ${bob}, are you ${bob.age:toNumber()} yet? $$$${bob}");
assertEquals(3, ranges.size());
range = ranges.get(0);
assertEquals(0, range.getStart());
assertEquals(28, range.getEnd());
range = ranges.get(1);
assertEquals(42, range.getStart());
assertEquals(47, range.getEnd());
range = ranges.get(2);
assertEquals(58, range.getStart());
assertEquals(78, range.getEnd());
ranges = Query.extractExpressionRanges("${x:matches( '.{4}' )}");
assertEquals(1, ranges.size());
range = ranges.get(0);
assertEquals(0, range.getStart());
assertEquals(21, range.getEnd());
}
use of org.apache.nifi.attribute.expression.language.Query.Range in project nifi by apache.
the class TestQuery method testEscapeQuotes.
@Test
public void testEscapeQuotes() {
final long timestamp = 1403620278642L;
final Map<String, String> attributes = new HashMap<>();
attributes.put("date", String.valueOf(timestamp));
final String format = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
final String query = "startDateTime=\"${date:toNumber():toDate():format(\"" + format + "\")}\"";
final String result = Query.evaluateExpressions(query, attributes, null);
final String expectedTime = new SimpleDateFormat(format, Locale.US).format(timestamp);
assertEquals("startDateTime=\"" + expectedTime + "\"", result);
final List<Range> ranges = Query.extractExpressionRanges(query);
assertEquals(1, ranges.size());
}
Aggregations