public ResultItem<T>[] getMergedResult() {
    Insertion[] insertedByA = new Insertion[base.length + 1];
    boolean[] deletedByA = new boolean[base.length];
    Insertion[] insertedByB = new Insertion[base.length + 1];
    boolean[] deletedByB = new boolean[base.length];

    Diff aDiff = new Diff(base, a);
    Diff.change aChanges = aDiff.diff_2(false);
    recordChanges(insertedByA, deletedByA, aChanges);

    Diff bDiff = new Diff(base, b);
    Diff.change bChanges = bDiff.diff_2(false);
    recordChanges(insertedByB, deletedByB, bChanges);

    List<ResultItem> result = new ArrayList<ResultItem>();
    InsertedItemFactory<T> aFactory = new InsertedItemFactoryA();
    InsertedItemFactory<T> bFactory = new InsertedItemFactoryB();
    int aPos = 0;
    int bPos = 0;

    for (int i = 0; i <= base.length; i++) {
      if (insertedByA[i] != null) aPos = addInsertions(result, insertedByA[i], aFactory);
      if (insertedByB[i] != null) bPos = addInsertions(result, insertedByB[i], bFactory);

      if (i < base.length) {
        ResultItem<T> item = new ResultItem<T>(base[i], i, -1, -1);
        if (deletedByA[i] == false) item.aPos = aPos++;
        if (deletedByB[i] == false) item.bPos = bPos++;
        result.add(item);
      }
    }

    return (ResultItem<T>[]) result.toArray(new ResultItem[result.size()]);
  }
Exemple #2
0
 public static void main(final String[] argv) throws IOException {
   final String filea = argv[argv.length - 2];
   final String fileb = argv[argv.length - 1];
   final String[] a = DiffPrint.slurp(filea);
   final String[] b = DiffPrint.slurp(fileb);
   final Diff d = new Diff(a, b);
   char style = 'n';
   for (int i = 0; i < argv.length - 2; ++i) {
     final String f = argv[i];
     if (f.startsWith("-")) {
       for (int j = 1; j < f.length(); ++j) {
         switch (f.charAt(j)) {
           case 'e': // Ed style
             style = 'e';
             break;
           case 'c': // Context diff
             style = 'c';
             break;
           case 'u':
             style = 'u';
             break;
         }
       }
     }
   }
   final boolean reverse = style == 'e';
   final Diff.change script = d.diff_2(reverse);
   if (script == null) {
     System.err.println("No differences");
   } else {
     Base p;
     switch (style) {
       case 'e':
         p = new EdPrint(a, b);
         break;
       case 'c':
         p = new ContextPrint(a, b);
         break;
       case 'u':
         p = new UnifiedPrint(a, b);
         break;
       default:
         p = new NormalPrint(a, b);
     }
     p.print_header(filea, fileb);
     p.print_script(script);
   }
 }
 public File createDiff(File out, String dest) {
   File result = out;
   try {
     String[] a = DiffPrint.slurp(original);
     String[] b = DiffPrint.slurp(revised);
     Diff d = new Diff(a, b);
     Diff.change script = d.diff_2(false);
     if (script == null) {
       JOptionPane.showMessageDialog(null, "Error: No differences");
       result = null;
     } else {
       DiffPrint.Base p = new DiffPrint.UnifiedPrint(a, b);
       FileWriter fw = new FileWriter(result);
       p.setOutput(fw);
       p.print_header(".orig" + dest, dest);
       p.print_script(script);
       p.close();
       doubleCheckChunkHeaders(result);
     }
   } catch (Exception e) {
     result = null;
   }
   return result;
 }