Author: vmassol
Date: 2008-01-25 11:26:04 +0100 (Fri, 25 Jan 2008)
New Revision: 7082
Modified:
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/doc/XWikiDocumentArchive.java
Log:
Merged old code reformatting from Sergiu (rev 6509)
+ improved exception reporting (revs 7079 and 7080)
Modified:
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/doc/XWikiDocumentArchive.java
===================================================================
---
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/doc/XWikiDocumentArchive.java
2008-01-25 10:16:07 UTC (rev 7081)
+++
xwiki-platform/core/branches/xwiki-core-1.2/xwiki-core/src/main/java/com/xpn/xwiki/doc/XWikiDocumentArchive.java
2008-01-25 10:26:04 UTC (rev 7082)
@@ -31,8 +31,6 @@
import java.util.TreeMap;
import java.util.TreeSet;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import org.suigeneris.jrcs.rcs.Version;
import org.suigeneris.jrcs.util.ToString;
@@ -43,49 +41,56 @@
import com.xpn.xwiki.doc.rcs.XWikiRCSNodeContent;
import com.xpn.xwiki.doc.rcs.XWikiRCSNodeId;
import com.xpn.xwiki.doc.rcs.XWikiRCSNodeInfo;
-import com.xpn.xwiki.plugin.packaging.PackagePlugin;
/**
- * Contains document history.
- * Allows to load any version of document.
- * @version $Id: $
+ * Contains document history. Allows to load any version of document.
+ *
+ * @version $Id: $
*/
public class XWikiDocumentArchive
{
- /** logger. */
- private static final Log LOG =
LogFactory.getLog(XWikiDocumentArchive.class);
-
/** =docId. */
private long id;
+
/** SortedMap from Version to XWikiRCSNodeInfo. */
- private SortedMap versionToNode = new TreeMap();
- /** SortedSet of Version - versions which has full document, not patch.
- * Latest version is always full. */
- private SortedSet fullVersions = new TreeSet();
-
+ private SortedMap versionToNode = new TreeMap();
+
+ /**
+ * SortedSet of Version - versions which has full document, not patch.
Latest version is always
+ * full.
+ */
+ private SortedSet fullVersions = new TreeSet();
+
// store-specific information
/** Set of [EMAIL PROTECTED] XWikiRCSNodeInfo} which need to delete. */
private Set deletedNodes = new TreeSet();
+
/** Set of [EMAIL PROTECTED] XWikiRCSNodeInfo} which need to saveOrUpdate.
*/
private Set updatedNodeInfos = new TreeSet();
+
/** Set of [EMAIL PROTECTED] XWikiRCSNodeContent} which need to update. */
private Set updatedNodeContents = new TreeSet();
-
+
/** @param id = [EMAIL PROTECTED] XWikiDocument#getId()} */
- public XWikiDocumentArchive(long id) {
+ public XWikiDocumentArchive(long id)
+ {
this();
setId(id);
}
+
/** default constructor. */
- public XWikiDocumentArchive() { }
-
+ public XWikiDocumentArchive()
+ {
+ }
+
// helper methods
/**
* @param cur - current version
* @param isMinor - is modification is minor
* @return next version
*/
- protected Version createNextVersion(Version cur, boolean isMinor) {
+ protected Version createNextVersion(Version cur, boolean isMinor)
+ {
Version result;
if (cur == null) {
result = new Version(1, 1);
@@ -96,8 +101,10 @@
}
return result;
}
+
/** @param node - node added to versionToNode and fullNodes */
- protected void updateNode(XWikiRCSNodeInfo node) {
+ protected void updateNode(XWikiRCSNodeInfo node)
+ {
Version ver = node.getId().getVersion();
versionToNode.put(ver, node);
if (!node.isDiff()) {
@@ -106,11 +113,13 @@
fullVersions.remove(ver);
}
}
+
/**
* Make a patch. It is store only modified nodes(latest). New nodes need
be saved after.
- * @param newnode - new node information
- * @param doc - document for that patch created
- * @param context - used for loading node contents and generating xml
+ *
+ * @param newnode - new node information
+ * @param doc - document for that patch created
+ * @param context - used for loading node contents and generating xml
* @return node content for newnode
* @throws XWikiException if exception while loading content
*/
@@ -123,11 +132,12 @@
XWikiRCSNodeInfo latestNode = getLatestNode();
if (latestNode != null) {
int nodesCount = getNodes().size();
- int nodesPerFull = context.getWiki() == null ? 5 :
Integer.parseInt(
-
context.getWiki().getConfig().getProperty("xwiki.store.rcs.nodesPerFull", "5"));
+ int nodesPerFull =
+ context.getWiki() == null ? 5 :
Integer.parseInt(context.getWiki().getConfig()
+ .getProperty("xwiki.store.rcs.nodesPerFull", "5"));
if (nodesPerFull <= 0 || (nodesCount % nodesPerFull) != 0) {
XWikiRCSNodeContent latestContent =
latestNode.getContent(context);
- latestContent.getPatch().setDiffVersion(doc,
+ latestContent.getPatch().setDiffVersion(doc,
latestContent.getPatch().getContent(), context);
latestNode.setContent(latestContent);
updateNode(latestNode);
@@ -136,30 +146,40 @@
}
return result;
}
- /** @return [EMAIL PROTECTED] XWikiDocument#getId()} - primary key */
- public long getId() {
+
+ /** @return [EMAIL PROTECTED] XWikiDocument#getId()} - primary key */
+ public long getId()
+ {
return id;
}
+
/** @param id = [EMAIL PROTECTED] XWikiDocument#getId()} */
- public void setId(long id) {
+ public void setId(long id)
+ {
this.id = id;
}
+
/** @return collection of XWikiRCSNodeInfo order by version desc */
- public Collection getNodes() {
+ public Collection getNodes()
+ {
return versionToNode.values();
}
- /**
+
+ /**
* @return collection of XWikiRCSNodeInfo where vfrom>=version>=vto order
by version desc
* @param vfrom - start version
- * @param vto - end version
+ * @param vto - end version
*/
- public Collection getNodes(Version vfrom, Version vto) {
+ public Collection getNodes(Version vfrom, Version vto)
+ {
int[] ito = vto.getNumbers();
ito[1]--;
return versionToNode.subMap(vfrom, new Version(ito)).values();
}
+
/** @param versions - collection of XWikiRCSNodeInfo */
- public void setNodes(Collection versions) {
+ public void setNodes(Collection versions)
+ {
resetArchive();
for (Iterator it = versions.iterator(); it.hasNext();) {
updateNode((XWikiRCSNodeInfo) it.next());
@@ -170,46 +190,51 @@
updateNode(getLatestNode());
}
}
+
/**
* @param context - used for load nodes content
- * @return serialization of class
- * used in [EMAIL PROTECTED] PackagePlugin}.
- * @throws XWikiException if any error
+ * @return serialization of class used in [EMAIL PROTECTED]
com.xpn.xwiki.plugin.packaging.PackagePlugin}.
+ * @throws XWikiException if any error
*/
- public String getArchive(XWikiContext context) throws XWikiException {
- XWikiRCSArchive archive = new XWikiRCSArchive(getNodes(), context);
+ public String getArchive(XWikiContext context) throws XWikiException
+ {
+ XWikiRCSArchive archive = new XWikiRCSArchive(getNodes(), context);
return archive.toString();
}
+
/**
- * Deserialize class.
- * Used in [EMAIL PROTECTED] PackagePlugin}.
- * @param text - archive in JRCS format
+ * Deserialize class. Used in [EMAIL PROTECTED]
com.xpn.xwiki.plugin.packaging.PackagePlugin}.
+ *
+ * @param text - archive in JRCS format
* @throws XWikiException if parse error
*/
- public void setArchive(String text) throws XWikiException {
+ public void setArchive(String text) throws XWikiException
+ {
try {
XWikiRCSArchive archive = new XWikiRCSArchive(text);
resetArchive();
Collection nodes = archive.getNodes(getId());
for (Iterator it = nodes.iterator(); it.hasNext();) {
- XWikiRCSNodeInfo nodeInfo = (XWikiRCSNodeInfo) it.next();
+ XWikiRCSNodeInfo nodeInfo = (XWikiRCSNodeInfo) it.next();
XWikiRCSNodeContent nodeContent = (XWikiRCSNodeContent)
it.next();
-
updateNode(nodeInfo);
updatedNodeInfos.add(nodeInfo);
updatedNodeContents.add(nodeContent);
}
} catch (Exception e) {
- throw new XWikiException(XWikiException.MODULE_XWIKI_DIFF,
- XWikiException.ERROR_XWIKI_DIFF_CONTENT_ERROR,
- "Exception while constructing document archive", e);
+ Object[] args = {text, getId()};
+ throw new XWikiException(XWikiException.MODULE_XWIKI_DIFF,
+ XWikiException.ERROR_XWIKI_DIFF_CONTENT_ERROR,
+ "Exception while constructing archive for JRCS string [{0}]
for document [{1}]", e, args);
}
}
+
/**
* Update history with new document version.
- * @param doc - document for this version
- * @param author - author of version
- * @param date - date of version
+ *
+ * @param doc - document for this version
+ * @param author - author of version
+ * @param date - date of version
* @param comment - version comment
* @param version - preferably document version in history
* @param context - used for loading nodes content
@@ -228,20 +253,22 @@
newNode.setComment(comment);
newNode.setDate(date);
XWikiRCSNodeContent newContent = makePatch(newNode, doc, context);
-
+
updateNode(newNode);
updatedNodeInfos.add(newNode);
updatedNodeContents.add(newContent);
}
+
/**
* Remove document versions from vfrom to vto, inclusive.
+ *
* @param context - used for loading nodes content
- * @param vfrom - start version
- * @param vto - end version
- * @throws XWikiException if any error
+ * @param vfrom - start version
+ * @param vto - end version
+ * @throws XWikiException if any error
*/
public void removeVersions(Version vfrom, Version vto, XWikiContext
context)
- throws XWikiException
+ throws XWikiException
{
Version vfrom0 = vfrom;
Version vto0 = vto;
@@ -272,7 +299,7 @@
// the deleted versions.
String xmlfrom1 = getVersionXml(vfrom1, context);
String xmlto1 = getVersionXml(vto1, context);
- XWikiRCSNodeInfo nito1 = getNode(vto1);
+ XWikiRCSNodeInfo nito1 = getNode(vto1);
XWikiRCSNodeContent ncto1 = nito1.getContent(context);
ncto1.getPatch().setDiffVersion(xmlfrom1, xmlto1, "");
nito1.setContent(ncto1);
@@ -287,6 +314,7 @@
it.remove();
}
}
+
/**
* @return selected version of document, null if version is not found.
* @param version - which version to load
@@ -298,7 +326,7 @@
{
XWikiRCSNodeInfo nodeInfo = getNode(version);
if (nodeInfo == null) {
- return null;
+ return null;
}
try {
String content = getVersionXml(version, context);
@@ -312,12 +340,23 @@
doc.setMostRecent(version.equals(getLatestVersion()));
return doc;
} catch (Exception e) {
- Object[] args = {version.toString()};
- throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
+ Object[] args = {version.toString(), getId()};
+ throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
XWikiException.ERROR_XWIKI_STORE_RCS_READING_REVISIONS,
- "Exception while reading document version {1}", e, args);
+ "Exception while reading version [{0}] for document id
[{1,number}]", e, args);
}
}
+
+ /**
+ * Return the XML corresponding to a version. If the version node contains
just a diff, then
+ * restore the complete XML by applying all patches from the nearest full
version to the
+ * requested version.
+ *
+ * @param version The version to retrieve.
+ * @param context The [EMAIL PROTECTED] com.xpn.xwiki.XWikiContext
context}.
+ * @return The XML corresponding to the version.
+ * @throws XWikiException If any exception occured.
+ */
public String getVersionXml(Version version, XWikiContext context) throws
XWikiException
{
Version nearestFullVersion = getNearestFullVersion(version);
@@ -331,6 +370,7 @@
return ToString.arrayToString(origText.toArray());
}
+
/**
* @return [EMAIL PROTECTED] XWikiRCSNodeInfo} by version. null if none.
* @param version which version to get
@@ -339,29 +379,36 @@
{
return version == null ? null : (XWikiRCSNodeInfo)
versionToNode.get(version);
}
+
/** @return latest version in history for document. null if none. */
public Version getLatestVersion()
{
return versionToNode.size() == 0 ? null : (Version)
versionToNode.firstKey();
}
+
/** @return latest node in history for document. null if none. */
- public XWikiRCSNodeInfo getLatestNode() {
+ public XWikiRCSNodeInfo getLatestNode()
+ {
return getNode(getLatestVersion());
}
- /**
+
+ /**
* @return next version in history. null if none
* @param ver - current version
- */
- public Version getNextVersion(Version ver) {
+ */
+ public Version getNextVersion(Version ver)
+ {
// headMap is exclusive
SortedMap headmap = versionToNode.headMap(ver);
return (headmap.size() == 0) ? null : (Version) headmap.lastKey();
}
+
/**
* @return previous version in history. null if none
* @param ver - current version
*/
- public Version getPrevVersion(Version ver) {
+ public Version getPrevVersion(Version ver)
+ {
// tailMap is inclusive
SortedMap tailmap = versionToNode.tailMap(ver);
if (tailmap.size() <= 1) {
@@ -371,6 +418,7 @@
it.next();
return (Version) it.next();
}
+
/**
* @param ver - for what version find nearest
* @return nearest version which contain full information (not patch)
@@ -381,8 +429,9 @@
return ver;
}
SortedSet headSet = fullVersions.headSet(ver);
- return (Version) ((headSet.size() == 0) ? null : headSet.last());
+ return (Version) ((headSet.size() == 0) ? null : headSet.last());
}
+
/**
* @return List of [EMAIL PROTECTED] XWikiRCSNodeContent} where
vfrom<=version<=vto order by version
* @param vfrom - start version
@@ -401,6 +450,7 @@
}
return result;
}
+
/** reset history. history becomes empty. */
public void resetArchive()
{
@@ -410,21 +460,25 @@
updatedNodeInfos.clear();
updatedNodeContents.clear();
}
- /** @return mutable Set of [EMAIL PROTECTED] XWikiRCSNodeInfo} which are
need for delete */
+
+ /** @return mutable Set of [EMAIL PROTECTED] XWikiRCSNodeInfo} which are
need for delete */
public Set getDeletedNodeInfo()
{
return deletedNodes;
}
+
/** @return mutable Set of [EMAIL PROTECTED] XWikiRCSNodeInfo} which are
need for saveOrUpdate */
public Set getUpdatedNodeInfos()
{
- return updatedNodeInfos;
+ return updatedNodeInfos;
}
+
/** @return mutable Set of [EMAIL PROTECTED] XWikiRCSNodeContent} which
are need for update */
public Set getUpdatedNodeContents()
{
- return updatedNodeContents;
+ return updatedNodeContents;
}
+
/**
* @return full copy of this archive with specified docId
* @param docId - new [EMAIL PROTECTED] #getId()}
_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications