Return-Path: Delivered-To: apmail-poi-commits-archive@locus.apache.org Received: (qmail 34936 invoked from network); 2 Apr 2008 23:03:09 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 2 Apr 2008 23:03:09 -0000 Received: (qmail 59354 invoked by uid 500); 2 Apr 2008 23:03:09 -0000 Delivered-To: apmail-poi-commits-archive@poi.apache.org Received: (qmail 59314 invoked by uid 500); 2 Apr 2008 23:03:09 -0000 Mailing-List: contact commits-help@poi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@poi.apache.org Delivered-To: mailing list commits@poi.apache.org Received: (qmail 59305 invoked by uid 99); 2 Apr 2008 23:03:09 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Apr 2008 16:03:09 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Apr 2008 23:02:25 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 618431A9838; Wed, 2 Apr 2008 16:02:44 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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... Date: Wed, 02 Apr 2008 23:02:42 -0000 To: commits@poi.apache.org From: nick@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080402230244.618431A9838@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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