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

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@644104 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-04-02 23:02:41 +00:00
parent 55c0ba6a1f
commit ed2da7e40b
6 changed files with 127 additions and 12 deletions

View 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.

View 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.

View 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();
}
}

View 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);

View 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;
}
}

View File

@ -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 @@ 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
}
}