Index: src/main/java/com/xpn/xwiki/api/Document.java
===================================================================
--- src/main/java/com/xpn/xwiki/api/Document.java	(révision 4505)
+++ src/main/java/com/xpn/xwiki/api/Document.java	(copie de travail)
@@ -53,8 +53,8 @@
 
 public class Document extends Api
 {
-    protected XWikiDocument clonedDoc = null;
     protected XWikiDocument doc;
+    protected boolean cloned = false;
 
     protected Object currentObj;
 
@@ -79,9 +79,12 @@
 
     protected XWikiDocument getDoc()
     {
-        if (clonedDoc==null)
-         clonedDoc = (XWikiDocument) doc.clone();
-        return clonedDoc;
+        if (!cloned) {
+            doc = (XWikiDocument) doc.clone();
+            cloned = true;
+        }
+        
+        return doc;
     }
 
     /**
@@ -495,7 +498,7 @@
 
     public Class getxWikiClass()
     {
-        BaseClass bclass = getDoc().getxWikiClass();
+        BaseClass bclass = doc.getxWikiClass();
         if (bclass == null) {
             return null;
         } else {
@@ -505,7 +508,7 @@
 
     public Class[] getxWikiClasses()
     {
-        List list = getDoc().getxWikiClasses(getXWikiContext());
+        List list = doc.getxWikiClasses(getXWikiContext());
         if (list == null) {
             return null;
         }
@@ -534,12 +537,12 @@
 
     public int getObjectNumbers(String classname)
     {
-        return getDoc().getObjectNumbers(classname);
+        return doc.getObjectNumbers(classname);
     }
 
     public Map getxWikiObjects()
     {
-        Map map = getDoc().getxWikiObjects();
+        Map map = doc.getxWikiObjects();
         Map resultmap = new HashMap();
         for (Iterator it = map.keySet().iterator(); it.hasNext();) {
             String name = (String) it.next();
@@ -568,14 +571,14 @@
 
     public Vector getObjects(String classname)
     {
-        Vector objects = getDoc().getObjects(classname);
+        Vector objects = doc.getObjects(classname);
         return getObjects(objects);
     }
 
     public Object getFirstObject(String fieldname)
     {
         try {
-            BaseObject obj = getDoc().getFirstObject(fieldname, getXWikiContext());
+            BaseObject obj = doc.getFirstObject(fieldname, getXWikiContext());
             if (obj == null) {
                 return null;
             } else {
@@ -589,7 +592,7 @@
     public Object getObject(String classname, String key, String value, boolean failover)
     {
         try {
-            BaseObject obj = getDoc().getObject(classname, key, value, failover);
+            BaseObject obj = doc.getObject(classname, key, value, failover);
             if (obj == null) {
                 return null;
             } else {
@@ -603,7 +606,7 @@
     public Object getObject(String classname, String key, String value)
     {
         try {
-            BaseObject obj = getDoc().getObject(classname, key, value);
+            BaseObject obj = doc.getObject(classname, key, value);
             if (obj == null) {
                 return null;
             } else {
@@ -626,7 +629,7 @@
     public Object getObject(String classname, boolean create)
     {
         try {
-            BaseObject obj = getDoc().getObject(classname, create, getXWikiContext());
+            BaseObject obj = doc.getObject(classname, create, getXWikiContext());
 
             if (obj == null) {
                 return null;
@@ -641,7 +644,7 @@
     public Object getObject(String classname, int nb)
     {
         try {
-            BaseObject obj = getDoc().getObject(classname, nb);
+            BaseObject obj = doc.getObject(classname, nb);
             if (obj == null) {
                 return null;
             } else {
@@ -703,7 +706,7 @@
 
     public List getAttachmentList()
     {
-        List list = getDoc().getAttachmentList();
+        List list = doc.getAttachmentList();
         List list2 = new ArrayList();
         for (int i = 0; i < list.size(); i++) {
             list2.add(new Attachment(this, (XWikiAttachment) list.get(i), getXWikiContext()));
@@ -952,7 +955,7 @@
 
     public Attachment getAttachment(String filename)
     {
-        XWikiAttachment attach = getDoc().getAttachment(filename);
+        XWikiAttachment attach = doc.getAttachment(filename);
         if (attach == null) {
             return null;
         } else {
@@ -969,14 +972,14 @@
             }
             if (origdoc == null) {
                 return doc.getContentDiff(new XWikiDocument(newdoc.getSpace(), newdoc.getName()),
-                    newdoc.getDoc(), getXWikiContext());
+                    newdoc.doc, getXWikiContext());
             }
             if (newdoc == null) {
-                return doc.getContentDiff(origdoc.getDoc(),
+                return doc.getContentDiff(origdoc.doc,
                     new XWikiDocument(origdoc.getSpace(), origdoc.getName()), getXWikiContext());
             }
 
-            return doc.getContentDiff(origdoc.getDoc(), newdoc.getDoc(), getXWikiContext());
+            return doc.getContentDiff(origdoc.doc, newdoc.doc, getXWikiContext());
         } catch (Exception e) {
             java.lang.Object[] args =
                 {origdoc.getFullName(), origdoc.getVersion(), newdoc.getVersion()};
@@ -1000,14 +1003,14 @@
             }
             if (origdoc == null) {
                 return doc.getXMLDiff(new XWikiDocument(newdoc.getSpace(), newdoc.getName()),
-                    newdoc.getDoc(), getXWikiContext());
+                    newdoc.doc, getXWikiContext());
             }
             if (newdoc == null) {
-                return doc.getXMLDiff(origdoc.getDoc(),
+                return doc.getXMLDiff(origdoc.doc,
                     new XWikiDocument(origdoc.getSpace(), origdoc.getName()), getXWikiContext());
             }
 
-            return doc.getXMLDiff(origdoc.getDoc(), newdoc.getDoc(), getXWikiContext());
+            return doc.getXMLDiff(origdoc.doc, newdoc.doc, getXWikiContext());
         } catch (Exception e) {
             java.lang.Object[] args =
                 {origdoc.getFullName(), origdoc.getVersion(), newdoc.getVersion()};
@@ -1030,15 +1033,15 @@
             }
             if (origdoc == null) {
                 return doc.getRenderedContentDiff(
-                    new XWikiDocument(newdoc.getSpace(), newdoc.getName()), newdoc.getDoc(),
+                    new XWikiDocument(newdoc.getSpace(), newdoc.getName()), newdoc.doc,
                     getXWikiContext());
             }
             if (newdoc == null) {
-                return doc.getRenderedContentDiff(origdoc.getDoc(),
+                return doc.getRenderedContentDiff(origdoc.doc,
                     new XWikiDocument(origdoc.getSpace(), origdoc.getName()), getXWikiContext());
             }
 
-            return doc.getRenderedContentDiff(origdoc.getDoc(), newdoc.getDoc(), getXWikiContext());
+            return doc.getRenderedContentDiff(origdoc.doc, newdoc.doc, getXWikiContext());
         } catch (Exception e) {
             java.lang.Object[] args =
                 {origdoc.getFullName(), origdoc.getVersion(), newdoc.getVersion()};
@@ -1061,14 +1064,14 @@
             }
             if (origdoc == null) {
                 return doc.getMetaDataDiff(new XWikiDocument(newdoc.getSpace(), newdoc.getName()),
-                    newdoc.getDoc(), getXWikiContext());
+                    newdoc.doc, getXWikiContext());
             }
             if (newdoc == null) {
-                return doc.getMetaDataDiff(origdoc.getDoc(),
+                return doc.getMetaDataDiff(origdoc.doc,
                     new XWikiDocument(origdoc.getSpace(), origdoc.getName()), getXWikiContext());
             }
 
-            return doc.getMetaDataDiff(origdoc.getDoc(), newdoc.getDoc(), getXWikiContext());
+            return doc.getMetaDataDiff(origdoc.doc, newdoc.doc, getXWikiContext());
         } catch (Exception e) {
             java.lang.Object[] args =
                 {origdoc.getFullName(), origdoc.getVersion(), newdoc.getVersion()};
@@ -1090,16 +1093,16 @@
                 return new ArrayList();
             }
             if (origdoc == null) {
-                return getDoc().getObjectDiff(
-                    new XWikiDocument(newdoc.getSpace(), newdoc.getName()), newdoc.getDoc(),
+                return doc.getObjectDiff(
+                    new XWikiDocument(newdoc.getSpace(), newdoc.getName()), newdoc.doc,
                     getXWikiContext());
             }
             if (newdoc == null) {
-                return getDoc().getObjectDiff(origdoc.getDoc(),
+                return doc.getObjectDiff(origdoc.doc,
                     new XWikiDocument(origdoc.getSpace(), origdoc.getName()), getXWikiContext());
             }
 
-            return getDoc().getObjectDiff(origdoc.getDoc(), newdoc.getDoc(), getXWikiContext());
+            return doc.getObjectDiff(origdoc.doc, newdoc.doc, getXWikiContext());
         } catch (Exception e) {
             java.lang.Object[] args =
                 {origdoc.getFullName(), origdoc.getVersion(), newdoc.getVersion()};
@@ -1122,14 +1125,14 @@
             }
             if (origdoc == null) {
                 return doc.getClassDiff(new XWikiDocument(newdoc.getSpace(), newdoc.getName()),
-                    newdoc.getDoc(), getXWikiContext());
+                    newdoc.doc, getXWikiContext());
             }
             if (newdoc == null) {
-                return doc.getClassDiff(origdoc.getDoc(),
+                return doc.getClassDiff(origdoc.doc,
                     new XWikiDocument(origdoc.getSpace(), origdoc.getName()), getXWikiContext());
             }
 
-            return doc.getClassDiff(origdoc.getDoc(), newdoc.getDoc(), getXWikiContext());
+            return doc.getClassDiff(origdoc.doc, newdoc.doc, getXWikiContext());
         } catch (Exception e) {
             java.lang.Object[] args =
                 {origdoc.getFullName(), origdoc.getVersion(), newdoc.getVersion()};
@@ -1151,16 +1154,16 @@
                 return new ArrayList();
             }
             if (origdoc == null) {
-                return getDoc().getObjectDiff(
-                    new XWikiDocument(newdoc.getSpace(), newdoc.getName()), newdoc.getDoc(),
+                return doc.getObjectDiff(
+                    new XWikiDocument(newdoc.getSpace(), newdoc.getName()), newdoc.doc,
                     getXWikiContext());
             }
             if (newdoc == null) {
-                return getDoc().getObjectDiff(origdoc.getDoc(),
+                return doc.getObjectDiff(origdoc.doc,
                     new XWikiDocument(origdoc.getSpace(), origdoc.getName()), getXWikiContext());
             }
 
-            return getDoc().getAttachmentDiff(origdoc.getDoc(), newdoc.getDoc(), getXWikiContext());
+            return doc.getAttachmentDiff(origdoc.doc, newdoc.doc, getXWikiContext());
         } catch (Exception e) {
             java.lang.Object[] args =
                 {origdoc.getFullName(), origdoc.getVersion(), newdoc.getVersion()};
@@ -1201,7 +1204,7 @@
     public boolean checkAccess(String right)
     {
         try {
-            return getXWikiContext().getWiki().checkAccess(right, getDoc(), getXWikiContext());
+            return getXWikiContext().getWiki().checkAccess(right, doc, getXWikiContext());
         } catch (XWikiException e) {
             return false;
         }
@@ -1211,7 +1214,7 @@
     {
         try {
             return getXWikiContext().getWiki().getRightService().hasAccessLevel(level,
-                getXWikiContext().getUser(), getDoc().getFullName(), getXWikiContext());
+                getXWikiContext().getUser(), doc.getFullName(), getXWikiContext());
         } catch (Exception e) {
             return false;
         }
@@ -1272,20 +1275,20 @@
     public java.lang.Object get(String classOrFieldName)
     {
         if (currentObj != null) {
-            return getDoc().display(classOrFieldName, currentObj.getBaseObject(), getXWikiContext());
+            return doc.display(classOrFieldName, currentObj.getBaseObject(), getXWikiContext());
         }
-        BaseObject object = getDoc().getFirstObject(classOrFieldName, getXWikiContext());
+        BaseObject object = doc.getFirstObject(classOrFieldName, getXWikiContext());
         if (object != null) {
-            return getDoc().display(classOrFieldName, object, getXWikiContext());
+            return doc.display(classOrFieldName, object, getXWikiContext());
         }
-        return getDoc().getObject(classOrFieldName);
+        return doc.getObject(classOrFieldName);
     }
 
     public java.lang.Object getValue(String fieldName)
     {
         Object object;
         if (currentObj == null) {
-            object = new Object(getDoc().getFirstObject(fieldName, getXWikiContext()), getXWikiContext());
+            object = new Object(doc.getFirstObject(fieldName, getXWikiContext()), getXWikiContext());
         } else {
             object = currentObj;
         }
@@ -1463,9 +1466,8 @@
     {
         if (hasAccessLevel("edit")) {
             saveDocument(comment);
-            clonedDoc = null;
         } else {
-            java.lang.Object[] args = {getDoc().getFullName()};
+            java.lang.Object[] args = {doc.getFullName()};
             throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS,
                 XWikiException.ERROR_XWIKI_ACCESS_DENIED,
                 "Access denied in edit mode on document {0}", null, args);
@@ -1481,9 +1483,8 @@
     {
         if (hasProgrammingRights()) {
             saveDocument(comment);
-            clonedDoc = null;
         } else {
-            java.lang.Object[] args = {getDoc().getFullName()};
+            java.lang.Object[] args = {doc.getFullName()};
             throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS,
                 XWikiException.ERROR_XWIKI_ACCESS_DENIED,
                 "Access denied with no programming rights document {0}", null, args);
@@ -1498,6 +1499,7 @@
             doc.setCreator(context.getUser());
         }
         getXWikiContext().getWiki().saveDocument(doc, comment, getXWikiContext());
+        cloned = false;
     }
 
     public com.xpn.xwiki.api.Object addObjectFromRequest() throws XWikiException
@@ -1589,7 +1591,7 @@
     {
         if (hasAccessLevel("delete")) {
             getXWikiContext().getWiki().deleteDocument(getDocument(), getXWikiContext());
-            clonedDoc = null;
+            cloned = false;
         } else {
             java.lang.Object[] args = {doc.getFullName()};
             throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS,
@@ -1602,7 +1604,7 @@
     {
         if (hasProgrammingRights()) {
             getXWikiContext().getWiki().deleteDocument(getDocument(), getXWikiContext());
-            clonedDoc = null;
+            cloned = false;
         } else {
             java.lang.Object[] args = {doc.getFullName()};
             throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS,
@@ -1624,7 +1626,7 @@
     public int addAttachments(String fieldName) throws XWikiException
     {
         if (!hasAccessLevel("edit")) {
-            java.lang.Object[] args = {getDoc().getFullName()};
+            java.lang.Object[] args = {doc.getFullName()};
             throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS,
                 XWikiException.ERROR_XWIKI_ACCESS_DENIED,
                 "Access denied in edit mode on document {0}", null, args);
@@ -1670,7 +1672,7 @@
         }
         if (nb > 0) {
             getXWikiContext().getWiki().saveDocument(getDoc(), getXWikiContext());
-            clonedDoc = null;
+            cloned = false;
         }
         return nb;
     }
@@ -1779,7 +1781,7 @@
      * @throws XWikiException
      */
     public Document getDocumentRevision(String revision) throws XWikiException {
-        return new Document(context.getWiki().getDocument(getDoc(), revision, context), context);
+        return new Document(context.getWiki().getDocument(doc, revision, context), context);
     }
 
     /**
@@ -1796,6 +1798,6 @@
      *  false if and only if there are older versions of this document.
      */
     public boolean isMostRecent() {
-        return getDoc().isMostRecent();
+        return doc.isMostRecent();
     }
 }
