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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 static org.apache.poi.openxml4j.opc.PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS;
  21. import java.awt.geom.Rectangle2D;
  22. import java.io.IOException;
  23. import javax.xml.namespace.QName;
  24. import org.apache.logging.log4j.LogManager;
  25. import org.apache.logging.log4j.Logger;
  26. import org.apache.poi.ooxml.POIXMLException;
  27. import org.apache.poi.ooxml.util.POIXMLUnits;
  28. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  29. import org.apache.poi.openxml4j.opc.PackagePart;
  30. import org.apache.poi.openxml4j.opc.PackageRelationship;
  31. import org.apache.poi.sl.usermodel.GraphicalFrame;
  32. import org.apache.poi.sl.usermodel.ShapeType;
  33. import org.apache.poi.util.Beta;
  34. import org.apache.poi.util.Units;
  35. import org.apache.xmlbeans.XmlCursor;
  36. import org.apache.xmlbeans.XmlException;
  37. import org.apache.xmlbeans.XmlObject;
  38. import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
  39. import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData;
  40. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  41. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  42. import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
  43. import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
  44. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
  45. @Beta
  46. public class XSLFGraphicFrame extends XSLFShape implements GraphicalFrame<XSLFShape, XSLFTextParagraph> {
  47. private static final String DRAWINGML_CHART_URI = "http://schemas.openxmlformats.org/drawingml/2006/chart";
  48. private static final String DRAWINGML_DIAGRAM_URI = "http://schemas.openxmlformats.org/drawingml/2006/diagram";
  49. private static final Logger LOG = LogManager.getLogger(XSLFGraphicFrame.class);
  50. /*package*/ XSLFGraphicFrame(CTGraphicalObjectFrame shape, XSLFSheet sheet){
  51. super(shape,sheet);
  52. }
  53. public ShapeType getShapeType(){
  54. throw new UnsupportedOperationException();
  55. }
  56. @Override
  57. public Rectangle2D getAnchor(){
  58. CTTransform2D xfrm = ((CTGraphicalObjectFrame)getXmlObject()).getXfrm();
  59. CTPoint2D off = xfrm.getOff();
  60. double x = Units.toPoints(POIXMLUnits.parseLength(off.xgetX()));
  61. double y = Units.toPoints(POIXMLUnits.parseLength(off.xgetY()));
  62. CTPositiveSize2D ext = xfrm.getExt();
  63. double cx = Units.toPoints(ext.getCx());
  64. double cy = Units.toPoints(ext.getCy());
  65. return new Rectangle2D.Double(x, y, cx, cy);
  66. }
  67. @Override
  68. public void setAnchor(Rectangle2D anchor){
  69. CTTransform2D xfrm = ((CTGraphicalObjectFrame)getXmlObject()).getXfrm();
  70. CTPoint2D off = xfrm.isSetOff() ? xfrm.getOff() : xfrm.addNewOff();
  71. long x = Units.toEMU(anchor.getX());
  72. long y = Units.toEMU(anchor.getY());
  73. off.setX(x);
  74. off.setY(y);
  75. CTPositiveSize2D ext = xfrm.isSetExt() ? xfrm.getExt() : xfrm
  76. .addNewExt();
  77. long cx = Units.toEMU(anchor.getWidth());
  78. long cy = Units.toEMU(anchor.getHeight());
  79. ext.setCx(cx);
  80. ext.setCy(cy);
  81. }
  82. static XSLFGraphicFrame create(CTGraphicalObjectFrame shape, XSLFSheet sheet){
  83. final String uri = getUri(shape);
  84. switch (uri == null ? "" : uri) {
  85. case XSLFTable.TABLE_URI:
  86. return new XSLFTable(shape, sheet);
  87. case XSLFObjectShape.OLE_URI:
  88. return new XSLFObjectShape(shape, sheet);
  89. default:
  90. return new XSLFGraphicFrame(shape, sheet);
  91. }
  92. }
  93. private static String getUri(CTGraphicalObjectFrame shape) {
  94. final CTGraphicalObject g = shape.getGraphic();
  95. if (g == null) {
  96. return null;
  97. }
  98. CTGraphicalObjectData gd = g.getGraphicData();
  99. return (gd == null) ? null : gd.getUri();
  100. }
  101. /**
  102. * Rotate this shape.
  103. * <p>
  104. * Positive angles are clockwise (i.e., towards the positive y axis);
  105. * negative angles are counter-clockwise (i.e., towards the negative y axis).
  106. * </p>
  107. *
  108. * @param theta the rotation angle in degrees.
  109. */
  110. @Override
  111. public void setRotation(double theta){
  112. throw new IllegalArgumentException("Operation not supported");
  113. }
  114. /**
  115. * Rotation angle in degrees
  116. * <p>
  117. * Positive angles are clockwise (i.e., towards the positive y axis);
  118. * negative angles are counter-clockwise (i.e., towards the negative y axis).
  119. * </p>
  120. *
  121. * @return rotation angle in degrees
  122. */
  123. @Override
  124. public double getRotation(){
  125. return 0;
  126. }
  127. @Override
  128. public void setFlipHorizontal(boolean flip){
  129. throw new IllegalArgumentException("Operation not supported");
  130. }
  131. @Override
  132. public void setFlipVertical(boolean flip){
  133. throw new IllegalArgumentException("Operation not supported");
  134. }
  135. /**
  136. * Whether the shape is horizontally flipped
  137. *
  138. * @return whether the shape is horizontally flipped
  139. */
  140. @Override
  141. public boolean getFlipHorizontal(){
  142. return false;
  143. }
  144. @Override
  145. public boolean getFlipVertical(){
  146. return false;
  147. }
  148. public boolean hasChart() {
  149. String uri = getGraphicalData().getUri();
  150. return uri.equals(DRAWINGML_CHART_URI);
  151. }
  152. /**
  153. * @since POI 5.2.0
  154. */
  155. public boolean hasDiagram() {
  156. String uri = getGraphicalData().getUri();
  157. return uri.equals(DRAWINGML_DIAGRAM_URI);
  158. }
  159. private CTGraphicalObjectData getGraphicalData() {
  160. return ((CTGraphicalObjectFrame)getXmlObject()).getGraphic().getGraphicData();
  161. }
  162. public XSLFChart getChart() {
  163. if (hasChart()) {
  164. String id = null;
  165. String xpath = "declare namespace c='" + DRAWINGML_CHART_URI + "' c:chart";
  166. XmlObject[] obj = getGraphicalData().selectPath(xpath);
  167. if (obj != null && obj.length == 1) {
  168. XmlCursor c = obj[0].newCursor();
  169. QName idQualifiedName = new QName(CORE_PROPERTIES_ECMA376_NS, "id");
  170. id = c.getAttributeText(idQualifiedName);
  171. c.dispose();
  172. }
  173. if (id == null) {
  174. return null;
  175. } else {
  176. return (XSLFChart) getSheet().getRelationById(id);
  177. }
  178. } else {
  179. return null;
  180. }
  181. }
  182. @Override
  183. void copy(XSLFShape sh){
  184. super.copy(sh);
  185. CTGraphicalObjectData data = getGraphicalData();
  186. String uri = data.getUri();
  187. if(uri.equals(DRAWINGML_DIAGRAM_URI)){
  188. copyDiagram(data, (XSLFGraphicFrame)sh);
  189. } if(uri.equals(DRAWINGML_CHART_URI)){
  190. copyChart(data, (XSLFGraphicFrame)sh);
  191. } else {
  192. // TODO support other types of objects
  193. }
  194. }
  195. private void copyChart(CTGraphicalObjectData objData, XSLFGraphicFrame srcShape) {
  196. XSLFSlide slide = (XSLFSlide) getSheet();
  197. XSLFSheet src = srcShape.getSheet();
  198. String xpath = "declare namespace c='" + DRAWINGML_CHART_URI + "' c:chart";
  199. XmlObject[] obj = objData.selectPath(xpath);
  200. if (obj != null && obj.length == 1) {
  201. XmlCursor c = obj[0].newCursor();
  202. try {
  203. // duplicate chart with embedded workbook
  204. QName idQualifiedName = new QName(CORE_PROPERTIES_ECMA376_NS, "id");
  205. String id = c.getAttributeText(idQualifiedName);
  206. XSLFChart srcChart = (XSLFChart) src.getRelationById(id);
  207. XSLFChart chartCopy = slide.getSlideShow().createChart(slide);
  208. chartCopy.importContent(srcChart);
  209. chartCopy.setWorkbook(srcChart.getWorkbook());
  210. c.setAttributeText(idQualifiedName, slide.getRelationId(chartCopy));
  211. } catch (InvalidFormatException | IOException e) {
  212. throw new POIXMLException(e);
  213. }
  214. c.dispose();
  215. }
  216. }
  217. // TODO should be moved to a sub-class
  218. private void copyDiagram(CTGraphicalObjectData objData, XSLFGraphicFrame srcShape){
  219. String xpath = "declare namespace dgm='" + DRAWINGML_DIAGRAM_URI + "' $this//dgm:relIds";
  220. XmlObject[] obj = objData.selectPath(xpath);
  221. if(obj != null && obj.length == 1) {
  222. XmlCursor c = obj[0].newCursor();
  223. XSLFSheet sheet = srcShape.getSheet();
  224. try {
  225. String dm = c.getAttributeText(new QName(CORE_PROPERTIES_ECMA376_NS, "dm"));
  226. PackageRelationship dmRel = sheet.getPackagePart().getRelationship(dm);
  227. PackagePart dmPart = sheet.getPackagePart().getRelatedPart(dmRel);
  228. getSheet().importPart(dmRel, dmPart);
  229. String lo = c.getAttributeText(new QName(CORE_PROPERTIES_ECMA376_NS, "lo"));
  230. PackageRelationship loRel = sheet.getPackagePart().getRelationship(lo);
  231. PackagePart loPart = sheet.getPackagePart().getRelatedPart(loRel);
  232. getSheet().importPart(loRel, loPart);
  233. String qs = c.getAttributeText(new QName(CORE_PROPERTIES_ECMA376_NS, "qs"));
  234. PackageRelationship qsRel = sheet.getPackagePart().getRelationship(qs);
  235. PackagePart qsPart = sheet.getPackagePart().getRelatedPart(qsRel);
  236. getSheet().importPart(qsRel, qsPart);
  237. String cs = c.getAttributeText(new QName(CORE_PROPERTIES_ECMA376_NS, "cs"));
  238. PackageRelationship csRel = sheet.getPackagePart().getRelationship(cs);
  239. PackagePart csPart = sheet.getPackagePart().getRelatedPart(csRel);
  240. getSheet().importPart(csRel, csPart);
  241. } catch (InvalidFormatException e){
  242. throw new POIXMLException(e);
  243. }
  244. c.dispose();
  245. }
  246. }
  247. @Override
  248. public XSLFPictureShape getFallbackPicture() {
  249. String xquery =
  250. "declare namespace p='http://schemas.openxmlformats.org/presentationml/2006/main'; "
  251. + "declare namespace mc='http://schemas.openxmlformats.org/markup-compatibility/2006' "
  252. + ".//mc:Fallback/*/p:pic"
  253. ;
  254. XmlObject xo = selectProperty(XmlObject.class, xquery);
  255. if (xo == null) {
  256. return null;
  257. }
  258. CTGroupShape gs;
  259. try {
  260. gs = CTGroupShape.Factory.parse(xo.newDomNode());
  261. } catch (XmlException e) {
  262. LOG.atWarn().withThrowable(e).log("Can't parse fallback picture stream of graphical frame");
  263. return null;
  264. }
  265. if (gs.sizeOfPicArray() == 0) {
  266. return null;
  267. }
  268. return new XSLFPictureShape(gs.getPicArray(0), getSheet());
  269. }
  270. }