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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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;
  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.io.PushbackInputStream;
  22. import java.util.HashMap;
  23. import java.util.HashSet;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Set;
  27. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  28. import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
  29. import org.apache.poi.openxml4j.opc.OPCPackage;
  30. import org.apache.poi.openxml4j.opc.PackageAccess;
  31. import org.apache.poi.openxml4j.opc.PackagePart;
  32. import org.apache.poi.openxml4j.opc.PackageRelationship;
  33. import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
  34. import org.apache.poi.poifs.common.POIFSConstants;
  35. import org.apache.poi.util.IOUtils;
  36. import org.apache.xmlbeans.impl.common.SystemCache;
  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 pkg) {
  58. this.pkg = pkg;
  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, returning an IOException
  66. * in the event of a problem.
  67. * Works around shortcomings in java's this() constructor calls
  68. */
  69. public static OPCPackage openPackage(String path) throws IOException {
  70. try {
  71. return OPCPackage.open(path);
  72. } catch (InvalidFormatException e) {
  73. throw new IOException(e.toString());
  74. }
  75. }
  76. public OPCPackage getPackage() {
  77. return this.pkg;
  78. }
  79. protected PackagePart getCorePart() {
  80. return getPackagePart();
  81. }
  82. /**
  83. * Retrieves all the PackageParts which are defined as
  84. * relationships of the base document with the
  85. * specified content type.
  86. */
  87. protected PackagePart[] getRelatedByType(String contentType) throws InvalidFormatException {
  88. PackageRelationshipCollection partsC =
  89. getPackagePart().getRelationshipsByType(contentType);
  90. PackagePart[] parts = new PackagePart[partsC.size()];
  91. int count = 0;
  92. for (PackageRelationship rel : partsC) {
  93. parts[count] = getPackagePart().getRelatedPart(rel);
  94. count++;
  95. }
  96. return parts;
  97. }
  98. /**
  99. * Checks that the supplied InputStream (which MUST
  100. * support mark and reset, or be a PushbackInputStream)
  101. * has a OOXML (zip) header at the start of it.
  102. * If your InputStream does not support mark / reset,
  103. * then wrap it in a PushBackInputStream, then be
  104. * sure to always use that, and not the original!
  105. * @param inp An InputStream which supports either mark/reset, or is a PushbackInputStream
  106. */
  107. public static boolean hasOOXMLHeader(InputStream inp) throws IOException {
  108. // We want to peek at the first 4 bytes
  109. inp.mark(4);
  110. byte[] header = new byte[4];
  111. int bytesRead = IOUtils.readFully(inp, header);
  112. // Wind back those 4 bytes
  113. if(inp instanceof PushbackInputStream) {
  114. PushbackInputStream pin = (PushbackInputStream)inp;
  115. pin.unread(header, 0, bytesRead);
  116. } else {
  117. inp.reset();
  118. }
  119. // Did it match the ooxml zip signature?
  120. return (
  121. bytesRead == 4 &&
  122. header[0] == POIFSConstants.OOXML_FILE_HEADER[0] &&
  123. header[1] == POIFSConstants.OOXML_FILE_HEADER[1] &&
  124. header[2] == POIFSConstants.OOXML_FILE_HEADER[2] &&
  125. header[3] == POIFSConstants.OOXML_FILE_HEADER[3]
  126. );
  127. }
  128. /**
  129. * Get the document properties. This gives you access to the
  130. * core ooxml properties, and the extended ooxml properties.
  131. */
  132. public POIXMLProperties getProperties() {
  133. if(properties == null) {
  134. try {
  135. properties = new POIXMLProperties(pkg);
  136. } catch (Exception e){
  137. throw new POIXMLException(e);
  138. }
  139. }
  140. return properties;
  141. }
  142. /**
  143. * Get the document's embedded files.
  144. */
  145. public abstract List<PackagePart> getAllEmbedds() throws OpenXML4JException;
  146. protected final void load(POIXMLFactory factory) throws IOException {
  147. Map<PackagePart, POIXMLDocumentPart> context = new HashMap<PackagePart, POIXMLDocumentPart>();
  148. try {
  149. read(factory, context);
  150. } catch (OpenXML4JException e){
  151. throw new POIXMLException(e);
  152. }
  153. onDocumentRead();
  154. context.clear();
  155. }
  156. /**
  157. * Closes the underlying {@link OPCPackage} from which this
  158. * document was read, if there is one
  159. */
  160. public void close() throws IOException {
  161. if (pkg != null) {
  162. if (pkg.getPackageAccess() == PackageAccess.READ) {
  163. pkg.revert();
  164. } else {
  165. pkg.close();
  166. }
  167. pkg = null;
  168. }
  169. }
  170. /**
  171. * Write out this document to an Outputstream.
  172. *
  173. * Note - if the Document was opened from a {@link File} rather
  174. * than an {@link InputStream}, you <b>must</b> write out to
  175. * a different file, overwriting via an OutputStream isn't possible.
  176. *
  177. * @param stream - the java OutputStream you wish to write the file to
  178. *
  179. * @exception IOException if anything can't be written.
  180. */
  181. public final void write(OutputStream stream) throws IOException {
  182. //force all children to commit their changes into the underlying OOXML Package
  183. Set<PackagePart> context = new HashSet<PackagePart>();
  184. onSave(context);
  185. context.clear();
  186. //save extended and custom properties
  187. getProperties().commit();
  188. getPackage().save(stream);
  189. }
  190. }