NIFI-52: Created new implementation of PreparedQuery so that we can still prepare a query even if the expression is invalid

This commit is contained in:
Mark Payne 2014-12-10 11:47:00 -05:00
parent cce36335e1
commit 8254b75437
3 changed files with 96 additions and 20 deletions

View File

@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.attribute.expression.language;
import java.util.Map;
import org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException;
import org.apache.nifi.expression.AttributeValueDecorator;
import org.apache.nifi.flowfile.FlowFile;
import org.apache.nifi.processor.exception.ProcessException;
/**
* An implementation of PreparedQuery that throws an {@link AttributeExpressionLanguageException} when attempting
* to evaluate the query. This allows a PreparedQuery to be created, even though it can't
* be evaluated.
*/
public class InvalidPreparedQuery implements PreparedQuery {
private final String query;
private final String explanation;
public InvalidPreparedQuery(final String query, final String explanation) {
this.query = query;
this.explanation = explanation;
}
@Override
public String evaluateExpressions(final FlowFile flowFile, final AttributeValueDecorator decorator) throws ProcessException {
throw new AttributeExpressionLanguageException("Invalid Expression: " + query + " due to " + explanation);
}
@Override
public String evaluateExpressions() throws ProcessException {
throw new AttributeExpressionLanguageException("Invalid Expression: " + query + " due to " + explanation);
}
@Override
public String evaluateExpressions(final AttributeValueDecorator decorator) throws ProcessException {
throw new AttributeExpressionLanguageException("Invalid Expression: " + query + " due to " + explanation);
}
@Override
public String evaluateExpressions(final FlowFile flowFile) throws ProcessException {
throw new AttributeExpressionLanguageException("Invalid Expression: " + query + " due to " + explanation);
}
@Override
public String evaluateExpressions(final Map<String, String> attributes) throws ProcessException {
throw new AttributeExpressionLanguageException("Invalid Expression: " + query + " due to " + explanation);
}
@Override
public String evaluateExpressions(final Map<String, String> attributes, final AttributeValueDecorator decorator) throws ProcessException {
throw new AttributeExpressionLanguageException("Invalid Expression: " + query + " due to " + explanation);
}
}

View File

@ -569,29 +569,33 @@ public class Query {
return new EmptyPreparedQuery(query.replace("$$", "$"));
}
final List<String> substrings = new ArrayList<>();
final Map<String, Tree> trees = new HashMap<>();
int lastIndex = 0;
for (final Range range : ranges) {
if (range.getStart() > lastIndex) {
substrings.add(query.substring(lastIndex, range.getStart()).replace("$$", "$"));
try {
final List<String> substrings = new ArrayList<>();
final Map<String, Tree> trees = new HashMap<>();
int lastIndex = 0;
for (final Range range : ranges) {
if (range.getStart() > lastIndex) {
substrings.add(query.substring(lastIndex, range.getStart()).replace("$$", "$"));
lastIndex = range.getEnd() + 1;
}
final String treeText = query.substring(range.getStart(), range.getEnd() + 1).replace("$$", "$");
substrings.add(treeText);
trees.put(treeText, Query.compileTree(treeText));
lastIndex = range.getEnd() + 1;
}
final String treeText = query.substring(range.getStart(), range.getEnd() + 1).replace("$$", "$");
substrings.add(treeText);
trees.put(treeText, Query.compileTree(treeText));
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("$$", "$");
substrings.add(treeText);
}
return new StandardPreparedQuery(substrings, trees);
} catch (final AttributeExpressionLanguageParsingException e) {
return new InvalidPreparedQuery(query, e.getMessage());
}
final Range lastRange = ranges.get(ranges.size() - 1);
if (lastRange.getEnd() + 1 < query.length()) {
final String treeText = query.substring(lastRange.getEnd() + 1).replace("$$", "$");
substrings.add(treeText);
}
return new StandardPreparedQuery(substrings, trees);
}
public static Query compile(final String query) throws AttributeExpressionLanguageParsingException {

View File

@ -17,6 +17,7 @@
package org.apache.nifi.attribute.expression.language.exception;
public class IllegalAttributeException extends RuntimeException {
private static final long serialVersionUID = 12348721897342L;
public IllegalAttributeException() {
super();