You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XSLFGraphicFrame.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.usermodel;
  20. import java.awt.geom.Rectangle2D;
  21. import java.io.IOException;
  22. import javax.xml.namespace.QName;
  23. import org.apache.poi.POIXMLException;
  24. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  25. import org.apache.poi.openxml4j.opc.PackagePart;
  26. import org.apache.poi.openxml4j.opc.PackageRelationship;
  27. import org.apache.poi.sl.usermodel.GraphicalFrame;
  28. import org.apache.poi.sl.usermodel.ShapeType;
  29. import org.apache.poi.util.Beta;
  30. import org.apache.poi.util.POILogFactory;
  31. import org.apache.poi.util.POILogger;
  32. import org.apache.poi.util.Units;
  33. import org.apache.xmlbeans.XmlCursor;
  34. import org.apache.xmlbeans.XmlException;
  35. import org.apache.xmlbeans.XmlObject;
  36. import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData;
  37. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  38. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  39. import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
  40. import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
  41. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
  42. @Beta
  43. public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFShape, XSLFTextParagraph> {
  44. private static final POILogger LOG = POILogFactory.getLogger(XSLFGraphicFrame.class);
  45. /*package*/ XSLFGraphicFrame(CTGraphicalObjectFrame shape, XSLFSheet sheet){
  46. super(shape,sheet);
  47. }
  48. public ShapeType getShapeType(){
  49. throw new UnsupportedOperationException();
  50. }
  51. @Override
  52. public Rectangle2D getAnchor(){
  53. CTTransform2D xfrm = ((CTGraphicalObjectFrame)getXmlObject()).getXfrm();
  54. CTPoint2D off = xfrm.getOff();
  55. double x = Units.toPoints(off.getX());
  56. double y = Units.toPoints(off.getY());
  57. CTPositiveSize2D ext = xfrm.getExt();
  58. double cx = Units.toPoints(ext.getCx());
  59. double cy = Units.toPoints(ext.getCy());
  60. return new Rectangle2D.Double(x, y, cx, cy);
  61. }
  62. @Override
  63. public void setAnchor(Rectangle2D anchor){
  64. CTTransform2D xfrm = ((CTGraphicalObjectFrame)getXmlObject()).getXfrm();
  65. CTPoint2D off = xfrm.isSetOff() ? xfrm.getOff() : xfrm.addNewOff();
  66. long x = Units.toEMU(anchor.getX());
  67. long y = Units.toEMU(anchor.getY());
  68. off.setX(x);
  69. off.setY(y);
  70. CTPositiveSize2D ext = xfrm.isSetExt() ? xfrm.getExt() : xfrm
  71. .addNewExt();
  72. long cx = Units.toEMU(anchor.getWidth());
  73. long cy = Units.toEMU(anchor.getHeight());
  74. ext.setCx(cx);
  75. ext.setCy(cy);
  76. }
  77. static XSLFGraphicFrame create(CTGraphicalObjectFrame shape, XSLFSheet sheet){
  78. String uri = shape.getGraphic().getGraphicData().getUri();
  79. if(XSLFTable.TABLE_URI.equals(uri)){
  80. return new XSLFTable(shape, sheet);
  81. } else {
  82. return new XSLFGraphicFrame(shape, sheet);
  83. }
  84. }
  85. /**
  86. * Rotate this shape.
  87. * <p>
  88. * Positive angles are clockwise (i.e., towards the positive y axis);
  89. * negative angles are counter-clockwise (i.e., towards the negative y axis).
  90. * </p>
  91. *
  92. * @param theta the rotation angle in degrees.
  93. */
  94. @Override
  95. public void setRotation(double theta){
  96. throw new IllegalArgumentException("Operation not supported");
  97. }
  98. /**
  99. * Rotation angle in degrees
  100. * <p>
  101. * Positive angles are clockwise (i.e., towards the positive y axis);
  102. * negative angles are counter-clockwise (i.e., towards the negative y axis).
  103. * </p>
  104. *
  105. * @return rotation angle in degrees
  106. */
  107. @Override
  108. public double getRotation(){
  109. return 0;
  110. }
  111. @Override
  112. public void setFlipHorizontal(boolean flip){
  113. throw new IllegalArgumentException("Operation not supported");
  114. }
  115. @Override
  116. public void setFlipVertical(boolean flip){
  117. throw new IllegalArgumentException("Operation not supported");
  118. }
  119. /**
  120. * Whether the shape is horizontally flipped
  121. *
  122. * @return whether the shape is horizontally flipped
  123. */
  124. @Override
  125. public boolean getFlipHorizontal(){
  126. return false;
  127. }
  128. @Override
  129. public boolean getFlipVertical(){
  130. return false;
  131. }
  132. @Override
  133. void copy(XSLFShape sh){
  134. super.copy(sh);
  135. CTGraphicalObjectData data = ((CTGraphicalObjectFrame)getXmlObject()).getGraphic().getGraphicData();
  136. String uri = data.getUri();
  137. if(uri.equals("http://schemas.openxmlformats.org/drawingml/2006/diagram")){
  138. copyDiagram(data, (XSLFGraphicFrame)sh);
  139. } if(uri.equals("http://schemas.openxmlformats.org/drawingml/2006/chart")){
  140. copyChart(data, (XSLFGraphicFrame)sh);
  141. } else {
  142. // TODO support other types of objects
  143. }
  144. }
  145. private void copyChart(CTGraphicalObjectData objData, XSLFGraphicFrame srcShape) {
  146. XSLFSlide slide = (XSLFSlide) getSheet();
  147. XSLFSheet src = srcShape.getSheet();
  148. String xpath = "declare namespace c='http://schemas.openxmlformats.org/drawingml/2006/chart' c:chart";
  149. XmlObject[] obj = objData.selectPath(xpath);
  150. if (obj != null && obj.length == 1) {
  151. XmlCursor c = obj[0].newCursor();
  152. try {
  153. // duplicate chart with embedded workbook
  154. QName idQualifiedName = new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "id");
  155. String id = c.getAttributeText(idQualifiedName);
  156. XSLFChart srcChart = (XSLFChart) src.getRelationById(id);
  157. XSLFChart chartCopy = slide.getSlideShow().createChart(slide);
  158. chartCopy.importContent(srcChart);
  159. chartCopy.saveWorkbook(srcChart.getWorkbook());
  160. c.setAttributeText(idQualifiedName, slide.getRelationId(chartCopy));
  161. } catch (InvalidFormatException e) {
  162. throw new POIXMLException(e);
  163. } catch (IOException e) {
  164. throw new POIXMLException(e);
  165. }
  166. c.dispose();
  167. }
  168. }
  169. // TODO should be moved to a sub-class
  170. private void copyDiagram(CTGraphicalObjectData objData, XSLFGraphicFrame srcShape){
  171. String xpath = "declare namespace dgm='http://schemas.openxmlformats.org/drawingml/2006/diagram' $this//dgm:relIds";
  172. XmlObject[] obj = objData.selectPath(xpath);
  173. if(obj != null && obj.length == 1){
  174. XmlCursor c = obj[0].newCursor();
  175. XSLFSheet sheet = srcShape.getSheet();
  176. try {
  177. String dm = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "dm"));
  178. PackageRelationship dmRel = sheet.getPackagePart().getRelationship(dm);
  179. PackagePart dmPart = sheet.getPackagePart().getRelatedPart(dmRel);
  180. getSheet().importPart(dmRel, dmPart);
  181. String lo = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "lo"));
  182. PackageRelationship loRel = sheet.getPackagePart().getRelationship(lo);
  183. PackagePart loPart = sheet.getPackagePart().getRelatedPart(loRel);
  184. getSheet().importPart(loRel, loPart);
  185. String qs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "qs"));
  186. PackageRelationship qsRel = sheet.getPackagePart().getRelationship(qs);
  187. PackagePart qsPart = sheet.getPackagePart().getRelatedPart(qsRel);
  188. getSheet().importPart(qsRel, qsPart);
  189. String cs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "cs"));
  190. PackageRelationship csRel = sheet.getPackagePart().getRelationship(cs);
  191. PackagePart csPart = sheet.getPackagePart().getRelatedPart(csRel);
  192. getSheet().importPart(csRel, csPart);
  193. } catch (InvalidFormatException e){
  194. throw new POIXMLException(e);
  195. }
  196. c.dispose();
  197. }
  198. }
  199. @Override
  200. public XSLFPictureShape getFallbackPicture() {
  201. String xquery =
  202. "declare namespace p='http://schemas.openxmlformats.org/presentationml/2006/main'; "
  203. + "declare namespace mc='http://schemas.openxmlformats.org/markup-compatibility/2006' "
  204. + ".//mc:Fallback/*/p:pic"
  205. ;
  206. XmlObject xo = selectProperty(XmlObject.class, xquery);
  207. if (xo == null) {
  208. return null;
  209. }
  210. CTGroupShape gs;
  211. try {
  212. gs = CTGroupShape.Factory.parse(xo.newDomNode());
  213. } catch (XmlException e) {
  214. LOG.log(POILogger.WARN, "Can't parse fallback picture stream of graphical frame", e);
  215. return null;
  216. }
  217. if (gs.sizeOfPicArray() == 0) {
  218. return null;
  219. }
  220. return new XSLFPictureShape(gs.getPicArray(0), getSheet());
  221. }
  222. }