From: PJ Fanning Date: Wed, 17 Nov 2021 23:26:31 +0000 (+0000) Subject: try to make comments table more extensible X-Git-Tag: REL_5_2_0~218 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=df7fccb3d0dc40c48b98e5f9fee366792eef786a;p=poi.git try to make comments table more extensible git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1895128 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java index bb7b65082b..f0ab8deb61 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java @@ -24,9 +24,7 @@ import java.io.OutputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import java.util.Set; -import com.microsoft.schemas.vml.CTShape; import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.ss.util.CellAddress; @@ -97,6 +95,29 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { writeTo(out); out.close(); } + + /** + * @return iterator of comments + * @since POI 5.2.0 + */ + public Iterator commentIterator() { + final CommentsTable table = this; + return new Iterator() { + private final CTComment[] commentsArray = getCTComments().getCommentList().getCommentArray(); + private int counter = 0; + + @Override + public boolean hasNext() { + return counter < commentsArray.length; + } + + @Override + public XSSFComment next() { + CTComment ctComment = commentsArray[counter++]; + return new XSSFComment(table, ctComment, null); + } + }; + } /** * Called after the reference is updated, so that @@ -287,29 +308,6 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments { return false; } - /** - * Remove the comment at cellRef location, if one exists - * - * @param rowNums the rows for which all comments will be removed - * @since POI 5.2.0 - */ - public void removeCommentsFromRows(XSSFSheet sheet, Set rowNums) { - CTCommentList lst = getCTComments().getCommentList(); - XSSFVMLDrawing vml = sheet.getVMLDrawing(false); - for (CTComment comment : lst.getCommentArray()) { - String strRef = comment.getRef(); - CellAddress ref = new CellAddress(strRef); - - // is this comment part of the current row? - if(rowNums.contains(ref.getRow())) { - removeComment(ref); - if (vml != null) { - vml.removeCommentShape(ref.getRow(), ref.getColumn()); - } - } - } - } - /** * Returns the underlying CTComments list xmlbean * diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index d9b4331885..11fc454a24 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -3052,7 +3052,20 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { // also remove any comments associated with this row if (sheetComments != null) { - sheetComments.removeCommentsFromRows(this, rowsToRemoveSet); + Iterator commentIterator = sheetComments.commentIterator(); + while (commentIterator.hasNext()) { + XSSFComment comment = commentIterator.next(); + CellAddress ref = comment.getAddress(); + + // is this comment part of the current row? + if(rowsToRemoveSet.contains(ref.getRow())) { + sheetComments.removeComment(ref); + if (vml != null) { + vml.removeCommentShape(ref.getRow(), ref.getColumn()); + } + } + + } } // also remove any hyperlinks associated with this row diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFVMLDrawing.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFVMLDrawing.java index e09504c725..640534354a 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFVMLDrawing.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFVMLDrawing.java @@ -314,11 +314,7 @@ public final class XSSFVMLDrawing extends POIXMLDocumentPart { return (crow == row && ccol == col); } - /** - * Internal use only - */ - @Internal - public boolean removeCommentShape(int row, int col){ + protected boolean removeCommentShape(int row, int col){ XmlCursor cur = root.getXml().newCursor(); for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) { XmlObject itm = cur.getObject();