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);
}
}
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();
}
}
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;
}
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;
}
Aggregations