Search in sources :

Example 1 with StringQueryResult

use of org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult in project nifi by apache.

the class FormatEvaluator method evaluate.

@Override
public QueryResult<String> evaluate(final Map<String, String> attributes) {
    final Date subjectValue = subject.evaluate(attributes).getValue();
    if (subjectValue == null) {
        return new StringQueryResult(null);
    }
    final QueryResult<String> formatResult = format.evaluate(attributes);
    final String format = formatResult.getValue();
    if (format == null) {
        return null;
    }
    final SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
    if (timeZone != null) {
        final QueryResult<String> tzResult = timeZone.evaluate(attributes);
        final String tz = tzResult.getValue();
        if (tz != null && TimeZone.getTimeZone(tz) != null) {
            sdf.setTimeZone(TimeZone.getTimeZone(tz));
        }
    }
    return new StringQueryResult(sdf.format(subjectValue));
}
Also used : StringQueryResult(org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 2 with StringQueryResult

use of org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult in project nifi by apache.

the class GetDelimitedFieldEvaluator method evaluate.

@Override
public QueryResult<String> evaluate(final Map<String, String> attributes) {
    final String subject = subjectEval.evaluate(attributes).getValue();
    if (subject == null || subject.isEmpty()) {
        return new StringQueryResult("");
    }
    final Long index = indexEval.evaluate(attributes).getValue();
    if (index == null) {
        throw new AttributeExpressionLanguageException("Cannot evaluate getDelimitedField function because the index (which field to obtain) was not specified");
    }
    if (index < 1) {
        return new StringQueryResult("");
    }
    final String delimiter = delimiterEval.evaluate(attributes).getValue();
    if (delimiter == null || delimiter.isEmpty()) {
        throw new AttributeExpressionLanguageException("Cannot evaluate getDelimitedField function because the delimiter was not specified");
    } else if (delimiter.length() > 1) {
        throw new AttributeExpressionLanguageException("Cannot evaluate getDelimitedField function because the delimiter evaluated to \"" + delimiter + "\", but only a single character is allowed.");
    }
    final String quoteString = quoteCharEval.evaluate(attributes).getValue();
    if (quoteString == null || quoteString.isEmpty()) {
        throw new AttributeExpressionLanguageException("Cannot evaluate getDelimitedField function because the quote character " + "(which character is used to enclose values that contain the delimiter) was not specified");
    } else if (quoteString.length() > 1) {
        throw new AttributeExpressionLanguageException("Cannot evaluate getDelimitedField function because the quote character " + "(which character is used to enclose values that contain the delimiter) evaluated to \"" + quoteString + "\", but only a single character is allowed.");
    }
    final String escapeString = escapeCharEval.evaluate(attributes).getValue();
    if (escapeString == null || escapeString.isEmpty()) {
        throw new AttributeExpressionLanguageException("Cannot evaluate getDelimitedField function because the escape character " + "(which character is used to escape the quote character or delimiter) was not specified");
    } else if (escapeString.length() > 1) {
        throw new AttributeExpressionLanguageException("Cannot evaluate getDelimitedField function because the escape character " + "(which character is used to escape the quote character or delimiter) evaluated to \"" + escapeString + "\", but only a single character is allowed.");
    }
    Boolean stripChars = stripCharsEval.evaluate(attributes).getValue();
    if (stripChars == null) {
        stripChars = Boolean.FALSE;
    }
    final char quoteChar = quoteString.charAt(0);
    final char delimiterChar = delimiter.charAt(0);
    final char escapeChar = escapeString.charAt(0);
    // ensure that quoteChar, delimiterChar, escapeChar are all different.
    if (quoteChar == delimiterChar) {
        throw new AttributeExpressionLanguageException("Cannot evaluate getDelimitedField function because the quote character and the delimiter are the same");
    }
    if (quoteChar == escapeChar) {
        throw new AttributeExpressionLanguageException("Cannot evaluate getDelimitedField function because the quote character and the escape character are the same");
    }
    if (delimiterChar == escapeChar) {
        throw new AttributeExpressionLanguageException("Cannot evaluate getDelimitedField function because the delimiter and the escape character are the same");
    }
    // Iterate through each character in the subject, trying to find the field index that we care about and extracting the chars from it.
    final StringBuilder fieldBuilder = new StringBuilder();
    final int desiredFieldIndex = index.intValue();
    final int numChars = subject.length();
    boolean inQuote = false;
    int curFieldIndex = 1;
    boolean lastCharIsEscape = false;
    for (int i = 0; i < numChars; i++) {
        final char c = subject.charAt(i);
        if (c == quoteChar && !lastCharIsEscape) {
            // we found a quote character that is not escaped. Flip the value of 'inQuote'
            inQuote = !inQuote;
            if (!stripChars && curFieldIndex == desiredFieldIndex) {
                fieldBuilder.append(c);
            }
        } else if (c == delimiterChar && !lastCharIsEscape && !inQuote) {
            // We found a delimiter that is not escaped and we are not in quotes - or we ran out of characters so we consider this
            // the last character.
            final int indexJustFinished = curFieldIndex++;
            if (indexJustFinished == desiredFieldIndex) {
                return new StringQueryResult(fieldBuilder.toString());
            }
        } else if (curFieldIndex == desiredFieldIndex) {
            if (c != escapeChar || !stripChars) {
                fieldBuilder.append(c);
            }
        }
        lastCharIsEscape = (c == escapeChar) && !lastCharIsEscape;
    }
    if (curFieldIndex == desiredFieldIndex) {
        // we have run out of characters and we are on the desired field. Return the characters from this field.
        return new StringQueryResult(fieldBuilder.toString());
    }
    // We did not find enough fields. Return an empty string.
    return new StringQueryResult("");
}
Also used : StringQueryResult(org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult) AttributeExpressionLanguageException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException)

Example 3 with StringQueryResult

use of org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult in project nifi by apache.

the class GetStateVariableEvaluator method evaluate.

@Override
public QueryResult<String> evaluate(Map<String, String> attributes) {
    if (!(attributes instanceof AttributesAndState)) {
        return new StringQueryResult(null);
    }
    final String subjectValue = subject.evaluate(attributes).getValue();
    if (subjectValue == null) {
        return new StringQueryResult(null);
    }
    AttributesAndState attributesAndState = (AttributesAndState) attributes;
    Map<String, String> stateMap = attributesAndState.getStateMap();
    String stateValue = stateMap.get(subjectValue);
    return new StringQueryResult(stateValue);
}
Also used : AttributesAndState(org.apache.nifi.attribute.expression.language.AttributesAndState) StringQueryResult(org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult)

Example 4 with StringQueryResult

use of org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult in project nifi by apache.

the class JsonPathEvaluator method evaluate.

@Override
public QueryResult<String> evaluate(final Map<String, String> attributes) {
    final String subjectValue = subject.evaluate(attributes).getValue();
    if (subjectValue == null || subjectValue.length() == 0) {
        throw new AttributeExpressionLanguageException("Subject is empty");
    }
    DocumentContext documentContext = null;
    try {
        documentContext = validateAndEstablishJsonContext(subjectValue);
    } catch (InvalidJsonException e) {
        throw new AttributeExpressionLanguageException("Subject contains invalid JSON: " + subjectValue, e);
    }
    final JsonPath compiledJsonPath;
    if (precompiledJsonPathExp != null) {
        compiledJsonPath = precompiledJsonPathExp;
    } else {
        compiledJsonPath = compileJsonPathExpression(jsonPathExp.evaluate(attributes).getValue());
    }
    Object result = null;
    try {
        result = documentContext.read(compiledJsonPath);
    } catch (Exception e) {
        // assume the path did not match anything in the document
        return EMPTY_RESULT;
    }
    return new StringQueryResult(getResultRepresentation(result, EMPTY_RESULT.getValue()));
}
Also used : StringQueryResult(org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult) AttributeExpressionLanguageException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException) InvalidJsonException(com.jayway.jsonpath.InvalidJsonException) DocumentContext(com.jayway.jsonpath.DocumentContext) JsonPath(com.jayway.jsonpath.JsonPath) AttributeExpressionLanguageException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException) InvalidJsonException(com.jayway.jsonpath.InvalidJsonException)

Aggregations

StringQueryResult (org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult)4 AttributeExpressionLanguageException (org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException)2 DocumentContext (com.jayway.jsonpath.DocumentContext)1 InvalidJsonException (com.jayway.jsonpath.InvalidJsonException)1 JsonPath (com.jayway.jsonpath.JsonPath)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 AttributesAndState (org.apache.nifi.attribute.expression.language.AttributesAndState)1