Author: fanningpj
Date: Thu Jul 26 12:12:39 2018
New Revision: 1836721
URL: http://svn.apache.org/viewvc?rev=1836721&view=rev
Log:
start process of introducing an interface for Comments Table
Added:
poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/Comments.java
- copied, changed from r1836648,
poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java
Modified:
poi/trunk/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java
poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFBEventBasedExcelExtractor.java
poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java
poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java
poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/SharedStrings.java
Modified:
poi/trunk/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java
URL:
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java?rev=1836721&r1=1836720&r2=1836721&view=diff
==============================================================================
---
poi/trunk/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java
(original)
+++
poi/trunk/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java
Thu Jul 26 12:12:39 2018
@@ -27,6 +27,7 @@ import org.apache.poi.ss.usermodel.RichT
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
+import org.apache.poi.xssf.model.Comments;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.model.SharedStrings;
import org.apache.poi.xssf.model.StylesTable;
@@ -68,7 +69,7 @@ public class XSSFSheetXMLHandler extends
/**
* Table with cell comments
*/
- private CommentsTable commentsTable;
+ private Comments comments;
/**
* Read only access to the shared strings table, for looking
@@ -124,13 +125,13 @@ public class XSSFSheetXMLHandler extends
DataFormatter dataFormatter,
boolean formulasNotResults) {
this.stylesTable = styles;
- this.commentsTable = comments;
+ this.comments = comments;
this.sharedStringsTable = strings;
this.output = sheetContentsHandler;
this.formulasNotResults = formulasNotResults;
this.nextDataType = xssfDataType.NUMBER;
this.formatter = dataFormatter;
- init();
+ init(comments);
}
/**
@@ -162,7 +163,7 @@ public class XSSFSheetXMLHandler extends
this(styles, strings, sheetContentsHandler, new DataFormatter(),
formulasNotResults);
}
- private void init() {
+ private void init(CommentsTable commentsTable) {
if (commentsTable != null) {
commentCellRefs = new LinkedList<>();
//noinspection deprecation
@@ -376,7 +377,7 @@ public class XSSFSheetXMLHandler extends
// Do we have a comment for this cell?
checkForEmptyCellComments(EmptyCellCommentsCheckType.CELL);
- XSSFComment comment = commentsTable != null ?
commentsTable.findCellComment(new CellAddress(cellRef)) : null;
+ XSSFComment comment = comments != null ?
comments.findCellComment(new CellAddress(cellRef)) : null;
// Output
output.cell(cellRef, thisStr, comment);
@@ -490,7 +491,7 @@ public class XSSFSheetXMLHandler extends
* Output an empty-cell comment.
*/
private void outputEmptyCellComment(CellAddress cellRef) {
- XSSFComment comment = commentsTable.findCellComment(cellRef);
+ XSSFComment comment = comments.findCellComment(cellRef);
output.cell(cellRef.formatAsString(), null, comment);
}
@@ -506,10 +507,10 @@ public class XSSFSheetXMLHandler extends
*/
public interface SheetContentsHandler {
/** A row with the (zero based) row number has started */
- public void startRow(int rowNum);
+ void startRow(int rowNum);
/** A row with the (zero based) row number has ended */
- public void endRow(int rowNum);
+ void endRow(int rowNum);
/**
* A cell, with the given formatted value (may be null),
@@ -520,12 +521,12 @@ public class XSSFSheetXMLHandler extends
*
<code>src/examples/src/org/apache/poi/xssf/eventusermodel/XLSX2CSV.java</code>
* for an example of how to handle this scenario.
*/
- public void cell(String cellReference, String formattedValue,
XSSFComment comment);
+ void cell(String cellReference, String formattedValue, XSSFComment
comment);
/** A header or footer has been encountered */
- public default void headerFooter(String text, boolean isHeader, String
tagName) {}
+ default void headerFooter(String text, boolean isHeader, String tagName)
{}
/** Signal that the end of a sheet was been reached */
- public default void endSheet() {}
+ default void endSheet() {}
}
}
Modified:
poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFBEventBasedExcelExtractor.java
URL:
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFBEventBasedExcelExtractor.java?rev=1836721&r1=1836720&r2=1836721&view=diff
==============================================================================
---
poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFBEventBasedExcelExtractor.java
(original)
+++
poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFBEventBasedExcelExtractor.java
Thu Jul 26 12:12:39 2018
@@ -97,7 +97,7 @@ public class XSSFBEventBasedExcelExtract
XSSFBCommentsTable comments,
SharedStrings strings,
InputStream sheetInputStream)
- throws IOException, SAXException {
+ throws IOException {
DataFormatter formatter;
if (getLocale() == null) {
Modified:
poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java
URL:
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java?rev=1836721&r1=1836720&r2=1836721&view=diff
==============================================================================
---
poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java
(original)
+++
poi/trunk/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java
Thu Jul 26 12:12:39 2018
@@ -39,6 +39,7 @@ import org.apache.poi.xssf.eventusermode
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
import
org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler;
+import org.apache.poi.xssf.model.Comments;
import org.apache.poi.xssf.model.CommentsTable;
import org.apache.poi.xssf.model.SharedStrings;
import org.apache.poi.xssf.model.StylesTable;
Copied: poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/Comments.java (from
r1836648, poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java)
URL:
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/Comments.java?p2=poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/Comments.java&p1=poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java&r1=1836648&r2=1836721&rev=1836721&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java
(original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/Comments.java Thu Jul 26
12:12:39 2018
@@ -16,113 +16,23 @@
==================================================================== */
package org.apache.poi.xssf.model;
-import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.TreeMap;
-
-import org.apache.poi.ooxml.POIXMLDocumentPart;
-import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.ss.util.CellAddress;
-import org.apache.poi.util.Internal;
import org.apache.poi.xssf.usermodel.XSSFComment;
-import org.apache.xmlbeans.XmlException;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CommentsDocument;
-
-@Internal
-public class CommentsTable extends POIXMLDocumentPart {
- public static final String DEFAULT_AUTHOR = "";
- public static final int DEFAULT_AUTHOR_ID = 0;
- /**
- * Underlying XML Beans CTComment list.
- */
- private CTComments comments;
- /**
- * XML Beans uses a list, which is very slow
- * to search, so we wrap things with our own
- * map for fast lookup.
- */
- private Map<CellAddress, CTComment> commentRefs;
- public CommentsTable() {
- super();
- comments = CTComments.Factory.newInstance();
- comments.addNewCommentList();
- comments.addNewAuthors().addAuthor(DEFAULT_AUTHOR);
- }
+/**
+ * An interface exposing useful functions for dealing with Excel Workbook
Comments.
+ * It is intended that this interface should support low level access and not
expose
+ * all the comments in memory
+ */
+public interface Comments {
- /**
- * @since POI 3.14-Beta1
- */
- public CommentsTable(PackagePart part) throws IOException {
- super(part);
- readFrom(part.getInputStream());
- }
-
- public void readFrom(InputStream is) throws IOException {
- try {
- CommentsDocument doc = CommentsDocument.Factory.parse(is,
DEFAULT_XML_OPTIONS);
- comments = doc.getComments();
- } catch (XmlException e) {
- throw new IOException(e.getLocalizedMessage());
- }
- }
- public void writeTo(OutputStream out) throws IOException {
- CommentsDocument doc = CommentsDocument.Factory.newInstance();
- doc.setComments(comments);
- doc.save(out, DEFAULT_XML_OPTIONS);
- }
-
- @Override
- protected void commit() throws IOException {
- PackagePart part = getPackagePart();
- OutputStream out = part.getOutputStream();
- writeTo(out);
- out.close();
- }
-
- /**
- * Called after the reference is updated, so that
- * we can reflect that in our cache
- * @param oldReference the comment to remove from the commentRefs map
- * @param comment the comment to replace in the commentRefs map
- */
- public void referenceUpdated(CellAddress oldReference, CTComment comment) {
- if(commentRefs != null) {
- commentRefs.remove(oldReference);
- commentRefs.put(new CellAddress(comment.getRef()), comment);
- }
- }
-
- public int getNumberOfComments() {
- return comments.getCommentList().sizeOfCommentArray();
- }
-
- public int getNumberOfAuthors() {
- return comments.getAuthors().sizeOfAuthorArray();
- }
-
- public String getAuthor(long authorId) {
- return comments.getAuthors().getAuthorArray((int)authorId);
- }
-
- public int findAuthor(String author) {
- String[] authorArray = comments.getAuthors().getAuthorArray();
- for (int i = 0 ; i < authorArray.length; i++) {
- if (authorArray[i].equals(author)) {
- return i;
- }
- }
- return addNewAuthor(author);
- }
+ int getNumberOfComments();
+
+ int getNumberOfAuthors();
+
+ String getAuthor(long authorId);
+
+ int findAuthor(String author);
/**
* Finds the cell comment at cellAddress, if one exists
@@ -130,74 +40,7 @@ public class CommentsTable extends POIXM
* @param cellAddress the address of the cell to find a comment
* @return cell comment if one exists, otherwise returns null
*/
- public XSSFComment findCellComment(CellAddress cellAddress) {
- CTComment ct = getCTComment(cellAddress);
- return ct == null ? null : new XSSFComment(this, ct, null);
- }
-
- /**
- * Get the underlying CTComment xmlbean for a comment located at cellRef,
if it exists
- *
- * @param cellRef the location of the cell comment
- * @return CTComment xmlbean if comment exists, otherwise return null.
- */
- @Internal
- public CTComment getCTComment(CellAddress cellRef) {
- // Create the cache if needed
- prepareCTCommentCache();
-
- // Return the comment, or null if not known
- return commentRefs.get(cellRef);
- }
-
- /**
- * Returns all cell comments on this sheet.
- * @return A map of each Comment in this sheet, keyed on the cell address
where
- * the comment is located.
- */
- public Map<CellAddress, XSSFComment> getCellComments() {
- prepareCTCommentCache();
- final TreeMap<CellAddress, XSSFComment> map = new TreeMap<>();
-
- for (final Entry<CellAddress, CTComment> e: commentRefs.entrySet()) {
- map.put(e.getKey(), new XSSFComment(this, e.getValue(), null));
- }
-
- return map;
- }
-
- /**
- * Refresh Map<CellAddress, CTComment> commentRefs cache,
- * Calls that use the commentRefs cache will perform in O(1)
- * time rather than O(n) lookup time for List<CTComment> comments.
- */
- private void prepareCTCommentCache() {
- // Create the cache if needed
- if(commentRefs == null) {
- commentRefs = new HashMap<>();
- for (CTComment comment :
comments.getCommentList().getCommentArray()) {
- commentRefs.put(new CellAddress(comment.getRef()), comment);
- }
- }
- }
-
- /**
- * Create a new comment located` at cell address
- *
- * @param ref the location to add the comment
- * @return a new CTComment located at ref with default author
- */
- @Internal
- public CTComment newComment(CellAddress ref) {
- CTComment ct = comments.getCommentList().addNewComment();
- ct.setRef(ref.formatAsString());
- ct.setAuthorId(DEFAULT_AUTHOR_ID);
-
- if(commentRefs != null) {
- commentRefs.put(ref, ct);
- }
- return ct;
- }
+ XSSFComment findCellComment(CellAddress cellAddress);
/**
* Remove the comment at cellRef location, if one exists
@@ -205,46 +48,5 @@ public class CommentsTable extends POIXM
* @param cellRef the location of the comment to remove
* @return returns true if a comment was removed
*/
- public boolean removeComment(CellAddress cellRef) {
- final String stringRef = cellRef.formatAsString();
- CTCommentList lst = comments.getCommentList();
- if(lst != null) {
- CTComment[] commentArray = lst.getCommentArray();
- for (int i = 0; i < commentArray.length; i++) {
- CTComment comment = commentArray[i];
- if (stringRef.equals(comment.getRef())) {
- lst.removeComment(i);
-
- if(commentRefs != null) {
- commentRefs.remove(cellRef);
- }
- return true;
- }
- }
- }
- return false;
- }
-
- /**
- * Add a new author to the CommentsTable.
- * This does not check if the author already exists.
- *
- * @param author the name of the comment author
- * @return the index of the new author
- */
- private int addNewAuthor(String author) {
- int index = comments.getAuthors().sizeOfAuthorArray();
- comments.getAuthors().insertAuthor(index, author);
- return index;
- }
-
- /**
- * Returns the underlying CTComments list xmlbean
- *
- * @return underlying comments list xmlbean
- */
- @Internal
- public CTComments getCTComments(){
- return comments;
- }
+ boolean removeComment(CellAddress cellRef);
}
Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java
URL:
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java?rev=1836721&r1=1836720&r2=1836721&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java
(original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java Thu
Jul 26 12:12:39 2018
@@ -38,9 +38,11 @@ import org.openxmlformats.schemas.spread
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CommentsDocument;
@Internal
-public class CommentsTable extends POIXMLDocumentPart {
+public class CommentsTable extends POIXMLDocumentPart implements Comments {
+
public static final String DEFAULT_AUTHOR = "";
public static final int DEFAULT_AUTHOR_ID = 0;
+
/**
* Underlying XML Beans CTComment list.
*/
@@ -75,6 +77,7 @@ public class CommentsTable extends POIXM
throw new IOException(e.getLocalizedMessage());
}
}
+
public void writeTo(OutputStream out) throws IOException {
CommentsDocument doc = CommentsDocument.Factory.newInstance();
doc.setComments(comments);
@@ -102,18 +105,22 @@ public class CommentsTable extends POIXM
}
}
+ @Override
public int getNumberOfComments() {
return comments.getCommentList().sizeOfCommentArray();
}
+ @Override
public int getNumberOfAuthors() {
return comments.getAuthors().sizeOfAuthorArray();
}
+ @Override
public String getAuthor(long authorId) {
return comments.getAuthors().getAuthorArray((int)authorId);
}
+ @Override
public int findAuthor(String author) {
String[] authorArray = comments.getAuthors().getAuthorArray();
for (int i = 0 ; i < authorArray.length; i++) {
@@ -130,6 +137,7 @@ public class CommentsTable extends POIXM
* @param cellAddress the address of the cell to find a comment
* @return cell comment if one exists, otherwise returns null
*/
+ @Override
public XSSFComment findCellComment(CellAddress cellAddress) {
CTComment ct = getCTComment(cellAddress);
return ct == null ? null : new XSSFComment(this, ct, null);
@@ -205,6 +213,7 @@ public class CommentsTable extends POIXM
* @param cellRef the location of the comment to remove
* @return returns true if a comment was removed
*/
+ @Override
public boolean removeComment(CellAddress cellRef) {
final String stringRef = cellRef.formatAsString();
CTCommentList lst = comments.getCommentList();
Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/SharedStrings.java
URL:
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/SharedStrings.java?rev=1836721&r1=1836720&r2=1836721&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/SharedStrings.java
(original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/model/SharedStrings.java Thu
Jul 26 12:12:39 2018
@@ -48,7 +48,7 @@ public interface SharedStrings {
* @param idx index of item to return.
* @return the item at the specified position in this Shared String table.
*/
- public RichTextString getItemAt(int idx);
+ RichTextString getItemAt(int idx);
/**
* Return an integer representing the total count of strings in the
workbook. This count does not
@@ -56,7 +56,7 @@ public interface SharedStrings {
*
* @return the total count of strings in the workbook
*/
- public int getCount();
+ int getCount();
/**
* Returns an integer representing the total count of unique strings in
the Shared String Table.
@@ -65,5 +65,5 @@ public interface SharedStrings {
*
* @return the total count of unique strings in the workbook
*/
- public int getUniqueCount();
+ int getUniqueCount();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]