ijokarumawak commented on a change in pull request #3375: NIFI-5979 : enhanced 
ReplaceText processor with "Number of Occurrences" and "Occurrence offset" 
configurations
URL: https://github.com/apache/nifi/pull/3375#discussion_r270272973
 
 

 ##########
 File path: 
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ReplaceText.java
 ##########
 @@ -586,52 +672,76 @@ public void process(final InputStream in, final 
OutputStream out) throws IOExcep
                         try (final LineDemarcator demarcator = new 
LineDemarcator(in, charset, maxBufferSize, 8192);
                             final BufferedWriter bw = new BufferedWriter(new 
OutputStreamWriter(out, charset))) {
 
-                            String oneLine;
 
-                            final StringBuffer sb = new StringBuffer();
+//                            final StringBuffer sb = new StringBuffer();
                             Matcher matcher = null;
-
-                            while (null != (oneLine = demarcator.nextLine())) {
-                                additionalAttrs.clear();
-                                if (matcher == null) {
-                                    matcher = searchPattern.matcher(oneLine);
+                            String precedingLine = demarcator.nextLine();
+                            String succeedingLine;
+                            boolean firstLine = true;
+                            while (null != (succeedingLine = 
demarcator.nextLine())) {
+                                if(firstLine && 
evaluateMode.equalsIgnoreCase(FIRST_LINE)){
+                                    replaceRegexInLine(bw, precedingLine, 
matcher, searchPattern, context, flowFile);
+                                    firstLine = false;
+                                } else if(firstLine && 
evaluateMode.equalsIgnoreCase(EXCEPT_FIRST_LINE)) {
+                                    firstLine = false;
+                                    bw.write(precedingLine);
+                                } else 
if(evaluateMode.equalsIgnoreCase(LINE_BY_LINE)
+                                    || 
evaluateMode.equalsIgnoreCase(EXCEPT_LAST_LINE)
+                                    || (!firstLine && 
evaluateMode.equalsIgnoreCase(EXCEPT_FIRST_LINE))) {
+                                    replaceRegexInLine(bw, precedingLine, 
matcher, searchPattern, context, flowFile);
                                 } else {
-                                    matcher.reset(oneLine);
+                                    bw.write(precedingLine);
                                 }
+                                precedingLine = succeedingLine;
+                            }
 
-                                int matches = 0;
-                                sb.setLength(0);
+                            if 
(evaluateMode.equalsIgnoreCase(EXCEPT_LAST_LINE) || (!firstLine && 
evaluateMode.equalsIgnoreCase(FIRST_LINE))) {
+                                bw.write(precedingLine);
+                            } else {
+                                replaceRegexInLine(bw, precedingLine, matcher, 
searchPattern, context, flowFile);
+                            }
+                        }
+                    }
+                });
+            }
 
-                                while (matcher.find()) {
-                                    matches++;
+            return updatedFlowFile;
+        }
 
-                                    for (int i=0; i <= matcher.groupCount(); 
i++) {
-                                        additionalAttrs.put("$" + i, 
matcher.group(i));
-                                    }
+        private void replaceRegexInLine(BufferedWriter bw, String oneLine, 
/*StringBuffer sb,*/ Matcher matcher, Pattern searchPattern, ProcessContext 
context, FlowFile flowFile) throws IOException {
+            additionalAttrs.clear();
+            if (matcher == null) {
+                matcher = searchPattern.matcher(oneLine);
 
 Review comment:
   Please fix this with an unit test. A 0 byte empty FlowFIle causes 
NullPointerException:
   
   ```
   2019-03-29 12:34:50,430 WARN [Timer-Driven Process Thread-8] 
o.a.n.controller.tasks.ConnectableTask Administratively Yielding 
ReplaceText[id=c7810642-0169-1000-fd46-7dc8b987cb64] due to uncaught Exception: 
java.lang.NullPointerException
   java.lang.NullPointerException: null
           at java.util.regex.Matcher.getTextLength(Matcher.java:1283)
           at java.util.regex.Matcher.reset(Matcher.java:309)
           at java.util.regex.Matcher.<init>(Matcher.java:229)
           at java.util.regex.Pattern.matcher(Pattern.java:1093)
           at 
org.apache.nifi.processors.standard.ReplaceText$RegexReplace.replaceRegexInLine(ReplaceText.java:714)
           at 
org.apache.nifi.processors.standard.ReplaceText$RegexReplace.access$800(ReplaceText.java:593)
           at 
org.apache.nifi.processors.standard.ReplaceText$RegexReplace$4.process(ReplaceText.java:701)
           at 
org.apache.nifi.controller.repository.StandardProcessSession.write(StandardProcessSession.java:2925)
           at 
org.apache.nifi.processors.standard.ReplaceText$RegexReplace.replace(ReplaceText.java:669)
           at 
org.apache.nifi.processors.standard.ReplaceText.onTrigger(ReplaceText.java:305)
           at 
org.apache.nifi.processor.AbstractProcessor.onTrigger(AbstractProcessor.java:27)
           at 
org.apache.nifi.controller.StandardProcessorNode.onTrigger(StandardProcessorNode.java:1162)
           at 
org.apache.nifi.controller.tasks.ConnectableTask.invoke(ConnectableTask.java:205)
           at 
org.apache.nifi.controller.scheduling.TimerDrivenSchedulingAgent$1.run(TimerDrivenSchedulingAgent.java:117)
   ```

----------------------------------------------------------------
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

Reply via email to