]> source.dussan.org Git - poi.git/commitdiff
Fix the comments code so that we can correctly process existing XSSF comments, and...
authorNick Burch <nick@apache.org>
Wed, 2 Apr 2008 23:02:41 +0000 (23:02 +0000)
committerNick Burch <nick@apache.org>
Wed, 2 Apr 2008 23:02:41 +0000 (23:02 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@644104 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFComment.java
src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Comment.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/RichTextStringHelper.java
src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java

index 8ae4fb8926a85e8f605591cb057d1d9ecc417fa2..ed723f3d9dcf4cd601942e11f1219b26b7c20c5c 100644 (file)
@@ -137,6 +137,13 @@ public class HSSFComment extends HSSFTextbox implements Comment {
         if(note != null) note.setAuthor(author);
         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 333c44e551d20075d7f9781d510281969494c867..4081e6d5307ef64db7d44d9bfcd3b61828ac7cb4 100644 (file)
@@ -17,7 +17,6 @@
 
 package org.apache.poi.ss.usermodel;
 
-
 public interface Comment {
 
     /**
@@ -75,6 +74,11 @@ public interface Comment {
      * @param author the name of the original author of the comment
      */
     void setAuthor(String author);
+    
+    /**
+     * Fetches the rich text string of the comment
+     */
+    public RichTextString getString();
 
     /**
      * Sets the rich text string used by this comment.
index f726a22b41c4b4b1a852aacede8c38dbadf0fa9a..830910d756fed4fca0b7350d93d97f9c22e4ce2e 100644 (file)
@@ -76,6 +76,10 @@ public class XSSFComment implements Comment {
                String newRef = (new CellReference(comment.getRef())).convertRowColToString((short) row, getColumn());
                comment.setRef(newRef);
        }
+       
+       public RichTextString getString() {
+               return RichTextStringHelper.convertFromRst(comment.getText());
+       }
 
        public void setString(RichTextString string) {
                CTRst text = comment.addNewText();
@@ -91,9 +95,4 @@ public class XSSFComment implements Comment {
                // TODO Auto-generated method stub
 
        }
-       
-       public String getString() {
-               return comment.getText().getT().toString();
-       }
-
 }
index 4444017d319c52e703a301b2b4e0d280c039378a..ded600fc3957a8ecb134d6b2ca7fdb7105209773 100644 (file)
@@ -164,6 +164,14 @@ public class XSSFSheet implements Sheet {
         // TODO Auto-generated method stub
 
     }
+    
+    /**
+     * Creates a new comment for this sheet. You still
+     *  need to assign it to a cell though
+     */
+    public Comment createComment() {
+       return getComments().addComment();
+    }
 
     protected XSSFRow addRow(int index, int rownum) {
         CTRow row = this.worksheet.getSheetData().insertNewRow(index);
index d90c034e52fdb5cfab99be17b081788e1d3e6200..a87a722de4beba359e4630daabc36e14d165510e 100644 (file)
@@ -17,6 +17,9 @@
 package org.apache.poi.xssf.usermodel.helpers;
 
 import org.apache.poi.ss.usermodel.RichTextString;
+import org.apache.poi.xssf.usermodel.XSSFRichTextString;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
 
 public class RichTextStringHelper {
@@ -26,4 +29,28 @@ public class RichTextStringHelper {
                text.setT(string.getString());
        }
 
+       public static RichTextString convertFromRst(CTRst ctText) {
+               if(ctText == null) {
+                       return new XSSFRichTextString("");
+               }
+               if(ctText.getT() != null) {
+                       return new XSSFRichTextString(ctText.getT());
+               }
+               
+               // Grab all the text
+               StringBuffer t = new StringBuffer();
+               for(CTRElt r : ctText.getRArray()) {
+                       t.append( r.getT() );
+               }
+               XSSFRichTextString rtxt = new XSSFRichTextString(t.toString());
+               
+               // Now get all the formatting
+               // TODO: implement Rst/RpR to RichTextString conversion
+               for(CTRElt r : ctText.getRArray()) {
+                       // Formatting info comes from rPr
+                       CTRPrElt rPr = r.getRPr();
+                       rPr.getRFontArray();
+               }
+               return rtxt;
+       }
 }
index 12ecce94aa7965bd79720d943cb494c1d513f232..268af091981d75ab2c5e4ba84c36fdb8603900b3 100644 (file)
 
 package org.apache.poi.xssf.model;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Comment;
+import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.xssf.usermodel.XSSFComment;
+import org.apache.poi.xssf.usermodel.XSSFRichTextString;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.openxml4j.opc.Package;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
@@ -64,10 +71,10 @@ public class TestCommentsTable extends TestCase {
                comment1.setText(ctrst1);
                
                // test finding the right comment for a cell
-               assertEquals(TEST_A1_TEXT, sheetComments.findCellComment("A1").getString());
-               assertEquals(TEST_A1_TEXT, sheetComments.findCellComment(0, 0).getString());
-               assertEquals(TEST_A2_TEXT, sheetComments.findCellComment("A2").getString());
-               assertEquals(TEST_A2_TEXT, sheetComments.findCellComment(1, 0).getString());
+               assertEquals(TEST_A1_TEXT, sheetComments.findCellComment("A1").getString().getString());
+               assertEquals(TEST_A1_TEXT, sheetComments.findCellComment(0, 0).getString().getString());
+               assertEquals(TEST_A2_TEXT, sheetComments.findCellComment("A2").getString().getString());
+               assertEquals(TEST_A2_TEXT, sheetComments.findCellComment(1, 0).getString().getString());
                assertNull(sheetComments.findCellComment("A3"));
                assertNull(sheetComments.findCellComment(2, 0));
        }
@@ -99,10 +106,73 @@ public class TestCommentsTable extends TestCase {
                assertTrue( ((XSSFSheet)sheet1).hasComments() );
                assertFalse( ((XSSFSheet)sheet2).hasComments() );
                
-               // TODO - check rest of comments
+               // Comments should be in C5 and C7
+               Row r5 = sheet1.getRow(4);
+               Row r7 = sheet1.getRow(6);
+               assertNotNull( r5.getCell(2).getCellComment() );
+               assertNotNull( r7.getCell(2).getCellComment() );
+               
+               // Check they have what we expect
+               // TODO: Rich text formatting
+               Comment cc5 = r5.getCell(2).getCellComment();
+               Comment cc7 = r7.getCell(2).getCellComment();
+               
+               assertEquals("Nick Burch", cc5.getAuthor());
+               assertEquals("Nick Burch:\nThis is a comment", cc5.getString().getString());
+               assertEquals(4, cc5.getRow());
+               assertEquals(2, cc5.getColumn());
+               
+               assertEquals("Nick Burch", cc7.getAuthor());
+               assertEquals("Nick Burch:\nComment #1\n", cc7.getString().getString());
+               assertEquals(6, cc7.getRow());
+               assertEquals(2, cc7.getColumn());
        }
        
-       public void testWriteRead() throws Exception {
+       public void DISABLEDtestWriteRead() 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);
+               XSSFSheet sheet2 = (XSSFSheet)workbook.getSheetAt(1);
+               
+               assertTrue( ((XSSFSheet)sheet1).hasComments() );
+               assertFalse( ((XSSFSheet)sheet2).hasComments() );
+               
+               // Change on comment on sheet 1, and add another into
+               //  sheet 2
+               Row r5 = sheet1.getRow(4);
+               Comment cc5 = r5.getCell(2).getCellComment();
+               cc5.setAuthor("Apache POI");
+               cc5.setString(new XSSFRichTextString("Hello!"));
+               
+               Row r2s2 = sheet2.createRow(2);
+               Cell c1r2s2 = r2s2.createCell(1);
+               assertNull(c1r2s2.getCellComment());
+               
+               Comment cc2 = sheet2.createComment();
+               cc2.setAuthor("Also POI");
+               cc2.setString(new XSSFRichTextString("A new comment"));
+               c1r2s2.setCellComment(cc2);
+               
+               
+               // Save, and re-load the file
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               workbook.write(baos);
+               ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+               workbook = new XSSFWorkbook(Package.open(bais));
+               
+               // Check we still have comments where we should do
+               sheet1 = workbook.getSheetAt(0);
+               sheet2 = (XSSFSheet)workbook.getSheetAt(1);
+               assertNotNull(sheet1.getRow(4).getCell(2).getCellComment());
+               assertNotNull(sheet1.getRow(6).getCell(2).getCellComment());
+               assertNotNull(sheet2.getRow(2).getCell(1).getCellComment());
+               
+               // And check they still have the contents they should do
                // TODO
        }
 }