Author: yegor Date: Tue Jan 22 13:37:14 2013 New Revision: 1436913 URL: http://svn.apache.org/viewvc?rev=1436913&view=rev Log: Bugzilla 54407 - Avoid XmlValueDisconnectedException when merging slides
Modified: poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGraphicFrame.java poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java Modified: poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGraphicFrame.java URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGraphicFrame.java?rev=1436913&r1=1436912&r2=1436913&view=diff ============================================================================== --- poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGraphicFrame.java (original) +++ poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFGraphicFrame.java Tue Jan 22 13:37:14 2013 @@ -19,13 +19,21 @@ package org.apache.poi.xslf.usermodel; +import org.apache.poi.POIXMLException; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.openxml4j.opc.PackagePart; +import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.util.Beta; import org.apache.poi.util.Units; +import org.apache.xmlbeans.XmlCursor; +import org.apache.xmlbeans.XmlObject; +import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData; import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D; import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D; import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D; import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame; +import javax.xml.namespace.QName; import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; @@ -151,5 +159,54 @@ public class XSLFGraphicFrame extends XS } + @Override + void copy(XSLFShape sh){ + super.copy(sh); + + CTGraphicalObjectData data = _shape.getGraphic().getGraphicData(); + String uri = data.getUri(); + if(uri.equals("http://schemas.openxmlformats.org/drawingml/2006/diagram")){ + copyDiagram(data, (XSLFGraphicFrame)sh); + } else { + // TODO support other types of objects + + } + } + + // TODO should be moved to a sub-class + private void copyDiagram(CTGraphicalObjectData objData, XSLFGraphicFrame srcShape){ + String xpath = "declare namespace dgm='http://schemas.openxmlformats.org/drawingml/2006/diagram' $this//dgm:relIds"; + XmlObject[] obj = objData.selectPath(xpath); + if(obj != null && obj.length == 1){ + XmlCursor c = obj[0].newCursor(); + + XSLFSheet sheet = srcShape.getSheet(); + try { + String dm = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "dm")); + PackageRelationship dmRel = sheet.getPackagePart().getRelationship(dm); + PackagePart dmPart = sheet.getPackagePart().getRelatedPart(dmRel); + _sheet.importPart(dmRel, dmPart); + + String lo = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "lo")); + PackageRelationship loRel = sheet.getPackagePart().getRelationship(lo); + PackagePart loPart = sheet.getPackagePart().getRelatedPart(loRel); + _sheet.importPart(loRel, loPart); + + String qs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "qs")); + PackageRelationship qsRel = sheet.getPackagePart().getRelationship(qs); + PackagePart qsPart = sheet.getPackagePart().getRelatedPart(qsRel); + _sheet.importPart(qsRel, qsPart); + + String cs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "cs")); + PackageRelationship csRel = sheet.getPackagePart().getRelationship(cs); + PackagePart csPart = sheet.getPackagePart().getRelatedPart(csRel); + _sheet.importPart(csRel, csPart); + + } catch (InvalidFormatException e){ + throw new POIXMLException(e); + } + c.dispose(); + } + } } \ No newline at end of file Modified: poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java?rev=1436913&r1=1436912&r2=1436913&view=diff ============================================================================== --- poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java (original) +++ poi/trunk/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java Tue Jan 22 13:37:14 2013 @@ -18,11 +18,13 @@ package org.apache.poi.xslf.usermodel; import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.POIXMLException; +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.TargetMode; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.util.Beta; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.Internal; import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlOptions; @@ -39,6 +41,7 @@ import javax.xml.namespace.QName; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; @@ -297,6 +300,8 @@ public abstract class XSLFSheet extends _spTree = null; _drawing = null; _spTree = null; + _placeholders = null; + // first copy the source xml getSpTree().set(src.getSpTree()); @@ -534,4 +539,30 @@ public abstract class XSLFSheet extends return rel.getId(); } + + /** + * Import a package part into this sheet. + */ + PackagePart importPart(PackageRelationship srcRel, PackagePart srcPafrt) { + + OPCPackage pkg = getPackagePart().getPackage(); + if(!pkg.containPart(srcPafrt.getPartName())){ + PackageRelationship rel = getPackagePart().addRelationship( + srcPafrt.getPartName(), TargetMode.INTERNAL, srcRel.getRelationshipType()); + + PackagePart part = pkg.createPart(srcPafrt.getPartName(), 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; + } else { + // already exists + return pkg.getPart(srcPafrt.getPartName()); + } + } } \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@poi.apache.org For additional commands, e-mail: commits-h...@poi.apache.org