import org.openxml4j.opc.*;
import org.openxml4j.opc.Package;
-public class POIXMLDocument extends POIXMLDocumentPart{
+public abstract class POIXMLDocument extends POIXMLDocumentPart{
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;
-
/**
* The properties of the OPC package, opened as needed
*/
private POIXMLProperties properties;
- /**
- * The embedded OLE2 files in the OPC package
- */
- protected List<PackagePart> embedds;
-
protected POIXMLDocument() {
super(null, null);
- embedds = new LinkedList<PackagePart>();
+ try {
+ Package pkg = newPackage();
+ initialize(pkg);
+ } catch (IOException e){
+ throw new POIXMLException(e);
+ }
}
protected POIXMLDocument(Package pkg) throws IOException {
- this();
+ super(null, null);
initialize(pkg);
}
- protected POIXMLDocument(String path) throws IOException {
- this(openPackage(path));
- }
-
/**
* Wrapper to open a package, returning an IOException
* in the event of a problem.
}
}
- protected void initialize(Package pkg) throws IOException {
+ private void initialize(Package pkg) throws IOException {
try {
this.pkg = pkg;
PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0);
// Get core part
- this.corePart = super.packagePart = this.pkg.getPart(coreDocRelationship);
+ this.packagePart = this.pkg.getPart(coreDocRelationship);
+ this.packageRel = coreDocRelationship;
// Verify it's there
- if(corePart == null) {
+ if(this.packagePart == null) {
throw new IllegalArgumentException("No core part found for this document! Nothing with " + coreDocRelationship.getRelationshipType() + " present as a relation.");
}
} catch (OpenXML4JException e) {
}
}
+ protected Package newPackage() throws IOException {
+ throw new POIXMLException("Must be overridden");
+ }
+
public Package getPackage() {
return this.pkg;
}
protected PackagePart getCorePart() {
- return this.corePart;
+ return this.packagePart;
}
/**
* @return The target part
* @throws InvalidFormatException
*/
- public static PackagePart getTargetPart(Package pkg, PackageRelationship rel) throws InvalidFormatException {
+ protected static PackagePart getTargetPart(Package pkg, PackageRelationship rel) throws InvalidFormatException {
PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
PackagePart part = pkg.getPart(relName);
if (part == null) {
return part;
}
- /**
- * Fetches the (single) PackagePart which is defined as
- * the supplied relation content type of the base
- * package/container, or null if none found.
- * @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 =
- pkg.getRelationshipsByType(relationType);
- 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);
- }
-
/**
* Retrieves all the PackageParts which are defined as
* relationships of the base document with the
*/
protected PackagePart[] getRelatedByType(String contentType) throws InvalidFormatException {
PackageRelationshipCollection partsC =
- getCorePart().getRelationshipsByType(contentType);
+ getPackagePart().getRelationshipsByType(contentType);
PackagePart[] parts = new PackagePart[partsC.size()];
int count = 0;
/**
* Get the document's embedded files.
*/
- public List<PackagePart> getAllEmbedds() throws OpenXML4JException
- {
- return embedds;
- }
+ public abstract List<PackagePart> getAllEmbedds() throws OpenXML4JException;
+
}
/**\r
* Fired when a new package part is created\r
*/\r
- public void onDocumentCreate(){\r
+ protected void onDocumentCreate(){\r
\r
}\r
\r
/**\r
* Fired when a package part is read\r
*/\r
- public void onDocumentRead(){\r
+ protected void onDocumentRead(){\r
\r
}\r
}\r
package org.apache.poi.xslf;
import java.io.IOException;
+import java.util.List;
+import java.util.LinkedList;
import org.apache.poi.POIXMLDocument;
import org.apache.xmlbeans.XmlException;
public static final String COMMENT_RELATION_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments";
private PresentationDocument presentationDoc;
-
+ /**
+ * The embedded OLE2 files in the OPC package
+ */
+ private List<PackagePart> embedds;
+
public XSLFSlideShow(Package container) throws OpenXML4JException, IOException, XmlException {
super(container);
presentationDoc =
PresentationDocument.Factory.parse(getCorePart().getInputStream());
+ embedds = new LinkedList<PackagePart>();
for (CTSlideIdListEntry ctSlide : getSlideReferences().getSldIdArray()) {
PackagePart slidePart =
getTargetPart(getCorePart().getRelationship(ctSlide.getId2()));
throw new IllegalStateException(e);
}
}
+
+ /**
+ * Get the document's embedded files.
+ */
+ public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
+ return embedds;
+ }
+
}
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.openxml4j.exceptions.OpenXML4JException;
+import org.openxml4j.exceptions.InvalidFormatException;
import org.openxml4j.opc.*;
import org.openxml4j.opc.Package;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
private static POILogger log = POILogFactory.getLogger(XSSFWorkbook.class);
+ /**
+ * The embedded OLE2 files in the OPC package
+ */
+ private List<PackagePart> embedds;
+
/**
* Create a new SpreadsheetML workbook.
*/
public XSSFWorkbook() {
super();
- try {
- newWorkbook();
- }catch (Exception e){
- throw new POIXMLException(e);
- }
+ onDocumentCreate();
}
/**
* @param pkg the OpenXML4J <code>Package</code> object.
*/
public XSSFWorkbook(Package pkg) throws IOException {
- super();
+ super(ensureWriteAccess(pkg));
+ onDocumentRead();
+ }
+
+ /**
+ * YK: current implementation of OpenXML4J is funny.
+ * Packages opened by Package.open(InputStream is) are read-only,
+ * there is no way to change or even save such an instance in a OutputStream.
+ * The workaround is to create a copy via a temp file
+ */
+ private static Package ensureWriteAccess(Package pkg) throws IOException {
if(pkg.getPackageAccess() == PackageAccess.READ){
//YK: current implementation of OpenXML4J is funny.
- //Packages opened by Package.open(InputStream is) are read-only,
- //there is no way to change or even save such an instance in a OutputStream.
- //The workaround is to create a copy via a temp file
try {
- Package tmp = PackageHelper.clone(pkg);
- initialize(tmp);
+ return PackageHelper.clone(pkg);
} catch (OpenXML4JException e){
throw new POIXMLException(e);
}
- } else {
- initialize(pkg);
}
+ return pkg;
}
/**
* Initialize this workbook from the specified Package
*/
@Override
- protected void initialize(Package pkg) throws IOException {
- super.initialize(pkg);
-
+ protected void onDocumentRead() {
try {
//build the POIXMLDocumentPart tree, this workbook is the root
read(XSSFFactory.getInstance());
- PackagePart corePart = getCorePart();
-
- WorkbookDocument doc = WorkbookDocument.Factory.parse(corePart.getInputStream());
+ WorkbookDocument doc = WorkbookDocument.Factory.parse(getPackagePart().getInputStream());
this.workbook = doc.getWorkbook();
HashMap<String, XSSFSheet> shIdMap = new HashMap<String, XSSFSheet>();
shIdMap.put(p.getPackageRelationship().getId(), (XSSFSheet)p);
}
}
+
// Load individual sheets
sheets = new LinkedList<XSSFSheet>();
+ embedds = new LinkedList<PackagePart>();
for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
String id = ctSheet.getId();
XSSFSheet sh = shIdMap.get(id);
}
}
- /**
- * Create a new SpreadsheetML OOXML package and setup the default minimal content
- */
- protected void newWorkbook() throws IOException, OpenXML4JException{
- Package pkg = Package.create(PackageHelper.createTempFile());
- // Main part
- PackagePartName corePartName = PackagingURIHelper.createPartName(XSSFRelation.WORKBOOK.getDefaultFileName());
- // Create main part relationship
- pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
- // Create main document part
- pkg.createPart(corePartName, XSSFRelation.WORKBOOK.getContentType());
-
- pkg.getPackageProperties().setCreatorProperty("Apache POI");
-
- super.initialize(pkg);
-
+ @Override
+ protected void onDocumentCreate() {
workbook = CTWorkbook.Factory.newInstance();
CTBookViews bvs = workbook.addNewBookViews();
CTBookView bv = bvs.addNewWorkbookView();
namedRanges = new LinkedList<XSSFName>();
sheets = new LinkedList<XSSFSheet>();
+ embedds = new LinkedList<PackagePart>();
+ }
+
+ /**
+ * Create a new SpreadsheetML package and setup the default minimal content
+ */
+ protected Package newPackage() throws IOException {
+ try {
+ Package pkg = Package.create(PackageHelper.createTempFile());
+ // Main part
+ PackagePartName corePartName = PackagingURIHelper.createPartName(XSSFRelation.WORKBOOK.getDefaultFileName());
+ // Create main part relationship
+ pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
+ // Create main document part
+ pkg.createPart(corePartName, XSSFRelation.WORKBOOK.getContentType());
+
+ pkg.getPackageProperties().setCreatorProperty("Apache POI");
+
+ return pkg;
+ } catch (InvalidFormatException e){
+ throw new POIXMLException(e);
+ }
}
/**
* macro enabled workbook (.xlsm)?
*/
public boolean isMacroEnabled() {
- return getCorePart().getContentType().equals(XSSFRelation.MACROS_WORKBOOK.getContentType());
+ return getPackagePart().getContentType().equals(XSSFRelation.MACROS_WORKBOOK.getContentType());
}
public void removeName(int index) {
CTWorkbookPr workbookPr = workbook.getWorkbookPr();
return workbookPr != null && workbookPr.getDate1904();
}
+
+ /**
+ * Get the document's embedded files.
+ */
+ public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
+ return embedds;
+ }
}
protected List<XWPFHyperlink> hyperlinks;
protected List<XWPFParagraph> paragraphs;
protected List<XWPFTable> tables;
-
+ /**
+ * The embedded OLE2 files in the OPC package
+ */
+ private List<PackagePart> embedds;
+
/** Handles the joy of different headers/footers for different pages */
private XWPFHeaderFooterPolicy headerFooterPolicy;
{
return getCorePart().getRelationshipsByType(COMMENT_RELATION_TYPE);
}
+
+ /**
+ * Get the document's embedded files.
+ */
+ public List<PackagePart> getAllEmbedds() throws OpenXML4JException {
+ return embedds;
+ }
+
}