2929import java .util .function .Function ;
3030import java .util .regex .Matcher ;
3131import java .util .regex .Pattern ;
32+ import static java .util .stream .Collectors .toList ;
3233
3334/**
3435 * This class for generating DiffRows for side-by-sidy view. You can customize the way of generating. For example, show
4243 * ignoreWhiteSpaces(true).columnWidth(100).build();
4344 * </code>
4445 */
45- public class DiffRowGenerator {
46+ public final class DiffRowGenerator {
4647
4748 public static final BiPredicate <String , String > DEFAULT_EQUALIZER = Object ::equals ;
4849
4950 public static final BiPredicate <String , String > IGNORE_WHITESPACE_EQUALIZER = (original , revised )
5051 -> adjustWhitespace (original ).equals (adjustWhitespace (revised ));
5152
53+ public static final Function <String , String > LINE_NORMALIZER_FOR_HTML = StringUtils ::normalize ;
54+
55+
5256 /**
5357 * Splitting lines by character to achieve char by char diff checking.
5458 */
@@ -60,6 +64,7 @@ public class DiffRowGenerator {
6064 return list ;
6165 };
6266 public static final Pattern SPLIT_BY_WORD_PATTERN = Pattern .compile ("\\ s+|[,.\\ [\\ ](){}/\\ \\ *+\\ -#]" );
67+
6368 /**
6469 * Splitting lines by word to achieve word by word diff checking.
6570 */
@@ -144,6 +149,7 @@ static void wrapInTag(List<String> sequence, int startPosition,
144149 private final Function <Boolean , String > newTag ;
145150 private final Function <Boolean , String > oldTag ;
146151 private final boolean reportLinesUnchanged ;
152+ private final Function <String , String > lineNormalizer ;
147153
148154 private final boolean showInlineDiffs ;
149155
@@ -157,8 +163,10 @@ private DiffRowGenerator(Builder builder) {
157163 inlineDiffSplitter = builder .inlineDiffSplitter ;
158164 equalizer = ignoreWhiteSpaces ? IGNORE_WHITESPACE_EQUALIZER : DEFAULT_EQUALIZER ;
159165 reportLinesUnchanged = builder .reportLinesUnchanged ;
166+ lineNormalizer = builder .lineNormalizer ;
160167
161168 Objects .requireNonNull (inlineDiffSplitter );
169+ Objects .requireNonNull (lineNormalizer );
162170 }
163171
164172 /**
@@ -258,14 +266,20 @@ private DiffRow buildDiffRowWithoutNormalizing(Tag type, String orgline, String
258266 StringUtils .wrapText (newline , columnWidth ));
259267 }
260268
269+ List <String > normalizeLines (List <String > list ) {
270+ return list .stream ()
271+ .map (lineNormalizer ::apply )
272+ .collect (toList ());
273+ }
274+
261275 /**
262276 * Add the inline diffs for given delta
263277 *
264278 * @param delta the given delta
265279 */
266280 private List <DiffRow > generateInlineDiffs (AbstractDelta <String > delta ) throws DiffException {
267- List <String > orig = StringUtils . normalize (delta .getSource ().getLines ());
268- List <String > rev = StringUtils . normalize (delta .getTarget ().getLines ());
281+ List <String > orig = normalizeLines (delta .getSource ().getLines ());
282+ List <String > rev = normalizeLines (delta .getTarget ().getLines ());
269283 List <String > origList ;
270284 List <String > revList ;
271285 String joinedOrig = String .join ("\n " , orig );
@@ -334,9 +348,9 @@ private List<DiffRow> generateInlineDiffs(AbstractDelta<String> delta) throws Di
334348
335349 private String preprocessLine (String line ) {
336350 if (columnWidth == 0 ) {
337- return StringUtils . normalize (line );
351+ return lineNormalizer . apply (line );
338352 } else {
339- return StringUtils .wrapText (StringUtils . normalize (line ), columnWidth );
353+ return StringUtils .wrapText (lineNormalizer . apply (line ), columnWidth );
340354 }
341355 }
342356
@@ -358,6 +372,7 @@ public static class Builder {
358372 private boolean mergeOriginalRevised = false ;
359373 private boolean reportLinesUnchanged = false ;
360374 private Function <String , List <String >> inlineDiffSplitter = SPLITTER_BY_CHARACTER ;
375+ private Function <String , String > lineNormalizer = LINE_NORMALIZER_FOR_HTML ;
361376
362377 private Builder () {
363378 }
@@ -463,5 +478,10 @@ public Builder inlineDiffBySplitter(Function<String, List<String>> inlineDiffSpl
463478 this .inlineDiffSplitter = inlineDiffSplitter ;
464479 return this ;
465480 }
481+
482+ public Builder lineNormalizer (Function <String , String > lineNormalizer ) {
483+ this .lineNormalizer = lineNormalizer ;
484+ return this ;
485+ }
466486 }
467487}
0 commit comments