]> source.dussan.org Git - poi.git/commitdiff
Start to wire up the commentstable stuff, now partly in place, and partly tested
authorNick Burch <nick@apache.org>
Mon, 31 Mar 2008 23:30:56 +0000 (23:30 +0000)
committerNick Burch <nick@apache.org>
Mon, 31 Mar 2008 23:30:56 +0000 (23:30 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@643208 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/CommentsSource.java
src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.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

index 190b740fbfe778f654e1984a5d97c337ca6aed9a..88e0971e76b6cd03317a90ccba5bb3dbc4458512 100644 (file)
@@ -24,6 +24,8 @@ package org.apache.poi.ss.usermodel;
 public interface CommentsSource {
        public String getAuthor(long authorId);
        
+       public int getNumberOfComments();
+       
        public int findAuthor(String author);
        
        public Comment findCellComment(int row, int column);
index 4372e0e641f2362b9e688b105c005ccbd9565da6..02602551ebee918042648bdf1b4297c71d055bab 100644 (file)
@@ -69,6 +69,10 @@ public class CommentsTable implements CommentsSource, XSSFModel {
         doc.save(out, options);
        }
        
+       public int getNumberOfComments() {
+               return comments.getCommentList().sizeOfCommentArray();
+       }
+       
        public String getAuthor(long authorId) {
                return getCommentsAuthors().getAuthorArray((int)authorId);
        }
index a9476d453e6a9dc49612c4f09e1e3aa6d9764cff..4444017d319c52e703a301b2b4e0d280c039378a 100644 (file)
@@ -911,4 +911,13 @@ public class XSSFSheet implements Sheet {
                }
                return sheetComments;
        }
+       
+       /**
+        * Does this sheet have any comments on it? We need to know,
+        *  so we can decide about writing it to disk or not
+        */
+       public boolean hasComments() {
+               if(sheetComments == null) { return false; }
+               return (sheetComments.getNumberOfComments() > 0);
+       }
 }
index 05479d9b7414bac5b9d2fa11354fc76f88c7551c..cbaa92b8c10756d6929eeeffe41466d0ac911987 100644 (file)
@@ -29,10 +29,10 @@ import javax.xml.namespace.QName;
 
 import org.apache.poi.POIXMLDocument;
 import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.CommentsSource;
 import org.apache.poi.ss.usermodel.CreationHelper;
 import org.apache.poi.ss.usermodel.DataFormat;
 import org.apache.poi.ss.usermodel.Font;
-import org.apache.poi.ss.usermodel.Name;
 import org.apache.poi.ss.usermodel.Palette;
 import org.apache.poi.ss.usermodel.PictureData;
 import org.apache.poi.ss.usermodel.SharedStringSource;
@@ -41,6 +41,7 @@ import org.apache.poi.ss.usermodel.StylesSource;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
+import org.apache.poi.xssf.model.CommentsTable;
 import org.apache.poi.xssf.model.SharedStringsTable;
 import org.apache.poi.xssf.model.StylesTable;
 import org.apache.poi.xssf.model.XSSFModel;
@@ -105,6 +106,18 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
                "/xl/image#.xml",
                null
     );
+       public static final XSSFRelation SHEET_COMMENTS = new XSSFRelation(
+                   "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml",
+                   "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments",
+                   "/xl/comments#.xml",
+                   CommentsTable.class
+       );
+       public static final XSSFRelation SHEET_HYPERLINKS = new XSSFRelation(
+                   null,
+                   "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
+                   null,
+                   null
+       );
     
        public static class XSSFRelation {
                private String TYPE;
@@ -241,8 +254,20 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
                        log.log(POILogger.WARN, "Sheet with name " + ctSheet.getName() + " and r:id " + ctSheet.getId()+ " was defined, but didn't exist in package, skipping");
                     continue;
                 }
+                
+                // Get the comments for the sheet, if there are any
+                CommentsSource comments = null;
+                PackageRelationshipCollection commentsRel =
+                       part.getRelationshipsByType(SHEET_COMMENTS.REL);
+                if(commentsRel != null && commentsRel.size() > 0) {
+                       PackagePart commentsPart = 
+                               getTargetPart(commentsRel.getRelationship(0));
+                       comments = new CommentsTable(commentsPart.getInputStream());
+                }
+                
+                // Now create the sheet
                 WorksheetDocument worksheetDoc = WorksheetDocument.Factory.parse(part.getInputStream());
-                XSSFSheet sheet = new XSSFSheet(ctSheet, worksheetDoc.getWorksheet(), this);
+                XSSFSheet sheet = new XSSFSheet(ctSheet, worksheetDoc.getWorksheet(), this, comments);
                 this.sheets.add(sheet);
             }
         } catch (XmlException e) {
@@ -656,6 +681,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
                 // Update our internal reference for the package part
                 workbook.getSheets().getSheetArray(i).setId(rel.getId());
                 workbook.getSheets().getSheetArray(i).setSheetId(sheetNumber);
+                
+                // If our sheet has comments, then write out those
+                // TODO
             }
              
             // Write shared strings and styles
index c231ba69ce5434d27f53899675eb82edf90d4fd4..12ecce94aa7965bd79720d943cb494c1d513f232 100644 (file)
 
 package org.apache.poi.xssf.model;
 
+import java.io.File;
+
+import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.xssf.usermodel.XSSFComment;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
@@ -78,7 +83,26 @@ public class TestCommentsTable extends TestCase {
                sheetComments.setCellComment("A1", comment);
                assertEquals(1, commentList.sizeOfCommentArray());
                assertEquals("test A1 author", sheetComments.getAuthor(commentList.getCommentArray(0).getAuthorId()));
+       }
+
+       public void testExisting() throws Exception {
+               File xml = new File(
+                               System.getProperty("HSSF.testdata.path") +
+                               File.separator + "WithVariousData.xlsx"
+               );
+               assertTrue(xml.exists());
+       
+               XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
+               Sheet sheet1 = workbook.getSheetAt(0);
+               Sheet sheet2 = workbook.getSheetAt(1);
                
+               assertTrue( ((XSSFSheet)sheet1).hasComments() );
+               assertFalse( ((XSSFSheet)sheet2).hasComments() );
+               
+               // TODO - check rest of comments
+       }
+       
+       public void testWriteRead() throws Exception {
+               // TODO
        }
-    
 }