PageRenderTime 66ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/apache/poi/POIDocument.java

https://github.com/minstrelsy/SimpleAndroidDocView
Java | 274 lines | 149 code | 26 blank | 99 comment | 28 complexity | afa24f033acdbeef13a37ab141698759 MD5 | raw file
Possible License(s): Apache-2.0
  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.OutputStream;
  20. import java.util.List;
  21. import org.apache.poi.hpsf.DocumentSummaryInformation;
  22. import org.apache.poi.hpsf.MutablePropertySet;
  23. import org.apache.poi.hpsf.PropertySet;
  24. import org.apache.poi.hpsf.PropertySetFactory;
  25. import org.apache.poi.hpsf.SummaryInformation;
  26. import org.apache.poi.poifs.filesystem.DirectoryEntry;
  27. import org.apache.poi.poifs.filesystem.DirectoryNode;
  28. import org.apache.poi.poifs.filesystem.DocumentInputStream;
  29. import org.apache.poi.poifs.filesystem.Entry;
  30. import org.apache.poi.poifs.filesystem.EntryUtils;
  31. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  32. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  33. import org.apache.poi.util.Internal;
  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. /**
  58. * @deprecated use {@link POIDocument#POIDocument(DirectoryNode)} instead
  59. */
  60. @Deprecated
  61. protected POIDocument(DirectoryNode dir, POIFSFileSystem fs) {
  62. this.directory = dir;
  63. }
  64. protected POIDocument(POIFSFileSystem fs) {
  65. this(fs.getRoot());
  66. }
  67. protected POIDocument(NPOIFSFileSystem fs) {
  68. this(fs.getRoot());
  69. }
  70. /**
  71. * Fetch the Document Summary Information of the document
  72. */
  73. public DocumentSummaryInformation getDocumentSummaryInformation() {
  74. if(!initialized) readProperties();
  75. return dsInf;
  76. }
  77. /**
  78. * Fetch the Summary Information of the document
  79. */
  80. public SummaryInformation getSummaryInformation() {
  81. if(!initialized) readProperties();
  82. return sInf;
  83. }
  84. /**
  85. * Will create whichever of SummaryInformation
  86. * and DocumentSummaryInformation (HPSF) properties
  87. * are not already part of your document.
  88. * This is normally useful when creating a new
  89. * document from scratch.
  90. * If the information properties are already there,
  91. * then nothing will happen.
  92. */
  93. public void createInformationProperties() {
  94. if(!initialized) readProperties();
  95. if(sInf == null) {
  96. sInf = PropertySetFactory.newSummaryInformation();
  97. }
  98. if(dsInf == null) {
  99. dsInf = PropertySetFactory.newDocumentSummaryInformation();
  100. }
  101. }
  102. /**
  103. * Find, and create objects for, the standard
  104. * Documment Information Properties (HPSF).
  105. * If a given property set is missing or corrupt,
  106. * it will remain null;
  107. */
  108. protected void readProperties() {
  109. PropertySet ps;
  110. // DocumentSummaryInformation
  111. ps = getPropertySet(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
  112. if(ps != null && ps instanceof DocumentSummaryInformation) {
  113. dsInf = (DocumentSummaryInformation)ps;
  114. } else if(ps != null) {
  115. logger.log(POILogger.WARN, "DocumentSummaryInformation property set came back with wrong class - ", ps.getClass());
  116. }
  117. // SummaryInformation
  118. ps = getPropertySet(SummaryInformation.DEFAULT_STREAM_NAME);
  119. if(ps instanceof SummaryInformation) {
  120. sInf = (SummaryInformation)ps;
  121. } else if(ps != null) {
  122. logger.log(POILogger.WARN, "SummaryInformation property set came back with wrong class - ", ps.getClass());
  123. }
  124. // Mark the fact that we've now loaded up the properties
  125. initialized = true;
  126. }
  127. /**
  128. * For a given named property entry, either return it or null if
  129. * if it wasn't found
  130. */
  131. protected PropertySet getPropertySet(String setName) {
  132. //directory can be null when creating new documents
  133. if(directory == null || !directory.hasEntry(setName)) return null;
  134. DocumentInputStream dis;
  135. try {
  136. // Find the entry, and get an input stream for it
  137. dis = directory.createDocumentInputStream( directory.getEntry(setName) );
  138. } catch(IOException ie) {
  139. // Oh well, doesn't exist
  140. logger.log(POILogger.WARN, "Error getting property set with name " + setName + "\n" + ie);
  141. return null;
  142. }
  143. try {
  144. // Create the Property Set
  145. PropertySet set = PropertySetFactory.create(dis);
  146. return set;
  147. } catch(IOException ie) {
  148. // Must be corrupt or something like that
  149. logger.log(POILogger.WARN, "Error creating property set with name " + setName + "\n" + ie);
  150. } catch(org.apache.poi.hpsf.HPSFException he) {
  151. // Oh well, doesn't exist
  152. logger.log(POILogger.WARN, "Error creating property set with name " + setName + "\n" + he);
  153. }
  154. catch (Exception e)
  155. {
  156. // TODO Auto-generated catch block
  157. e.printStackTrace();
  158. }
  159. return null;
  160. }
  161. /**
  162. * Writes out the standard Documment Information Properties (HPSF)
  163. * @param outFS the POIFSFileSystem to write the properties into
  164. */
  165. protected void writeProperties(POIFSFileSystem outFS) throws IOException {
  166. writeProperties(outFS, null);
  167. }
  168. /**
  169. * Writes out the standard Documment Information Properties (HPSF)
  170. * @param outFS the POIFSFileSystem to write the properties into
  171. * @param writtenEntries a list of POIFS entries to add the property names too
  172. */
  173. protected void writeProperties(POIFSFileSystem outFS, List<String> writtenEntries) throws IOException {
  174. SummaryInformation si = getSummaryInformation();
  175. if(si != null) {
  176. writePropertySet(SummaryInformation.DEFAULT_STREAM_NAME, si, outFS);
  177. if(writtenEntries != null) {
  178. writtenEntries.add(SummaryInformation.DEFAULT_STREAM_NAME);
  179. }
  180. }
  181. DocumentSummaryInformation dsi = getDocumentSummaryInformation();
  182. if(dsi != null) {
  183. writePropertySet(DocumentSummaryInformation.DEFAULT_STREAM_NAME, dsi, outFS);
  184. if(writtenEntries != null) {
  185. writtenEntries.add(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
  186. }
  187. }
  188. }
  189. /**
  190. * Writes out a given ProperySet
  191. * @param name the (POIFS Level) name of the property to write
  192. * @param set the PropertySet to write out
  193. * @param outFS the POIFSFileSystem to write the property into
  194. */
  195. protected void writePropertySet(String name, PropertySet set, POIFSFileSystem outFS) throws IOException {
  196. try {
  197. MutablePropertySet mSet = new MutablePropertySet(set);
  198. ByteArrayOutputStream bOut = new ByteArrayOutputStream();
  199. mSet.write(bOut);
  200. byte[] data = bOut.toByteArray();
  201. ByteArrayInputStream bIn = new ByteArrayInputStream(data);
  202. outFS.createDocument(bIn,name);
  203. logger.log(POILogger.INFO, "Wrote property set " + name + " of size " + data.length);
  204. } catch(org.apache.poi.hpsf.WritingNotSupportedException wnse) {
  205. logger.log( POILogger.ERROR, "Couldn't write property set with name " + name + " as not supported by HPSF yet");
  206. }
  207. }
  208. /**
  209. * Writes the document out to the specified output stream
  210. */
  211. public abstract void write(OutputStream out) throws IOException;
  212. /**
  213. * Copies nodes from one POIFS to the other minus the excepts
  214. * @param source is the source POIFS to copy from
  215. * @param target is the target POIFS to copy to
  216. * @param excepts is a list of Strings specifying what nodes NOT to copy
  217. */
  218. @Deprecated
  219. protected void copyNodes( POIFSFileSystem source, POIFSFileSystem target,
  220. List<String> excepts ) throws IOException
  221. {
  222. EntryUtils.copyNodes( source, target, excepts );
  223. }
  224. /**
  225. * Copies nodes from one POIFS to the other minus the excepts
  226. * @param sourceRoot is the source POIFS to copy from
  227. * @param targetRoot is the target POIFS to copy to
  228. * @param excepts is a list of Strings specifying what nodes NOT to copy
  229. */
  230. @Deprecated
  231. protected void copyNodes( DirectoryNode sourceRoot,
  232. DirectoryNode targetRoot, List<String> excepts ) throws IOException
  233. {
  234. EntryUtils.copyNodes( sourceRoot, targetRoot, excepts );
  235. }
  236. /**
  237. * Copies an Entry into a target POIFS directory, recursively
  238. */
  239. @Internal
  240. @Deprecated
  241. protected void copyNodeRecursively( Entry entry, DirectoryEntry target )
  242. throws IOException
  243. {
  244. EntryUtils.copyNodeRecursively( entry, target );
  245. }
  246. }