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;
|
|
|
|
|
2016-06-12 15:54:35 -04:00
|
|
|
import java.io.Closeable;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2009-01-29 07:44:31 -05:00
|
|
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
|
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
|
2016-06-12 15:54:35 -04:00
|
|
|
import org.apache.poi.openxml4j.opc.OPCPackage;
|
|
|
|
import org.apache.poi.openxml4j.opc.PackageAccess;
|
|
|
|
import org.apache.poi.openxml4j.opc.PackagePart;
|
|
|
|
import org.apache.poi.openxml4j.opc.PackageRelationship;
|
|
|
|
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
|
2016-03-12 06:37:32 -05:00
|
|
|
import org.apache.poi.poifs.filesystem.DocumentFactoryHelper;
|
2014-08-04 16:42:41 -04:00
|
|
|
import org.apache.xmlbeans.impl.common.SystemCache;
|
2008-02-04 11:34:44 -05:00
|
|
|
|
2016-06-12 15:54:35 -04:00
|
|
|
/**
|
|
|
|
* This holds the common functionality for all POI OOXML Document classes.
|
|
|
|
*/
|
2016-09-10 15:33:32 -04:00
|
|
|
// TODO: implements AutoCloseable in Java 7+ when POI drops support for Java 6.
|
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);
|
|
|
|
}
|
|
|
|
|
2016-06-12 15:54:35 -04:00
|
|
|
private void init(OPCPackage p) {
|
|
|
|
this.pkg = p;
|
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
|
|
|
/**
|
2016-06-12 15:54:35 -04:00
|
|
|
* Wrapper to open a package, which works around shortcomings in java's this() constructor calls
|
|
|
|
*
|
|
|
|
* @param path the path to the document
|
|
|
|
* @return the new OPCPackage
|
|
|
|
*
|
|
|
|
* @exception IOException if there was a problem opening the document
|
2008-03-08 12:39:56 -05:00
|
|
|
*/
|
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) {
|
2016-06-12 15:54:35 -04:00
|
|
|
throw new IOException(e.toString(), e);
|
2008-02-04 11:34:44 -05:00
|
|
|
}
|
|
|
|
}
|
2008-09-30 09:57:36 -04:00
|
|
|
|
2016-06-12 15:54:35 -04:00
|
|
|
/**
|
|
|
|
* Get the assigned OPCPackage
|
|
|
|
*
|
|
|
|
* @return the assigned OPCPackage
|
|
|
|
*/
|
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
|
|
|
/**
|
2016-06-12 15:54:35 -04:00
|
|
|
* Retrieves all the PackageParts which are defined as relationships of the base document with the
|
|
|
|
* specified content type.
|
|
|
|
*
|
|
|
|
* @param contentType the content type
|
|
|
|
*
|
|
|
|
* @return all the base document PackageParts which match the content type
|
|
|
|
*
|
|
|
|
* @throws InvalidFormatException when the relationships or the parts contain errors
|
|
|
|
*
|
|
|
|
* @see org.apache.poi.xssf.usermodel.XSSFRelation
|
|
|
|
* @see org.apache.poi.xslf.usermodel.XSLFRelation
|
|
|
|
* @see org.apache.poi.xwpf.usermodel.XWPFRelation
|
|
|
|
* @see org.apache.poi.xdgf.usermodel.XDGFRelation
|
2008-09-30 09:57:36 -04:00
|
|
|
*/
|
|
|
|
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!
|
2016-06-12 15:54:35 -04:00
|
|
|
*
|
2008-09-30 09:57:36 -04:00
|
|
|
* @param inp An InputStream which supports either mark/reset, or is a PushbackInputStream
|
2016-06-12 15:54:35 -04:00
|
|
|
* @return true, if the InputStream is an ooxml document
|
|
|
|
*
|
|
|
|
* @throws IOException if the InputStream can't be read
|
2016-03-12 06:37:32 -05:00
|
|
|
*
|
|
|
|
* @deprecated use the method from DocumentFactoryHelper, deprecated as of 3.15-beta1, therefore eligible for removal in 3.17
|
2008-03-08 12:13:30 -05:00
|
|
|
*/
|
2016-06-12 15:54:35 -04:00
|
|
|
@Deprecated
|
2008-03-08 12:13:30 -05:00
|
|
|
public static boolean hasOOXMLHeader(InputStream inp) throws IOException {
|
2016-03-12 06:37:32 -05:00
|
|
|
return DocumentFactoryHelper.hasOOXMLHeader(inp);
|
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.
|
2016-06-12 15:54:35 -04:00
|
|
|
*
|
|
|
|
* @return the document properties
|
2008-09-30 09:57:36 -04:00
|
|
|
*/
|
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.
|
2016-06-12 15:54:35 -04:00
|
|
|
*
|
|
|
|
* @return the document's embedded files
|
|
|
|
*
|
|
|
|
* @throws OpenXML4JException if the embedded parts can't be determined
|
2008-05-23 11:05:12 -04:00
|
|
|
*/
|
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
|
2016-07-24 07:38:50 -04:00
|
|
|
*
|
|
|
|
* <p>Once this has been called, no further
|
|
|
|
* operations, updates or reads should be performed on the
|
|
|
|
* document.
|
|
|
|
*
|
|
|
|
* @throws IOException for writable packages, if an IO exception occur during the saving process.
|
2014-06-11 09:48:54 -04:00
|
|
|
*/
|
2016-06-12 15:54:35 -04:00
|
|
|
@Override
|
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.
|
2016-07-17 16:25:37 -04:00
|
|
|
*
|
|
|
|
* If {@code stream} is a {@link java.io.FileOutputStream} on a networked drive
|
|
|
|
* or has a high cost/latency associated with each written byte,
|
|
|
|
* consider wrapping the OutputStream in a {@link java.io.BufferedOutputStream}
|
|
|
|
* to improve write performance.
|
2015-12-16 13:15:31 -05:00
|
|
|
*
|
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.
|
|
|
|
*/
|
2016-06-12 15:54:35 -04:00
|
|
|
@SuppressWarnings("resource")
|
2008-11-09 11:18:46 -05:00
|
|
|
public final void write(OutputStream stream) throws IOException {
|
2016-09-10 15:33:32 -04:00
|
|
|
OPCPackage p = getPackage();
|
|
|
|
if(p == null) {
|
|
|
|
throw new IOException("Cannot write data, document seems to have been closed already");
|
|
|
|
}
|
|
|
|
|
2008-11-09 11:18:46 -05:00
|
|
|
//force all children to commit their changes into the underlying OOXML Package
|
2016-06-02 18:59:47 -04:00
|
|
|
// TODO Shouldn't they be committing to the new one instead?
|
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();
|
|
|
|
|
2016-06-12 15:54:35 -04:00
|
|
|
p.save(stream);
|
2008-11-09 11:18:46 -05:00
|
|
|
}
|
2008-02-04 11:34:44 -05:00
|
|
|
}
|