Search in sources :

Example 1 with ToString

use of org.suigeneris.jrcs.util.ToString in project xwiki-platform by xwiki.

the class ListAttachmentArchive method fromRCS.

/**
 * @param rcsArchive the RCS archive to import.
 * @throws Exception if getting a revision from the RCS archive or deserializing an attachment from XML fails
 */
private void fromRCS(final Archive rcsArchive) throws Exception {
    if (rcsArchive == null) {
        return;
    }
    final Node[] nodes = rcsArchive.changeLog();
    for (int i = nodes.length - 1; i > -1; i--) {
        final Object[] lines = rcsArchive.getRevision(nodes[i].getVersion());
        final StringBuilder content = new StringBuilder();
        for (int j = 0; j < lines.length; j++) {
            String line = lines[j].toString();
            content.append(line);
            if (j != lines.length - 1) {
                content.append("\n");
            }
        }
        final XWikiAttachment rev = new XWikiAttachment();
        rev.fromXML(content.toString());
        rev.setDoc(getAttachment().getDoc());
        rev.setAttachment_archive(this);
        // this should not be necessary, keeping to maintain behavior.
        rev.setVersion(nodes[i].getVersion().toString());
        revisions.add(rev);
    }
}
Also used : Node(org.suigeneris.jrcs.rcs.impl.Node) ToString(org.suigeneris.jrcs.util.ToString) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment)

Example 2 with ToString

use of org.suigeneris.jrcs.util.ToString in project xwiki-platform by xwiki.

the class XWikiDocument method getLastChanges.

public List<Delta> getLastChanges(XWikiContext context) throws XWikiException, DifferentiationFailedException {
    Version version = getRCSVersion();
    try {
        String prev = getDocumentArchive(context).getPrevVersion(version).toString();
        XWikiDocument prevDoc = context.getWiki().getDocument(this, prev, context);
        return getDeltas(Diff.diff(ToString.stringToArray(prevDoc.getContent()), ToString.stringToArray(getContent())));
    } catch (Exception ex) {
        LOGGER.debug("Exception getting differences from previous version: " + ex.getMessage());
    }
    return new ArrayList<Delta>();
}
Also used : Version(org.suigeneris.jrcs.rcs.Version) ArrayList(java.util.ArrayList) ToString(org.suigeneris.jrcs.util.ToString) XWikiException(com.xpn.xwiki.XWikiException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DifferentiationFailedException(org.suigeneris.jrcs.diff.DifferentiationFailedException) ExecutionContextException(org.xwiki.context.ExecutionContextException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MissingParserException(org.xwiki.rendering.parser.MissingParserException) IOException(java.io.IOException) ParseException(org.xwiki.rendering.parser.ParseException) TransformationException(org.xwiki.rendering.transformation.TransformationException) QueryException(org.xwiki.query.QueryException)

Example 3 with ToString

use of org.suigeneris.jrcs.util.ToString in project xwiki-platform by xwiki.

the class XWikiDocument method getRevisions.

/**
 * Get document versions matching criterias like author, minimum creation date, etc.
 *
 * @param criteria criteria used to match versions
 * @return a list of matching versions
 */
public List<String> getRevisions(RevisionCriteria criteria, XWikiContext context) throws XWikiException {
    List<String> results = new ArrayList<String>();
    Version[] revisions = getRevisions(context);
    XWikiRCSNodeInfo nextNodeinfo = null;
    XWikiRCSNodeInfo nodeinfo;
    for (Version revision : revisions) {
        nodeinfo = nextNodeinfo;
        nextNodeinfo = getRevisionInfo(revision.toString(), context);
        if (nodeinfo == null) {
            continue;
        }
        // Minor/Major version matching
        if (criteria.getIncludeMinorVersions() || !nextNodeinfo.isMinorEdit()) {
            // Author matching
            if (criteria.getAuthor().equals("") || criteria.getAuthor().equals(nodeinfo.getAuthor())) {
                // Date range matching
                Date versionDate = nodeinfo.getDate();
                if (versionDate.after(criteria.getMinDate()) && versionDate.before(criteria.getMaxDate())) {
                    results.add(nodeinfo.getVersion().toString());
                }
            }
        }
    }
    nodeinfo = nextNodeinfo;
    if (nodeinfo != null) {
        if (criteria.getAuthor().equals("") || criteria.getAuthor().equals(nodeinfo.getAuthor())) {
            // Date range matching
            Date versionDate = nodeinfo.getDate();
            if (versionDate.after(criteria.getMinDate()) && versionDate.before(criteria.getMaxDate())) {
                results.add(nodeinfo.getVersion().toString());
            }
        }
    }
    return criteria.getRange().subList(results);
}
Also used : Version(org.suigeneris.jrcs.rcs.Version) XWikiRCSNodeInfo(com.xpn.xwiki.doc.rcs.XWikiRCSNodeInfo) ArrayList(java.util.ArrayList) ToString(org.suigeneris.jrcs.util.ToString) Date(java.util.Date)

Example 4 with ToString

use of org.suigeneris.jrcs.util.ToString 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&lt;String&gt; 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 5 with ToString

use of org.suigeneris.jrcs.util.ToString in project xwiki-platform by xwiki.

the class XWikiAttachmentArchive method getRevision.

/**
 * Get an old revision of the attachment which this is an archive of.
 *
 * @param attachment This attachment will be used to get the document to associate the attachment revision with.
 * @param rev a String representation of the version to load.
 * @param context the context for the request which needed this revision.
 * @return an XWikiAttachment for the given revision.
 * @throws XWikiException if any Exception is thrown while getting the revision.
 */
public XWikiAttachment getRevision(final XWikiAttachment attachment, final String rev, final XWikiContext context) throws XWikiException {
    try {
        final Archive rcsArchive = getRCSArchive();
        if (rcsArchive == null) {
            // No archive means there is no history and only the current version.
            return this.attachment.getVersion().equals(rev) ? this.attachment : null;
        }
        final Version version = rcsArchive.getRevisionVersion(rev);
        if (version == null) {
            // The requested revision doesn't exist.
            return null;
        }
        final Object[] lines = rcsArchive.getRevision(version);
        final StringBuilder content = new StringBuilder();
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i].toString();
            content.append(line);
            if (i != lines.length - 1) {
                content.append("\n");
            }
        }
        final String scontent = content.toString();
        final XWikiAttachment revattach = new XWikiAttachment();
        revattach.fromXML(scontent);
        revattach.setDoc(attachment.getDoc(), false);
        revattach.setVersion(rev);
        return revattach;
    } catch (Exception e) {
        final Object[] args = { attachment.getFilename() };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_ATTACHMENT_ARCHIVEFORMAT, GENERIC_EXCEPTION_MESSAGE, e, args);
    }
}
Also used : Archive(org.suigeneris.jrcs.rcs.Archive) Version(org.suigeneris.jrcs.rcs.Version) ToString(org.suigeneris.jrcs.util.ToString) XWikiException(com.xpn.xwiki.XWikiException) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

ToString (org.suigeneris.jrcs.util.ToString)6 XWikiException (com.xpn.xwiki.XWikiException)3 ArrayList (java.util.ArrayList)3 Version (org.suigeneris.jrcs.rcs.Version)3 DifferentiationFailedException (org.suigeneris.jrcs.diff.DifferentiationFailedException)2 PatchFailedException (org.suigeneris.jrcs.diff.PatchFailedException)2 InvalidFileFormatException (org.suigeneris.jrcs.rcs.InvalidFileFormatException)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 XWikiRCSNodeInfo (com.xpn.xwiki.doc.rcs.XWikiRCSNodeInfo)1 IOException (java.io.IOException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 StringTokenizer (java.util.StringTokenizer)1 DecoderException (org.apache.commons.codec.DecoderException)1 Revision (org.suigeneris.jrcs.diff.Revision)1 AddDelta (org.suigeneris.jrcs.diff.delta.AddDelta)1 Chunk (org.suigeneris.jrcs.diff.delta.Chunk)1