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

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