turcsanyip commented on a change in pull request #3504: NIFI-6318: Support EL
in CSV formatting properties
URL: https://github.com/apache/nifi/pull/3504#discussion_r304804115
##########
File path:
nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-standard-record-utils/src/main/java/org/apache/nifi/csv/CSVUtils.java
##########
@@ -190,57 +199,88 @@ public static CSVFormat createCSVFormat(final
PropertyContext context) {
}
}
- private static char getUnescapedChar(final PropertyContext context, final
PropertyDescriptor property) {
- return
StringEscapeUtils.unescapeJava(context.getProperty(property).getValue()).charAt(0);
+ private static Character getCharUnescapedJava(final PropertyContext
context, final PropertyDescriptor property, final Map<String, String>
variables) {
+ String value =
context.getProperty(property).evaluateAttributeExpressions(variables).getValue();
+
+ if (value != null) {
+ String unescaped = unescapeJava(value);
+ if (unescaped.length() == 1) {
+ return unescaped.charAt(0);
+ }
+ }
+
+ LOG.warn("'{}' property evaluated to an invalid value: \"{}\". It must
be a single character. The property value will be skipped.",
property.getName(), value);
+ return null;
}
- private static char getChar(final PropertyContext context, final
PropertyDescriptor property) {
- return
CSVUtils.unescape(context.getProperty(property).getValue()).charAt(0);
+ private static Character getCharUnescaped(final PropertyContext context,
final PropertyDescriptor property, final Map<String, String> variables) {
+ String value =
context.getProperty(property).evaluateAttributeExpressions(variables).getValue();
+
+ if (value != null) {
+ String unescaped = unescape(value);
+ if (unescaped.length() == 1) {
+ return unescaped.charAt(0);
+ }
+ }
+
+ LOG.warn("'{}' property evaluated to an invalid value: \"{}\". It must
be a single character. The property value will be skipped.",
property.getName(), value);
+ return null;
}
- private static CSVFormat buildCustomFormat(final PropertyContext context) {
- final char valueSeparator = getUnescapedChar(context, VALUE_SEPARATOR);
- CSVFormat format = CSVFormat.newFormat(valueSeparator)
+ private static CSVFormat buildCustomFormat(final PropertyContext context,
final Map<String, String> variables) {
+ final Character valueSeparator = getCharUnescapedJava(context,
VALUE_SEPARATOR, variables);
+ CSVFormat format = CSVFormat.newFormat(valueSeparator != null ?
valueSeparator : VALUE_SEPARATOR.getDefaultValue().charAt(0))
.withAllowMissingColumnNames()
.withIgnoreEmptyLines();
- final PropertyValue skipHeaderPropertyValue =
context.getProperty(FIRST_LINE_IS_HEADER);
- if (skipHeaderPropertyValue.getValue() != null &&
skipHeaderPropertyValue.asBoolean()) {
+ final PropertyValue firstLineIsHeaderPropertyValue =
context.getProperty(FIRST_LINE_IS_HEADER);
+ if (firstLineIsHeaderPropertyValue.getValue() != null &&
firstLineIsHeaderPropertyValue.asBoolean()) {
format = format.withFirstRecordAsHeader();
}
- format = format.withQuote(getChar(context, QUOTE_CHAR));
- format = format.withEscape(getChar(context, ESCAPE_CHAR));
+ final Character quoteChar = getCharUnescaped(context, QUOTE_CHAR,
variables);
+ format = format.withQuote(quoteChar != null ? quoteChar :
QUOTE_CHAR.getDefaultValue().charAt(0));
+
+ final Character escapeChar = getCharUnescaped(context, ESCAPE_CHAR,
variables);
+ format = format.withEscape(escapeChar != null ? escapeChar :
ESCAPE_CHAR.getDefaultValue().charAt(0));
+
format = format.withTrim(context.getProperty(TRIM_FIELDS).asBoolean());
if (context.getProperty(COMMENT_MARKER).isSet()) {
- format = format.withCommentMarker(getChar(context,
COMMENT_MARKER));
+ format = format.withCommentMarker(getCharUnescaped(context,
COMMENT_MARKER, variables));
}
if (context.getProperty(NULL_STRING).isSet()) {
- format =
format.withNullString(CSVUtils.unescape(context.getProperty(NULL_STRING).getValue()));
+ format =
format.withNullString(unescape(context.getProperty(NULL_STRING).getValue()));
}
final PropertyValue quoteValue = context.getProperty(QUOTE_MODE);
- if (quoteValue != null) {
+ if (quoteValue.isSet() && quoteValue != null) {
Review comment:
Fixed.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services