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.

POIDocument.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.ByteArrayInputStream;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import org.apache.poi.hpsf.DocumentSummaryInformation;
  24. import org.apache.poi.hpsf.MutablePropertySet;
  25. import org.apache.poi.hpsf.PropertySet;
  26. import org.apache.poi.hpsf.PropertySetFactory;
  27. import org.apache.poi.hpsf.SummaryInformation;
  28. import org.apache.poi.poifs.filesystem.DirectoryEntry;
  29. import org.apache.poi.poifs.filesystem.DirectoryNode;
  30. import org.apache.poi.poifs.filesystem.DocumentEntry;
  31. import org.apache.poi.poifs.filesystem.DocumentInputStream;
  32. import org.apache.poi.poifs.filesystem.Entry;
  33. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  34. import org.apache.poi.util.POILogFactory;
  35. import org.apache.poi.util.POILogger;
  36. /**
  37. * This holds the common functionality for all POI
  38. * Document classes.
  39. * Currently, this relates to Document Information Properties
  40. *
  41. * @author Nick Burch
  42. */
  43. public abstract class POIDocument {
  44. /** Holds metadata on our document */
  45. private SummaryInformation sInf;
  46. /** Holds further metadata on our document */
  47. private DocumentSummaryInformation dsInf;
  48. /** The directory that our document lives in */
  49. protected DirectoryNode directory;
  50. /** For our own logging use */
  51. private final static POILogger logger = POILogFactory.getLogger(POIDocument.class);
  52. /* Have the property streams been read yet? (Only done on-demand) */
  53. private boolean initialized = false;
  54. protected POIDocument(DirectoryNode dir) {
  55. this.directory = dir;
  56. }
  57. @Deprecated
  58. protected POIDocument(DirectoryNode dir, POIFSFileSystem fs) {
  59. this.directory = dir;
  60. }
  61. protected POIDocument(POIFSFileSystem fs) {
  62. this(fs.getRoot());
  63. }
  64. /**
  65. * Fetch the Document Summary Information of the document
  66. */
  67. public DocumentSummaryInformation getDocumentSummaryInformation() {
  68. if(!initialized) readProperties();
  69. return dsInf;
  70. }
  71. /**
  72. * Fetch the Summary Information of the document
  73. */
  74. public SummaryInformation getSummaryInformation() {
  75. if(!initialized) readProperties();
  76. return sInf;
  77. }
  78. /**
  79. * Will create whichever of SummaryInformation
  80. * and DocumentSummaryInformation (HPSF) properties
  81. * are not already part of your document.
  82. * This is normally useful when creating a new
  83. * document from scratch.
  84. * If the information properties are already there,
  85. * then nothing will happen.
  86. */
  87. public void createInformationProperties() {
  88. if(!initialized) readProperties();
  89. if(sInf == null) {
  90. sInf = PropertySetFactory.newSummaryInformation();
  91. }
  92. if(dsInf == null) {
  93. dsInf = PropertySetFactory.newDocumentSummaryInformation();
  94. }
  95. }
  96. /**
  97. * Find, and create objects for, the standard
  98. * Documment Information Properties (HPSF).
  99. * If a given property set is missing or corrupt,
  100. * it will remain null;
  101. */
  102. protected void readProperties() {
  103. PropertySet ps;
  104. // DocumentSummaryInformation
  105. ps = getPropertySet(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
  106. if(ps != null && ps instanceof DocumentSummaryInformation) {
  107. dsInf = (DocumentSummaryInformation)ps;
  108. } else if(ps != null) {
  109. logger.log(POILogger.WARN, "DocumentSummaryInformation property set came back with wrong class - ", ps.getClass());
  110. }
  111. // SummaryInformation
  112. ps = getPropertySet(SummaryInformation.DEFAULT_STREAM_NAME);
  113. if(ps instanceof SummaryInformation) {
  114. sInf = (SummaryInformation)ps;
  115. } else if(ps != null) {
  116. logger.log(POILogger.WARN, "SummaryInformation property set came back with wrong class - ", ps.getClass());
  117. }
  118. // Mark the fact that we've now loaded up the properties
  119. initialized = true;
  120. }
  121. /**
  122. * For a given named property entry, either return it or null if
  123. * if it wasn't found
  124. */
  125. protected PropertySet getPropertySet(String setName) {
  126. //directory can be null when creating new documents
  127. if(directory == null) return null;
  128. DocumentInputStream dis;
  129. try {
  130. // Find the entry, and get an input stream for it
  131. dis = directory.createDocumentInputStream( directory.getEntry(setName) );
  132. } catch(IOException ie) {
  133. // Oh well, doesn't exist
  134. logger.log(POILogger.WARN, "Error getting property set with name " + setName + "\n" + ie);
  135. return null;
  136. }
  137. try {
  138. // Create the Property Set
  139. PropertySet set = PropertySetFactory.create(dis);
  140. return set;
  141. } catch(IOException ie) {
  142. // Must be corrupt or something like that
  143. logger.log(POILogger.WARN, "Error creating property set with name " + setName + "\n" + ie);
  144. } catch(org.apache.poi.hpsf.HPSFException he) {
  145. // Oh well, doesn't exist
  146. logger.log(POILogger.WARN, "Error creating property set with name " + setName + "\n" + he);
  147. }
  148. return null;
  149. }
  150. /**
  151. * Writes out the standard Documment Information Properties (HPSF)
  152. * @param outFS the POIFSFileSystem to write the properties into
  153. */
  154. protected void writeProperties(POIFSFileSystem outFS) throws IOException {
  155. writeProperties(outFS, null);
  156. }
  157. /**
  158. * Writes out the standard Documment Information Properties (HPSF)
  159. * @param outFS the POIFSFileSystem to write the properties into
  160. * @param writtenEntries a list of POIFS entries to add the property names too
  161. */
  162. protected void writeProperties(POIFSFileSystem outFS, List writtenEntries) throws IOException {
  163. SummaryInformation si = getSummaryInformation();
  164. if(si != null) {
  165. writePropertySet(SummaryInformation.DEFAULT_STREAM_NAME, si, outFS);
  166. if(writtenEntries != null) {
  167. writtenEntries.add(SummaryInformation.DEFAULT_STREAM_NAME);
  168. }
  169. }
  170. DocumentSummaryInformation dsi = getDocumentSummaryInformation();
  171. if(dsi != null) {
  172. writePropertySet(DocumentSummaryInformation.DEFAULT_STREAM_NAME, dsi, outFS);
  173. if(writtenEntries != null) {
  174. writtenEntries.add(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
  175. }
  176. }
  177. }
  178. /**
  179. * Writes out a given ProperySet
  180. * @param name the (POIFS Level) name of the property to write
  181. * @param set the PropertySet to write out
  182. * @param outFS the POIFSFileSystem to write the property into
  183. */
  184. protected void writePropertySet(String name, PropertySet set, POIFSFileSystem outFS) throws IOException {
  185. try {
  186. MutablePropertySet mSet = new MutablePropertySet(set);
  187. ByteArrayOutputStream bOut = new ByteArrayOutputStream();
  188. mSet.write(bOut);
  189. byte[] data = bOut.toByteArray();
  190. ByteArrayInputStream bIn = new ByteArrayInputStream(data);
  191. outFS.createDocument(bIn,name);
  192. logger.log(POILogger.INFO, "Wrote property set " + name + " of size " + data.length);
  193. } catch(org.apache.poi.hpsf.WritingNotSupportedException wnse) {
  194. System.err.println("Couldn't write property set with name " + name + " as not supported by HPSF yet");
  195. }
  196. }
  197. /**
  198. * Writes the document out to the specified output stream
  199. */
  200. public abstract void write(OutputStream out) throws IOException;
  201. /**
  202. * Copies nodes from one POIFS to the other minus the excepts
  203. * @param source is the source POIFS to copy from
  204. * @param target is the target POIFS to copy to
  205. * @param excepts is a list of Strings specifying what nodes NOT to copy
  206. */
  207. protected void copyNodes(POIFSFileSystem source, POIFSFileSystem target,
  208. List excepts) throws IOException {
  209. //System.err.println("CopyNodes called");
  210. DirectoryEntry root = source.getRoot();
  211. DirectoryEntry newRoot = target.getRoot();
  212. Iterator entries = root.getEntries();
  213. while (entries.hasNext()) {
  214. Entry entry = (Entry)entries.next();
  215. if (!isInList(entry.getName(), excepts)) {
  216. copyNodeRecursively(entry,newRoot);
  217. }
  218. }
  219. }
  220. /**
  221. * Checks to see if the String is in the list, used when copying
  222. * nodes between one POIFS and another
  223. */
  224. private boolean isInList(String entry, List list) {
  225. for (int k = 0; k < list.size(); k++) {
  226. if (list.get(k).equals(entry)) {
  227. return true;
  228. }
  229. }
  230. return false;
  231. }
  232. /**
  233. * Copies an Entry into a target POIFS directory, recursively
  234. */
  235. private void copyNodeRecursively(Entry entry, DirectoryEntry target)
  236. throws IOException {
  237. //System.err.println("copyNodeRecursively called with "+entry.getName()+
  238. // ","+target.getName());
  239. DirectoryEntry newTarget = null;
  240. if (entry.isDirectoryEntry()) {
  241. newTarget = target.createDirectory(entry.getName());
  242. Iterator entries = ((DirectoryEntry)entry).getEntries();
  243. while (entries.hasNext()) {
  244. copyNodeRecursively((Entry)entries.next(),newTarget);
  245. }
  246. } else {
  247. DocumentEntry dentry = (DocumentEntry)entry;
  248. DocumentInputStream dstream = new DocumentInputStream(dentry);
  249. target.createDocument(dentry.getName(),dstream);
  250. dstream.close();
  251. }
  252. }
  253. }