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;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2008-03-08 12:21:29 -05:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.PushbackInputStream;
|
2008-02-04 11:34:44 -05:00
|
|
|
|
2008-03-08 12:21:29 -05:00
|
|
|
import org.apache.poi.poifs.common.POIFSConstants;
|
|
|
|
import org.apache.poi.util.IOUtils;
|
2008-03-09 10:21:34 -04:00
|
|
|
import org.apache.xmlbeans.XmlException;
|
2008-02-04 11:34:44 -05:00
|
|
|
import org.openxml4j.exceptions.InvalidFormatException;
|
|
|
|
import org.openxml4j.exceptions.OpenXML4JException;
|
|
|
|
import org.openxml4j.opc.Package;
|
|
|
|
import org.openxml4j.opc.PackagePart;
|
2008-02-13 08:43:42 -05:00
|
|
|
import org.openxml4j.opc.PackagePartName;
|
2008-02-04 11:34:44 -05:00
|
|
|
import org.openxml4j.opc.PackageRelationship;
|
2008-03-09 10:21:34 -04:00
|
|
|
import org.openxml4j.opc.PackageRelationshipCollection;
|
2008-02-04 11:34:44 -05:00
|
|
|
import org.openxml4j.opc.PackageRelationshipTypes;
|
2008-02-13 08:43:42 -05:00
|
|
|
import org.openxml4j.opc.PackagingURIHelper;
|
2008-02-04 11:34:44 -05:00
|
|
|
|
|
|
|
public abstract class POIXMLDocument {
|
|
|
|
|
|
|
|
public static final String CORE_PROPERTIES_REL_TYPE = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties";
|
|
|
|
|
|
|
|
public static final String EXTENDED_PROPERTIES_REL_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties";
|
|
|
|
|
|
|
|
/** The OPC Package */
|
|
|
|
private Package pkg;
|
|
|
|
|
|
|
|
/** The OPC core Package Part */
|
|
|
|
private PackagePart corePart;
|
|
|
|
|
2008-04-09 08:22:23 -04:00
|
|
|
/**
|
|
|
|
* The properties of the OPC package, opened as needed
|
|
|
|
*/
|
|
|
|
private POIXMLProperties properties;
|
|
|
|
|
|
|
|
|
2008-02-04 11:34:44 -05:00
|
|
|
protected POIXMLDocument() {}
|
|
|
|
|
2008-03-08 12:39:56 -05:00
|
|
|
protected POIXMLDocument(Package pkg) throws IOException {
|
|
|
|
try {
|
|
|
|
this.pkg = pkg;
|
|
|
|
|
|
|
|
PackageRelationship coreDocRelationship = this.pkg.getRelationshipsByType(
|
|
|
|
PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0);
|
|
|
|
|
|
|
|
// Get core part
|
|
|
|
this.corePart = this.pkg.getPart(coreDocRelationship);
|
|
|
|
} catch (OpenXML4JException e) {
|
|
|
|
throw new IOException(e.toString());
|
|
|
|
}
|
|
|
|
}
|
2008-02-04 11:34:44 -05:00
|
|
|
protected POIXMLDocument(String path) throws IOException {
|
2008-03-08 12:39:56 -05:00
|
|
|
this(openPackage(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper to open a package, returning an IOException
|
|
|
|
* in the event of a problem.
|
|
|
|
* Works around shortcomings in java's this() constructor calls
|
|
|
|
*/
|
2008-03-09 10:21:34 -04:00
|
|
|
public static Package openPackage(String path) throws IOException {
|
2008-02-04 11:34:44 -05:00
|
|
|
try {
|
2008-03-08 12:39:56 -05:00
|
|
|
return Package.open(path);
|
2008-02-04 11:34:44 -05:00
|
|
|
} catch (InvalidFormatException e) {
|
|
|
|
throw new IOException(e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Package getPackage() {
|
|
|
|
return this.pkg;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected PackagePart getCorePart() {
|
|
|
|
return this.corePart;
|
|
|
|
}
|
2008-02-13 08:43:42 -05:00
|
|
|
|
2008-02-19 12:34:39 -05:00
|
|
|
/**
|
|
|
|
* Get the PackagePart that is the target of a relationship.
|
|
|
|
*
|
|
|
|
* @param rel The relationship
|
|
|
|
* @return The target part
|
|
|
|
* @throws InvalidFormatException
|
|
|
|
*/
|
|
|
|
protected PackagePart getTargetPart(PackageRelationship rel) throws InvalidFormatException {
|
2008-02-13 08:43:42 -05:00
|
|
|
PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
|
|
|
|
PackagePart part = getPackage().getPart(relName);
|
|
|
|
if (part == null) {
|
|
|
|
throw new IllegalArgumentException("No part found for relationship " + rel);
|
|
|
|
}
|
|
|
|
return part;
|
|
|
|
}
|
2008-03-09 10:21:34 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the (single) PackagePart which is defined as
|
|
|
|
* the supplied relation content type of the base
|
2008-03-09 11:05:22 -04:00
|
|
|
* package/container, or null if none found.
|
2008-03-09 10:21:34 -04:00
|
|
|
* @param relationType The relation content type to search for
|
|
|
|
* @throws IllegalArgumentException If we find more than one part of that type
|
|
|
|
*/
|
|
|
|
protected PackagePart getSinglePartByRelationType(String relationType) throws IllegalArgumentException, OpenXML4JException {
|
|
|
|
PackageRelationshipCollection rels =
|
2008-03-09 11:05:22 -04:00
|
|
|
pkg.getRelationshipsByType(relationType);
|
2008-03-09 10:21:34 -04:00
|
|
|
if(rels.size() == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if(rels.size() > 1) {
|
|
|
|
throw new IllegalArgumentException("Found " + rels.size() + " relations for the type " + relationType + ", should only ever be one!");
|
|
|
|
}
|
|
|
|
PackageRelationship rel = rels.getRelationship(0);
|
|
|
|
return getTargetPart(rel);
|
|
|
|
}
|
2008-03-09 10:39: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 =
|
|
|
|
getCorePart().getRelationshipsByType(contentType);
|
|
|
|
|
|
|
|
PackagePart[] parts = new PackagePart[partsC.size()];
|
|
|
|
int count = 0;
|
|
|
|
for (PackageRelationship rel : partsC) {
|
|
|
|
parts[count] = getTargetPart(rel);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
2008-03-09 10:21:34 -04:00
|
|
|
|
2008-03-08 12:13:30 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks that the supplied InputStream (which MUST
|
|
|
|
* support mark and reset, or be a PushbackInputStream)
|
|
|
|
* 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!
|
|
|
|
* @param inp An InputStream which supports either mark/reset, or is a PushbackInputStream
|
|
|
|
*/
|
|
|
|
public static boolean hasOOXMLHeader(InputStream inp) throws IOException {
|
|
|
|
// We want to peek at the first 4 bytes
|
|
|
|
inp.mark(4);
|
|
|
|
|
|
|
|
byte[] header = new byte[4];
|
|
|
|
IOUtils.readFully(inp, header);
|
|
|
|
|
|
|
|
// Wind back those 4 bytes
|
|
|
|
if(inp instanceof PushbackInputStream) {
|
|
|
|
PushbackInputStream pin = (PushbackInputStream)inp;
|
|
|
|
pin.unread(header);
|
|
|
|
} else {
|
|
|
|
inp.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Did it match the ooxml zip signature?
|
|
|
|
return (
|
|
|
|
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-03-09 10:21:34 -04:00
|
|
|
|
|
|
|
/**
|
2008-04-09 08:22:23 -04:00
|
|
|
* Get the document properties. This gives you access to the
|
|
|
|
* core ooxml properties, and the extended ooxml properties.
|
2008-03-09 10:21:34 -04:00
|
|
|
*/
|
2008-04-09 08:22:23 -04:00
|
|
|
public POIXMLProperties getProperties() throws OpenXML4JException, IOException, XmlException {
|
|
|
|
if(properties == null) {
|
|
|
|
properties = new POIXMLProperties(pkg);
|
2008-03-09 10:21:34 -04:00
|
|
|
}
|
2008-04-09 08:22:23 -04:00
|
|
|
return properties;
|
2008-03-09 10:21:34 -04:00
|
|
|
}
|
2008-02-04 11:34:44 -05:00
|
|
|
}
|