// Diff.java // --------- // part of YaCy // (C) by Michael Peter Christen; mc@anomic.de // first published on http://www.anomic.de // Frankfurt, Germany, 2007 // Created 03.02.2007 // // This file is contributed by Franz Brausze // // $LastChangedDate: $ // $LastChangedRevision: $ // $LastChangedBy: $ // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // Using this software in any meaning (reading, learning, copying, compiling, // running) means that you agree that the Author(s) is (are) not responsible // for cost, loss of data or any harm that may be caused directly or indirectly // by usage of this softare or this documentation. The usage of this software // is on your own risk. The installation and usage (starting/running) of this // software may allow other people or application to access your computer and // any attached devices and is highly dependent on the configuration of the // software which must be done by the user of the software; the author(s) is // (are) also not responsible for proper configuration and usage of the // software, even if provoked by documentation provided together with // the software. // // Any changes to this file according to the GPL as documented in the file // gpl.txt aside this file in the shipment you received can be done to the // lines that follows this copyright notice here, but changes must not be // done inside the copyright notive above. A re-distribution must contain // the intact and unchanged copyright notice. // Contributions and changes to the program code must be marked as such. package de.anomic.data; import java.util.ArrayList; /** * This class provides a diff-functionality. */ public class diff { private final ArrayList parts = new ArrayList(); final Object[] o; final Object[] n; /** * @param o the original String * @param n the new String * @throws NullPointerException if one of the arguments is null */ public diff(String o, String n) { this(o, n, 1); } /** * @param o the original String * @param n the new String * @param minConsecutive the minimum number of consecutive equal characters in * both Strings. Smaller seperations will only be performed on the end of either * String if needed * @throws NullPointerException if o or n is * null */ public diff(String o, String n, int minConsecutive) { if (o == null || n == null) throw new NullPointerException("neither o nor n must be null"); this.o = new Comparable[o.length()]; for (int i=0; i 0) ? minConsecutive : 1); } public diff(Object[] o, Object[] n, int minConsecutive) { if (o == null || n == null) throw new NullPointerException("neither o nor n must be null"); this.o = o; this.n = n; parse((minConsecutive > 0) ? minConsecutive : 1); } private void parse(int minLength) { /* Matrix: find as long diagonals as possible, * delete the old horizontally and add the new vertically * * ~ OLD ~ * |T|H|E| |F|I|R|S|T| |S|E|N|T|E|N|C|E| * T|#| | | | | | | |#| | | | |#| | | | | * H| |#| | | | | | | | | | | | | | | | | * E| | |#| | | | | | | | |#| | |#| | |#| * | | | |#| | | | | |#| | | | | | | | | * N| | | | | | | | | | | | |#| | |#| | | * E| | |#| | | | | | | | |#| | |#| | |#| * ~ X| | | | | | | | | | | | | | | | | | | * N T|#| | | | | | | |#| | | | |#| | | | | * E | | | |#| | | | | |#| | | | | | | | | * W S| | | | | | | |#| | |#| | | | | | | | * ~ E| | |#| | | | | | | | |#| | |#| | |#| * N| | | | | | | | | | | | |#| | |#| | | * T|#| | | | | | | |#| | | | |#| | | | | * E| | |#| | | | | | | | |#| | |#| | |#| * N| | | | | | | | | | | | |#| | |#| | | * C| | | | | | | | | | | | | | | | |#| | * E| | |#| | | | | | | | |#| | |#| | |#| */ boolean[][] matrix = new boolean[this.n.length][this.o.length]; for (int y=0; yminLength line by line in a submatrix * { x, y, matrix[0].length, matrix.length} of the matrix:
*       {_1,__,__} -> X axis
*      ,{__,_1,__}
*      ,{__,__,_1}
*
    * TODO: some optimisation ideas *
  • search for a better algorithm on the inet!!! :)
  • *
  • pass only the part of the matrix where the search takes place - not the whole matrix everytime
  • *
  • break the inner loop if the rest of the matrix is smaller than minLength (and no diagonal has been found yet)
  • *
  • return diagonal topologicaly closest to the {0,0}
  • *
* @param x the starting position of the search on the optical horizontal axis * @param y the starting position of the search on the optical vertical axis
* @param matrix the matrix to search through * @param minLength the minimal desired length of a diagonal to find * @return a vector in the form { diagStartX, diagStartY, diagLength } where diagLength >= minLength */ private static int[] findDiagonal(int x, int y, boolean[][] matrix, int minLength) { int rx, ry, yy, xx, i; for (yy=y; yy= minLength) return new int[] { rx, ry, i }; // swap back the x and y axes for better readability } return null; } /** * @return the original Object[] passed to this class on instantiation */ public Object[] getOriginal() { return this.o; } /** * @return the new Object[] passed to this class on instantiation */ public Object[] getNew() { return this.n; } /** * A diff is composed of different parts. Each of these parts stands for an * operation, like "do nothing", "add" or "delete". * * @see Part * @return all parts this diff consists of in correct order */ public Part[] getParts() { return this.parts.toArray(new Part[this.parts.size()]); } public String toString() { StringBuffer sb = new StringBuffer(this.parts.size() * 20); for (int j=0; j *
unchanged
"  STRING"
*
added
"+ STRING"
*
deleted
"- STRING"
* */ public String toString() { return ((this.action == UNCHANGED) ? " " : (this.action == ADDED) ? "+" : "-") + " " + getString(); } } public static String toHTML(diff[] diffs) { StringBuffer sb = new StringBuffer(diffs.length * 60); diff.Part[] ps; for (int i=0; i\n"); ps = diffs[i].getParts(); for (int j=0; j").append(htmlTools.encodeUnicode2html(ps[j].getString(), true).replaceAll("\n", "
")); sb.append(""); } sb.append("

"); } return new String(sb); } }