You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReadOnlySharedStringsTable.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.eventusermodel;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import javax.xml.parsers.ParserConfigurationException;
  21. import org.apache.poi.openxml4j.opc.OPCPackage;
  22. import org.apache.poi.openxml4j.opc.PackagePart;
  23. import org.apache.poi.openxml4j.opc.PackageRelationship;
  24. import org.apache.poi.util.SAXHelper;
  25. import org.apache.poi.xssf.usermodel.XSSFRelation;
  26. import org.xml.sax.Attributes;
  27. import org.xml.sax.InputSource;
  28. import org.xml.sax.SAXException;
  29. import org.xml.sax.XMLReader;
  30. import org.xml.sax.helpers.DefaultHandler;
  31. /**
  32. * <p>This is a lightweight way to process the Shared Strings
  33. * table. Most of the text cells will reference something
  34. * from in here.
  35. * <p>Note that each SI entry can have multiple T elements, if the
  36. * string is made up of bits with different formatting.
  37. * <p>Example input:
  38. * <pre>
  39. &lt;?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
  40. &lt;sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="2" uniqueCount="2">
  41. &lt;si>
  42. &lt;r>
  43. &lt;rPr>
  44. &lt;b />
  45. &lt;sz val="11" />
  46. &lt;color theme="1" />
  47. &lt;rFont val="Calibri" />
  48. &lt;family val="2" />
  49. &lt;scheme val="minor" />
  50. &lt;/rPr>
  51. &lt;t>This:&lt;/t>
  52. &lt;/r>
  53. &lt;r>
  54. &lt;rPr>
  55. &lt;sz val="11" />
  56. &lt;color theme="1" />
  57. &lt;rFont val="Calibri" />
  58. &lt;family val="2" />
  59. &lt;scheme val="minor" />
  60. &lt;/rPr>
  61. &lt;t xml:space="preserve">Causes Problems&lt;/t>
  62. &lt;/r>
  63. &lt;/si>
  64. &lt;si>
  65. &lt;t>This does not&lt;/t>
  66. &lt;/si>
  67. &lt;/sst>
  68. * </pre>
  69. *
  70. */
  71. public class ReadOnlySharedStringsTable extends DefaultHandler {
  72. /**
  73. * An integer representing the total count of strings in the workbook. This count does not
  74. * include any numbers, it counts only the total of text strings in the workbook.
  75. */
  76. private int count;
  77. /**
  78. * An integer representing the total count of unique strings in the Shared String Table.
  79. * A string is unique even if it is a copy of another string, but has different formatting applied
  80. * at the character level.
  81. */
  82. private int uniqueCount;
  83. /**
  84. * The shared strings table.
  85. */
  86. private List<String> strings;
  87. /**
  88. * @param pkg
  89. * @throws IOException
  90. * @throws SAXException
  91. * @throws ParserConfigurationException
  92. */
  93. public ReadOnlySharedStringsTable(OPCPackage pkg)
  94. throws IOException, SAXException {
  95. ArrayList<PackagePart> parts =
  96. pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType());
  97. // Some workbooks have no shared strings table.
  98. if (parts.size() > 0) {
  99. PackagePart sstPart = parts.get(0);
  100. readFrom(sstPart.getInputStream());
  101. }
  102. }
  103. /**
  104. * Like POIXMLDocumentPart constructor
  105. *
  106. * @param part
  107. * @param rel_ignored
  108. * @throws IOException
  109. */
  110. public ReadOnlySharedStringsTable(PackagePart part, PackageRelationship rel_ignored)
  111. throws IOException, SAXException {
  112. readFrom(part.getInputStream());
  113. }
  114. /**
  115. * Read this shared strings table from an XML file.
  116. *
  117. * @param is The input stream containing the XML document.
  118. * @throws IOException if an error occurs while reading.
  119. * @throws SAXException
  120. * @throws ParserConfigurationException
  121. */
  122. public void readFrom(InputStream is) throws IOException, SAXException {
  123. InputSource sheetSource = new InputSource(is);
  124. try {
  125. XMLReader sheetParser = SAXHelper.newXMLReader();
  126. sheetParser.setContentHandler(this);
  127. sheetParser.parse(sheetSource);
  128. } catch(ParserConfigurationException e) {
  129. throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage());
  130. }
  131. }
  132. /**
  133. * Return an integer representing the total count of strings in the workbook. This count does not
  134. * include any numbers, it counts only the total of text strings in the workbook.
  135. *
  136. * @return the total count of strings in the workbook
  137. */
  138. public int getCount() {
  139. return this.count;
  140. }
  141. /**
  142. * Returns an integer representing the total count of unique strings in the Shared String Table.
  143. * A string is unique even if it is a copy of another string, but has different formatting applied
  144. * at the character level.
  145. *
  146. * @return the total count of unique strings in the workbook
  147. */
  148. public int getUniqueCount() {
  149. return this.uniqueCount;
  150. }
  151. /**
  152. * Return the string at a given index.
  153. * Formatting is ignored.
  154. *
  155. * @param idx index of item to return.
  156. * @return the item at the specified position in this Shared String table.
  157. */
  158. public String getEntryAt(int idx) {
  159. return strings.get(idx);
  160. }
  161. public List<String> getItems() {
  162. return strings;
  163. }
  164. //// ContentHandler methods ////
  165. private StringBuffer characters;
  166. private boolean tIsOpen;
  167. public void startElement(String uri, String localName, String name,
  168. Attributes attributes) throws SAXException {
  169. if ("sst".equals(name)) {
  170. String count = attributes.getValue("count");
  171. if(count != null) this.count = Integer.parseInt(count);
  172. String uniqueCount = attributes.getValue("uniqueCount");
  173. if(uniqueCount != null) this.uniqueCount = Integer.parseInt(uniqueCount);
  174. this.strings = new ArrayList<String>(this.uniqueCount);
  175. characters = new StringBuffer();
  176. } else if ("si".equals(name)) {
  177. characters.setLength(0);
  178. } else if ("t".equals(name)) {
  179. tIsOpen = true;
  180. }
  181. }
  182. public void endElement(String uri, String localName, String name)
  183. throws SAXException {
  184. if ("si".equals(name)) {
  185. strings.add(characters.toString());
  186. } else if ("t".equals(name)) {
  187. tIsOpen = false;
  188. }
  189. }
  190. /**
  191. * Captures characters only if a t(ext) element is open.
  192. */
  193. public void characters(char[] ch, int start, int length)
  194. throws SAXException {
  195. if (tIsOpen)
  196. characters.append(ch, start, length);
  197. }
  198. }