Search in sources :

Example 1 with Range

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;
}
Also used : StringLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.StringLiteralEvaluator) ArrayList(java.util.ArrayList) AppendEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.AppendEvaluator) Range(org.apache.nifi.attribute.expression.language.Query.Range) IfElseEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.IfElseEvaluator) OrEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.OrEvaluator) GreaterThanEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.GreaterThanEvaluator) LastIndexOfEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.LastIndexOfEvaluator) UrlEncodeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.UrlEncodeEvaluator) IteratingEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.IteratingEvaluator) SubstringAfterLastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringAfterLastEvaluator) ReplaceNullEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ReplaceNullEvaluator) ToLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.ToLiteralEvaluator) AppendEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.AppendEvaluator) GetStateVariableEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.GetStateVariableEvaluator) MinusEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.MinusEvaluator) ReplaceAllEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ReplaceAllEvaluator) Base64DecodeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.Base64DecodeEvaluator) ToStringEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ToStringEvaluator) MultiAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiAttributeEvaluator) AttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.AttributeEvaluator) GreaterThanOrEqualEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.GreaterThanOrEqualEvaluator) AnyAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.AnyAttributeEvaluator) MappingEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MappingEvaluator) AndEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.AndEvaluator) NumberToDateEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.NumberToDateEvaluator) OneUpSequenceEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.OneUpSequenceEvaluator) StringLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.StringLiteralEvaluator) WholeNumberCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.WholeNumberCastEvaluator) IsEmptyEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.IsEmptyEvaluator) InEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.InEvaluator) LessThanOrEqualEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.LessThanOrEqualEvaluator) UuidEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.UuidEvaluator) IPEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.IPEvaluator) EqualsEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.EqualsEvaluator) CharSequenceTranslatorEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.CharSequenceTranslatorEvaluator) IsNullEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.IsNullEvaluator) NowEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.NowEvaluator) IndexOfEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.IndexOfEvaluator) CountEvaluator(org.apache.nifi.attribute.expression.language.evaluation.reduce.CountEvaluator) ToRadixEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ToRadixEvaluator) ReplaceFirstEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ReplaceFirstEvaluator) ToUpperEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ToUpperEvaluator) ReplaceEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ReplaceEvaluator) EqualsIgnoreCaseEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.EqualsIgnoreCaseEvaluator) ContainsEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ContainsEvaluator) PlusEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.PlusEvaluator) FindEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.FindEvaluator) MatchesEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.MatchesEvaluator) Evaluator(org.apache.nifi.attribute.expression.language.evaluation.Evaluator) ModEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ModEvaluator) Base64EncodeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.Base64EncodeEvaluator) GetDelimitedFieldEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.GetDelimitedFieldEvaluator) DateEvaluator(org.apache.nifi.attribute.expression.language.evaluation.DateEvaluator) TrimEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.TrimEvaluator) FromRadixEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.FromRadixEvaluator) JsonPathEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.JsonPathEvaluator) BooleanLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.BooleanLiteralEvaluator) DecimalLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.DecimalLiteralEvaluator) LessThanEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.LessThanEvaluator) WholeNumberLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.WholeNumberLiteralEvaluator) StringEvaluator(org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator) LengthEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.LengthEvaluator) ReduceEvaluator(org.apache.nifi.attribute.expression.language.evaluation.reduce.ReduceEvaluator) SubstringAfterEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringAfterEvaluator) MathEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.MathEvaluator) MultiplyEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.MultiplyEvaluator) AllAttributesEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.AllAttributesEvaluator) SubstringEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringEvaluator) MultiNamedAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiNamedAttributeEvaluator) NotEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.NotEvaluator) SubstringBeforeLastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringBeforeLastEvaluator) FormatEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.FormatEvaluator) StartsWithEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.StartsWithEvaluator) PrependEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.PrependEvaluator) EndsWithEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.EndsWithEvaluator) ToLowerEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ToLowerEvaluator) MultiMatchAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiMatchAttributeEvaluator) BooleanEvaluator(org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator) ReplaceEmptyEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ReplaceEmptyEvaluator) DelineatedAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.DelineatedAttributeEvaluator) NumberCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.NumberCastEvaluator) HostnameEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.HostnameEvaluator) StringCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.StringCastEvaluator) BooleanCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.BooleanCastEvaluator) UrlDecodeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.UrlDecodeEvaluator) RandomNumberGeneratorEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.RandomNumberGeneratorEvaluator) NotNullEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.NotNullEvaluator) DecimalCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.DecimalCastEvaluator) JoinEvaluator(org.apache.nifi.attribute.expression.language.evaluation.reduce.JoinEvaluator) SubstringBeforeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringBeforeEvaluator) StringToDateEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.StringToDateEvaluator) DateCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.DateCastEvaluator) DivideEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.DivideEvaluator)

Example 2 with Range

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());
}
Also used : Range(org.apache.nifi.attribute.expression.language.Query.Range) Test(org.junit.Test)

Example 3 with Range

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());
}
Also used : HashMap(java.util.HashMap) Range(org.apache.nifi.attribute.expression.language.Query.Range) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Aggregations

Range (org.apache.nifi.attribute.expression.language.Query.Range)3 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 BooleanEvaluator (org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator)1 DateEvaluator (org.apache.nifi.attribute.expression.language.evaluation.DateEvaluator)1 Evaluator (org.apache.nifi.attribute.expression.language.evaluation.Evaluator)1 StringEvaluator (org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator)1 BooleanCastEvaluator (org.apache.nifi.attribute.expression.language.evaluation.cast.BooleanCastEvaluator)1 DateCastEvaluator (org.apache.nifi.attribute.expression.language.evaluation.cast.DateCastEvaluator)1 DecimalCastEvaluator (org.apache.nifi.attribute.expression.language.evaluation.cast.DecimalCastEvaluator)1 NumberCastEvaluator (org.apache.nifi.attribute.expression.language.evaluation.cast.NumberCastEvaluator)1 StringCastEvaluator (org.apache.nifi.attribute.expression.language.evaluation.cast.StringCastEvaluator)1 WholeNumberCastEvaluator (org.apache.nifi.attribute.expression.language.evaluation.cast.WholeNumberCastEvaluator)1 AndEvaluator (org.apache.nifi.attribute.expression.language.evaluation.functions.AndEvaluator)1 AppendEvaluator (org.apache.nifi.attribute.expression.language.evaluation.functions.AppendEvaluator)1 Base64DecodeEvaluator (org.apache.nifi.attribute.expression.language.evaluation.functions.Base64DecodeEvaluator)1 Base64EncodeEvaluator (org.apache.nifi.attribute.expression.language.evaluation.functions.Base64EncodeEvaluator)1 CharSequenceTranslatorEvaluator (org.apache.nifi.attribute.expression.language.evaluation.functions.CharSequenceTranslatorEvaluator)1 ContainsEvaluator (org.apache.nifi.attribute.expression.language.evaluation.functions.ContainsEvaluator)1