diff options
author | Yegor Kozlov <yegor@apache.org> | 2011-12-07 08:49:09 +0000 |
---|---|---|
committer | Yegor Kozlov <yegor@apache.org> | 2011-12-07 08:49:09 +0000 |
commit | fc4c8d077ecb24cb6702f1b7792adb3d40455459 (patch) | |
tree | d548bb5a831e562b4dadbb04ba8cd9c88336e4c8 /src/ooxml/java/org | |
parent | 6ea60a0623f99bfe3e836ac2a993ec43a83aa82a (diff) | |
download | poi-fc4c8d077ecb24cb6702f1b7792adb3d40455459.tar.gz poi-fc4c8d077ecb24cb6702f1b7792adb3d40455459.zip |
Bugzilla 52268 - support cloning sheets with drawings in XSSF
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1211339 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/java/org')
3 files changed, 68 insertions, 31 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java index 0c13db8ffc..72794b6eff 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java @@ -36,15 +36,10 @@ import org.apache.poi.ss.usermodel.Drawing; import org.apache.poi.util.Internal; import org.apache.poi.xssf.model.CommentsTable; import org.apache.xmlbeans.XmlException; +import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlOptions; -import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTConnector; -import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing; -import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGraphicalObjectFrame; -import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGroupShape; -import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture; -import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape; -import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor; -import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.STEditAs; +import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps; +import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.*; import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId; /** @@ -57,7 +52,6 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing { * Root element of the SpreadsheetML Drawing part */ private CTDrawing drawing; - private boolean isNew; private long numOfGraphicFrames = 0L; protected static final String NAMESPACE_A = "http://schemas.openxmlformats.org/drawingml/2006/main"; @@ -71,7 +65,6 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing { protected XSSFDrawing() { super(); drawing = newDrawing(); - isNew = true; } /** @@ -84,8 +77,10 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing { */ protected XSSFDrawing(PackagePart part, PackageRelationship rel) throws IOException, XmlException { super(part, rel); - drawing = CTDrawing.Factory.parse(part.getInputStream()); - isNew = false; + XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS); + //Removing root element + options.setLoadReplaceDocumentElement(null); + drawing = CTDrawing.Factory.parse(part.getInputStream(),options); } /** @@ -117,13 +112,9 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing { xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"> */ - if(isNew) { - // Have it wrapped in a <xdr:wsDr> tag - xmlOptions.setSaveSyntheticDocumentElement( - new QName(CTDrawing.type.getName().getNamespaceURI(), "wsDr", "xdr") - ); - isNew = false; - } + xmlOptions.setSaveSyntheticDocumentElement( + new QName(CTDrawing.type.getName().getNamespaceURI(), "wsDr", "xdr") + ); Map<String, String> map = new HashMap<String, String>(); map.put(NAMESPACE_A, "a"); map.put(STRelationshipId.type.getName().getNamespaceURI(), "r"); @@ -372,4 +363,20 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing { private long newShapeId(){ return drawing.sizeOfTwoCellAnchorArray() + 1; } + + /** + * + * @return list of shapes in this drawing + */ + public List<XSSFShape> getShapes(){ + List<XSSFShape> lst = new ArrayList<XSSFShape>(); + for(XmlObject obj : drawing.selectPath("./*/*")) { + if(obj instanceof CTPicture) lst.add(new XSSFPicture(this, (CTPicture)obj)) ; + else if(obj instanceof CTConnector) lst.add(new XSSFConnector(this, (CTConnector)obj)) ; + else if(obj instanceof CTShape) lst.add(new XSSFSimpleShape(this, (CTShape)obj)) ; + else if(obj instanceof CTGraphicalObjectFrame) lst.add(new XSSFGraphicFrame(this, (CTGraphicalObjectFrame)obj)) ; + else if(obj instanceof CTGroupShape) lst.add(new XSSFShapeGroup(this, (CTGroupShape)obj)) ; + } + return lst; + } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFGraphicFrame.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFGraphicFrame.java index 3abc774f86..d22802ab2a 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFGraphicFrame.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFGraphicFrame.java @@ -25,14 +25,9 @@ import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.util.Internal; import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlCursor; +import org.openxmlformats.schemas.drawingml.x2006.main.*; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGraphicalObjectFrame; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGraphicalObjectFrameNonVisual; -import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject; -import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData; -import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps; -import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D; -import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D; -import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D; import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId; /** @@ -40,7 +35,7 @@ import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelations * * @author Roman Kashitsyn */ -public final class XSSFGraphicFrame { +public final class XSSFGraphicFrame extends XSSFShape { private static CTGraphicalObjectFrame prototype = null; @@ -186,4 +181,8 @@ public final class XSSFGraphicFrame { data.setUri(c_namespaceUri); } + @Override + protected CTShapeProperties getShapeProperties(){ + return null; + } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 8011281787..896be63bd0 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -375,25 +375,56 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X throw new POIXMLException("Failed to clone sheet", e); } CTWorksheet ct = clonedSheet.getCTWorksheet(); - if(ct.isSetDrawing()) { - logger.log(POILogger.WARN, "Cloning sheets with drawings is not yet supported."); - ct.unsetDrawing(); - } if(ct.isSetLegacyDrawing()) { logger.log(POILogger.WARN, "Cloning sheets with comments is not yet supported."); ct.unsetLegacyDrawing(); } + if (ct.isSetPageSetup()) { + logger.log(POILogger.WARN, "Cloning sheets with page setup is not yet supported."); + ct.unsetPageSetup(); + } clonedSheet.setSelected(false); // copy sheet's relations List<POIXMLDocumentPart> rels = srcSheet.getRelations(); + // if the sheet being cloned has a drawing then rememebr it and re-create tpoo + XSSFDrawing dg = null; for(POIXMLDocumentPart r : rels) { + // do not copy the drawing relationship, it will be re-created + if(r instanceof XSSFDrawing) { + dg = (XSSFDrawing)r; + continue; + } + PackageRelationship rel = r.getPackageRelationship(); - clonedSheet.getPackagePart().addRelationship(rel.getTargetURI(), rel.getTargetMode(),rel.getRelationshipType()); + clonedSheet.getPackagePart().addRelationship( + rel.getTargetURI(), rel.getTargetMode(),rel.getRelationshipType()); clonedSheet.addRelation(rel.getId(), r); } + // clone the sheet drawing alongs with its relationships + if (dg != null) { + if(ct.isSetDrawing()) { + // unset the existing reference to the drawing, + // so that subsequent call of clonedSheet.createDrawingPatriarch() will create a new one + ct.unsetDrawing(); + } + XSSFDrawing clonedDg = clonedSheet.createDrawingPatriarch(); + // copy drawing contents + clonedDg.getCTDrawing().set(dg.getCTDrawing()); + + // Clone drawing relations + List<POIXMLDocumentPart> srcRels = srcSheet.createDrawingPatriarch().getRelations(); + for (POIXMLDocumentPart rel : srcRels) { + PackageRelationship relation = rel.getPackageRelationship(); + clonedSheet + .createDrawingPatriarch() + .getPackagePart() + .addRelationship(relation.getTargetURI(), relation.getTargetMode(), + relation.getRelationshipType(), relation.getId()); + } + } return clonedSheet; } |