Start to wire up the commentstable stuff, now partly in place, and partly tested

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@643208 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-03-31 23:30:56 +00:00
parent d32fe3f319
commit 3ed4f3d913
5 changed files with 71 additions and 4 deletions

View File

@ -24,6 +24,8 @@ package org.apache.poi.ss.usermodel;
public interface CommentsSource { public interface CommentsSource {
public String getAuthor(long authorId); public String getAuthor(long authorId);
public int getNumberOfComments();
public int findAuthor(String author); public int findAuthor(String author);
public Comment findCellComment(int row, int column); public Comment findCellComment(int row, int column);

View File

@ -69,6 +69,10 @@ public class CommentsTable implements CommentsSource, XSSFModel {
doc.save(out, options); doc.save(out, options);
} }
public int getNumberOfComments() {
return comments.getCommentList().sizeOfCommentArray();
}
public String getAuthor(long authorId) { public String getAuthor(long authorId) {
return getCommentsAuthors().getAuthorArray((int)authorId); return getCommentsAuthors().getAuthorArray((int)authorId);
} }

View File

@ -911,4 +911,13 @@ public class XSSFSheet implements Sheet {
} }
return sheetComments; 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);
}
} }

View File

@ -29,10 +29,10 @@ import javax.xml.namespace.QName;
import org.apache.poi.POIXMLDocument; import org.apache.poi.POIXMLDocument;
import org.apache.poi.ss.usermodel.CellStyle; 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.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font; 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.Palette;
import org.apache.poi.ss.usermodel.PictureData; import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.SharedStringSource; 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.ss.usermodel.Workbook;
import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger; 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.SharedStringsTable;
import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.model.XSSFModel; import org.apache.poi.xssf.model.XSSFModel;
@ -105,6 +106,18 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
"/xl/image#.xml", "/xl/image#.xml",
null 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 { public static class XSSFRelation {
private String TYPE; 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"); log.log(POILogger.WARN, "Sheet with name " + ctSheet.getName() + " and r:id " + ctSheet.getId()+ " was defined, but didn't exist in package, skipping");
continue; 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()); 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); this.sheets.add(sheet);
} }
} catch (XmlException e) { } catch (XmlException e) {
@ -656,6 +681,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
// Update our internal reference for the package part // Update our internal reference for the package part
workbook.getSheets().getSheetArray(i).setId(rel.getId()); workbook.getSheets().getSheetArray(i).setId(rel.getId());
workbook.getSheets().getSheetArray(i).setSheetId(sheetNumber); workbook.getSheets().getSheetArray(i).setSheetId(sheetNumber);
// If our sheet has comments, then write out those
// TODO
} }
// Write shared strings and styles // Write shared strings and styles

View File

@ -17,7 +17,12 @@
package org.apache.poi.xssf.model; 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.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.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
@ -78,7 +83,26 @@ public class TestCommentsTable extends TestCase {
sheetComments.setCellComment("A1", comment); sheetComments.setCellComment("A1", comment);
assertEquals(1, commentList.sizeOfCommentArray()); assertEquals(1, commentList.sizeOfCommentArray());
assertEquals("test A1 author", sheetComments.getAuthor(commentList.getCommentArray(0).getAuthorId())); 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
}
} }