Search in sources :

Example 21 with Version

use of org.suigeneris.jrcs.rcs.Version 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)

Example 22 with Version

use of org.suigeneris.jrcs.rcs.Version in project xwiki-platform by xwiki.

the class XWikiDocumentArchive method removeVersions.

/**
 * Remove document versions from vfrom to vto, inclusive.
 *
 * @param newerVersion - start version
 * @param olderVersion - end version
 * @param context - used for loading nodes content
 * @throws XWikiException if any error
 */
public void removeVersions(Version newerVersion, Version olderVersion, XWikiContext context) throws XWikiException {
    Version upperBound = newerVersion;
    Version lowerBound = olderVersion;
    if (upperBound.compareVersions(lowerBound) < 0) {
        Version tmp = upperBound;
        upperBound = lowerBound;
        lowerBound = tmp;
    }
    Version firstVersionAfter = getNextVersion(upperBound);
    Version firstVersionBefore = getPrevVersion(lowerBound);
    if (firstVersionAfter == null && firstVersionBefore == null) {
        resetArchive();
        return;
    }
    if (firstVersionAfter == null) {
        // Deleting the most recent version.
        // Store full version in firstVersionBefore
        String xmlBefore = getVersionXml(firstVersionBefore, context);
        XWikiRCSNodeInfo niBefore = getNode(firstVersionBefore);
        XWikiRCSNodeContent ncBefore = niBefore.getContent(context);
        ncBefore.getPatch().setFullVersion(xmlBefore);
        niBefore.setContent(ncBefore);
        updateNode(niBefore);
        getUpdatedNodeContents().add(ncBefore);
    } else if (firstVersionBefore != null) {
        // We're not deleting from the first version, so we must make a new diff jumping over
        // the deleted versions.
        String xmlAfter = getVersionXml(firstVersionAfter, context);
        String xmlBefore = getVersionXml(firstVersionBefore, context);
        XWikiRCSNodeInfo niBefore = getNode(firstVersionBefore);
        XWikiRCSNodeContent ncBefore = niBefore.getContent(context);
        ncBefore.getPatch().setDiffVersion(xmlBefore, xmlAfter, "");
        niBefore.setContent(ncBefore);
        updateNode(niBefore);
        getUpdatedNodeContents().add(ncBefore);
    }
    // if (firstVersionBefore == null) => nothing else to do, except delete
    for (Iterator<XWikiRCSNodeInfo> it = getNodes(upperBound, lowerBound).iterator(); it.hasNext(); ) {
        XWikiRCSNodeInfo ni = it.next();
        this.fullVersions.remove(ni.getId().getVersion());
        this.deletedNodes.add(ni);
        it.remove();
    }
}
Also used : XWikiRCSNodeContent(com.xpn.xwiki.doc.rcs.XWikiRCSNodeContent) Version(org.suigeneris.jrcs.rcs.Version) XWikiRCSNodeInfo(com.xpn.xwiki.doc.rcs.XWikiRCSNodeInfo) ToString(org.suigeneris.jrcs.util.ToString)

Example 23 with Version

use of org.suigeneris.jrcs.rcs.Version in project xwiki-platform by xwiki.

the class ListAttachmentArchive method toRCS.

/**
 * Convert this attachment archive into JRCS format.
 *
 * @param context the XWikiContext for the request.
 * @return this archive in JRCS format.
 * @throws Exception if something goes wrong while serializing the attachment to XML or inserting it into the RCS
 * archive.
 */
private Archive toRCS(final XWikiContext context) throws Exception {
    final Version[] versions = this.getVersions();
    Archive rcsArch = null;
    for (XWikiAttachment rev : this.revisions) {
        final String sdata = rev.toStringXML(true, false, context);
        final Object[] lines = ToString.stringToArray(sdata);
        if (rcsArch == null) {
            // First cycle.
            rcsArch = new Archive(lines, rev.getFilename(), rev.getVersion());
        } else {
            rcsArch.addRevision(lines, "");
        }
    }
    return rcsArch;
}
Also used : XWikiAttachmentArchive(com.xpn.xwiki.doc.XWikiAttachmentArchive) Archive(org.suigeneris.jrcs.rcs.Archive) Version(org.suigeneris.jrcs.rcs.Version) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) ToString(org.suigeneris.jrcs.util.ToString)

Example 24 with Version

use of org.suigeneris.jrcs.rcs.Version in project xwiki-platform by xwiki.

the class ListAttachmentArchive method getVersions.

@Override
public Version[] getVersions() {
    final Version[] versions = new Version[this.revisions.size()];
    int i = this.revisions.size();
    for (XWikiAttachment attach : this.revisions) {
        i--;
        versions[i] = attach.getRCSVersion();
    }
    return versions;
}
Also used : Version(org.suigeneris.jrcs.rcs.Version) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment)

Aggregations

Version (org.suigeneris.jrcs.rcs.Version)24 XWikiException (com.xpn.xwiki.XWikiException)7 ToString (org.suigeneris.jrcs.util.ToString)6 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)5 ArrayList (java.util.ArrayList)5 DocumentReference (org.xwiki.model.reference.DocumentReference)5 XWikiRCSNodeInfo (com.xpn.xwiki.doc.rcs.XWikiRCSNodeInfo)4 Date (java.util.Date)4 Archive (org.suigeneris.jrcs.rcs.Archive)4 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)3 XWikiDocumentArchive (com.xpn.xwiki.doc.XWikiDocumentArchive)3 XWikiRCSNodeContent (com.xpn.xwiki.doc.rcs.XWikiRCSNodeContent)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 Test (org.junit.Test)2 Document (com.xpn.xwiki.api.Document)1 XWikiAttachmentArchive (com.xpn.xwiki.doc.XWikiAttachmentArchive)1 XWikiRCSNodeId (com.xpn.xwiki.doc.rcs.XWikiRCSNodeId)1 IOException (java.io.IOException)1 URL (java.net.URL)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1