diff --git a/src/scratchpad/ooxml-src/org/apache/poi/POIXMLDocument.java b/src/scratchpad/ooxml-src/org/apache/poi/POIXMLDocument.java index 36a2e8ffb3..86b0d557b1 100644 --- a/src/scratchpad/ooxml-src/org/apache/poi/POIXMLDocument.java +++ b/src/scratchpad/ooxml-src/org/apache/poi/POIXMLDocument.java @@ -27,7 +27,19 @@ import org.apache.poi.hxf.HXFDocument; public abstract class POIXMLDocument { private HXFDocument document; + /** + * Creates a new POI XML Document, wrapping up + * the underlying raw HXFDocument + */ protected POIXMLDocument(HXFDocument document) { this.document = document; } + + /** + * Returns the underlying HXFDocument, typically + * used for unit testing + */ + public HXFDocument _getHXFDocument() { + return document; + } } diff --git a/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java b/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java index b9fdfab36d..36a890b97a 100644 --- a/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java +++ b/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java @@ -34,6 +34,7 @@ import org.openxml4j.opc.PackagePartName; import org.openxml4j.opc.PackageRelationship; import org.openxml4j.opc.PackageRelationshipCollection; import org.openxml4j.opc.PackagingURIHelper; +import org.openxml4j.opc.internal.PackagePropertiesPart; import org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties; import org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument; @@ -52,6 +53,9 @@ import org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.Proper * WARNING - APIs expected to change rapidly */ public abstract class HXFDocument { + 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"; + /** * File package/container. */ @@ -70,12 +74,7 @@ public abstract class HXFDocument { this.container = container; // Find the base document - ArrayList baseParts = - container.getPartsByContentType(baseContentType); - if(baseParts.size() != 1) { - throw new OpenXML4JException("Expecting one entry with content type of " + baseContentType + ", but found " + baseParts.size()); - } - basePart = baseParts.get(0); + basePart = getSinglePartByType(baseContentType); // And load it up try { @@ -88,6 +87,41 @@ public abstract class HXFDocument { } } + /** + * Fetches the (single) PackagePart with the supplied + * content type. + * @param contentType The content type to search for + * @throws IllegalArgumentException If we don't find a single part of that type + */ + private PackagePart getSinglePartByType(String contentType) throws IllegalArgumentException { + ArrayList parts = + container.getPartsByContentType(contentType); + if(parts.size() != 1) { + throw new IllegalArgumentException("Expecting one entry with content type of " + contentType + ", but found " + parts.size()); + } + return parts.get(0); + } + + /** + * Fetches the (single) PackagePart which is defined as + * the supplied relation content type of the base + * 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 + */ + private PackagePart getSinglePartByRelationType(String relationType) throws IllegalArgumentException, OpenXML4JException { + PackageRelationshipCollection rels = + container.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 getPackagePart(rel); + } + /** * Retrieves the PackagePart for the given relation * id. This will normally come from a r:id attribute @@ -147,19 +181,21 @@ public abstract class HXFDocument { } /** - * Get the document properties (extended ooxml properties) + * Get the core document properties (core ooxml properties). */ - public CTProperties getDocumentProperties() throws OpenXML4JException, XmlException, IOException { - PackageRelationshipCollection docProps = - container.getRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"); - if(docProps.size() == 0) { + public PackagePropertiesPart getCoreProperties() throws OpenXML4JException, XmlException, IOException { + PackagePart propsPart = getSinglePartByRelationType(CORE_PROPERTIES_REL_TYPE); + if(propsPart == null) { return null; } - if(docProps.size() > 1) { - throw new IllegalStateException("Found " + docProps.size() + " relations for the extended properties, should only ever be one!"); - } - PackageRelationship rel = docProps.getRelationship(0); - PackagePart propsPart = getPackagePart(rel); + return (PackagePropertiesPart)propsPart; + } + + /** + * Get the extended document properties (extended ooxml properties) + */ + public CTProperties getExtendedProperties() throws OpenXML4JException, XmlException, IOException { + PackagePart propsPart = getSinglePartByRelationType(EXTENDED_PROPERTIES_REL_TYPE); PropertiesDocument props = PropertiesDocument.Factory.parse( propsPart.getInputStream()); diff --git a/src/scratchpad/ooxml-testcases/org/apache/poi/hslf/TestHSLFXML.java b/src/scratchpad/ooxml-testcases/org/apache/poi/hslf/TestHSLFXML.java index 9c122da6dc..fd4653a854 100644 --- a/src/scratchpad/ooxml-testcases/org/apache/poi/hslf/TestHSLFXML.java +++ b/src/scratchpad/ooxml-testcases/org/apache/poi/hslf/TestHSLFXML.java @@ -113,10 +113,15 @@ public class TestHSLFXML extends TestCase { HSLFXML xml = new HSLFXML( HXFDocument.openPackage(sampleFile) ); - assertNotNull(xml.getDocumentProperties()); - assertEquals("Microsoft Office PowerPoint", xml.getDocumentProperties().getApplication()); - assertEquals(0, xml.getDocumentProperties().getCharacters()); - assertEquals(0, xml.getDocumentProperties().getLines()); + assertNotNull(xml.getCoreProperties()); + assertNotNull(xml.getExtendedProperties()); + + assertEquals("Microsoft Office PowerPoint", xml.getExtendedProperties().getApplication()); + assertEquals(0, xml.getExtendedProperties().getCharacters()); + assertEquals(0, xml.getExtendedProperties().getLines()); + + assertEquals(null, xml.getCoreProperties().getTitleProperty().getValue()); + assertEquals(null, xml.getCoreProperties().getSubjectProperty().getValue()); } } diff --git a/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java b/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java index c4b21dffab..9c3ef65c44 100644 --- a/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java +++ b/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java @@ -114,10 +114,14 @@ public class TestHSSFXML extends TestCase { HSSFXML xml = new HSSFXML( HXFDocument.openPackage(sampleFile) ); - assertNotNull(xml.getDocumentProperties()); + assertNotNull(xml.getCoreProperties()); + assertNotNull(xml.getExtendedProperties()); - assertEquals("Microsoft Excel", xml.getDocumentProperties().getApplication()); - assertEquals(0, xml.getDocumentProperties().getCharacters()); - assertEquals(0, xml.getDocumentProperties().getLines()); + assertEquals("Microsoft Excel", xml.getExtendedProperties().getApplication()); + assertEquals(0, xml.getExtendedProperties().getCharacters()); + assertEquals(0, xml.getExtendedProperties().getLines()); + + assertEquals(null, xml.getCoreProperties().getTitleProperty().getValue()); + assertEquals(null, xml.getCoreProperties().getSubjectProperty().getValue()); } } \ No newline at end of file diff --git a/src/scratchpad/ooxml-testcases/org/apache/poi/hwpf/TestHWPFXML.java b/src/scratchpad/ooxml-testcases/org/apache/poi/hwpf/TestHWPFXML.java index 78209b9e1e..0d8e196f43 100644 --- a/src/scratchpad/ooxml-testcases/org/apache/poi/hwpf/TestHWPFXML.java +++ b/src/scratchpad/ooxml-testcases/org/apache/poi/hwpf/TestHWPFXML.java @@ -82,10 +82,29 @@ public class TestHWPFXML extends TestCase { HWPFXML xml = new HWPFXML( HXFDocument.openPackage(sampleFile) ); - assertNotNull(xml.getDocumentProperties()); + assertNotNull(xml.getCoreProperties()); + assertNotNull(xml.getExtendedProperties()); - assertEquals("Microsoft Office Word", xml.getDocumentProperties().getApplication()); - assertEquals(1315, xml.getDocumentProperties().getCharacters()); - assertEquals(10, xml.getDocumentProperties().getLines()); + assertEquals("Microsoft Office Word", xml.getExtendedProperties().getApplication()); + assertEquals(1315, xml.getExtendedProperties().getCharacters()); + assertEquals(10, xml.getExtendedProperties().getLines()); + + assertEquals(null, xml.getCoreProperties().getTitleProperty().getValue()); + assertEquals(null, xml.getCoreProperties().getSubjectProperty().getValue()); + } + + public void testMetadataComplex() throws Exception { + HWPFXML xml = new HWPFXML( + HXFDocument.openPackage(complexFile) + ); + assertNotNull(xml.getCoreProperties()); + assertNotNull(xml.getExtendedProperties()); + + assertEquals("Microsoft Office Outlook", xml.getExtendedProperties().getApplication()); + assertEquals(5184, xml.getExtendedProperties().getCharacters()); + assertEquals(0, xml.getExtendedProperties().getLines()); + + assertEquals(" ", xml.getCoreProperties().getTitleProperty().getValue()); + assertEquals(" ", xml.getCoreProperties().getSubjectProperty().getValue()); } }