You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2008/04/03 01:02:42 UTC

svn commit: r644104 - in /poi/branches/ooxml/src: java/org/apache/poi/hssf/usermodel/ ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/ ooxml/java/org/apache/poi/xssf/usermodel/ ooxml/java/org/apache/poi/xssf/usermodel/helpers/ ooxml/testcases/org/ap...

Author: nick
Date: Wed Apr  2 16:02:41 2008
New Revision: 644104

URL: http://svn.apache.org/viewvc?rev=644104&view=rev
Log:
Fix the comments code so that we can correctly process existing XSSF comments, and add tests for this. Also tweak hssf comments slightly to match

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

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java?rev=644104&r1=644103&r2=644104&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java Wed Apr  2 16:02:41 2008
@@ -137,6 +137,13 @@
         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.

Modified: poi/branches/ooxml/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Comment.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Comment.java?rev=644104&r1=644103&r2=644104&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Comment.java (original)
+++ poi/branches/ooxml/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Comment.java Wed Apr  2 16:02:41 2008
@@ -17,7 +17,6 @@
 
 package org.apache.poi.ss.usermodel;
 
-
 public interface Comment {
 
     /**
@@ -75,6 +74,11 @@
      * @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.

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java?rev=644104&r1=644103&r2=644104&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java Wed Apr  2 16:02:41 2008
@@ -76,6 +76,10 @@
 		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 @@
 		// TODO Auto-generated method stub
 
 	}
-	
-	public String getString() {
-		return comment.getText().getT().toString();
-	}
-
 }

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java?rev=644104&r1=644103&r2=644104&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java Wed Apr  2 16:02:41 2008
@@ -164,6 +164,14 @@
         // 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);

Modified: poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/RichTextStringHelper.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/RichTextStringHelper.java?rev=644104&r1=644103&r2=644104&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/RichTextStringHelper.java (original)
+++ poi/branches/ooxml/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/RichTextStringHelper.java Wed Apr  2 16:02:41 2008
@@ -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 @@
 		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;
+	}
 }

Modified: poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java?rev=644104&r1=644103&r2=644104&view=diff
==============================================================================
--- poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java (original)
+++ poi/branches/ooxml/src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java Wed Apr  2 16:02:41 2008
@@ -17,12 +17,19 @@
 
 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 @@
 		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 @@
 		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
 	}
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org