use of org.apache.nifi.attribute.expression.language.compile.ExpressionCompiler in project nifi by apache.
the class Query method prepare.
public static PreparedQuery prepare(final String query) throws AttributeExpressionLanguageParsingException {
if (query == null) {
return new EmptyPreparedQuery(null);
}
final List<Range> ranges = extractExpressionRanges(query);
if (ranges.isEmpty()) {
return new EmptyPreparedQuery(query.replace("$$", "$"));
}
final ExpressionCompiler compiler = new ExpressionCompiler();
try {
final List<Expression> expressions = new ArrayList<>();
int lastIndex = 0;
for (final Range range : ranges) {
if (range.getStart() > lastIndex) {
final String substring = query.substring(lastIndex, range.getStart()).replace("$$", "$");
expressions.add(new StringLiteralExpression(substring));
lastIndex = range.getEnd() + 1;
}
final String treeText = query.substring(range.getStart(), range.getEnd() + 1).replace("$$", "$");
final CompiledExpression compiledExpression = compiler.compile(treeText);
expressions.add(compiledExpression);
lastIndex = range.getEnd() + 1;
}
final Range lastRange = ranges.get(ranges.size() - 1);
if (lastRange.getEnd() + 1 < query.length()) {
final String treeText = query.substring(lastRange.getEnd() + 1).replace("$$", "$");
expressions.add(new StringLiteralExpression(treeText));
}
return new StandardPreparedQuery(expressions);
} catch (final AttributeExpressionLanguageParsingException e) {
return new InvalidPreparedQuery(query, e.getMessage());
}
}
use of org.apache.nifi.attribute.expression.language.compile.ExpressionCompiler in project nifi by apache.
the class Query method compile.
public static Query compile(final String query) throws AttributeExpressionLanguageParsingException {
try {
final ExpressionCompiler compiler = new ExpressionCompiler();
final CompiledExpression compiledExpression = compiler.compile(query);
return new Query(compiledExpression.getExpression(), compiledExpression.getTree(), compiledExpression.getRootEvaluator());
} catch (final AttributeExpressionLanguageParsingException e) {
throw e;
} catch (final Exception e) {
throw new AttributeExpressionLanguageParsingException(e);
}
}
Aggregations