Search in sources :

Example 1 with Archive

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

the class XWikiAttachmentArchive method getVersions.

/**
 * @return an array of versions which are available for this attachment, ordered by version number descending.
 */
public Version[] getVersions() {
    final Archive rcsArchive = getRCSArchive();
    Version[] versions;
    if (rcsArchive != null) {
        final Node[] nodes = rcsArchive.changeLog();
        versions = new Version[nodes.length];
        for (int i = 0; i < nodes.length; i++) {
            versions[i] = nodes[i].getVersion();
        }
    } else {
        // No archive means there is no history and only the current version
        versions = new Version[] { this.attachment.getRCSVersion() };
    }
    return versions;
}
Also used : Archive(org.suigeneris.jrcs.rcs.Archive) Version(org.suigeneris.jrcs.rcs.Version) Node(org.suigeneris.jrcs.rcs.impl.Node)

Example 2 with Archive

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

the class XWikiAttachmentArchive method updateArchive.

/**
 * Update the archive.
 *
 * @param context the XWikiContext for the request used to load the correct attachment content from the database.
 * @throws XWikiException if anything goes wrong.
 * @since 7.1M1
 */
public void updateArchive(final XWikiContext context) throws XWikiException {
    try {
        this.attachment.incrementVersion();
        this.attachment.setDate(new Date());
        final Object[] lines = ToString.stringToArray(this.attachment.toStringXML(true, false, context));
        if (this.archive != null) {
            this.archive.addRevision(lines, "");
        } else {
            this.archive = new Archive(lines, getAttachment().getFilename(), getAttachment().getVersion());
        }
        // Set a standard author, since by default the operating system user is set, and it might contain confusing
        // characters (JRCS is very fragile and breaks easily if a wrong value is used)
        this.archive.findNode(this.archive.getRevisionVersion()).setAuthor("xwiki");
    } catch (Exception e) {
        Object[] args = { getAttachment().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) Date(java.util.Date) XWikiException(com.xpn.xwiki.XWikiException) XWikiException(com.xpn.xwiki.XWikiException)

Example 3 with Archive

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

the class XWikiAttachmentArchiveTest method getRevisionWhichDoesNotExist.

@Test
public void getRevisionWhichDoesNotExist() throws Exception {
    archive.setRCSArchive(new Archive(new Object[] { "text" }, "file.txt", "1.2"));
    assertNull(archive.getRevision(null, "7.2", null));
}
Also used : Archive(org.suigeneris.jrcs.rcs.Archive) Test(org.junit.Test)

Example 4 with Archive

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

the class XWikiAttachmentArchiveTest method getVersions.

@Test
public void getVersions() throws Exception {
    Archive rcsArchive = new Archive(new Object[] { "line" }, "file.txt", "5.2");
    rcsArchive.addRevision(new Object[] { "line modified" }, "");
    archive.setRCSArchive(rcsArchive);
    assertArrayEquals(new Version[] { new Version(5, 2), new Version(5, 3) }, archive.getVersions());
}
Also used : Archive(org.suigeneris.jrcs.rcs.Archive) Version(org.suigeneris.jrcs.rcs.Version) Test(org.junit.Test)

Example 5 with Archive

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

Archive (org.suigeneris.jrcs.rcs.Archive)7 Version (org.suigeneris.jrcs.rcs.Version)4 XWikiException (com.xpn.xwiki.XWikiException)3 XWikiAttachmentArchive (com.xpn.xwiki.doc.XWikiAttachmentArchive)2 Test (org.junit.Test)2 ToString (org.suigeneris.jrcs.util.ToString)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Date (java.util.Date)1 Node (org.suigeneris.jrcs.rcs.impl.Node)1