]> source.dussan.org Git - poi.git/commitdiff
New ooxml4j, and finish getting xssf comments to be saved and re-loaded again
authorNick Burch <nick@apache.org>
Thu, 3 Apr 2008 00:23:59 +0000 (00:23 +0000)
committerNick Burch <nick@apache.org>
Thu, 3 Apr 2008 00:23:59 +0000 (00:23 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@644118 13f79535-47bb-0310-9956-ffa450edef68

build.xml
src/java/org/apache/poi/hssf/usermodel/HSSFComment.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFComment.java

index 45274b63fb7bafc46f41edbca73e7d0d4021caf1..5c7a159c588942803e348ffe9c99f1ecd9fcd5f3 100644 (file)
--- a/build.xml
+++ b/build.xml
@@ -151,8 +151,8 @@ under the License.
   <property name="ooxml.jar5.dir" location="${ooxml.lib}/jsr173_1.0_api.jar"/>
   <property name="ooxml.jar5.url" value="${repository}/xmlbeans/jars/jsr173_1.0_api.jar"/>
   <!-- No official release of openxml4j yet -->
-  <property name="ooxml.jar6.dir" location="${ooxml.lib}/openxml4j-bin-alpha-080322.jar"/>
-  <property name="ooxml.jar6.url" value="http://people.apache.org/~nick/openxml4j-bin-alpha-080322.jar"/>
+  <property name="ooxml.jar6.dir" location="${ooxml.lib}/openxml4j-bin-alpha-080403.jar"/>
+  <property name="ooxml.jar6.url" value="http://people.apache.org/~nick/openxml4j-bin-alpha-080403.jar"/>
 
   <!-- See http://www.ecma-international.org/publications/standards/Ecma-376.htm -->
   <!-- "Copy these file(s), free of charge" -->
index ed723f3d9dcf4cd601942e11f1219b26b7c20c5c..57f18c34da097cb3f63925497d54d90cf321a630 100644 (file)
@@ -138,13 +138,6 @@ public class HSSFComment extends HSSFTextbox implements Comment {
         this.author = author;
     }
     
-    /**
-     * Fetches the rich text string of the comment
-     */
-    public HSSFRichTextString getString() {
-       return txo.getStr();
-    }
-
     /**
      * Sets the rich text string used by this comment.
      *
index ded600fc3957a8ecb134d6b2ca7fdb7105209773..94b4f536e25a79852d98ff9e8f4e8657ab255902 100644 (file)
@@ -919,6 +919,13 @@ public class XSSFSheet implements Sheet {
                }
                return sheetComments;
        }
+       /**
+        * Returns the sheet's comments object if there is one,
+        *  or null if not
+        */
+       protected CommentsSource getCommentsSourceIfExists() {
+               return sheetComments;
+       }
        
        /**
         * Does this sheet have any comments on it? We need to know,
index cbaa92b8c10756d6929eeeffe41466d0ac911987..e308a7c1a059fa0bea6bab12db4692cd41fcc5d6 100644 (file)
@@ -133,6 +133,18 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
                public String getContentType() { return TYPE; }
                public String getRelation() { return REL; }
                public String getDefaultFileName() { return DEFAULT_NAME; }
+               
+               /**
+                * Returns the filename for the nth one of these, 
+                *  eg /xl/comments4.xml
+                */
+               public String getFileName(int index) {
+                       if(DEFAULT_NAME.indexOf("#") == -1) {
+                               // Generic filename in all cases
+                               return getDefaultFileName();
+                       }
+                       return DEFAULT_NAME.replace("#", Integer.toString(index));
+               }
 
                /**
                 * Fetches the InputStream to read the contents, based
@@ -667,7 +679,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
             for (int i=0 ; i < this.getNumberOfSheets(); i++) {
                int sheetNumber = (i+1);
                XSSFSheet sheet = (XSSFSheet) this.getSheetAt(i);
-                PackagePartName partName = PackagingURIHelper.createPartName("/xl/worksheets/sheet" + sheetNumber + ".xml");
+                PackagePartName partName = PackagingURIHelper.createPartName(
+                               WORKSHEET.getFileName(sheetNumber));
                 PackageRelationship rel =
                         corePart.addRelationship(partName, TargetMode.INTERNAL, WORKSHEET.getRelation(), "rSheet" + sheetNumber);
                 PackagePart part = pkg.createPart(partName, WORKSHEET.getContentType());
@@ -683,7 +696,17 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
                 workbook.getSheets().getSheetArray(i).setSheetId(sheetNumber);
                 
                 // If our sheet has comments, then write out those
-                // TODO
+                if(sheet.hasComments()) {
+                       CommentsTable ct = (CommentsTable)sheet.getCommentsSourceIfExists();
+                    PackagePartName ctName = PackagingURIHelper.createPartName(
+                               SHEET_COMMENTS.getFileName(sheetNumber));
+                    part.addRelationship(ctName, TargetMode.INTERNAL, SHEET_COMMENTS.getRelation(), "rComments");
+                    PackagePart ctPart = pkg.createPart(ctName, SHEET_COMMENTS.getContentType());
+                    
+                    out = ctPart.getOutputStream();
+                    ct.writeTo(out);
+                    out.close();
+                }
             }
              
             // Write shared strings and styles
index 268af091981d75ab2c5e4ba84c36fdb8603900b3..413c96b83748a76dff6d61185ba3eb56270e0993 100644 (file)
@@ -20,6 +20,7 @@ package org.apache.poi.xssf.model;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileOutputStream;
 
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.Comment;
@@ -128,7 +129,7 @@ public class TestCommentsTable extends TestCase {
                assertEquals(2, cc7.getColumn());
        }
        
-       public void DISABLEDtestWriteRead() throws Exception {
+       public void testWriteRead() throws Exception {
                File xml = new File(
                                System.getProperty("HSSF.testdata.path") +
                                File.separator + "WithVariousData.xlsx"
index f1f18827321bc77226b575791026225a7c7b7452..6a977effb89690f7f50dc95e3529fa311bbf5cb4 100644 (file)
@@ -78,6 +78,26 @@ public class TestHSSFComment extends TestCase {
         assertEquals(commentText, comment.getString().getString());
         assertEquals(cellRow, comment.getRow());
         assertEquals(cellColumn, comment.getColumn());
+        
+        
+        // Change slightly, and re-test
+        comment.setString(new HSSFRichTextString("New Comment Text"));
+        
+        out = new ByteArrayOutputStream();
+        wb.write(out);
+        out.close();
+
+        wb = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
+        sheet = wb.getSheetAt(0);
+        cell = sheet.getRow(cellRow).getCell(cellColumn);
+        comment = cell.getCellComment();
+
+        assertNotNull(comment);
+        assertEquals(HSSFSimpleShape.OBJECT_TYPE_COMMENT, comment.getShapeType());
+        assertEquals(commentAuthor, comment.getAuthor());
+        assertEquals("New Comment Text", comment.getString().getString());
+        assertEquals(cellRow, comment.getRow());
+        assertEquals(cellColumn, comment.getColumn());
     }
 
     /**