try to make CommentsTable more extensible

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895148 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2021-11-18 11:51:24 +00:00
parent 14480be6de
commit ef1dd54f36
6 changed files with 33 additions and 18 deletions
poi-ooxml/src
main/java/org/apache/poi/xssf
test/java/org/apache/poi/xssf/usermodel

View File

@ -46,6 +46,16 @@ public interface Comments {
*/
XSSFComment findCellComment(CellAddress cellAddress);
/**
* Finds the cell comment at cellAddress, if one exists
*
* @param sheet the sheet that has the comment
* @param cellAddress the address of the cell to find a comment
* @return cell comment if one exists, otherwise returns null
* @since POI 5.2.0
*/
public XSSFComment findCellComment(XSSFSheet sheet, CellAddress cellAddress);
/**
* Remove the comment at cellRef location, if one exists
*

View File

@ -197,6 +197,7 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments {
* @return cell comment if one exists, otherwise returns null
* @since POI 5.2.0
*/
@Override
public XSSFComment findCellComment(XSSFSheet sheet, CellAddress cellAddress) {
CTComment ctComment = getCTComment(cellAddress);
if(ctComment == null) {

View File

@ -45,7 +45,7 @@ import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.util.ImageUtils;
import org.apache.poi.util.Internal;
import org.apache.poi.util.Units;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.model.Comments;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
@ -383,7 +383,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSS
XSSFClientAnchor ca = (XSSFClientAnchor) anchor;
XSSFSheet sheet = getSheet();
CommentsTable comments = sheet.getCommentsTable(true);
Comments comments = sheet.getCommentsTable(true);
return comments.createNewComment(getSheet(), ca);
}

View File

@ -56,6 +56,7 @@ import org.apache.poi.ss.util.SheetUtil;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal;
import org.apache.poi.util.Units;
import org.apache.poi.xssf.model.Comments;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.usermodel.XSSFPivotTable.PivotTableReferenceConfigurator;
import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
@ -95,7 +96,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
private final SortedMap<Integer, XSSFRow> _rows = new TreeMap<>();
private List<XSSFHyperlink> hyperlinks;
private ColumnHelper columnHelper;
private CommentsTable sheetComments;
private Comments sheetComments;
/**
* cache of master shared formulas in this sheet.
* Master shared formula is the first formula in a group of shared formulas is saved in the f element.
@ -163,8 +164,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
// Look for bits we're interested in
for(RelationPart rp : getRelationParts()){
POIXMLDocumentPart p = rp.getDocumentPart();
if(p instanceof CommentsTable) {
sheetComments = (CommentsTable)p;
if(p instanceof Comments) {
sheetComments = (Comments)p;
}
if(p instanceof XSSFTable) {
tables.put( rp.getRelationship().getId(), (XSSFTable)p );
@ -3506,18 +3507,18 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
*
* @param create create a new comments table if it does not exist
*/
protected CommentsTable getCommentsTable(boolean create) {
protected Comments getCommentsTable(boolean create) {
if(sheetComments == null && create){
// Try to create a comments table with the same number as
// the sheet has (i.e. sheet 1 -> comments 1)
try {
sheetComments = (CommentsTable)createRelationship(
sheetComments = (Comments)createRelationship(
XSSFRelation.SHEET_COMMENTS, getWorkbook().getXssfFactory(), Math.toIntExact(sheet.getSheetId()));
} catch(PartAlreadyExistsException e) {
// Technically a sheet doesn't need the same number as
// it's comments, and clearly someone has already pinched
// its comments, and clearly someone has already pinched
// our number! Go for the next available one instead
sheetComments = (CommentsTable)createRelationship(
sheetComments = (Comments)createRelationship(
XSSFRelation.SHEET_COMMENTS, getWorkbook().getXssfFactory(), -1);
}
}

View File

@ -44,6 +44,7 @@ import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.model.Comments;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.xmlbeans.XmlObject;
@ -210,7 +211,7 @@ public final class TestXSSFComment extends BaseTestCellComment {
XSSFClientAnchor ca = (XSSFClientAnchor) anchor;
// create comments and vmlDrawing parts if they don't exist
CommentsTable comments = wb.getXSSFWorkbook()
Comments comments = wb.getXSSFWorkbook()
.getSheetAt(0).getCommentsTable(true);
XSSFVMLDrawing vml = wb.getXSSFWorkbook()
.getSheetAt(0).getVMLDrawing(true);
@ -223,7 +224,8 @@ public final class TestXSSFComment extends BaseTestCellComment {
}
// create the comment in two different ways and verify that there is no difference
XSSFComment shape1 = new XSSFComment(comments, comments.newComment(CellAddress.A1), vmlShape1);
CommentsTable commentsTable = (CommentsTable)comments;
XSSFComment shape1 = new XSSFComment(comments, commentsTable.newComment(CellAddress.A1), vmlShape1);
shape1.setColumn(ca.getCol1());
shape1.setRow(ca.getRow1());
@ -236,7 +238,7 @@ public final class TestXSSFComment extends BaseTestCellComment {
}
CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
XSSFComment shape2 = new XSSFComment(comments, comments.newComment(ref), vmlShape2);
XSSFComment shape2 = new XSSFComment(comments, commentsTable.newComment(ref), vmlShape2);
assertEquals(shape1.getAuthor(), shape2.getAuthor());
assertEquals(shape1.getClientAnchor(), shape2.getClientAnchor());

View File

@ -74,6 +74,7 @@ import org.apache.poi.util.LocaleUtil;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.model.CalculationChain;
import org.apache.poi.xssf.model.Comments;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
@ -267,7 +268,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
XSSFComment comment = dg.createCellComment(new XSSFClientAnchor());
Cell cell = sheet.createRow(0).createCell(0);
CommentsTable comments = sheet.getCommentsTable(false);
CommentsTable comments = (CommentsTable) sheet.getCommentsTable(false);
CTComments ctComments = comments.getCTComments();
cell.setCellComment(comment);
@ -917,10 +918,10 @@ public final class TestXSSFSheet extends BaseTestXSheet {
void commentsTable() throws IOException {
try (XSSFWorkbook wb1 = new XSSFWorkbook()) {
XSSFSheet sheet1 = wb1.createSheet();
CommentsTable comment1 = sheet1.getCommentsTable(false);
CommentsTable comment1 = (CommentsTable) sheet1.getCommentsTable(false);
assertNull(comment1);
comment1 = sheet1.getCommentsTable(true);
comment1 = (CommentsTable) sheet1.getCommentsTable(true);
assertNotNull(comment1);
assertEquals("/xl/comments1.xml", comment1.getPackagePart().getPartName().getName());
@ -928,10 +929,10 @@ public final class TestXSSFSheet extends BaseTestXSheet {
//second sheet
XSSFSheet sheet2 = wb1.createSheet();
CommentsTable comment2 = sheet2.getCommentsTable(false);
CommentsTable comment2 = (CommentsTable) sheet2.getCommentsTable(false);
assertNull(comment2);
comment2 = sheet2.getCommentsTable(true);
comment2 = (CommentsTable) sheet2.getCommentsTable(true);
assertNotNull(comment2);
assertSame(comment2, sheet2.getCommentsTable(true));
@ -944,7 +945,7 @@ public final class TestXSSFSheet extends BaseTestXSheet {
//now test against a workbook containing cell comments
try (XSSFWorkbook wb2 = XSSFTestDataSamples.openSampleWorkbook("WithMoreVariousData.xlsx")) {
XSSFSheet sheet1 = wb2.getSheetAt(0);
CommentsTable comment1 = sheet1.getCommentsTable(true);
CommentsTable comment1 = (CommentsTable) sheet1.getCommentsTable(true);
assertNotNull(comment1);
assertEquals("/xl/comments1.xml", comment1.getPackagePart().getPartName().getName());
assertSame(comment1, sheet1.getCommentsTable(true));