2007-09-19 07:56:36 -04:00
|
|
|
/* ====================================================================
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
|
|
|
|
|
|
|
package org.apache.poi;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
2014-12-24 20:56:29 -05:00
|
|
|
import java.io.InputStream;
|
2008-05-20 13:12:08 -04:00
|
|
|
import java.io.OutputStream;
|
2007-09-19 07:56:36 -04:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.apache.poi.hpsf.DocumentSummaryInformation;
|
|
|
|
import org.apache.poi.hpsf.MutablePropertySet;
|
|
|
|
import org.apache.poi.hpsf.PropertySet;
|
|
|
|
import org.apache.poi.hpsf.PropertySetFactory;
|
|
|
|
import org.apache.poi.hpsf.SummaryInformation;
|
2014-12-24 20:56:29 -05:00
|
|
|
import org.apache.poi.poifs.crypt.EncryptionInfo;
|
2007-09-19 07:56:36 -04:00
|
|
|
import org.apache.poi.poifs.filesystem.DirectoryEntry;
|
2008-04-10 12:59:10 -04:00
|
|
|
import org.apache.poi.poifs.filesystem.DirectoryNode;
|
2007-09-19 07:56:36 -04:00
|
|
|
import org.apache.poi.poifs.filesystem.DocumentInputStream;
|
|
|
|
import org.apache.poi.poifs.filesystem.Entry;
|
2011-08-23 12:44:06 -04:00
|
|
|
import org.apache.poi.poifs.filesystem.EntryUtils;
|
2011-03-25 12:06:11 -04:00
|
|
|
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
|
2007-09-19 07:56:36 -04:00
|
|
|
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
2011-08-17 10:53:28 -04:00
|
|
|
import org.apache.poi.util.Internal;
|
2007-09-19 07:56:36 -04:00
|
|
|
import org.apache.poi.util.POILogFactory;
|
|
|
|
import org.apache.poi.util.POILogger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This holds the common functionality for all POI
|
|
|
|
* Document classes.
|
|
|
|
* Currently, this relates to Document Information Properties
|
|
|
|
*
|
|
|
|
* @author Nick Burch
|
|
|
|
*/
|
|
|
|
public abstract class POIDocument {
|
2013-06-25 19:34:22 -04:00
|
|
|
/** Holds metadata on our document */
|
|
|
|
private SummaryInformation sInf;
|
|
|
|
/** Holds further metadata on our document */
|
|
|
|
private DocumentSummaryInformation dsInf;
|
|
|
|
/** The directory that our document lives in */
|
|
|
|
protected DirectoryNode directory;
|
|
|
|
|
|
|
|
/** For our own logging use */
|
|
|
|
private final static POILogger logger = POILogFactory.getLogger(POIDocument.class);
|
2007-09-19 07:56:36 -04:00
|
|
|
|
2008-03-03 10:10:46 -05:00
|
|
|
/* Have the property streams been read yet? (Only done on-demand) */
|
2009-08-20 09:49:33 -04:00
|
|
|
private boolean initialized = false;
|
2008-04-10 12:59:10 -04:00
|
|
|
|
2014-07-27 14:32:24 -04:00
|
|
|
/**
|
|
|
|
* Constructs a POIDocument with the given directory node.
|
|
|
|
*
|
|
|
|
* @param dir The {@link DirectoryNode} where information is read from.
|
|
|
|
*/
|
2010-12-28 22:19:46 -05:00
|
|
|
protected POIDocument(DirectoryNode dir) {
|
2008-04-10 12:59:10 -04:00
|
|
|
this.directory = dir;
|
|
|
|
}
|
2014-07-27 14:32:24 -04:00
|
|
|
|
2011-03-25 12:06:11 -04:00
|
|
|
/**
|
|
|
|
* @deprecated use {@link POIDocument#POIDocument(DirectoryNode)} instead
|
|
|
|
*/
|
2010-12-28 22:19:46 -05:00
|
|
|
@Deprecated
|
|
|
|
protected POIDocument(DirectoryNode dir, POIFSFileSystem fs) {
|
|
|
|
this.directory = dir;
|
2011-03-25 12:06:11 -04:00
|
|
|
}
|
2014-07-27 14:32:24 -04:00
|
|
|
|
2008-04-10 12:59:10 -04:00
|
|
|
protected POIDocument(POIFSFileSystem fs) {
|
2011-03-25 12:06:11 -04:00
|
|
|
this(fs.getRoot());
|
|
|
|
}
|
2014-07-27 14:32:24 -04:00
|
|
|
|
2011-03-25 12:06:11 -04:00
|
|
|
protected POIDocument(NPOIFSFileSystem fs) {
|
|
|
|
this(fs.getRoot());
|
2008-04-10 12:59:10 -04:00
|
|
|
}
|
2008-03-03 10:10:46 -05:00
|
|
|
|
2013-06-25 19:34:22 -04:00
|
|
|
/**
|
|
|
|
* Fetch the Document Summary Information of the document
|
2014-07-27 14:32:24 -04:00
|
|
|
*
|
|
|
|
* @return The Document Summary Information or null
|
|
|
|
* if it could not be read for this document.
|
2013-06-25 19:34:22 -04:00
|
|
|
*/
|
|
|
|
public DocumentSummaryInformation getDocumentSummaryInformation() {
|
2008-03-03 10:10:46 -05:00
|
|
|
if(!initialized) readProperties();
|
|
|
|
return dsInf;
|
|
|
|
}
|
2007-09-19 07:56:36 -04:00
|
|
|
|
2013-06-25 19:34:22 -04:00
|
|
|
/**
|
|
|
|
* Fetch the Summary Information of the document
|
2014-07-27 14:32:24 -04:00
|
|
|
*
|
|
|
|
* @return The Summary information for the document or null
|
|
|
|
* if it could not be read for this document.
|
2013-06-25 19:34:22 -04:00
|
|
|
*/
|
|
|
|
public SummaryInformation getSummaryInformation() {
|
2008-03-03 10:10:46 -05:00
|
|
|
if(!initialized) readProperties();
|
|
|
|
return sInf;
|
|
|
|
}
|
2009-11-25 07:37:48 -05:00
|
|
|
|
2013-06-25 19:34:22 -04:00
|
|
|
/**
|
|
|
|
* Will create whichever of SummaryInformation
|
|
|
|
* and DocumentSummaryInformation (HPSF) properties
|
|
|
|
* are not already part of your document.
|
|
|
|
* This is normally useful when creating a new
|
|
|
|
* document from scratch.
|
|
|
|
* If the information properties are already there,
|
|
|
|
* then nothing will happen.
|
|
|
|
*/
|
|
|
|
public void createInformationProperties() {
|
|
|
|
if (!initialized) readProperties();
|
|
|
|
if (sInf == null) {
|
|
|
|
sInf = PropertySetFactory.newSummaryInformation();
|
|
|
|
}
|
|
|
|
if (dsInf == null) {
|
|
|
|
dsInf = PropertySetFactory.newDocumentSummaryInformation();
|
|
|
|
}
|
|
|
|
}
|
2007-09-19 07:56:36 -04:00
|
|
|
|
2013-06-25 19:34:22 -04:00
|
|
|
/**
|
|
|
|
* Find, and create objects for, the standard
|
2014-07-27 14:32:24 -04:00
|
|
|
* Document Information Properties (HPSF).
|
2013-06-25 19:34:22 -04:00
|
|
|
* If a given property set is missing or corrupt,
|
|
|
|
* it will remain null;
|
|
|
|
*/
|
|
|
|
protected void readProperties() {
|
|
|
|
PropertySet ps;
|
|
|
|
|
|
|
|
// DocumentSummaryInformation
|
|
|
|
ps = getPropertySet(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
|
|
|
|
if (ps != null && ps instanceof DocumentSummaryInformation) {
|
|
|
|
dsInf = (DocumentSummaryInformation)ps;
|
|
|
|
} else if(ps != null) {
|
|
|
|
logger.log(POILogger.WARN, "DocumentSummaryInformation property set came back with wrong class - ", ps.getClass());
|
|
|
|
}
|
2007-09-19 07:56:36 -04:00
|
|
|
|
2013-06-25 19:34:22 -04:00
|
|
|
// SummaryInformation
|
|
|
|
ps = getPropertySet(SummaryInformation.DEFAULT_STREAM_NAME);
|
|
|
|
if (ps instanceof SummaryInformation) {
|
|
|
|
sInf = (SummaryInformation)ps;
|
|
|
|
} else if(ps != null) {
|
|
|
|
logger.log(POILogger.WARN, "SummaryInformation property set came back with wrong class - ", ps.getClass());
|
|
|
|
}
|
2008-03-03 10:10:46 -05:00
|
|
|
|
2013-06-25 19:34:22 -04:00
|
|
|
// Mark the fact that we've now loaded up the properties
|
2008-03-03 10:10:46 -05:00
|
|
|
initialized = true;
|
2013-06-25 19:34:22 -04:00
|
|
|
}
|
2007-09-19 07:56:36 -04:00
|
|
|
|
2013-06-25 19:34:22 -04:00
|
|
|
/**
|
|
|
|
* For a given named property entry, either return it or null if
|
|
|
|
* if it wasn't found
|
2014-07-27 14:32:24 -04:00
|
|
|
*
|
|
|
|
* @param setName The property to read
|
|
|
|
* @return The value of the given property or null if it wasn't found.
|
2013-06-25 19:34:22 -04:00
|
|
|
*/
|
|
|
|
protected PropertySet getPropertySet(String setName) {
|
2014-12-24 20:56:29 -05:00
|
|
|
return getPropertySet(setName, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For a given named property entry, either return it or null if
|
|
|
|
* if it wasn't found
|
|
|
|
*
|
|
|
|
* @param setName The property to read
|
|
|
|
* @param encryptionInfo the encryption descriptor in case of cryptoAPI encryption
|
|
|
|
* @return The value of the given property or null if it wasn't found.
|
|
|
|
*/
|
|
|
|
protected PropertySet getPropertySet(String setName, EncryptionInfo encryptionInfo) {
|
|
|
|
DirectoryNode dirNode = directory;
|
|
|
|
|
|
|
|
if (encryptionInfo != null) {
|
|
|
|
try {
|
|
|
|
InputStream is = encryptionInfo.getDecryptor().getDataStream(directory);
|
|
|
|
POIFSFileSystem poifs = new POIFSFileSystem(is);
|
|
|
|
is.close();
|
|
|
|
dirNode = poifs.getRoot();
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.log(POILogger.ERROR, "Error getting encrypted property set with name " + setName, e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-25 19:34:22 -04:00
|
|
|
//directory can be null when creating new documents
|
2014-12-24 20:56:29 -05:00
|
|
|
if (dirNode == null || !dirNode.hasEntry(setName))
|
2013-06-25 19:34:22 -04:00
|
|
|
return null;
|
2007-09-19 07:56:36 -04:00
|
|
|
|
2013-06-25 19:34:22 -04:00
|
|
|
DocumentInputStream dis;
|
|
|
|
try {
|
|
|
|
// Find the entry, and get an input stream for it
|
2014-12-24 20:56:29 -05:00
|
|
|
dis = dirNode.createDocumentInputStream( dirNode.getEntry(setName) );
|
2013-06-25 19:34:22 -04:00
|
|
|
} catch(IOException ie) {
|
|
|
|
// Oh well, doesn't exist
|
|
|
|
logger.log(POILogger.WARN, "Error getting property set with name " + setName + "\n" + ie);
|
|
|
|
return null;
|
|
|
|
}
|
2010-12-28 22:30:07 -05:00
|
|
|
|
2013-06-25 19:34:22 -04:00
|
|
|
try {
|
|
|
|
// Create the Property Set
|
|
|
|
PropertySet set = PropertySetFactory.create(dis);
|
|
|
|
return set;
|
|
|
|
} catch(IOException ie) {
|
|
|
|
// Must be corrupt or something like that
|
|
|
|
logger.log(POILogger.WARN, "Error creating property set with name " + setName + "\n" + ie);
|
|
|
|
} catch(org.apache.poi.hpsf.HPSFException he) {
|
|
|
|
// Oh well, doesn't exist
|
|
|
|
logger.log(POILogger.WARN, "Error creating property set with name " + setName + "\n" + he);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes out the standard Documment Information Properties (HPSF)
|
|
|
|
* @param outFS the POIFSFileSystem to write the properties into
|
2014-07-27 14:32:24 -04:00
|
|
|
*
|
|
|
|
* @throws IOException if an error when writing to the
|
|
|
|
* {@link POIFSFileSystem} occurs
|
2013-06-25 19:34:22 -04:00
|
|
|
*/
|
|
|
|
protected void writeProperties(POIFSFileSystem outFS) throws IOException {
|
|
|
|
writeProperties(outFS, null);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Writes out the standard Documment Information Properties (HPSF)
|
|
|
|
* @param outFS the POIFSFileSystem to write the properties into
|
|
|
|
* @param writtenEntries a list of POIFS entries to add the property names too
|
2014-07-27 14:32:24 -04:00
|
|
|
*
|
|
|
|
* @throws IOException if an error when writing to the
|
|
|
|
* {@link POIFSFileSystem} occurs
|
2013-06-25 19:34:22 -04:00
|
|
|
*/
|
|
|
|
protected void writeProperties(POIFSFileSystem outFS, List<String> writtenEntries) throws IOException {
|
2009-08-20 09:49:33 -04:00
|
|
|
SummaryInformation si = getSummaryInformation();
|
2013-06-25 19:34:22 -04:00
|
|
|
if (si != null) {
|
|
|
|
writePropertySet(SummaryInformation.DEFAULT_STREAM_NAME, si, outFS);
|
|
|
|
if(writtenEntries != null) {
|
|
|
|
writtenEntries.add(SummaryInformation.DEFAULT_STREAM_NAME);
|
|
|
|
}
|
|
|
|
}
|
2009-08-20 09:49:33 -04:00
|
|
|
DocumentSummaryInformation dsi = getDocumentSummaryInformation();
|
2013-06-25 19:34:22 -04:00
|
|
|
if (dsi != null) {
|
|
|
|
writePropertySet(DocumentSummaryInformation.DEFAULT_STREAM_NAME, dsi, outFS);
|
|
|
|
if(writtenEntries != null) {
|
|
|
|
writtenEntries.add(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-19 07:56:36 -04:00
|
|
|
|
2013-06-25 19:34:22 -04:00
|
|
|
/**
|
|
|
|
* Writes out a given ProperySet
|
|
|
|
* @param name the (POIFS Level) name of the property to write
|
|
|
|
* @param set the PropertySet to write out
|
|
|
|
* @param outFS the POIFSFileSystem to write the property into
|
2014-07-27 14:32:24 -04:00
|
|
|
*
|
|
|
|
* @throws IOException if an error when writing to the
|
|
|
|
* {@link POIFSFileSystem} occurs
|
2013-06-25 19:34:22 -04:00
|
|
|
*/
|
|
|
|
protected void writePropertySet(String name, PropertySet set, POIFSFileSystem outFS) throws IOException {
|
|
|
|
try {
|
|
|
|
MutablePropertySet mSet = new MutablePropertySet(set);
|
|
|
|
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
2007-09-19 07:56:36 -04:00
|
|
|
|
2013-06-25 19:34:22 -04:00
|
|
|
mSet.write(bOut);
|
|
|
|
byte[] data = bOut.toByteArray();
|
|
|
|
ByteArrayInputStream bIn = new ByteArrayInputStream(data);
|
|
|
|
outFS.createDocument(bIn,name);
|
2007-09-19 07:56:36 -04:00
|
|
|
|
2013-06-25 19:34:22 -04:00
|
|
|
logger.log(POILogger.INFO, "Wrote property set " + name + " of size " + data.length);
|
|
|
|
} catch(org.apache.poi.hpsf.WritingNotSupportedException wnse) {
|
|
|
|
logger.log( POILogger.ERROR, "Couldn't write property set with name " + name + " as not supported by HPSF yet");
|
|
|
|
}
|
|
|
|
}
|
2007-09-19 07:56:36 -04:00
|
|
|
|
2013-06-25 19:34:22 -04:00
|
|
|
/**
|
2014-07-27 14:32:24 -04:00
|
|
|
* Writes the document out to the specified output stream. The
|
|
|
|
* stream is not closed as part of this operation.
|
|
|
|
*
|
|
|
|
* @param out The stream to write to.
|
|
|
|
*
|
|
|
|
* @throws IOException thrown on errors writing to the stream
|
2013-06-25 19:34:22 -04:00
|
|
|
*/
|
|
|
|
public abstract void write(OutputStream out) throws IOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies nodes from one POIFS to the other minus the excepts
|
|
|
|
* @param source is the source POIFS to copy from
|
|
|
|
* @param target is the target POIFS to copy to
|
|
|
|
* @param excepts is a list of Strings specifying what nodes NOT to copy
|
2014-07-27 14:32:24 -04:00
|
|
|
*
|
|
|
|
* @throws IOException thrown on errors writing to the target file system.
|
|
|
|
*
|
2013-06-25 20:56:28 -04:00
|
|
|
* @deprecated Use {@link EntryUtils#copyNodes(DirectoryEntry, DirectoryEntry, List)} instead
|
2013-06-25 19:34:22 -04:00
|
|
|
*/
|
|
|
|
@Deprecated
|
2011-08-22 04:50:48 -04:00
|
|
|
protected void copyNodes( POIFSFileSystem source, POIFSFileSystem target,
|
2013-06-25 19:34:22 -04:00
|
|
|
List<String> excepts ) throws IOException {
|
2011-08-23 12:44:06 -04:00
|
|
|
EntryUtils.copyNodes( source, target, excepts );
|
2011-08-22 04:50:48 -04:00
|
|
|
}
|
|
|
|
|
2011-07-15 11:02:19 -04:00
|
|
|
/**
|
|
|
|
* Copies nodes from one POIFS to the other minus the excepts
|
2011-12-09 11:48:38 -05:00
|
|
|
* @param sourceRoot is the source POIFS to copy from
|
|
|
|
* @param targetRoot is the target POIFS to copy to
|
2011-07-15 11:02:19 -04:00
|
|
|
* @param excepts is a list of Strings specifying what nodes NOT to copy
|
2014-07-27 14:32:24 -04:00
|
|
|
*
|
|
|
|
* @throws IOException thrown on errors writing to the target directory node.
|
|
|
|
*
|
2013-06-25 20:56:28 -04:00
|
|
|
* @deprecated Use {@link EntryUtils#copyNodes(DirectoryEntry, DirectoryEntry, List)} instead
|
2011-07-15 11:02:19 -04:00
|
|
|
*/
|
2011-08-22 04:50:48 -04:00
|
|
|
@Deprecated
|
|
|
|
protected void copyNodes( DirectoryNode sourceRoot,
|
|
|
|
DirectoryNode targetRoot, List<String> excepts ) throws IOException
|
|
|
|
{
|
2011-08-23 12:44:06 -04:00
|
|
|
EntryUtils.copyNodes( sourceRoot, targetRoot, excepts );
|
2011-08-22 04:50:48 -04:00
|
|
|
}
|
|
|
|
|
2013-06-25 19:34:22 -04:00
|
|
|
/**
|
|
|
|
* Copies an Entry into a target POIFS directory, recursively
|
2014-07-27 14:32:24 -04:00
|
|
|
*
|
|
|
|
* @param entry the entry to copy from
|
|
|
|
* @param target the entry to write to
|
|
|
|
*
|
|
|
|
* @throws IOException thrown on errors writing to the target directory entry.
|
|
|
|
*
|
2013-06-25 19:34:22 -04:00
|
|
|
* @deprecated Use {@link EntryUtils#copyNodeRecursively(Entry, DirectoryEntry)} instead
|
|
|
|
*/
|
2011-08-17 10:53:28 -04:00
|
|
|
@Internal
|
2011-08-22 04:50:48 -04:00
|
|
|
@Deprecated
|
|
|
|
protected void copyNodeRecursively( Entry entry, DirectoryEntry target )
|
|
|
|
throws IOException
|
|
|
|
{
|
2011-08-23 12:44:06 -04:00
|
|
|
EntryUtils.copyNodeRecursively( entry, target );
|
2011-08-22 04:50:48 -04:00
|
|
|
}
|
2007-09-19 07:56:36 -04:00
|
|
|
}
|