For ooxml properties, get the core ones as well as the extended ones, and add tests for this

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@608500 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-01-03 14:28:46 +00:00
parent 608392128b
commit 27b4980c2e
5 changed files with 104 additions and 28 deletions

View 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;
}
}

View 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());

View 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());
}
}

View 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());
}
}

View 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());
}
}