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.

POIXMLDocument.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.ooxml;
  16. import java.io.Closeable;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.util.HashMap;
  22. import java.util.HashSet;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  27. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  28. import org.apache.poi.openxml4j.opc.OPCPackage;
  29. import org.apache.poi.openxml4j.opc.PackageAccess;
  30. import org.apache.poi.openxml4j.opc.PackagePart;
  31. import org.apache.poi.openxml4j.opc.PackageRelationship;
  32. import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
  33. import org.apache.xmlbeans.impl.common.SystemCache;
  34. /**
  35. * This holds the common functionality for all POI OOXML Document classes.
  36. */
  37. public abstract class POIXMLDocument extends POIXMLDocumentPart implements Closeable {
  38. public static final String DOCUMENT_CREATOR = "Apache POI";
  39. // OLE embeddings relation name
  40. public static final String OLE_OBJECT_REL_TYPE="http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject";
  41. // Embedded OPC documents relation name
  42. public static final String PACK_OBJECT_REL_TYPE="http://schemas.openxmlformats.org/officeDocument/2006/relationships/package";
  43. /** The OPC Package */
  44. private OPCPackage pkg;
  45. /**
  46. * The properties of the OPC package, opened as needed
  47. */
  48. private POIXMLProperties properties;
  49. protected POIXMLDocument(OPCPackage pkg) {
  50. super(pkg);
  51. init(pkg);
  52. }
  53. protected POIXMLDocument(OPCPackage pkg, String coreDocumentRel) {
  54. super(pkg, coreDocumentRel);
  55. init(pkg);
  56. }
  57. private void init(OPCPackage p) {
  58. this.pkg = p;
  59. // Workaround for XMLBEANS-512 - ensure that when we parse
  60. // the file, we start with a fresh XML Parser each time,
  61. // and avoid the risk of getting a SaxHandler that's in error
  62. SystemCache.get().setSaxLoader(null);
  63. }
  64. /**
  65. * Wrapper to open a package, which works around shortcomings in java's this() constructor calls
  66. *
  67. * @param path the path to the document
  68. * @return the new OPCPackage
  69. *
  70. * @throws IOException if there was a problem opening the document
  71. */
  72. public static OPCPackage openPackage(String path) throws IOException {
  73. try {
  74. return OPCPackage.open(path);
  75. } catch (InvalidFormatException e) {
  76. throw new IOException(e.toString(), e);
  77. }
  78. }
  79. /**
  80. * Get the assigned OPCPackage
  81. *
  82. * @return the assigned OPCPackage
  83. */
  84. public OPCPackage getPackage() {
  85. return this.pkg;
  86. }
  87. protected PackagePart getCorePart() {
  88. return getPackagePart();
  89. }
  90. /**
  91. * Retrieves all the PackageParts which are defined as relationships of the base document with the
  92. * specified content type.
  93. *
  94. * @param contentType the content type
  95. *
  96. * @return all the base document PackageParts which match the content type
  97. *
  98. * @throws InvalidFormatException when the relationships or the parts contain errors
  99. *
  100. * @see org.apache.poi.xssf.usermodel.XSSFRelation
  101. * @see org.apache.poi.xslf.usermodel.XSLFRelation
  102. * @see org.apache.poi.xwpf.usermodel.XWPFRelation
  103. * @see org.apache.poi.xdgf.usermodel.XDGFRelation
  104. */
  105. protected PackagePart[] getRelatedByType(String contentType) throws InvalidFormatException {
  106. PackageRelationshipCollection partsC =
  107. getPackagePart().getRelationshipsByType(contentType);
  108. PackagePart[] parts = new PackagePart[partsC.size()];
  109. int count = 0;
  110. for (PackageRelationship rel : partsC) {
  111. parts[count] = getPackagePart().getRelatedPart(rel);
  112. count++;
  113. }
  114. return parts;
  115. }
  116. /**
  117. * Get the document properties. This gives you access to the
  118. * core ooxml properties, and the extended ooxml properties.
  119. *
  120. * @return the document properties
  121. */
  122. public POIXMLProperties getProperties() {
  123. if(properties == null) {
  124. try {
  125. properties = new POIXMLProperties(pkg);
  126. } catch (Exception e){
  127. throw new POIXMLException(e);
  128. }
  129. }
  130. return properties;
  131. }
  132. /**
  133. * Get the document's embedded files.
  134. *
  135. * @return the document's embedded files
  136. *
  137. * @throws OpenXML4JException if the embedded parts can't be determined
  138. * @since POI 4.0.0
  139. */
  140. public abstract List<PackagePart> getAllEmbeddedParts() throws OpenXML4JException;
  141. protected final void load(POIXMLFactory factory) throws IOException {
  142. Map<PackagePart, POIXMLDocumentPart> context = new HashMap<>();
  143. try {
  144. read(factory, context);
  145. } catch (OpenXML4JException e){
  146. throw new POIXMLException(e);
  147. }
  148. onDocumentRead();
  149. context.clear();
  150. }
  151. /**
  152. * Closes the underlying {@link OPCPackage} from which this
  153. * document was read, if there is one
  154. *
  155. * <p>Once this has been called, no further
  156. * operations, updates or reads should be performed on the
  157. * document.
  158. *
  159. * @throws IOException for writable packages, if an IO exception occur during the saving process.
  160. */
  161. @Override
  162. public void close() throws IOException {
  163. if (pkg != null) {
  164. if (pkg.getPackageAccess() == PackageAccess.READ) {
  165. pkg.revert();
  166. } else {
  167. pkg.close();
  168. }
  169. pkg = null;
  170. }
  171. }
  172. /**
  173. * Write out this document to an {@link OutputStream}.
  174. *
  175. * Note - if the Document was opened from a {@link File} rather
  176. * than an {@link InputStream}, you <b>must</b> write out to
  177. * a different file, overwriting via an OutputStream isn't possible.
  178. *
  179. * If {@code stream} is a {@link java.io.FileOutputStream} on a networked drive
  180. * or has a high cost/latency associated with each written byte,
  181. * consider wrapping the OutputStream in a {@link java.io.BufferedOutputStream}
  182. * to improve write performance.
  183. *
  184. * @param stream - the java OutputStream you wish to write the file to
  185. *
  186. * @throws IOException if anything can't be written.
  187. */
  188. @SuppressWarnings("resource")
  189. public final void write(OutputStream stream) throws IOException {
  190. OPCPackage p = getPackage();
  191. if(p == null) {
  192. throw new IOException("Cannot write data, document seems to have been closed already");
  193. }
  194. //force all children to commit their changes into the underlying OOXML Package
  195. // TODO Shouldn't they be committing to the new one instead?
  196. Set<PackagePart> context = new HashSet<>();
  197. onSave(context);
  198. context.clear();
  199. //save extended and custom properties
  200. getProperties().commit();
  201. p.save(stream);
  202. }
  203. }