Get the powerpoint ooxml stuff converted over, and fix up a few tests

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@635243 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-03-09 14:21:34 +00:00
parent 4f0afb0335
commit be6166389f
7 changed files with 137 additions and 77 deletions

View File

@ -22,15 +22,19 @@ import java.io.PushbackInputStream;
import org.apache.poi.poifs.common.POIFSConstants; import org.apache.poi.poifs.common.POIFSConstants;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.xmlbeans.XmlException;
import org.openxml4j.exceptions.InvalidFormatException; import org.openxml4j.exceptions.InvalidFormatException;
import org.openxml4j.exceptions.OpenXML4JException; import org.openxml4j.exceptions.OpenXML4JException;
import org.openxml4j.opc.Package; import org.openxml4j.opc.Package;
import org.openxml4j.opc.PackagePart; import org.openxml4j.opc.PackagePart;
import org.openxml4j.opc.PackagePartName; import org.openxml4j.opc.PackagePartName;
import org.openxml4j.opc.PackageRelationship; import org.openxml4j.opc.PackageRelationship;
import org.openxml4j.opc.PackageRelationshipCollection;
import org.openxml4j.opc.PackageRelationshipTypes; import org.openxml4j.opc.PackageRelationshipTypes;
import org.openxml4j.opc.PackagingURIHelper; 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;
public abstract class POIXMLDocument { public abstract class POIXMLDocument {
@ -68,7 +72,7 @@ public abstract class POIXMLDocument {
* in the event of a problem. * in the event of a problem.
* Works around shortcomings in java's this() constructor calls * Works around shortcomings in java's this() constructor calls
*/ */
protected static Package openPackage(String path) throws IOException { public static Package openPackage(String path) throws IOException {
try { try {
return Package.open(path); return Package.open(path);
} catch (InvalidFormatException e) { } catch (InvalidFormatException e) {
@ -100,6 +104,27 @@ public abstract class POIXMLDocument {
return part; return part;
} }
/**
* 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
*/
protected PackagePart getSinglePartByRelationType(String relationType) throws IllegalArgumentException, OpenXML4JException {
PackageRelationshipCollection rels =
getCorePart().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);
}
/** /**
* Checks that the supplied InputStream (which MUST * Checks that the supplied InputStream (which MUST
* support mark and reset, or be a PushbackInputStream) * support mark and reset, or be a PushbackInputStream)
@ -132,4 +157,30 @@ public abstract class POIXMLDocument {
header[3] == POIFSConstants.OOXML_FILE_HEADER[3] header[3] == POIFSConstants.OOXML_FILE_HEADER[3]
); );
} }
/**
* Get the core document properties (core ooxml properties).
* TODO: Replace with nice usermodel wrapper
* @deprecated To be replaced with a proper user-model style view of the properties
*/
public PackagePropertiesPart getCoreProperties() throws OpenXML4JException, IOException {
PackagePart propsPart = getSinglePartByRelationType(CORE_PROPERTIES_REL_TYPE);
if(propsPart == null) {
return null;
}
return (PackagePropertiesPart)propsPart;
}
/**
* Get the extended document properties (extended ooxml properties)
* TODO: Replace with nice usermodel wrapper
* @deprecated To be replaced with a proper user-model style view of the properties
*/
public CTProperties getExtendedProperties() throws OpenXML4JException, XmlException, IOException {
PackagePart propsPart = getSinglePartByRelationType(EXTENDED_PROPERTIES_REL_TYPE);
PropertiesDocument props = PropertiesDocument.Factory.parse(
propsPart.getInputStream());
return props.getProperties();
}
} }

View File

@ -14,11 +14,11 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.hslf; package org.apache.poi.xslf;
import java.io.IOException; import java.io.IOException;
import org.apache.poi.hxf.HXFDocument; import org.apache.poi.POIXMLDocument;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
import org.openxml4j.exceptions.InvalidFormatException; import org.openxml4j.exceptions.InvalidFormatException;
import org.openxml4j.exceptions.OpenXML4JException; import org.openxml4j.exceptions.OpenXML4JException;
@ -49,7 +49,7 @@ import org.openxmlformats.schemas.presentationml.x2006.main.SldMasterDocument;
* *
* WARNING - APIs expected to change rapidly * WARNING - APIs expected to change rapidly
*/ */
public class HSLFXML extends HXFDocument { public class XSLFSlideShow extends POIXMLDocument {
public static final String MAIN_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"; public static final String MAIN_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml";
public static final String NOTES_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.presentationml.notesSlide+xml"; public static final String NOTES_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.presentationml.notesSlide+xml";
public static final String SLIDE_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.presentationml.slide+xml"; public static final String SLIDE_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.presentationml.slide+xml";
@ -58,11 +58,14 @@ public class HSLFXML extends HXFDocument {
private PresentationDocument presentationDoc; private PresentationDocument presentationDoc;
public HSLFXML(Package container) throws OpenXML4JException, IOException, XmlException { public XSLFSlideShow(Package container) throws OpenXML4JException, IOException, XmlException {
super(container, MAIN_CONTENT_TYPE); super(container);
presentationDoc = presentationDoc =
PresentationDocument.Factory.parse(basePart.getInputStream()); PresentationDocument.Factory.parse(getCorePart().getInputStream());
}
public XSLFSlideShow(String file) throws OpenXML4JException, IOException, XmlException {
this(openPackage(file));
} }
/** /**
@ -96,11 +99,16 @@ public class HSLFXML extends HXFDocument {
* the supplied slide master reference * the supplied slide master reference
*/ */
public CTSlideMaster getSlideMaster(CTSlideMasterIdListEntry master) throws IOException, XmlException { public CTSlideMaster getSlideMaster(CTSlideMasterIdListEntry master) throws IOException, XmlException {
PackagePart masterPart = try {
getRelatedPackagePart(master.getId2()); PackagePart masterPart =
SldMasterDocument masterDoc = getTargetPart(getCorePart().getRelationship(master.getId2()));
SldMasterDocument.Factory.parse(masterPart.getInputStream());
return masterDoc.getSldMaster(); SldMasterDocument masterDoc =
SldMasterDocument.Factory.parse(masterPart.getInputStream());
return masterDoc.getSldMaster();
} catch(InvalidFormatException e) {
throw new XmlException(e);
}
} }
/** /**
@ -108,11 +116,15 @@ public class HSLFXML extends HXFDocument {
* the supplied slide reference * the supplied slide reference
*/ */
public CTSlide getSlide(CTSlideIdListEntry slide) throws IOException, XmlException { public CTSlide getSlide(CTSlideIdListEntry slide) throws IOException, XmlException {
PackagePart slidePart = try {
getRelatedPackagePart(slide.getId2()); PackagePart slidePart =
SldDocument slideDoc = getTargetPart(getCorePart().getRelationship(slide.getId2()));
SldDocument.Factory.parse(slidePart.getInputStream()); SldDocument slideDoc =
return slideDoc.getSld(); SldDocument.Factory.parse(slidePart.getInputStream());
return slideDoc.getSld();
} catch(InvalidFormatException e) {
throw new XmlException(e);
}
} }
/** /**
@ -120,11 +132,11 @@ public class HSLFXML extends HXFDocument {
* slide, as found from the supplied slide reference * slide, as found from the supplied slide reference
*/ */
public CTNotesSlide getNotes(CTSlideIdListEntry slide) throws IOException, XmlException { public CTNotesSlide getNotes(CTSlideIdListEntry slide) throws IOException, XmlException {
PackagePart slidePart =
getRelatedPackagePart(slide.getId2());
PackageRelationshipCollection notes; PackageRelationshipCollection notes;
try { try {
PackagePart slidePart =
getTargetPart(getCorePart().getRelationship(slide.getId2()));
notes = slidePart.getRelationshipsByType(NOTES_RELATION_TYPE); notes = slidePart.getRelationshipsByType(NOTES_RELATION_TYPE);
} catch(InvalidFormatException e) { } catch(InvalidFormatException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
@ -138,11 +150,15 @@ public class HSLFXML extends HXFDocument {
throw new IllegalStateException("Expecting 0 or 1 notes for a slide, but found " + notes.size()); throw new IllegalStateException("Expecting 0 or 1 notes for a slide, but found " + notes.size());
} }
PackagePart notesPart = try {
getPackagePart(notes.getRelationship(0)); PackagePart notesPart =
NotesDocument notesDoc = getTargetPart(notes.getRelationship(0));
NotesDocument.Factory.parse(notesPart.getInputStream()); NotesDocument notesDoc =
NotesDocument.Factory.parse(notesPart.getInputStream());
return notesDoc.getNotes(); return notesDoc.getNotes();
} catch(InvalidFormatException e) {
throw new IllegalStateException(e);
}
} }
} }

View File

@ -14,15 +14,14 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.hslf.extractor; package org.apache.poi.xslf.extractor;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor; import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hslf.HSLFXML; import org.apache.poi.xslf.XSLFSlideShow;
import org.apache.poi.hslf.usermodel.HSLFXMLSlideShow;
import org.apache.poi.hxf.HXFDocument;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
import org.openxml4j.exceptions.OpenXML4JException; import org.openxml4j.exceptions.OpenXML4JException;
import org.openxml4j.opc.Package; import org.openxml4j.opc.Package;
@ -35,17 +34,15 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide; import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry; import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry;
public class HXFPowerPointExtractor extends POIXMLTextExtractor { public class XSLFPowerPointExtractor extends POIXMLTextExtractor {
private HSLFXMLSlideShow slideshow; private XSLFSlideShow slideshow;
private boolean slidesByDefault = true; private boolean slidesByDefault = true;
private boolean notesByDefault = false; private boolean notesByDefault = false;
public HXFPowerPointExtractor(Package container) throws XmlException, OpenXML4JException, IOException { public XSLFPowerPointExtractor(Package container) throws XmlException, OpenXML4JException, IOException {
this(new HSLFXMLSlideShow( this(new XSLFSlideShow(container));
new XSLFXML(container)
));
} }
public HXFPowerPointExtractor(HSLFXMLSlideShow slideshow) { public XSLFPowerPointExtractor(XSLFSlideShow slideshow) {
super(slideshow); super(slideshow);
this.slideshow = slideshow; this.slideshow = slideshow;
} }
@ -57,9 +54,8 @@ public class HXFPowerPointExtractor extends POIXMLTextExtractor {
System.exit(1); System.exit(1);
} }
POIXMLTextExtractor extractor = POIXMLTextExtractor extractor =
new HXFPowerPointExtractor(HXFDocument.openPackage( new XSLFPowerPointExtractor(
new File(args[0]) new XSLFSlideShow(args[0]));
));
System.out.println(extractor.getText()); System.out.println(extractor.getText());
} }
@ -94,13 +90,13 @@ public class HXFPowerPointExtractor extends POIXMLTextExtractor {
StringBuffer text = new StringBuffer(); StringBuffer text = new StringBuffer();
CTSlideIdListEntry[] slideRefs = CTSlideIdListEntry[] slideRefs =
slideshow._getHSLFXML().getSlideReferences().getSldIdArray(); slideshow.getSlideReferences().getSldIdArray();
for (int i = 0; i < slideRefs.length; i++) { for (int i = 0; i < slideRefs.length; i++) {
try { try {
CTSlide slide = CTSlide slide =
slideshow._getHSLFXML().getSlide(slideRefs[i]); slideshow.getSlide(slideRefs[i]);
CTNotesSlide notes = CTNotesSlide notes =
slideshow._getHSLFXML().getNotes(slideRefs[i]); slideshow.getNotes(slideRefs[i]);
if(slideText) { if(slideText) {
extractText(slide.getCSld().getSpTree(), text); extractText(slide.getCSld().getSpTree(), text);

View File

@ -14,10 +14,9 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.hslf.usermodel; package org.apache.poi.xslf.usermodel;
import org.apache.poi.POIXMLDocument; import org.apache.poi.xslf.XSLFSlideShow;
import org.apache.poi.hslf.HSLFXML;
/** /**
* High level representation of a ooxml slideshow. * High level representation of a ooxml slideshow.
@ -25,15 +24,17 @@ import org.apache.poi.hslf.HSLFXML;
* they are reading or writing a slideshow. It is also the * they are reading or writing a slideshow. It is also the
* top level object for creating new slides/etc. * top level object for creating new slides/etc.
*/ */
public class HSLFXMLSlideShow extends POIXMLDocument { public class XMLSlideShow {
private org.apache.poi.hslf.XSLFXML hslfXML; private XSLFSlideShow slideShow;
public HSLFXMLSlideShow(XSLFXML xml) { public XMLSlideShow(XSLFSlideShow xml) {
super(xml); this.slideShow = xml;
this.hslfXML = xml;
} }
public XSLFXML _getHSLFXML() { public XSLFSlideShow _getXSLFSlideShow() {
return hslfXML; return slideShow;
} }
// TODO: Get slides
// TODO: Get notes
} }

View File

@ -17,7 +17,7 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.hxf; package org.apache.poi;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.io.*; import java.io.*;
@ -38,7 +38,7 @@ public class TestDetectAsOOXML extends TestCase
{ {
File f = new File(dirname + "/sample.xlsx"); File f = new File(dirname + "/sample.xlsx");
HXFDocument.openPackage(f); POIXMLDocument.openPackage(f.toString());
} }
public void testDetectAsPOIFS() throws Exception { public void testDetectAsPOIFS() throws Exception {
@ -48,18 +48,18 @@ public class TestDetectAsOOXML extends TestCase
in = new PushbackInputStream( in = new PushbackInputStream(
new FileInputStream(dirname + "/SampleSS.xlsx"), 10 new FileInputStream(dirname + "/SampleSS.xlsx"), 10
); );
assertTrue(HXFDocument.hasOOXMLHeader(in)); assertTrue(POIXMLDocument.hasOOXMLHeader(in));
// xls file isn't // xls file isn't
in = new PushbackInputStream( in = new PushbackInputStream(
new FileInputStream(dirname + "/SampleSS.xls"), 10 new FileInputStream(dirname + "/SampleSS.xls"), 10
); );
assertFalse(HXFDocument.hasOOXMLHeader(in)); assertFalse(POIXMLDocument.hasOOXMLHeader(in));
// text file isn't // text file isn't
in = new PushbackInputStream( in = new PushbackInputStream(
new FileInputStream(dirname + "/SampleSS.txt"), 10 new FileInputStream(dirname + "/SampleSS.txt"), 10
); );
assertFalse(HXFDocument.hasOOXMLHeader(in)); assertFalse(POIXMLDocument.hasOOXMLHeader(in));
} }
} }

View File

@ -14,11 +14,11 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.hslf; package org.apache.poi.xslf;
import java.io.File; import java.io.File;
import org.apache.poi.hxf.HXFDocument; import org.apache.poi.POIXMLDocument;
import org.openxml4j.opc.Package; import org.openxml4j.opc.Package;
import org.openxml4j.opc.PackagePart; import org.openxml4j.opc.PackagePart;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry; import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry;
@ -26,8 +26,8 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterIdListE
import junit.framework.TestCase; import junit.framework.TestCase;
public class TestHSLFXML extends TestCase { public class TestXSLFSlideShow extends TestCase {
private File sampleFile; private String sampleFile;
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
@ -35,15 +35,15 @@ public class TestHSLFXML extends TestCase {
sampleFile = new File( sampleFile = new File(
System.getProperty("HSLF.testdata.path") + System.getProperty("HSLF.testdata.path") +
File.separator + "sample.pptx" File.separator + "sample.pptx"
); ).toString();
} }
public void testContainsMainContentType() throws Exception { public void testContainsMainContentType() throws Exception {
Package pack = HXFDocument.openPackage(sampleFile); Package pack = POIXMLDocument.openPackage(sampleFile);
boolean found = false; boolean found = false;
for(PackagePart part : pack.getParts()) { for(PackagePart part : pack.getParts()) {
if(part.getContentType().equals(HSLFXML.MAIN_CONTENT_TYPE)) { if(part.getContentType().equals(XSLFSlideShow.MAIN_CONTENT_TYPE)) {
found = true; found = true;
} }
System.out.println(part); System.out.println(part);
@ -52,13 +52,13 @@ public class TestHSLFXML extends TestCase {
} }
public void testOpen() throws Exception { public void testOpen() throws Exception {
HXFDocument.openPackage(sampleFile); POIXMLDocument.openPackage(sampleFile);
HSLFXML xml; XSLFSlideShow xml;
// With the finalised uri, should be fine // With the finalised uri, should be fine
xml = new HSLFXML( xml = new XSLFSlideShow(
HXFDocument.openPackage(sampleFile) POIXMLDocument.openPackage(sampleFile)
); );
// Check the core // Check the core
@ -74,9 +74,7 @@ public class TestHSLFXML extends TestCase {
} }
public void testSlideBasics() throws Exception { public void testSlideBasics() throws Exception {
HSLFXML xml = new HSLFXML( XSLFSlideShow xml = new XSLFSlideShow(sampleFile);
HXFDocument.openPackage(sampleFile)
);
// Should have 1 master // Should have 1 master
assertEquals(1, xml.getSlideMasterReferences().sizeOfSldMasterIdArray()); assertEquals(1, xml.getSlideMasterReferences().sizeOfSldMasterIdArray());
@ -110,9 +108,7 @@ public class TestHSLFXML extends TestCase {
} }
public void testMetadataBasics() throws Exception { public void testMetadataBasics() throws Exception {
HSLFXML xml = new HSLFXML( XSLFSlideShow xml = new XSLFSlideShow(sampleFile);
HXFDocument.openPackage(sampleFile)
);
assertNotNull(xml.getCoreProperties()); assertNotNull(xml.getCoreProperties());
assertNotNull(xml.getExtendedProperties()); assertNotNull(xml.getExtendedProperties());

View File

@ -49,7 +49,7 @@ public class TestHXFPowerPointExtractor extends TestCase {
*/ */
public void testGetSimpleText() throws Exception { public void testGetSimpleText() throws Exception {
new HXFPowerPointExtractor(xmlA.getPackage()); new HXFPowerPointExtractor(xmlA.getPackage());
new HXFPowerPointExtractor(new HSLFXMLSlideShow(xmlA)); new HXFPowerPointExtractor(new XMLSlideShow(xmlA));
HXFPowerPointExtractor extractor = HXFPowerPointExtractor extractor =
new HXFPowerPointExtractor(xmlA.getPackage()); new HXFPowerPointExtractor(xmlA.getPackage());