ReplaceText - Decorators

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

ReplaceText - Decorators

Arun Manivannan

I am using regular expressions as values for both the search and replace properties of the ReplaceText processor. When I am using literals as values for these properties, it all works great. 

However, when I try to use a FlowFile attribute (both properties support expression language), it fails, falling back to the default by returning the original flowfile. 

Created a Testcase for ReplaceText and figured out that the Decorators - quotedAttributeDecorator and escapeBackRefDecorator (one for search and replace regex) -escapes the Regex after evaluating the EL itself. When I remove them as parameters for evaluateAttributeExpressions, they work just like literals.

Not an expert with handling Regex but can I please understand the context behind escaping the values.  If escaping the EL isn't intended, I can make a PR to bypass this selectively - if the property has an EL (via the isExpressionLanguagePresent()) then avoid the quoting.

The testcase I am using is this : 

public void testAlwaysReplaceLineByLineUsingExpressionLanguage() {
final TestRunner runner = TestRunners.newTestRunner(new ReplaceTextTestProcessor());
runner.setProperty(ReplaceTextTestProcessor.EVALUATION_MODE, ReplaceTextTestProcessor.LINE_BY_LINE);
runner.setProperty(ReplaceTextTestProcessor.REPLACEMENT_STRATEGY, ReplaceTextTestProcessor.REGEX_REPLACE);
//runner.setProperty(ReplaceText.SEARCH_VALUE, "(.{1})(.{2})");
runner.setProperty(ReplaceTextTestProcessor.SEARCH_VALUE, "${source.regex}");
runner.setProperty(ReplaceTextTestProcessor.REPLACEMENT_VALUE, "${target.regex}");

final Map<String, String> attributes = new HashMap<>();
attributes.put("source.regex", "(.{1})(.{2})(.*)");
attributes.put("target.regex", "$1;$2");
runner.enqueue("Hel\nWor\r\ntody\n".getBytes(), attributes);;

runner.assertAllFlowFilesTransferred(ReplaceTextTestProcessor.REL_SUCCESS, 1);
final MockFlowFile out = runner.getFlowFilesForRelationship(ReplaceTextTestProcessor.REL_SUCCESS).get(0);

Thank you in advance.