2008-02-04 11:34:44 -05: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;
|
|
|
|
|
2015-05-26 15:30:04 -04:00
|
|
|
import java.io.Closeable;
|
2015-12-16 13:15:31 -05:00
|
|
|
import java.io.File;
|
2010-06-29 07:07:27 -04:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.PushbackInputStream;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2008-02-04 11:34:44 -05:00
|
|
|
|
2009-01-29 07:44:31 -05:00
|
|
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
|
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
|
2010-06-29 07:07:27 -04:00
|
|
|
import org.apache.poi.openxml4j.opc.OPCPackage;
|
2014-06-11 09:48:54 -04:00
|
|
|
import org.apache.poi.openxml4j.opc.PackageAccess;
|
2010-06-29 07:07:27 -04:00
|
|
|
import org.apache.poi.openxml4j.opc.PackagePart;
|
|
|
|
import org.apache.poi.openxml4j.opc.PackageRelationship;
|
|
|
|
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
|
|
|
|
import org.apache.poi.poifs.common.POIFSConstants;
|
|
|
|
import org.apache.poi.util.IOUtils;
|
2014-08-04 16:42:41 -04:00
|
|
|
import org.apache.xmlbeans.impl.common.SystemCache;
|
2008-02-04 11:34:44 -05:00
|
|
|
|
2015-05-26 15:30:04 -04:00
|
|
|
public abstract class POIXMLDocument extends POIXMLDocumentPart implements Closeable {
|
2009-08-09 09:08:59 -04:00
|
|
|
public static final String DOCUMENT_CREATOR = "Apache POI";
|
2008-02-04 11:34:44 -05:00
|
|
|
|
2008-05-28 09:32:03 -04:00
|
|
|
// OLE embeddings relation name
|
2008-05-23 11:05:12 -04:00
|
|
|
public static final String OLE_OBJECT_REL_TYPE="http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject";
|
2008-09-30 09:57:36 -04:00
|
|
|
|
2008-05-28 09:32:03 -04:00
|
|
|
// Embedded OPC documents relation name
|
|
|
|
public static final String PACK_OBJECT_REL_TYPE="http://schemas.openxmlformats.org/officeDocument/2006/relationships/package";
|
2008-09-30 09:57:36 -04:00
|
|
|
|
2008-02-04 11:34:44 -05:00
|
|
|
/** The OPC Package */
|
2009-03-18 14:54:01 -04:00
|
|
|
private OPCPackage pkg;
|
2008-02-04 11:34:44 -05:00
|
|
|
|
2008-04-09 08:22:23 -04:00
|
|
|
/**
|
|
|
|
* The properties of the OPC package, opened as needed
|
|
|
|
*/
|
|
|
|
private POIXMLProperties properties;
|
2008-09-30 09:57:36 -04:00
|
|
|
|
2009-03-18 14:54:01 -04:00
|
|
|
protected POIXMLDocument(OPCPackage pkg) {
|
2008-10-25 07:48:50 -04:00
|
|
|
super(pkg);
|
2015-07-19 08:11:19 -04:00
|
|
|
init(pkg);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected POIXMLDocument(OPCPackage pkg, String coreDocumentRel) {
|
|
|
|
super(pkg, coreDocumentRel);
|
|
|
|
init(pkg);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void init(OPCPackage pkg) {
|
2008-10-25 07:48:50 -04:00
|
|
|
this.pkg = pkg;
|
2014-08-04 16:42:41 -04:00
|
|
|
|
|
|
|
// Workaround for XMLBEANS-512 - ensure that when we parse
|
|
|
|
// the file, we start with a fresh XML Parser each time,
|
|
|
|
// and avoid the risk of getting a SaxHandler that's in error
|
|
|
|
SystemCache.get().setSaxLoader(null);
|
2008-03-08 12:39:56 -05:00
|
|
|
}
|
2008-09-30 09:57:36 -04:00
|
|
|
|
2008-03-08 12:39:56 -05:00
|
|
|
/**
|
|
|
|
* Wrapper to open a package, returning an IOException
|
|
|
|
* in the event of a problem.
|
|
|
|
* Works around shortcomings in java's this() constructor calls
|
|
|
|
*/
|
2009-03-18 14:54:01 -04:00
|
|
|
public static OPCPackage openPackage(String path) throws IOException {
|
2008-02-04 11:34:44 -05:00
|
|
|
try {
|
2009-03-18 14:54:01 -04:00
|
|
|
return OPCPackage.open(path);
|
2008-02-04 11:34:44 -05:00
|
|
|
} catch (InvalidFormatException e) {
|
|
|
|
throw new IOException(e.toString());
|
|
|
|
}
|
|
|
|
}
|
2008-09-30 09:57:36 -04:00
|
|
|
|
2009-03-18 14:54:01 -04:00
|
|
|
public OPCPackage getPackage() {
|
2008-02-04 11:34:44 -05:00
|
|
|
return this.pkg;
|
|
|
|
}
|
2008-09-30 09:57:36 -04:00
|
|
|
|
2008-02-04 11:34:44 -05:00
|
|
|
protected PackagePart getCorePart() {
|
2008-10-25 07:48:50 -04:00
|
|
|
return getPackagePart();
|
2008-02-04 11:34:44 -05:00
|
|
|
}
|
2008-02-13 08:43:42 -05:00
|
|
|
|
2008-09-30 09:57:36 -04:00
|
|
|
/**
|
|
|
|
* Retrieves all the PackageParts which are defined as
|
|
|
|
* relationships of the base document with the
|
|
|
|
* specified content type.
|
|
|
|
*/
|
|
|
|
protected PackagePart[] getRelatedByType(String contentType) throws InvalidFormatException {
|
|
|
|
PackageRelationshipCollection partsC =
|
2008-10-19 11:31:28 -04:00
|
|
|
getPackagePart().getRelationshipsByType(contentType);
|
2008-09-30 09:57:36 -04:00
|
|
|
|
|
|
|
PackagePart[] parts = new PackagePart[partsC.size()];
|
|
|
|
int count = 0;
|
|
|
|
for (PackageRelationship rel : partsC) {
|
2011-09-19 17:40:39 -04:00
|
|
|
parts[count] = getPackagePart().getRelatedPart(rel);
|
2008-09-30 09:57:36 -04:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2008-03-08 12:13:30 -05:00
|
|
|
/**
|
|
|
|
* Checks that the supplied InputStream (which MUST
|
2008-09-30 09:57:36 -04:00
|
|
|
* support mark and reset, or be a PushbackInputStream)
|
2008-03-08 12:13:30 -05:00
|
|
|
* has a OOXML (zip) header at the start of it.
|
|
|
|
* If your InputStream does not support mark / reset,
|
|
|
|
* then wrap it in a PushBackInputStream, then be
|
|
|
|
* sure to always use that, and not the original!
|
2008-09-30 09:57:36 -04:00
|
|
|
* @param inp An InputStream which supports either mark/reset, or is a PushbackInputStream
|
2008-03-08 12:13:30 -05:00
|
|
|
*/
|
|
|
|
public static boolean hasOOXMLHeader(InputStream inp) throws IOException {
|
2008-09-30 09:57:36 -04:00
|
|
|
// We want to peek at the first 4 bytes
|
|
|
|
inp.mark(4);
|
2008-03-08 12:13:30 -05:00
|
|
|
|
2008-09-30 09:57:36 -04:00
|
|
|
byte[] header = new byte[4];
|
2015-07-20 10:03:35 -04:00
|
|
|
int bytesRead = IOUtils.readFully(inp, header);
|
2008-03-08 12:13:30 -05:00
|
|
|
|
|
|
|
// Wind back those 4 bytes
|
|
|
|
if(inp instanceof PushbackInputStream) {
|
2008-09-30 09:57:36 -04:00
|
|
|
PushbackInputStream pin = (PushbackInputStream)inp;
|
2015-07-20 10:03:35 -04:00
|
|
|
pin.unread(header, 0, bytesRead);
|
2008-03-08 12:13:30 -05:00
|
|
|
} else {
|
2008-09-30 09:57:36 -04:00
|
|
|
inp.reset();
|
2008-03-08 12:13:30 -05:00
|
|
|
}
|
2008-09-30 09:57:36 -04:00
|
|
|
|
|
|
|
// Did it match the ooxml zip signature?
|
2008-03-08 12:13:30 -05:00
|
|
|
return (
|
2015-07-20 10:03:35 -04:00
|
|
|
bytesRead == 4 &&
|
2011-06-01 08:07:16 -04:00
|
|
|
header[0] == POIFSConstants.OOXML_FILE_HEADER[0] &&
|
|
|
|
header[1] == POIFSConstants.OOXML_FILE_HEADER[1] &&
|
|
|
|
header[2] == POIFSConstants.OOXML_FILE_HEADER[2] &&
|
|
|
|
header[3] == POIFSConstants.OOXML_FILE_HEADER[3]
|
2008-09-30 09:57:36 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the document properties. This gives you access to the
|
|
|
|
* core ooxml properties, and the extended ooxml properties.
|
|
|
|
*/
|
2009-07-19 14:26:36 -04:00
|
|
|
public POIXMLProperties getProperties() {
|
2008-09-30 09:57:36 -04:00
|
|
|
if(properties == null) {
|
2009-07-19 14:26:36 -04:00
|
|
|
try {
|
|
|
|
properties = new POIXMLProperties(pkg);
|
|
|
|
} catch (Exception e){
|
|
|
|
throw new POIXMLException(e);
|
|
|
|
}
|
2008-09-30 09:57:36 -04:00
|
|
|
}
|
|
|
|
return properties;
|
2008-03-08 12:13:30 -05:00
|
|
|
}
|
2008-03-09 10:21:34 -04:00
|
|
|
|
2008-05-23 11:05:12 -04:00
|
|
|
/**
|
|
|
|
* Get the document's embedded files.
|
|
|
|
*/
|
2008-10-19 11:31:28 -04:00
|
|
|
public abstract List<PackagePart> getAllEmbedds() throws OpenXML4JException;
|
|
|
|
|
2009-08-12 14:59:34 -04:00
|
|
|
protected final void load(POIXMLFactory factory) throws IOException {
|
2011-06-01 08:07:16 -04:00
|
|
|
Map<PackagePart, POIXMLDocumentPart> context = new HashMap<PackagePart, POIXMLDocumentPart>();
|
2009-08-12 14:59:34 -04:00
|
|
|
try {
|
|
|
|
read(factory, context);
|
|
|
|
} catch (OpenXML4JException e){
|
|
|
|
throw new POIXMLException(e);
|
|
|
|
}
|
2011-06-01 08:07:16 -04:00
|
|
|
onDocumentRead();
|
|
|
|
context.clear();
|
2009-08-12 14:59:34 -04:00
|
|
|
}
|
2014-06-11 09:48:54 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the underlying {@link OPCPackage} from which this
|
|
|
|
* document was read, if there is one
|
|
|
|
*/
|
2015-05-26 15:30:04 -04:00
|
|
|
public void close() throws IOException {
|
2014-06-11 09:48:54 -04:00
|
|
|
if (pkg != null) {
|
|
|
|
if (pkg.getPackageAccess() == PackageAccess.READ) {
|
|
|
|
pkg.revert();
|
|
|
|
} else {
|
|
|
|
pkg.close();
|
|
|
|
}
|
|
|
|
pkg = null;
|
|
|
|
}
|
|
|
|
}
|
2009-08-12 14:59:34 -04:00
|
|
|
|
2008-11-09 11:18:46 -05:00
|
|
|
/**
|
|
|
|
* Write out this document to an Outputstream.
|
|
|
|
*
|
2015-12-16 13:15:31 -05:00
|
|
|
* Note - if the Document was opened from a {@link File} rather
|
|
|
|
* than an {@link InputStream}, you <b>must</b> write out to
|
|
|
|
* a different file, overwriting via an OutputStream isn't possible.
|
|
|
|
*
|
2009-08-12 14:59:34 -04:00
|
|
|
* @param stream - the java OutputStream you wish to write the file to
|
2008-11-09 11:18:46 -05:00
|
|
|
*
|
|
|
|
* @exception IOException if anything can't be written.
|
|
|
|
*/
|
|
|
|
public final void write(OutputStream stream) throws IOException {
|
|
|
|
//force all children to commit their changes into the underlying OOXML Package
|
2009-08-14 13:25:22 -04:00
|
|
|
Set<PackagePart> context = new HashSet<PackagePart>();
|
2009-08-12 14:59:34 -04:00
|
|
|
onSave(context);
|
|
|
|
context.clear();
|
2008-11-09 11:18:46 -05:00
|
|
|
|
2009-07-19 14:26:36 -04:00
|
|
|
//save extended and custom properties
|
|
|
|
getProperties().commit();
|
|
|
|
|
2008-11-09 11:18:46 -05:00
|
|
|
getPackage().save(stream);
|
|
|
|
}
|
2008-02-04 11:34:44 -05:00
|
|
|
}
|