Example #1
0
 /**
  * Format the list of Difference objects into a list of WikiDiff objects, which will include
  * information about what values are different and also include some unchanged values surrounded
  * the changed values, thus giving some context.
  */
 private static List<WikiDiff> generateWikiDiffs(
     List<Difference> diffs, String[] oldArray, String[] newArray) {
   List<WikiDiff> wikiDiffs = new ArrayList<WikiDiff>();
   Difference previousDiff = null;
   Difference nextDiff = null;
   List<WikiDiff> changedLineWikiDiffs = null;
   String[] oldLineArray = null;
   String[] newLineArray = null;
   List<Difference> changedLineDiffs = null;
   List<WikiDiff> wikiSubDiffs = null;
   Difference nextLineDiff = null;
   int i = 0;
   for (Difference currentDiff : diffs) {
     i++;
     wikiDiffs.addAll(
         DiffUtil.preBufferDifference(
             currentDiff, previousDiff, oldArray, newArray, DIFF_UNCHANGED_LINE_DISPLAY));
     changedLineWikiDiffs = DiffUtil.processDifference(currentDiff, oldArray, newArray);
     // loop through the difference and diff the individual lines so that it is possible to
     // highlight the exact
     // text that was changed
     for (WikiDiff changedLineWikiDiff : changedLineWikiDiffs) {
       oldLineArray = DiffUtil.stringToArray(changedLineWikiDiff.getOldText());
       newLineArray = DiffUtil.stringToArray(changedLineWikiDiff.getNewText());
       changedLineDiffs = new Diff<String>(oldLineArray, newLineArray).diff();
       wikiSubDiffs = new ArrayList<WikiDiff>();
       int j = 0;
       for (Difference changedLineDiff : changedLineDiffs) {
         // build sub-diff list, which is the difference for the individual
         // line item
         j++;
         if (j == 1) {
           // pre-buffering is only necessary for the first element as post-buffering
           // will handle all further buffering when bufferAmount is -1.
           wikiSubDiffs.addAll(
               DiffUtil.preBufferDifference(
                   changedLineDiff, null, oldLineArray, newLineArray, -1));
         }
         wikiSubDiffs.addAll(
             DiffUtil.processDifference(changedLineDiff, oldLineArray, newLineArray));
         nextLineDiff = (j < changedLineDiffs.size()) ? changedLineDiffs.get(j) : null;
         wikiSubDiffs.addAll(
             DiffUtil.postBufferDifference(
                 changedLineDiff, nextLineDiff, oldLineArray, newLineArray, -1));
       }
       changedLineWikiDiff.setSubDiffs(wikiSubDiffs);
     }
     wikiDiffs.addAll(changedLineWikiDiffs);
     nextDiff = (i < diffs.size()) ? diffs.get(i) : null;
     wikiDiffs.addAll(
         DiffUtil.postBufferDifference(
             currentDiff, nextDiff, oldArray, newArray, DIFF_UNCHANGED_LINE_DISPLAY));
     previousDiff = currentDiff;
   }
   return wikiDiffs;
 }