Search in sources :

Example 1 with PatchFailedException

use of org.suigeneris.jrcs.diff.PatchFailedException in project xwiki-platform by xwiki.

the class XWikiPatchUtils method patch.

/**
 * From {@link org.suigeneris.jrcs.rcs.impl.Node#patch(List, boolean)}.
 *
 * @param orig - text to patch, List<String> of lines.
 * @param diff - diff to patch, {@link Diff} format
 * @throws InvalidFileFormatException if diff is incorrect
 * @throws PatchFailedException if error in patching
 */
public static void patch(List<String> orig, String diff) throws InvalidFileFormatException, PatchFailedException {
    Revision revision = new Revision();
    Object[] lines = ToString.stringToArray(diff);
    for (int it = 0; it < lines.length; it++) {
        String cmd = lines[it].toString();
        if (cmd.length() == 0) {
            break;
        }
        java.util.StringTokenizer t = new StringTokenizer(cmd, "ad ", true);
        char action;
        int n;
        int count;
        try {
            action = t.nextToken().charAt(0);
            n = Integer.parseInt(t.nextToken());
            // skip the space
            t.nextToken();
            count = Integer.parseInt(t.nextToken());
        } catch (Exception e) {
            throw new InvalidFileFormatException("line:" + ":" + e.getClass().getName(), e);
        }
        if (action == 'd') {
            revision.addDelta(new DeleteDelta(new Chunk(n - 1, count)));
        } else if (action == 'a') {
            revision.addDelta(new AddDelta(n, new Chunk(getTextLines(lines, it + 1, it + 1 + count), 0, count, n - 1)));
            it += count;
        } else {
            throw new InvalidFileFormatException();
        }
    }
    revision.applyTo(orig);
}
Also used : DeleteDelta(org.suigeneris.jrcs.diff.delta.DeleteDelta) ToString(org.suigeneris.jrcs.util.ToString) Chunk(org.suigeneris.jrcs.diff.delta.Chunk) InvalidFileFormatException(org.suigeneris.jrcs.rcs.InvalidFileFormatException) PatchFailedException(org.suigeneris.jrcs.diff.PatchFailedException) DifferentiationFailedException(org.suigeneris.jrcs.diff.DifferentiationFailedException) InvalidFileFormatException(org.suigeneris.jrcs.rcs.InvalidFileFormatException) StringTokenizer(java.util.StringTokenizer) Revision(org.suigeneris.jrcs.diff.Revision) StringTokenizer(java.util.StringTokenizer) AddDelta(org.suigeneris.jrcs.diff.delta.AddDelta)

Example 2 with PatchFailedException

use of org.suigeneris.jrcs.diff.PatchFailedException in project xwiki-platform by xwiki.

the class XWikiRCSArchive method getNodes.

/**
 * @return Collection of pairs [{@link XWikiRCSNodeInfo}, {@link XWikiRCSNodeContent}]
 * @param docId - docId which will be wrote in {@link XWikiRCSNodeId#setDocId(long)}
 * @throws PatchFailedException
 * @throws InvalidFileFormatException
 * @throws NodeNotFoundException
 */
public Collection getNodes(long docId) throws NodeNotFoundException, InvalidFileFormatException, PatchFailedException {
    Collection result = new ArrayList(this.nodes.values().size());
    for (Iterator it = this.nodes.values().iterator(); it.hasNext(); ) {
        XWikiJRCSNode node = new XWikiJRCSNode((Node) it.next());
        XWikiRCSNodeInfo nodeInfo = new XWikiRCSNodeInfo();
        nodeInfo.setId(new XWikiRCSNodeId(docId, node.getVersion()));
        nodeInfo.setDiff(node.isDiff());
        if (!node.hasOldFormat()) {
            nodeInfo.setAuthor(node.getAuthor1());
            nodeInfo.setComment(node.getLog());
            nodeInfo.setDate(node.getDate());
        } else {
            // ones from a XWikiDocment object that we construct using the archive content.
            try {
                String xml = getRevisionAsString(node.getVersion());
                XWikiDocument doc = new XWikiDocument();
                doc.fromXML(xml);
                // set this fields from old document
                nodeInfo.setAuthor(doc.getAuthor());
                nodeInfo.setComment(doc.getComment());
                nodeInfo.setDate(doc.getDate());
            } catch (Exception e) {
                // 3 potential known errors:
                // 1) Revision 1.1 doesn't exist. Some time in the past there was a bug in XWiki where version
                // were starting at 1.2. When this happens the returned xml has a value of "\n".
                // 2) A Class property with an invalid XML name was created.
                // See https://jira.xwiki.org/browse/XWIKI-1855
                // 3) Cannot get the revision as a string from a node version. Not sure why this
                // is happening though... See https://jira.xwiki.org/browse/XWIKI-2076
                LOGGER.warn("Error in revision [" + node.getVersion().toString() + "]: [" + e.getMessage() + "]. Ignoring non-fatal error, the Author, Comment and Date are not set.");
            }
        }
        XWikiRCSNodeContent content = new XWikiRCSNodeContent(nodeInfo.getId());
        content.setPatch(new XWikiPatch(node.getTextString(), node.isDiff()));
        nodeInfo.setContent(content);
        result.add(nodeInfo);
        result.add(content);
    }
    // Ensure that the latest revision is set set to have the full content and not a diff.
    if (result.size() > 0) {
        ((XWikiRCSNodeInfo) ((ArrayList) result).get(0)).setDiff(false);
        ((XWikiRCSNodeContent) ((ArrayList) result).get(1)).getPatch().setDiff(false);
    }
    return result;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Collection(java.util.Collection) ToString(org.suigeneris.jrcs.util.ToString) XWikiException(com.xpn.xwiki.XWikiException) InvalidTrunkVersionNumberException(org.suigeneris.jrcs.rcs.InvalidTrunkVersionNumberException) PatchFailedException(org.suigeneris.jrcs.diff.PatchFailedException) InvalidFileFormatException(org.suigeneris.jrcs.rcs.InvalidFileFormatException) ParseException(org.suigeneris.jrcs.rcs.parse.ParseException) DecoderException(org.apache.commons.codec.DecoderException) NodeNotFoundException(org.suigeneris.jrcs.rcs.impl.NodeNotFoundException)

Aggregations

PatchFailedException (org.suigeneris.jrcs.diff.PatchFailedException)2 InvalidFileFormatException (org.suigeneris.jrcs.rcs.InvalidFileFormatException)2 ToString (org.suigeneris.jrcs.util.ToString)2 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 StringTokenizer (java.util.StringTokenizer)1 DecoderException (org.apache.commons.codec.DecoderException)1 DifferentiationFailedException (org.suigeneris.jrcs.diff.DifferentiationFailedException)1 Revision (org.suigeneris.jrcs.diff.Revision)1 AddDelta (org.suigeneris.jrcs.diff.delta.AddDelta)1 Chunk (org.suigeneris.jrcs.diff.delta.Chunk)1 DeleteDelta (org.suigeneris.jrcs.diff.delta.DeleteDelta)1 InvalidTrunkVersionNumberException (org.suigeneris.jrcs.rcs.InvalidTrunkVersionNumberException)1 NodeNotFoundException (org.suigeneris.jrcs.rcs.impl.NodeNotFoundException)1 ParseException (org.suigeneris.jrcs.rcs.parse.ParseException)1