r949 - in xwiki/trunk/src: main/java/com/xpn/xwiki/doc test/java/com/xpn/xwiki/test

Phung Hai Nam namphunghai at users.forge.objectweb.org
Wed Mar 1 06:00:28 CET 2006


Author: namphunghai
Date: 2006-03-01 06:00:25 +0100 (Wed, 01 Mar 2006)
New Revision: 949

Modified:
   xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiAttachment.java
   xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiAttachmentArchive.java
   xwiki/trunk/src/test/java/com/xpn/xwiki/test/StoreTest.java
Log:
Implemented first part of XWIKI-219.  Create API getAttachmentRevision.

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiAttachment.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiAttachment.java	2006-03-01 02:45:05 UTC (rev 948)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiAttachment.java	2006-03-01 05:00:25 UTC (rev 949)
@@ -27,13 +27,22 @@
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
+import java.io.StringWriter;
+import java.io.IOException;
+import java.io.StringReader;
 
 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.jrcs.rcs.Archive;
 import org.apache.commons.jrcs.rcs.Node;
 import org.apache.commons.jrcs.rcs.Version;
 import org.dom4j.Element;
+import org.dom4j.Document ;
+import org.dom4j.DocumentException;
+import org.dom4j.io.OutputFormat;
+import org.dom4j.io.XMLWriter;
+import org.dom4j.io.SAXReader;
 import org.dom4j.dom.DOMElement;
+import org.dom4j.dom.DOMDocument;
 import org.hibernate.ObjectNotFoundException;
 
 import com.xpn.xwiki.XWikiContext;
@@ -216,6 +225,28 @@
         isMetaDataDirty = metaDataDirty;
     }
 
+    public String toStringXML(boolean bWithAttachmentContent, boolean bWithVersions, XWikiContext context) throws XWikiException {
+        // implement
+        Element ele = toXML(bWithAttachmentContent,bWithVersions,context);
+
+        Document doc = new DOMDocument();
+        doc.setRootElement(ele);
+        OutputFormat outputFormat = new OutputFormat("", true);
+        if ((context==null)||(context.getWiki()==null))
+            outputFormat.setEncoding("UTF-8");
+        else
+            outputFormat.setEncoding(context.getWiki().getEncoding());
+        StringWriter out = new StringWriter();
+        XMLWriter writer = new XMLWriter( out, outputFormat );
+        try {
+            writer.write(doc);
+            return out.toString();
+        } catch (IOException e) {
+            e.printStackTrace();
+            return "";
+        }
+    }
+
     public Element toXML(XWikiContext context) throws XWikiException {
         return toXML(false, false, context);
     }
@@ -279,6 +310,20 @@
         return docel;
     }
 
+
+    public void fromXML(String data) throws XWikiException {
+        SAXReader reader = new SAXReader();
+        Document domdoc = null;
+        try {
+            StringReader in = new StringReader(data);
+            domdoc = reader.read(in);
+        } catch (DocumentException e) {
+            throw new XWikiException(XWikiException.MODULE_XWIKI_DOC, XWikiException.ERROR_DOC_XML_PARSING, "Error parsing xml", e, null);
+        }
+        Element docel = domdoc.getRootElement();
+        fromXML(docel);
+    }
+
     public void fromXML(Element docel) throws XWikiException {
         setFilename(docel.element("filename").getText());
         setFilesize(Integer.parseInt(docel.element("filesize").getText()));
@@ -411,7 +456,7 @@
             attachment_archive.setAttachment(this);
         }
 
-        attachment_archive.updateArchive(getContent(context));
+        attachment_archive.updateArchive(getContent(context), context);
     }
 
     public String getMimeType(XWikiContext context) {
@@ -431,5 +476,75 @@
             return false;
     }
 
+    public XWikiAttachment getAttachmentRevision(String rev, XWikiContext context) throws XWikiException {
+
+        try {
+            context.getWiki().getStore().loadAttachmentArchive(this, context, true);
+
+            Archive archive = getArchive();
+
+            if (archive==null)
+             return null;
+
+            Version v = archive.getRevisionVersion(rev);
+            Object[] lines = archive.getRevision(v);
+            StringBuffer content = new StringBuffer();
+            for (int i=0;i<lines.length;i++) {
+                String line = lines[i].toString();
+                content.append(line);
+                if (i!=lines.length-1)
+                    content.append("\n");
+            }
+
+            String scontent = content.toString();
+            XWikiAttachment revattach = new XWikiAttachment();
+            revattach.fromXML(scontent);
+            return revattach;
+        }    catch (Exception e) {
+            Object[] args = { getFilename() };
+            throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_ATTACHMENT_ARCHIVEFORMAT,
+                    "Exception while manipulating the archive for file {0}", e, args);
+        }
+    }
+
+    /*
+    public XWikiAttachment getAttachmentRevision(String rev, XWikiContext context) throws XWikiException {
+        XWikiAttachment revattach = null;
+
+        try {
+            Version[] versions = getDoc().getRevisions(context);
+            for (int i=0;i<versions.length;i++) {
+                Version version = versions[i];
+                XWikiDocument revdoc = context.getWiki().getDocument(getDoc(),version.toString(),context);
+                revattach = revdoc.getAttachment(getFilename());
+                if ((revattach!=null)&&revattach.getVersion().equals(rev))
+                    break;
+                revattach = null;
+            }
+            if (revattach==null)
+             return null;
+
+            context.getWiki().getStore().loadAttachmentArchive(this, context, true);
+            Archive archive = getArchive();
+            Version v = archive.getRevisionVersion(rev);
+            Object[] lines = archive.getRevision(v);
+            StringBuffer content = new StringBuffer();
+            for (int i=0;i<lines.length;i++) {
+                String line = lines[i].toString();
+                content.append(line);
+                if (i!=lines.length-1)
+                    content.append("\n");
+            }
+            String scontent = content.toString();
+            byte[] vcontent = Base64.decodeBase64(scontent.getBytes());
+            revattach.setContent(vcontent);
+            return revattach;
+        }    catch (Exception e) {
+            Object[] args = { getFilename() };
+            throw new XWikiException( XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_ATTACHMENT_ARCHIVEFORMAT,
+                    "Exception while manipulating the archive for file {0}", e, args);
+        }
+    }
+   */
 }
 

Modified: xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiAttachmentArchive.java
===================================================================
--- xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiAttachmentArchive.java	2006-03-01 02:45:05 UTC (rev 948)
+++ xwiki/trunk/src/main/java/com/xpn/xwiki/doc/XWikiAttachmentArchive.java	2006-03-01 05:00:25 UTC (rev 949)
@@ -27,6 +27,7 @@
 
 import org.apache.commons.jrcs.rcs.Archive;
 import org.apache.commons.jrcs.rcs.Lines;
+import org.apache.commons.codec.binary.Base64;
 
 import com.xpn.xwiki.XWikiContext;
 import com.xpn.xwiki.XWikiException;
@@ -71,9 +72,11 @@
     }
 
     public byte[] getArchive(XWikiContext context) throws XWikiException {
+        if (archive==null) {
+            if (context!=null)
+                updateArchive(attachment.getContent(context), context);
+        }
         if (archive==null)
-            updateArchive(attachment.getContent(context));
-        if (archive==null)
             return new byte[0];
         else {
             return archive.toByteArray();
@@ -81,13 +84,15 @@
     }
 
     public void setArchive(byte[] data) throws XWikiException {
-        if (data==null) {
+        if ((data==null)||(data.length==0)) {
             archive = null;
 
         } else {
             try {
+                //attachment.fromXML(data.toString());
                 ByteArrayInputStream is = new ByteArrayInputStream(data);
                 archive = new Archive(getAttachment().getFilename(), is);
+
             }
             catch (Exception e) {
                 Object[] args = { getAttachment().getFilename() };
@@ -97,9 +102,9 @@
         }
     }
 
-    public void updateArchive(byte[] data) throws XWikiException {
+    public void updateArchive(byte[] data, XWikiContext context) throws XWikiException {
         try {
-            String sdata = data.toString();
+            String sdata = attachment.toStringXML(true, false, context);
             Lines lines = new Lines(sdata);
 
             if (archive!=null) {

Modified: xwiki/trunk/src/test/java/com/xpn/xwiki/test/StoreTest.java
===================================================================
--- xwiki/trunk/src/test/java/com/xpn/xwiki/test/StoreTest.java	2006-03-01 02:45:05 UTC (rev 948)
+++ xwiki/trunk/src/test/java/com/xpn/xwiki/test/StoreTest.java	2006-03-01 05:00:25 UTC (rev 949)
@@ -35,7 +35,6 @@
 import com.xpn.xwiki.doc.XWikiAttachment;
 import com.xpn.xwiki.doc.XWikiDocument;
 import com.xpn.xwiki.store.XWikiStoreInterface;
-import com.xpn.xwiki.store.XWikiHibernateStore;
 
 public class StoreTest extends HibernateTestCase {
 
@@ -268,6 +267,12 @@
         }
     }
 
+    public void testUpdateAttachmentReadWrite() throws XWikiException, IOException {
+        Utils.setStandardData();
+        attachmentReadWrite(getXWiki().getStore(), Utils.web, Utils.name);
+        updateAttachmentReadWrite(getXWiki().getStore(), Utils.web, Utils.name);
+    }
+
     public void deleteAttachmentReadWrite(XWikiStoreInterface store, String web, String name) throws XWikiException, IOException {
 
         XWikiDocument doc3 = new XWikiDocument(web, name);
@@ -305,12 +310,6 @@
     }
 
 
-    public void testUpdateAttachmentReadWrite() throws XWikiException, IOException {
-        Utils.setStandardData();
-        attachmentReadWrite(getXWiki().getStore(), Utils.web, Utils.name);
-        updateAttachmentReadWrite(getXWiki().getStore(), Utils.web, Utils.name);
-    }
-
     public void testDeleteAttachmentReadWrite() throws XWikiException, IOException {
         Utils.setStandardData();
         attachmentReadWrite(getXWiki().getStore(), Utils.web, Utils.name);
@@ -370,4 +369,82 @@
         assertEquals(doc3.getAuthor(), Utils.author2);
         assertEquals(doc3.getVersion(), Utils.version2);
     }
+
+
+    public void testUpdateAttachmentArchiveReadWrite(XWikiStoreInterface store, byte[] attachcontenta, byte[] attachcontentb) throws XWikiException, IOException {
+        // Prepare data
+        XWikiDocument doc = xwiki.getDocument(Utils.web, Utils.name, getXWikiContext());
+        doc.setContent("content 0");
+        xwiki.saveDocument(doc, context);
+        for (int i=0;i<100;i++) {
+         doc.setContent("content " + i);
+         xwiki.saveDocument(doc, context);
+        }
+        XWikiAttachment attach = new XWikiAttachment(doc, "file.txt");
+        attach.setContent(attachcontenta);
+        attach.setAuthor("XWiki.Author1");
+        attach.setComment("Test comment 1");
+        doc.getAttachmentList().add(attach);
+        doc.saveAttachmentContent(attach, getXWikiContext());
+
+        doc.setContent("content 1");
+        xwiki.saveDocument(doc, context);
+
+        attach.setContent(attachcontentb);
+        attach.setAuthor("XWiki.Author2");
+        attach.setComment("Test comment 2");
+        doc.saveAttachmentContent(attach, getXWikiContext());
+
+        getXWiki().flushCache();
+
+        // Verify data
+        XWikiDocument doc1 = new XWikiDocument(Utils.web, Utils.name);
+        doc1 = (XWikiDocument) store.loadXWikiDoc(doc1, getXWikiContext());
+        List attachlist = doc1.getAttachmentList();
+        assertEquals("Attachment is not listed", 1, attachlist.size());
+        XWikiAttachment attach1 = (XWikiAttachment) attachlist.get(0);
+        assertEquals("Attachment version is not correct", "1.2", attach.getVersion());
+        byte[] attachcontent1 = attach1.getContent(getXWikiContext());
+        assertEquals("Attachment content size is not correct", attachcontentb.length, attachcontent1.length);
+        for (int i=0;i<attachcontent1.length;i++) {
+            assertEquals("Attachment content byte " + i + " is not correct", attachcontentb[i], attachcontent1[i]);
+        }
+
+        XWikiAttachment attach2 = attach1.getAttachmentRevision("1.1", context);
+        // Test that content of attach is the one from version 1.1
+        assertEquals("Attachment version is not correct","1.1",attach2.getVersion());
+        byte[] attachcontent2 = attach2.getContent(getXWikiContext());
+        assertEquals("Attachment content size is not correct", attachcontenta.length,attachcontent2.length);
+        for (int i=0; i<attachcontent2.length; i++) {
+           assertEquals("Attachment content byte " + i + " is not correct",attachcontenta[i], attachcontent2[i]);
+        }
+        assertEquals("Attachment author is not correct", attach2.getAuthor(),"XWiki.Author1");
+        assertEquals("Attachment comment is not correct", attach2.getComment(),"Test comment 1");
+    }
+
+
+    public void testUpdateAttachmentArchiveReadWrite() throws XWikiException, IOException {
+        XWikiStoreInterface store = getXWiki().getStore();
+        String contenta = "content a";
+        String contentb = "content b";
+        byte[] attachcontenta = contenta.getBytes();
+        byte[] attachcontentb = contentb.getBytes();
+        testUpdateAttachmentArchiveReadWrite(store, attachcontenta, attachcontentb);
+    }
+
+    public void testUpdateAttachmentArchiveReadWrite2() throws XWikiException, IOException {
+        XWikiStoreInterface store = getXWiki().getStore();
+        String contenta = "this is content a with \nanother line and\nfinishing with an end line\n";
+        String contentb = "this is content b with \nanother line and\nfinishing with tzo end line\n\n";
+        byte[] attachcontenta = contenta.getBytes();
+        byte[] attachcontentb = contentb.getBytes();
+        testUpdateAttachmentArchiveReadWrite(store, attachcontenta, attachcontentb);
+    }
+
+    public void testUpdateAttachmentArchiveReadWriteBinary() throws XWikiException, IOException {
+        XWikiStoreInterface store = getXWiki().getStore();
+        byte[] attachcontenta = Utils.getDataAsBytes(new File(Utils.filename));
+        byte[] attachcontentb = Utils.getDataAsBytes(new File(Utils.filename2));
+        testUpdateAttachmentArchiveReadWrite(store, attachcontenta, attachcontentb);
+    }
 }





More information about the Xwiki-notifications mailing list