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