use of org.suigeneris.jrcs.rcs.InvalidFileFormatException 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.rcs.InvalidFileFormatException in project xwiki-platform by xwiki.
the class XWikiRCSArchive method getNodes.
/**
* @return Collection of pairs [{@link XWikiRCSNodeInfo}, {@link XWikiRCSNodeContent}]
* @param docId - docId which will be wrote in {@link XWikiRCSNodeId#setDocId(long)}
* @throws PatchFailedException
* @throws InvalidFileFormatException
* @throws NodeNotFoundException
*/
public Collection getNodes(long docId) throws NodeNotFoundException, InvalidFileFormatException, PatchFailedException {
Collection result = new ArrayList(this.nodes.values().size());
for (Iterator it = this.nodes.values().iterator(); it.hasNext(); ) {
XWikiJRCSNode node = new XWikiJRCSNode((Node) it.next());
XWikiRCSNodeInfo nodeInfo = new XWikiRCSNodeInfo();
nodeInfo.setId(new XWikiRCSNodeId(docId, node.getVersion()));
nodeInfo.setDiff(node.isDiff());
if (!node.hasOldFormat()) {
nodeInfo.setAuthor(node.getAuthor1());
nodeInfo.setComment(node.getLog());
nodeInfo.setDate(node.getDate());
} else {
// ones from a XWikiDocment object that we construct using the archive content.
try {
String xml = getRevisionAsString(node.getVersion());
XWikiDocument doc = new XWikiDocument();
doc.fromXML(xml);
// set this fields from old document
nodeInfo.setAuthor(doc.getAuthor());
nodeInfo.setComment(doc.getComment());
nodeInfo.setDate(doc.getDate());
} catch (Exception e) {
// 3 potential known errors:
// 1) Revision 1.1 doesn't exist. Some time in the past there was a bug in XWiki where version
// were starting at 1.2. When this happens the returned xml has a value of "\n".
// 2) A Class property with an invalid XML name was created.
// See https://jira.xwiki.org/browse/XWIKI-1855
// 3) Cannot get the revision as a string from a node version. Not sure why this
// is happening though... See https://jira.xwiki.org/browse/XWIKI-2076
LOGGER.warn("Error in revision [" + node.getVersion().toString() + "]: [" + e.getMessage() + "]. Ignoring non-fatal error, the Author, Comment and Date are not set.");
}
}
XWikiRCSNodeContent content = new XWikiRCSNodeContent(nodeInfo.getId());
content.setPatch(new XWikiPatch(node.getTextString(), node.isDiff()));
nodeInfo.setContent(content);
result.add(nodeInfo);
result.add(content);
}
// Ensure that the latest revision is set set to have the full content and not a diff.
if (result.size() > 0) {
((XWikiRCSNodeInfo) ((ArrayList) result).get(0)).setDiff(false);
((XWikiRCSNodeContent) ((ArrayList) result).get(1)).getPatch().setDiff(false);
}
return result;
}
Aggregations