]> source.dussan.org Git - poi.git/commitdiff
For ooxml properties, get the core ones as well as the extended ones, and add tests...
authorNick Burch <nick@apache.org>
Thu, 3 Jan 2008 14:28:46 +0000 (14:28 +0000)
committerNick Burch <nick@apache.org>
Thu, 3 Jan 2008 14:28:46 +0000 (14:28 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@608500 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/ooxml-src/org/apache/poi/POIXMLDocument.java
src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java
src/scratchpad/ooxml-testcases/org/apache/poi/hslf/TestHSLFXML.java
src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java
src/scratchpad/ooxml-testcases/org/apache/poi/hwpf/TestHWPFXML.java

index 36a2e8ffb317c2b4d79762d260755d4ebbf4a809..86b0d557b176526772e88707de87ce6a88986c82 100644 (file)
@@ -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;
+       }
 }
index b9fdfab36d2861d80a66b45eaf93fb0043fb7a75..36a890b97a95ac6e90e8dce0b85fd8d132019b2a 100644 (file)
@@ -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<PackagePart> 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<PackagePart> 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());
index 9c122da6dc8c2def6c8c2ad751fdb959734ed80d..fd4653a854b1bb05eeaebba89995c58ba2c7db1b 100644 (file)
@@ -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());
        }
 }
index c4b21dffabd36a1c3d9ca22152b37a9c01e1a9b8..9c3ef65c4441d3eb48d47d9607c9f1ea9daeb477 100644 (file)
@@ -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
index 78209b9e1e12b2298eeb2db62d646c5cd7cc288b..0d8e196f4385102475157585d95746aeeb3c88a2 100644 (file)
@@ -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());
        }
 }