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_r270273958
 
 

 ##########
 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 {
 
 Review comment:
   Please remove the commented sb argument `/*StringBuffer sb,*/` if it's not 
used anymore.

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