Example usage for org.apache.commons.jrcs.diff Diff Diff

List of usage examples for org.apache.commons.jrcs.diff Diff Diff

Introduction

In this page you can find the example usage for org.apache.commons.jrcs.diff Diff Diff.

Prototype

public Diff(Object[] original, DiffAlgorithm algorithm) 

Source Link

Document

Create a differencing object using the given algorithm

Usage

From source file:com.qumasoft.guitools.compare.ContentRow.java

/**
 * Decorate the differences between this row and its peer. This is used for replacement rows only.
 * Use the apache algorithm./*  w  ww .  java 2 s .  com*/
 *
 * @param peerRow the peer row from the other file.
 */
void decorateDifferences(ContentRow peerRow) {
    String s = getText();
    String peer = peerRow.getText();
    if (s.length() > 0 && peer.length() > 0) {
        Byte[] sBytes = new Byte[s.getBytes().length];
        Byte[] peerBytes = new Byte[peer.getBytes().length];
        int sIndex = 0;
        for (Byte sourceByte : s.getBytes()) {
            sBytes[sIndex++] = sourceByte;
        }
        int peerIndex = 0;
        for (Byte peerByte : peer.getBytes()) {
            peerBytes[peerIndex++] = peerByte;
        }
        try {
            Revision differences = Diff.diff(sBytes, peerBytes);
            deduceCharacterAnnotations(differences, sBytes);
        } catch (DifferentiationFailedException e) {
            LOGGER.log(Level.WARNING, "Decoration failed: " + e.getLocalizedMessage());
        }
    }
}

From source file:com.qumasoft.qvcslib.CompareFilesWithApacheDiff.java

private boolean compareFiles() throws DifferentiationFailedException, IOException, QVCSOperationException {
    CompareLineInfo[] fileA = buildLinesFromFile(inFileA);
    setFile1LineCount(fileA.length);//  w  w w  .  java2s . com
    CompareLineInfo[] fileB = buildLinesFromFile(inFileB);
    setFile2LineCount(fileB.length);

    Revision apacheRevision = Diff.diff(fileA, fileB);

    if (apacheRevision.size() == 0) {
        // The files are identical.
        comparisonResultFlag = true;
    }

    // Create the edit script that describes the changes we found.
    writeEditScript(apacheRevision, fileA, fileB);
    return true;
}

From source file:org.opensolaris.opengrok.web.PageConfig.java

/**
 * Get all data required to create a diff view wrt. to this request in one
 * go./*from w  ww.ja  va2  s.  c om*/
 *
 * @return an instance with just enough information to render a sufficient
 * view. If not all required parameters were given either they are
 * supplemented with reasonable defaults if possible, otherwise the related
 * field(s) are {@code null}. {@link DiffData#errorMsg}
 *  {@code != null} indicates, that an error occured and one should not try
 * to render a view.
 */
public DiffData getDiffData() {
    DiffData data = new DiffData();
    data.path = getPath().substring(0, path.lastIndexOf('/'));
    data.filename = Util.htmlize(getResourceFile().getName());

    String srcRoot = getSourceRootPath();
    String context = req.getContextPath();

    String[] filepath = new String[2];
    data.rev = new String[2];
    data.file = new String[2][];
    data.param = new String[2];

    /*
     * Basically the request URI looks like this:
     * http://$site/$webapp/diff/$resourceFile?r1=$fileA@$revA&r2=$fileB@$revB
     * The code below extracts file path and revision from the URI.
     */
    for (int i = 1; i <= 2; i++) {
        String p = req.getParameter("r" + i);
        if (p != null) {
            int j = p.lastIndexOf("@");
            if (j != -1) {
                filepath[i - 1] = p.substring(0, j);
                data.rev[i - 1] = p.substring(j + 1);
            }
        }
    }
    if (data.rev[0] == null || data.rev[1] == null || data.rev[0].length() == 0 || data.rev[1].length() == 0
            || data.rev[0].equals(data.rev[1])) {
        data.errorMsg = "Please pick two revisions to compare the changed " + "from the <a href=\"" + context
                + Prefix.HIST_L + getUriEncodedPath() + "\">history</a>";
        return data;
    }
    data.genre = AnalyzerGuru.getGenre(getResourceFile().getName());

    if (data.genre == null || txtGenres.contains(data.genre)) {
        InputStream[] in = new InputStream[2];
        try {
            // Get input stream for both older and newer file.
            for (int i = 0; i < 2; i++) {
                File f = new File(srcRoot + filepath[i]);
                in[i] = HistoryGuru.getInstance().getRevision(f.getParent(), f.getName(), data.rev[i]);
                if (in[i] == null) {
                    data.errorMsg = "Unable to get revision " + Util.htmlize(data.rev[i]) + " for file: "
                            + Util.htmlize(getPath());
                    return data;
                }
            }

            /*
             * If the genre of the older revision cannot be determined,
             * (this can happen if the file was empty), try with newer
             * version.
             */
            for (int i = 0; i < 2 && data.genre == null; i++) {
                try {
                    data.genre = AnalyzerGuru.getGenre(in[i]);
                } catch (IOException e) {
                    data.errorMsg = "Unable to determine the file type: " + Util.htmlize(e.getMessage());
                }
            }

            if (data.genre != Genre.PLAIN && data.genre != Genre.HTML) {
                return data;
            }

            ArrayList<String> lines = new ArrayList<>();
            Project p = getProject();
            for (int i = 0; i < 2; i++) {
                try (BufferedReader br = new BufferedReader(
                        ExpandTabsReader.wrap(new InputStreamReader(in[i]), p))) {
                    String line;
                    while ((line = br.readLine()) != null) {
                        lines.add(line);
                    }
                    data.file[i] = lines.toArray(new String[lines.size()]);
                    lines.clear();
                }
                in[i] = null;
            }
        } catch (Exception e) {
            data.errorMsg = "Error reading revisions: " + Util.htmlize(e.getMessage());
        } finally {
            for (int i = 0; i < 2; i++) {
                IOUtils.close(in[i]);
            }
        }
        if (data.errorMsg != null) {
            return data;
        }
        try {
            data.revision = Diff.diff(data.file[0], data.file[1]);
        } catch (DifferentiationFailedException e) {
            data.errorMsg = "Unable to get diffs: " + Util.htmlize(e.getMessage());
        }
        for (int i = 0; i < 2; i++) {
            try {
                URI u = new URI(null, null, null, filepath[i] + "@" + data.rev[i], null);
                data.param[i] = u.getRawQuery();
            } catch (URISyntaxException e) {
                LOGGER.log(Level.WARNING, "Failed to create URI: ", e);
            }
        }
        data.full = fullDiff();
        data.type = getDiffType();
    }
    return data;
}