package org.apache.poi.xslf.usermodel;
import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.util.Internal;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.presentationml.x2006.main.CTCommentList;
import org.openxmlformats.schemas.presentationml.x2006.main.CTNotesSlide;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPresentation;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdList;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideIdListEntry;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMaster;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterIdList;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlideMasterIdListEntry;
import org.openxmlformats.schemas.presentationml.x2006.main.CmLstDocument;
import org.openxmlformats.schemas.presentationml.x2006.main.NotesDocument;
import org.openxmlformats.schemas.presentationml.x2006.main.PresentationDocument;
import org.openxmlformats.schemas.presentationml.x2006.main.SldDocument;
import org.openxmlformats.schemas.presentationml.x2006.main.SldMasterDocument;
public class XSLFSlideShow extends POIXMLDocument {
private PresentationDocument presentationDoc;
private List<PackagePart> embedds;
public XSLFSlideShow(OPCPackage container) throws OpenXML4JException, IOException, XmlException {
super(container);
if(getCorePart().getContentType().equals(XSLFRelation.THEME_MANAGER.getContentType())) {
rebase(getPackage());
}
presentationDoc =
PresentationDocument.Factory.parse(getCorePart().getInputStream(), DEFAULT_XML_OPTIONS);
embedds = new LinkedList<>();
for (CTSlideIdListEntry ctSlide : getSlideReferences().getSldIdArray()) {
PackagePart corePart = getCorePart();
PackagePart slidePart = corePart.getRelatedPart(corePart.getRelationship(ctSlide.getId2()));
for(PackageRelationship rel : slidePart.getRelationshipsByType(OLE_OBJECT_REL_TYPE)) {
if (TargetMode.EXTERNAL == rel.getTargetMode()) {
continue;
}
embedds.add(slidePart.getRelatedPart(rel));
}
for (PackageRelationship rel : slidePart.getRelationshipsByType(PACK_OBJECT_REL_TYPE)) {
embedds.add(slidePart.getRelatedPart(rel));
}
}
}
public XSLFSlideShow(String file) throws OpenXML4JException, IOException, XmlException {
this(openPackage(file));
}
@Internal
public CTPresentation getPresentation() {
return presentationDoc.getPresentation();
}
@Internal
public CTSlideIdList getSlideReferences() {
if(! getPresentation().isSetSldIdLst()) {
getPresentation().setSldIdLst(CTSlideIdList.Factory.newInstance());
}
return getPresentation().getSldIdLst();
}
@Internal
public CTSlideMasterIdList getSlideMasterReferences() {
return getPresentation().getSldMasterIdLst();
}
public PackagePart getSlideMasterPart(CTSlideMasterIdListEntry master) throws IOException, XmlException {
try {
PackagePart corePart = getCorePart();
return corePart.getRelatedPart(
corePart.getRelationship(master.getId2())
);
} catch(InvalidFormatException e) {
throw new XmlException(e);
}
}
@Internal
public CTSlideMaster getSlideMaster(CTSlideMasterIdListEntry master) throws IOException, XmlException {
PackagePart masterPart = getSlideMasterPart(master);
SldMasterDocument masterDoc =
SldMasterDocument.Factory.parse(masterPart.getInputStream(), DEFAULT_XML_OPTIONS);
return masterDoc.getSldMaster();
}
public PackagePart getSlidePart(CTSlideIdListEntry slide) throws IOException, XmlException {
try {
PackagePart corePart = getCorePart();
return corePart.getRelatedPart(corePart.getRelationship(slide.getId2()));
} catch(InvalidFormatException e) {
throw new XmlException(e);
}
}
@Internal
public CTSlide getSlide(CTSlideIdListEntry slide) throws IOException, XmlException {
PackagePart slidePart = getSlidePart(slide);
SldDocument slideDoc =
SldDocument.Factory.parse(slidePart.getInputStream(), DEFAULT_XML_OPTIONS);
return slideDoc.getSld();
}
public PackagePart getNodesPart(CTSlideIdListEntry parentSlide) throws IOException, XmlException {
PackageRelationshipCollection notes;
PackagePart slidePart = getSlidePart(parentSlide);
try {
notes = slidePart.getRelationshipsByType(XSLFRelation.NOTES.getRelation());
} catch(InvalidFormatException e) {
throw new IllegalStateException(e);
}
if(notes.size() == 0) {
return null;
}
if(notes.size() > 1) {
throw new IllegalStateException("Expecting 0 or 1 notes for a slide, but found " + notes.size());
}
try {
return slidePart.getRelatedPart(notes.getRelationship(0));
} catch(InvalidFormatException e) {
throw new IllegalStateException(e);
}
}
@Internal
public CTNotesSlide getNotes(CTSlideIdListEntry slide) throws IOException, XmlException {
PackagePart notesPart = getNodesPart(slide);
if(notesPart == null)
return null;
NotesDocument notesDoc =
NotesDocument.Factory.parse(notesPart.getInputStream(), DEFAULT_XML_OPTIONS);
return notesDoc.getNotes();
}
@Internal
public CTCommentList getSlideComments(CTSlideIdListEntry slide) throws IOException, XmlException {
PackageRelationshipCollection commentRels;
PackagePart slidePart = getSlidePart(slide);
try {
commentRels = slidePart.getRelationshipsByType(XSLFRelation.COMMENTS.getRelation());
} catch(InvalidFormatException e) {
throw new IllegalStateException(e);
}
if(commentRels.size() == 0) {
return null;
}
if(commentRels.size() > 1) {
throw new IllegalStateException("Expecting 0 or 1 comments for a slide, but found " + commentRels.size());
}
try {
PackagePart cPart = slidePart.getRelatedPart(
commentRels.getRelationship(0)
);
CmLstDocument commDoc =
CmLstDocument.Factory.parse(cPart.getInputStream(), DEFAULT_XML_OPTIONS);
return commDoc.getCmLst();
} catch(InvalidFormatException e) {
throw new IllegalStateException(e);
}
}
@Override
public List<PackagePart> getAllEmbeddedParts() throws OpenXML4JException {
return embedds;
}
}