Author: nick
Date: Thu Jan  3 06:28:46 2008
New Revision: 608500

URL: http://svn.apache.org/viewvc?rev=608500&view=rev
Log:
For ooxml properties, get the core ones as well as the extended ones, and add 
tests for this

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

Modified: poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/POIXMLDocument.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/POIXMLDocument.java?rev=608500&r1=608499&r2=608500&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/POIXMLDocument.java 
(original)
+++ poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/POIXMLDocument.java Thu 
Jan  3 06:28:46 2008
@@ -27,7 +27,19 @@
 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;
        }
 }

Modified: poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java?rev=608500&r1=608499&r2=608500&view=diff
==============================================================================
--- poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java 
(original)
+++ poi/trunk/src/scratchpad/ooxml-src/org/apache/poi/hxf/HXFDocument.java Thu 
Jan  3 06:28:46 2008
@@ -34,6 +34,7 @@
 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 @@
  * 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 @@
                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 {
@@ -89,6 +88,41 @@
        }
        
        /**
+        * 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
         *  on part of the base document. 
@@ -147,19 +181,21 @@
        }
        
        /**
-        * 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());

Modified: 
poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hslf/TestHSLFXML.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hslf/TestHSLFXML.java?rev=608500&r1=608499&r2=608500&view=diff
==============================================================================
--- 
poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hslf/TestHSLFXML.java 
(original)
+++ 
poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hslf/TestHSLFXML.java 
Thu Jan  3 06:28:46 2008
@@ -113,10 +113,15 @@
                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());
        }
 }

Modified: 
poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java?rev=608500&r1=608499&r2=608500&view=diff
==============================================================================
--- 
poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java 
(original)
+++ 
poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hssf/TestHSSFXML.java 
Thu Jan  3 06:28:46 2008
@@ -114,10 +114,14 @@
                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());
        }
 }

Modified: 
poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hwpf/TestHWPFXML.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hwpf/TestHWPFXML.java?rev=608500&r1=608499&r2=608500&view=diff
==============================================================================
--- 
poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hwpf/TestHWPFXML.java 
(original)
+++ 
poi/trunk/src/scratchpad/ooxml-testcases/org/apache/poi/hwpf/TestHWPFXML.java 
Thu Jan  3 06:28:46 2008
@@ -82,10 +82,29 @@
                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());
        }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to