From 4b125e7ccdd5e8530152be708a67ea10c7c5fae5 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sun, 4 Sep 2011 20:14:43 +0000 Subject: [PATCH] Start on XSLF notes master support, and add a XMLSlideShow basics test, along with making it easier to get to the common slide data via XSLFSheet git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1165104 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xslf/usermodel/XSLFNotesMaster.java | 78 +++++++++++ .../poi/xslf/usermodel/XSLFRelation.java | 19 ++- .../apache/poi/xslf/usermodel/XSLFSheet.java | 21 ++- .../apache/poi/xslf/usermodel/XSLFSlide.java | 1 + .../poi/xslf/usermodel/XSLFSlideLayout.java | 1 + .../poi/xslf/usermodel/XSLFSlideMaster.java | 1 + .../poi/xslf/usermodel/TestXMLSlideShow.java | 122 ++++++++++++++++++ 7 files changed, 233 insertions(+), 10 deletions(-) create mode 100644 src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFNotesMaster.java create mode 100644 src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXMLSlideShow.java diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFNotesMaster.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFNotesMaster.java new file mode 100644 index 0000000000..32bf5ac2dc --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFNotesMaster.java @@ -0,0 +1,78 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xslf.usermodel; + +import org.apache.poi.POIXMLDocumentPart; +import org.apache.poi.openxml4j.opc.PackagePart; +import org.apache.poi.openxml4j.opc.PackageRelationship; +import org.apache.poi.sl.usermodel.MasterSheet; +import org.apache.poi.util.Beta; +import org.apache.xmlbeans.XmlException; +import org.openxmlformats.schemas.presentationml.x2006.main.CTNotesMaster; +import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMaster; +import org.openxmlformats.schemas.presentationml.x2006.main.NotesMasterDocument; +import org.openxmlformats.schemas.presentationml.x2006.main.SldMasterDocument; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +/** +* Notes master object associated with this layout. +*

+* Within a notes master slide are contained all elements +* that describe the objects and their corresponding formatting +* for within a presentation slide. +*

+*

+* Within a nodes master slide are two main elements. +* The cSld element specifies the common slide elements such as shapes and +* their attached text bodies. Then the notesStyles element specifies the +* formatting for the text within each of these shapes. +*

+ * + * @author Yegor Kozlov +*/ +@Beta + public class XSLFNotesMaster extends XSLFSheet { + private CTNotesMaster _slide; + private Map _layouts; + private XSLFTheme _theme; + + XSLFNotesMaster() { + super(); + _slide = CTNotesMaster.Factory.newInstance(); + } + + protected XSLFNotesMaster(PackagePart part, PackageRelationship rel) throws IOException, XmlException { + super(part, rel); + NotesMasterDocument doc = + NotesMasterDocument.Factory.parse(getPackagePart().getInputStream()); + _slide = doc.getNotesMaster(); + setCommonSlideData(_slide.getCSld()); + } + + @Override + public CTNotesMaster getXmlObject() { + return _slide; + } + + @Override + protected String getRootElementName(){ + return "notesMaster"; + } +} \ No newline at end of file diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFRelation.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFRelation.java index 15f91c6ec5..5d0826510f 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFRelation.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFRelation.java @@ -90,12 +90,19 @@ public class XSLFRelation extends POIXMLRelation { XSLFSlideLayout.class ); - public static final XSLFRelation SLIDE_MASTER = new XSLFRelation( - "application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml", - "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster", - "/ppt/slideMasters/slideMaster#.xml", - XSLFSlideMaster.class - ); + public static final XSLFRelation SLIDE_MASTER = new XSLFRelation( + "application/vnd.openxmlformats-officedocument.presentationml.slideMaster+xml", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster", + "/ppt/slideMasters/slideMaster#.xml", + XSLFSlideMaster.class + ); + + public static final XSLFRelation NOTES_MASTER = new XSLFRelation( + "application/vnd.openxmlformats-officedocument.presentationml.notesMaster+xml", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesMaster", + "/ppt/notesMasters/notesMaster#.xml", + XSLFNotesMaster.class + ); public static final XSLFRelation COMMENTS = new XSLFRelation( "application/vnd.openxmlformats-officedocument.presentationml.comments+xml", diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java index d97600d398..58e26eb873 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java @@ -24,11 +24,13 @@ import org.apache.poi.util.Beta; import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlOptions; import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId; +import org.openxmlformats.schemas.presentationml.x2006.main.CTCommonSlideData; import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector; import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame; import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape; import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture; import org.openxmlformats.schemas.presentationml.x2006.main.CTShape; +import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide; import javax.xml.namespace.QName; import java.io.IOException; @@ -41,11 +43,12 @@ import java.util.regex.Pattern; @Beta public abstract class XSLFSheet extends POIXMLDocumentPart { + private XSLFCommonSlideData _commonSlideData; private XSLFDrawing _drawing; private List _shapes; private CTGroupShape _spTree; - public XSLFSheet(){ + public XSLFSheet() { super(); } @@ -53,9 +56,9 @@ public abstract class XSLFSheet extends POIXMLDocumentPart { super(part, rel); } - public XMLSlideShow getSlideShow() { - return (XMLSlideShow)getParent(); - } + public XMLSlideShow getSlideShow() { + return (XMLSlideShow)getParent(); + } protected List buildShapes(CTGroupShape spTree){ List shapes = new ArrayList(); @@ -79,6 +82,16 @@ public abstract class XSLFSheet extends POIXMLDocumentPart { public abstract XmlObject getXmlObject(); + public XSLFCommonSlideData getCommonSlideData() { + return _commonSlideData; + } + protected void setCommonSlideData(CTCommonSlideData data) { + if(data == null) { + _commonSlideData = null; + } else { + _commonSlideData = new XSLFCommonSlideData(data); + } + } private XSLFDrawing getDrawing(){ if(_drawing == null) { diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlide.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlide.java index ff4e64d308..228a51ae85 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlide.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlide.java @@ -70,6 +70,7 @@ public final class XSLFSlide extends XSLFSheet { SldDocument doc = SldDocument.Factory.parse(getPackagePart().getInputStream()); _slide = doc.getSld(); + setCommonSlideData(_slide.getCSld()); } diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlideLayout.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlideLayout.java index 871ece50dd..2d385382f5 100755 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlideLayout.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlideLayout.java @@ -43,6 +43,7 @@ public class XSLFSlideLayout extends XSLFSheet { SldLayoutDocument doc = SldLayoutDocument.Factory.parse(getPackagePart().getInputStream()); _layout = doc.getSldLayout(); + setCommonSlideData(_layout.getCSld()); } diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlideMaster.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlideMaster.java index a0229f9017..b7dcad7ce8 100755 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlideMaster.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlideMaster.java @@ -64,6 +64,7 @@ import java.util.Map; SldMasterDocument doc = SldMasterDocument.Factory.parse(getPackagePart().getInputStream()); _slide = doc.getSldMaster(); + setCommonSlideData(_slide.getCSld()); } @Override diff --git a/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXMLSlideShow.java b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXMLSlideShow.java new file mode 100644 index 0000000000..7625c84878 --- /dev/null +++ b/src/ooxml/testcases/org/apache/poi/xslf/usermodel/TestXMLSlideShow.java @@ -0,0 +1,122 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.xslf.usermodel; + +import junit.framework.TestCase; + +import org.apache.poi.POIDataSamples; +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.openxml4j.opc.PackagePart; +import org.apache.poi.xslf.usermodel.XMLSlideShow; +import org.apache.poi.xslf.usermodel.XSLFRelation; +import org.openxmlformats.schemas.presentationml.x2006.main.CTNotesMasterIdListEntry; +import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry; +import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterIdListEntry; + +public class TestXMLSlideShow extends TestCase { + private static final POIDataSamples slTests = POIDataSamples.getSlideShowInstance(); + private OPCPackage pack; + + protected void setUp() throws Exception { + pack = OPCPackage.open(slTests.openResourceAsStream("sample.pptx")); + } + + public void testContainsMainContentType() throws Exception { + boolean found = false; + for(PackagePart part : pack.getParts()) { + if(part.getContentType().equals(XSLFRelation.MAIN.getContentType())) { + found = true; + } + //System.out.println(part); + } + assertTrue(found); + } + + public void testOpen() throws Exception { + XMLSlideShow xml; + + // With the finalised uri, should be fine + xml = new XMLSlideShow(pack); + // Check the core + assertNotNull(xml.getCTPresentation()); + + // Check it has some slides + assertNotNull(xml.getSlides().length); + assertTrue(xml.getSlides().length > 0); + + assertNotNull(xml.getSlideMasters().length); + assertTrue(xml.getSlideMasters().length > 0); + } + + public void testSlideBasics() throws Exception { + XMLSlideShow xml = new XMLSlideShow(pack); + + // Should have 1 master + assertEquals(1, xml.getSlideMasters().length); + + // Should have two sheets + assertEquals(2, xml.getSlides().length); + + // Check they're as expected + CTSlideIdListEntry[] slides = new CTSlideIdListEntry[ + xml.getCTPresentation().getSldIdLst().getSldIdList().size()]; + xml.getCTPresentation().getSldIdLst().getSldIdList().toArray(slides); + + assertEquals(256, slides[0].getId()); + assertEquals(257, slides[1].getId()); + assertEquals("rId2", slides[0].getId2()); + assertEquals("rId3", slides[1].getId2()); + + // Now get those objects + assertNotNull(xml.getSlides()[0]); + assertNotNull(xml.getSlides()[0]); + + // And check they have notes as expected + // TODO +// assertNotNull(xml.getNotes(slides[0])); +// assertNotNull(xml.getNotes(slides[1])); + + // Next up look for the slide master + CTSlideMasterIdListEntry[] masters = new CTSlideMasterIdListEntry[ + xml.getCTPresentation().getSldMasterIdLst().getSldMasterIdList().size()]; + xml.getCTPresentation().getSldMasterIdLst().getSldMasterIdList().toArray(masters); + + assertEquals(2147483648l, masters[0].getId()); + assertEquals("rId1", masters[0].getId2()); + assertNotNull(xml.getSlideMasters()[0]); + + // Finally look for the notes master + CTNotesMasterIdListEntry notesMaster = + xml.getCTPresentation().getNotesMasterIdLst().getNotesMasterId(); + assertNotNull(notesMaster); + // TODO Get the wrapper + } + + public void testMetadataBasics() throws Exception { + XMLSlideShow xml = new XMLSlideShow(pack); + + assertNotNull(xml.getProperties().getCoreProperties()); + assertNotNull(xml.getProperties().getExtendedProperties()); + + assertEquals("Microsoft Office PowerPoint", xml.getProperties().getExtendedProperties().getUnderlyingProperties().getApplication()); + assertEquals(0, xml.getProperties().getExtendedProperties().getUnderlyingProperties().getCharacters()); + assertEquals(0, xml.getProperties().getExtendedProperties().getUnderlyingProperties().getLines()); + + assertEquals(null, xml.getProperties().getCoreProperties().getTitle()); + assertEquals(null, xml.getProperties().getCoreProperties().getUnderlyingProperties().getSubjectProperty().getValue()); + } +} -- 2.39.5