]> source.dussan.org Git - poi.git/commitdiff
small refactor of XSSFReader
authorPJ Fanning <fanningpj@apache.org>
Wed, 22 Dec 2021 04:00:44 +0000 (04:00 +0000)
committerPJ Fanning <fanningpj@apache.org>
Wed, 22 Dec 2021 04:00:44 +0000 (04:00 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896260 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java

index bc1bad8abf48182c165c92b63339af4e71032f25..fa834e1c760e03554add25fb24dc2e49f38f3b8f 100644 (file)
@@ -74,6 +74,7 @@ public class XSSFReader {
 
     protected OPCPackage pkg;
     protected PackagePart workbookPart;
+    protected boolean useReadOnlySharedStringsTable;
 
     /**
      * Creates a new XSSFReader, for the given package
@@ -115,14 +116,40 @@ public class XSSFReader {
         workbookPart = this.pkg.getPart(coreDocRelationship);
     }
 
+    /**
+     * Controls whether {@link #getSharedStringsTable()} uses {@link SharedStringsTable}
+     * or {@link ReadOnlySharedStringsTable}.
+     * @param useReadOnlySharedStringsTable
+     * @since POI 5.2.0
+     */
+    public void setUseReadOnlySharedStringsTable(boolean useReadOnlySharedStringsTable) {
+        this.useReadOnlySharedStringsTable = useReadOnlySharedStringsTable;
+    }
+
+    /**
+     * @return whether {@link #getSharedStringsTable()} uses {@link SharedStringsTable}
+     * or {@link ReadOnlySharedStringsTable}.
+     * @since POI 5.2.0
+     */
+    public boolean useReadOnlySharedStringsTable(boolean useReadOnlySharedStringsTable) {
+        return useReadOnlySharedStringsTable;
+    }
+
     /**
      * Opens up the Shared Strings Table, parses it, and
      * returns a handy object for working with
      * shared strings.
+     * @see #setUseReadOnlySharedStringsTable(boolean)
      */
     public SharedStrings getSharedStringsTable() throws IOException, InvalidFormatException {
         ArrayList<PackagePart> parts = pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType());
-        return parts.size() == 0 ? null : new SharedStringsTable(parts.get(0));
+        try {
+            return parts.size() == 0 ? null :
+                    useReadOnlySharedStringsTable ? new ReadOnlySharedStringsTable(parts.get(0)) :
+                            new SharedStringsTable(parts.get(0));
+        } catch (SAXException se) {
+            throw new InvalidFormatException("Failed to parse SharedStringsTable", se);
+        }
     }
 
     /**