123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545 |
- /* ====================================================================
- 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 java.awt.Graphics2D;
- import java.io.*;
- import java.util.*;
- import java.util.regex.Pattern;
-
- import javax.xml.namespace.QName;
-
- import org.apache.poi.POIXMLDocumentPart;
- import org.apache.poi.POIXMLException;
- import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
- import org.apache.poi.openxml4j.opc.*;
- import org.apache.poi.sl.draw.DrawFactory;
- import org.apache.poi.sl.draw.Drawable;
- import org.apache.poi.sl.usermodel.Sheet;
- import org.apache.poi.util.*;
- 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.*;
-
- @Beta
- public abstract class XSLFSheet extends POIXMLDocumentPart implements XSLFShapeContainer, Sheet<XSLFShape, XMLSlideShow> {
- private XSLFCommonSlideData _commonSlideData;
- private XSLFDrawing _drawing;
- private List<XSLFShape> _shapes;
- private CTGroupShape _spTree;
-
- private List<XSLFTextShape>_placeholders;
- private Map<Integer, XSLFSimpleShape> _placeholderByIdMap;
- private Map<Integer, XSLFSimpleShape> _placeholderByTypeMap;
-
- public XSLFSheet() {
- super();
- }
-
- public XSLFSheet(PackagePart part, PackageRelationship rel){
- super(part, rel);
- }
-
- /**
- *
- * @return the XMLSlideShow this sheet belongs to
- */
- public XMLSlideShow getSlideShow() {
- POIXMLDocumentPart p = getParent();
- while(p != null) {
- if(p instanceof XMLSlideShow){
- return (XMLSlideShow)p;
- }
- p = p.getParent();
- }
- throw new IllegalStateException("SlideShow was not found");
- }
-
- protected List<XSLFShape> buildShapes(CTGroupShape spTree){
- List<XSLFShape> shapes = new ArrayList<XSLFShape>();
- for(XmlObject ch : spTree.selectPath("*")){
- if(ch instanceof CTShape){ // simple shape
- XSLFAutoShape shape = XSLFAutoShape.create((CTShape)ch, this);
- shapes.add(shape);
- } else if (ch instanceof CTGroupShape){
- shapes.add(new XSLFGroupShape((CTGroupShape)ch, this));
- } else if (ch instanceof CTConnector){
- shapes.add(new XSLFConnectorShape((CTConnector)ch, this));
- } else if (ch instanceof CTPicture){
- shapes.add(new XSLFPictureShape((CTPicture)ch, this));
- } else if (ch instanceof CTGraphicalObjectFrame){
- XSLFGraphicFrame shape = XSLFGraphicFrame.create((CTGraphicalObjectFrame)ch, this);
- shapes.add(shape);
- }
- }
- return shapes;
- }
-
- /**
- * @return top-level Xml bean representing this sheet
- */
- public abstract XmlObject getXmlObject();
-
- @Internal
- 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) {
- _drawing = new XSLFDrawing(this, getSpTree());
- }
- return _drawing;
- }
-
- private List<XSLFShape> getShapeList(){
- if(_shapes == null){
- _shapes = buildShapes(getSpTree());
- }
- return _shapes;
- }
-
- // shape factory methods
-
- public XSLFAutoShape createAutoShape(){
- List<XSLFShape> shapes = getShapeList();
- XSLFAutoShape sh = getDrawing().createAutoShape();
- shapes.add(sh);
- sh.setParent(this);
- return sh;
- }
-
- public XSLFFreeformShape createFreeform(){
- List<XSLFShape> shapes = getShapeList();
- XSLFFreeformShape sh = getDrawing().createFreeform();
- shapes.add(sh);
- sh.setParent(this);
- return sh;
- }
-
- public XSLFTextBox createTextBox(){
- List<XSLFShape> shapes = getShapeList();
- XSLFTextBox sh = getDrawing().createTextBox();
- shapes.add(sh);
- sh.setParent(this);
- return sh;
- }
-
- public XSLFConnectorShape createConnector(){
- List<XSLFShape> shapes = getShapeList();
- XSLFConnectorShape sh = getDrawing().createConnector();
- shapes.add(sh);
- sh.setParent(this);
- return sh;
- }
-
- public XSLFGroupShape createGroup(){
- List<XSLFShape> shapes = getShapeList();
- XSLFGroupShape sh = getDrawing().createGroup();
- shapes.add(sh);
- sh.setParent(this);
- return sh;
- }
-
- public XSLFPictureShape createPicture(int pictureIndex){
- List<PackagePart> pics = getPackagePart().getPackage()
- .getPartsByName(Pattern.compile("/ppt/media/image" + (pictureIndex + 1) + ".*?"));
-
- if(pics.size() == 0) {
- throw new IllegalArgumentException("Picture with index=" + pictureIndex + " was not found");
- }
-
- PackagePart pic = pics.get(0);
-
- PackageRelationship rel = getPackagePart().addRelationship(
- pic.getPartName(), TargetMode.INTERNAL, XSLFRelation.IMAGES.getRelation());
- addRelation(rel.getId(), new XSLFPictureData(pic, rel));
-
- XSLFPictureShape sh = getDrawing().createPicture(rel.getId());
- sh.resize();
-
- getShapeList().add(sh);
- sh.setParent(this);
- return sh;
- }
-
- public XSLFTable createTable(){
- List<XSLFShape> shapes = getShapeList();
- XSLFTable sh = getDrawing().createTable();
- shapes.add(sh);
- sh.setParent(this);
- return sh;
- }
-
- /**
- * Returns an array containing all of the shapes in this sheet
- *
- * @return an array of all shapes in this sheet
- */
- public List<XSLFShape> getShapes(){
- return getShapeList();
- }
-
- /**
- * Returns an iterator over the shapes in this sheet
- *
- * @return an iterator over the shapes in this sheet
- */
- public Iterator<XSLFShape> iterator(){
- return getShapeList().iterator();
- }
-
- public void addShape(XSLFShape shape) {
- throw new UnsupportedOperationException(
- "Adding a shape from a different container is not supported -"
- + " create it from scratch witht XSLFSheet.create* methods");
- }
-
- /**
- * Removes the specified shape from this sheet, if it is present
- * (optional operation). If this sheet does not contain the element,
- * it is unchanged.
- *
- * @param xShape shape to be removed from this sheet, if present
- * @return <tt>true</tt> if this sheet contained the specified element
- * @throws IllegalArgumentException if the type of the specified shape
- * is incompatible with this sheet (optional)
- */
- public boolean removeShape(XSLFShape xShape) {
- XmlObject obj = xShape.getXmlObject();
- CTGroupShape spTree = getSpTree();
- if(obj instanceof CTShape){
- spTree.getSpList().remove(obj);
- } else if (obj instanceof CTGroupShape){
- spTree.getGrpSpList().remove(obj);
- } else if (obj instanceof CTConnector){
- spTree.getCxnSpList().remove(obj);
- } else {
- throw new IllegalArgumentException("Unsupported shape: " + xShape);
- }
- return getShapeList().remove(xShape);
- }
-
- /**
- * Removes all of the elements from this container (optional operation).
- * The container will be empty after this call returns.
- */
- public void clear() {
- for(XSLFShape shape : getShapes()){
- removeShape(shape);
- }
- }
-
- protected abstract String getRootElementName();
-
- protected CTGroupShape getSpTree(){
- if(_spTree == null) {
- XmlObject root = getXmlObject();
- XmlObject[] sp = root.selectPath(
- "declare namespace p='http://schemas.openxmlformats.org/presentationml/2006/main' .//*/p:spTree");
- if(sp.length == 0) throw new IllegalStateException("CTGroupShape was not found");
- _spTree = (CTGroupShape)sp[0];
- }
- return _spTree;
- }
-
- protected final void commit() throws IOException {
- XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
-
- Map<String, String> map = new HashMap<String, String>();
- map.put(STRelationshipId.type.getName().getNamespaceURI(), "r");
- map.put("http://schemas.openxmlformats.org/drawingml/2006/main", "a");
- map.put("http://schemas.openxmlformats.org/presentationml/2006/main", "p");
- xmlOptions.setSaveSuggestedPrefixes(map);
- String docName = getRootElementName();
- if(docName != null) {
- xmlOptions.setSaveSyntheticDocumentElement(
- new QName("http://schemas.openxmlformats.org/presentationml/2006/main", docName));
- }
-
- PackagePart part = getPackagePart();
- OutputStream out = part.getOutputStream();
- getXmlObject().save(out, xmlOptions);
- out.close();
- }
-
- /**
- * Set the contents of this sheet to be a copy of the source sheet.
- * This method erases any existing shapes and replaces them with
- * object from the source sheet.
- *
- * @param src the source sheet to copy data from
- * @return modified 'this'
- */
- public XSLFSheet importContent(XSLFSheet src){
- _shapes = null;
- _spTree = null;
- _drawing = null;
- _spTree = null;
- _placeholders = null;
-
- // fix-me: wth would this ever happen to work ...
-
-
- // first copy the source xml
- getSpTree().set(src.getSpTree());
-
- // recursively update each shape
- List<XSLFShape> tgtShapes = getShapeList();
- List<XSLFShape> srcShapes = src.getShapeList();
- for(int i = 0; i < tgtShapes.size(); i++){
- XSLFShape s1 = srcShapes.get(i);
- XSLFShape s2 = tgtShapes.get(i);
-
- s2.copy(s1);
- }
- return this;
- }
-
- /**
- * Append content to this sheet.
- *
- * @param src the source sheet
- * @return modified <code>this</code>.
- */
- public XSLFSheet appendContent(XSLFSheet src){
- CTGroupShape spTree = getSpTree();
- int numShapes = getShapeList().size();
-
- CTGroupShape srcTree = src.getSpTree();
- for(XmlObject ch : srcTree.selectPath("*")){
- if(ch instanceof CTShape){ // simple shape
- spTree.addNewSp().set(ch);
- } else if (ch instanceof CTGroupShape){
- spTree.addNewGrpSp().set(ch);
- } else if (ch instanceof CTConnector){
- spTree.addNewCxnSp().set(ch);
- } else if (ch instanceof CTPicture){
- spTree.addNewPic().set(ch);
- } else if (ch instanceof CTGraphicalObjectFrame){
- spTree.addNewGraphicFrame().set(ch);
- }
- }
-
- _shapes = null;
- _spTree = null;
- _drawing = null;
- _spTree = null;
- _placeholders = null;
-
- // recursively update each shape
- List<XSLFShape> tgtShapes = getShapeList();
- List<XSLFShape> srcShapes = src.getShapeList();
- for(int i = 0; i < srcShapes.size(); i++){
- XSLFShape s1 = srcShapes.get(i);
- XSLFShape s2 = tgtShapes.get(numShapes + i);
-
- s2.copy(s1);
- }
- return this;
- }
-
- /**
- * @return theme (shared styles) associated with this theme.
- * By default returns <code>null</code> which means that this sheet is theme-less.
- * Sheets that support the notion of themes (slides, masters, layouts, etc.) should override this
- * method and return the corresponding package part.
- */
- XSLFTheme getTheme(){
- return null;
- }
-
- protected XSLFTextShape getTextShapeByType(Placeholder type){
- for(XSLFShape shape : this.getShapes()){
- if(shape instanceof XSLFTextShape) {
- XSLFTextShape txt = (XSLFTextShape)shape;
- if(txt.getTextType() == type) {
- return txt;
- }
- }
- }
- return null;
- }
-
- XSLFSimpleShape getPlaceholder(CTPlaceholder ph) {
- XSLFSimpleShape shape = null;
- if(ph.isSetIdx()) shape = getPlaceholderById((int)ph.getIdx());
-
- if (shape == null && ph.isSetType()) {
- shape = getPlaceholderByType(ph.getType().intValue());
- }
- return shape;
- }
-
- void initPlaceholders() {
- if(_placeholders == null) {
- _placeholders = new ArrayList<XSLFTextShape>();
- _placeholderByIdMap = new HashMap<Integer, XSLFSimpleShape>();
- _placeholderByTypeMap = new HashMap<Integer, XSLFSimpleShape>();
-
- for(XSLFShape sh : getShapes()){
- if(sh instanceof XSLFTextShape){
- XSLFTextShape sShape = (XSLFTextShape)sh;
- CTPlaceholder ph = sShape.getCTPlaceholder();
- if(ph != null) {
- _placeholders.add(sShape);
- if(ph.isSetIdx()) {
- int idx = (int)ph.getIdx();
- _placeholderByIdMap.put(idx, sShape);
- }
- if(ph.isSetType()){
- _placeholderByTypeMap.put(ph.getType().intValue(), sShape);
- }
- }
- }
- }
- }
- }
-
- XSLFSimpleShape getPlaceholderById(int id) {
- initPlaceholders();
- return _placeholderByIdMap.get(id);
- }
-
- XSLFSimpleShape getPlaceholderByType(int ordinal) {
- initPlaceholders();
- return _placeholderByTypeMap.get(ordinal);
- }
-
- /**
- *
- * @param idx 0-based index of a placeholder in the sheet
- * @return placeholder
- */
- public XSLFTextShape getPlaceholder(int idx) {
- initPlaceholders();
- return _placeholders.get(idx);
- }
-
- /**
- *
- * @return all placeholder shapes in this sheet
- */
- public XSLFTextShape[] getPlaceholders() {
- initPlaceholders();
- return _placeholders.toArray(new XSLFTextShape[_placeholders.size()]);
- }
-
- /**
- * Checks if this <code>sheet</code> displays the specified shape.
- *
- * Subclasses can override it and skip certain shapes from drawings,
- * for instance, slide masters and layouts don't display placeholders
- */
- protected boolean canDraw(XSLFShape shape){
- return true;
- }
-
- /**
- *
- * @return whether shapes on the master sheet should be shown. By default master graphics is turned off.
- * Sheets that support the notion of master (slide, slideLayout) should override it and
- * check this setting in the sheet XML
- */
- public boolean getFollowMasterGraphics(){
- return false;
- }
-
- /**
- *
- * @return background for this sheet
- */
- public XSLFBackground getBackground() {
- return null;
- }
-
- /**
- * Render this sheet into the supplied graphics object
- *
- * @param graphics
- */
- @Override
- public void draw(Graphics2D graphics){
- DrawFactory drawFact = DrawFactory.getInstance(graphics);
- Drawable draw = drawFact.getDrawable(this);
- draw.draw(graphics);
- }
-
- /**
- * Import a picture data from another document.
- *
- * @param blipId ID of the package relationship to retrieve.
- * @param packagePart package part containing the data to import
- * @return ID of the created relationship
- */
- String importBlip(String blipId, PackagePart packagePart) {
- PackageRelationship blipRel = packagePart.getRelationship(blipId);
- PackagePart blipPart;
- try {
- blipPart = packagePart.getRelatedPart(blipRel);
- } catch (InvalidFormatException e){
- throw new POIXMLException(e);
- }
- XSLFPictureData data = new XSLFPictureData(blipPart, null);
-
- XMLSlideShow ppt = getSlideShow();
- int pictureIdx = ppt.addPicture(data.getData(), data.getPictureType());
- PackagePart pic = ppt.getAllPictures().get(pictureIdx).getPackagePart();
-
- PackageRelationship rel = getPackagePart().addRelationship(
- pic.getPartName(), TargetMode.INTERNAL, blipRel.getRelationshipType());
- addRelation(rel.getId(), new XSLFPictureData(pic, rel));
-
- return rel.getId();
- }
-
- /**
- * Import a package part into this sheet.
- */
- PackagePart importPart(PackageRelationship srcRel, PackagePart srcPafrt) {
- PackagePart destPP = getPackagePart();
- PackagePartName srcPPName = srcPafrt.getPartName();
-
- OPCPackage pkg = destPP.getPackage();
- if(pkg.containPart(srcPPName)){
- // already exists
- return pkg.getPart(srcPPName);
- }
-
- destPP.addRelationship(srcPPName, TargetMode.INTERNAL, srcRel.getRelationshipType());
-
- PackagePart part = pkg.createPart(srcPPName, srcPafrt.getContentType());
- OutputStream out = part.getOutputStream();
- try {
- InputStream is = srcPafrt.getInputStream();
- IOUtils.copy(is, out);
- out.close();
- } catch (IOException e){
- throw new POIXMLException(e);
- }
- return part;
- }
- }
|