mirror of
https://github.com/apache/nifi.git
synced 2025-02-21 18:05:39 +00:00
NIFI-911:
- Updating default value for Regex so it matches once (?s:^.*$) instead of twice (.*). Matching on .* results in matching for every character and then again for 0 characters.
This commit is contained in:
parent
2bb7853001
commit
cd2e1424cb
@ -71,6 +71,9 @@ public class ReplaceText extends AbstractProcessor {
|
||||
public static final String ENTIRE_TEXT = "Entire text";
|
||||
private final Pattern backReferencePattern = Pattern.compile("\\$(\\d+)");
|
||||
private static final byte[] ZERO_BYTE_BUFFER = new byte[0];
|
||||
private static final String DEFAULT_REGEX = "(?s:^.*$)";
|
||||
private static final String DEFAULT_REPLACEMENT_VALUE = "$1";
|
||||
|
||||
// Properties
|
||||
public static final PropertyDescriptor REGEX = new PropertyDescriptor.Builder()
|
||||
.name("Regular Expression")
|
||||
@ -78,14 +81,14 @@ public class ReplaceText extends AbstractProcessor {
|
||||
.required(true)
|
||||
.addValidator(StandardValidators.createRegexValidator(0, Integer.MAX_VALUE, true))
|
||||
.expressionLanguageSupported(true)
|
||||
.defaultValue("(.*)")
|
||||
.defaultValue(DEFAULT_REGEX)
|
||||
.build();
|
||||
public static final PropertyDescriptor REPLACEMENT_VALUE = new PropertyDescriptor.Builder()
|
||||
.name("Replacement Value")
|
||||
.description("The value to replace the regular expression with. Back-references to Regular Expression capturing groups are supported, but "
|
||||
+ "back-references that reference capturing groups that do not exist in the regular expression will be treated as literal value.")
|
||||
.required(true)
|
||||
.defaultValue("$1")
|
||||
.defaultValue(DEFAULT_REPLACEMENT_VALUE)
|
||||
.addValidator(Validator.VALID)
|
||||
.expressionLanguageSupported(true)
|
||||
.build();
|
||||
@ -166,7 +169,7 @@ public class ReplaceText extends AbstractProcessor {
|
||||
final ProcessorLog logger = getLogger();
|
||||
final String unsubstitutedRegex = context.getProperty(REGEX).getValue();
|
||||
String unsubstitutedReplacement = context.getProperty(REPLACEMENT_VALUE).getValue();
|
||||
if (unsubstitutedRegex.equals("(.*)") && unsubstitutedReplacement.equals("$1")) {
|
||||
if (unsubstitutedRegex.equals(DEFAULT_REGEX) && unsubstitutedReplacement.equals(DEFAULT_REPLACEMENT_VALUE)) {
|
||||
// This pattern says replace content with itself. We can highly optimize this process by simply transferring
|
||||
// all FlowFiles to the 'success' relationship
|
||||
session.transfer(flowFiles, REL_SUCCESS);
|
||||
|
@ -16,7 +16,6 @@
|
||||
*/
|
||||
package org.apache.nifi.processors.standard;
|
||||
|
||||
import org.apache.nifi.processors.standard.ReplaceText;
|
||||
import org.apache.nifi.util.MockFlowFile;
|
||||
import org.apache.nifi.util.TestRunner;
|
||||
import org.apache.nifi.util.TestRunners;
|
||||
@ -386,4 +385,41 @@ public class TestReplaceText {
|
||||
out.assertContentEquals("{ abc.txt }");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultReplacement() throws Exception {
|
||||
final String defaultValue = "default-replacement-value";
|
||||
|
||||
// leave the default regex settings
|
||||
final TestRunner runner = TestRunners.newTestRunner(new ReplaceText());
|
||||
runner.setValidateExpressionUsage(false);
|
||||
runner.setProperty(ReplaceText.REPLACEMENT_VALUE, defaultValue);
|
||||
|
||||
final Map<String, String> attributes = new HashMap<>();
|
||||
runner.enqueue("original-text".getBytes(StandardCharsets.UTF_8), attributes);
|
||||
|
||||
runner.run();
|
||||
|
||||
runner.assertAllFlowFilesTransferred(ReplaceText.REL_SUCCESS, 1);
|
||||
final MockFlowFile out = runner.getFlowFilesForRelationship(ReplaceText.REL_SUCCESS).get(0);
|
||||
out.assertContentEquals(defaultValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultMultilineReplacement() throws Exception {
|
||||
final String defaultValue = "default-replacement-value";
|
||||
|
||||
// leave the default regex settings
|
||||
final TestRunner runner = TestRunners.newTestRunner(new ReplaceText());
|
||||
runner.setValidateExpressionUsage(false);
|
||||
runner.setProperty(ReplaceText.REPLACEMENT_VALUE, defaultValue);
|
||||
|
||||
final Map<String, String> attributes = new HashMap<>();
|
||||
runner.enqueue(("original-text-line-1" + System.lineSeparator() + "original-text-line-2").getBytes(StandardCharsets.UTF_8), attributes);
|
||||
|
||||
runner.run();
|
||||
|
||||
runner.assertAllFlowFilesTransferred(ReplaceText.REL_SUCCESS, 1);
|
||||
final MockFlowFile out = runner.getFlowFilesForRelationship(ReplaceText.REL_SUCCESS).get(0);
|
||||
out.assertContentEquals(defaultValue);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user