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.

XSSFDrawing.java 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.util.ArrayList;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import javax.xml.namespace.QName;
  24. import org.apache.poi.POIXMLDocumentPart;
  25. import org.apache.poi.openxml4j.opc.PackagePart;
  26. import org.apache.poi.openxml4j.opc.PackageRelationship;
  27. import org.apache.poi.ss.usermodel.ClientAnchor;
  28. import org.apache.poi.ss.usermodel.Drawing;
  29. import org.apache.poi.ss.util.CellAddress;
  30. import org.apache.poi.ss.util.ImageUtils;
  31. import org.apache.poi.util.Internal;
  32. import org.apache.poi.util.POILogFactory;
  33. import org.apache.poi.util.POILogger;
  34. import org.apache.poi.util.Units;
  35. import org.apache.poi.xssf.model.CommentsTable;
  36. import org.apache.xmlbeans.XmlCursor;
  37. import org.apache.xmlbeans.XmlException;
  38. import org.apache.xmlbeans.XmlObject;
  39. import org.apache.xmlbeans.XmlOptions;
  40. import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl;
  41. import org.openxmlformats.schemas.drawingml.x2006.main.CTGroupTransform2D;
  42. import org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D;
  43. import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
  44. import org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D;
  45. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTConnector;
  46. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing;
  47. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGraphicalObjectFrame;
  48. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGroupShape;
  49. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;
  50. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTOneCellAnchor;
  51. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture;
  52. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape;
  53. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor;
  54. import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.STEditAs;
  55. /**
  56. * Represents a SpreadsheetML drawing
  57. */
  58. public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing<XSSFShape> {
  59. private static final POILogger LOG = POILogFactory.getLogger(XSSFDrawing.class);
  60. /**
  61. * Root element of the SpreadsheetML Drawing part
  62. */
  63. private CTDrawing drawing;
  64. private long numOfGraphicFrames = 0L;
  65. protected static final String NAMESPACE_A = XSSFRelation.NS_DRAWINGML;
  66. protected static final String NAMESPACE_C = XSSFRelation.NS_CHART;
  67. /**
  68. * Create a new SpreadsheetML drawing
  69. *
  70. * @see org.apache.poi.xssf.usermodel.XSSFSheet#createDrawingPatriarch()
  71. */
  72. protected XSSFDrawing() {
  73. super();
  74. drawing = newDrawing();
  75. }
  76. /**
  77. * Construct a SpreadsheetML drawing from a package part
  78. *
  79. * @param part the package part holding the drawing data,
  80. * the content type must be <code>application/vnd.openxmlformats-officedocument.drawing+xml</code>
  81. *
  82. * @since POI 3.14-Beta1
  83. */
  84. public XSSFDrawing(PackagePart part) throws IOException, XmlException {
  85. super(part);
  86. XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS);
  87. //Removing root element
  88. options.setLoadReplaceDocumentElement(null);
  89. InputStream is = part.getInputStream();
  90. try {
  91. drawing = CTDrawing.Factory.parse(is,options);
  92. } finally {
  93. is.close();
  94. }
  95. }
  96. /**
  97. * Construct a new CTDrawing bean. By default, it's just an empty placeholder for drawing objects
  98. *
  99. * @return a new CTDrawing bean
  100. */
  101. private static CTDrawing newDrawing(){
  102. return CTDrawing.Factory.newInstance();
  103. }
  104. /**
  105. * Return the underlying CTDrawing bean, the root element of the SpreadsheetML Drawing part.
  106. *
  107. * @return the underlying CTDrawing bean
  108. */
  109. @Internal
  110. public CTDrawing getCTDrawing(){
  111. return drawing;
  112. }
  113. @Override
  114. protected void commit() throws IOException {
  115. XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
  116. /*
  117. Saved drawings must have the following namespaces set:
  118. <xdr:wsDr
  119. xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
  120. xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing">
  121. */
  122. xmlOptions.setSaveSyntheticDocumentElement(
  123. new QName(CTDrawing.type.getName().getNamespaceURI(), "wsDr", "xdr")
  124. );
  125. PackagePart part = getPackagePart();
  126. OutputStream out = part.getOutputStream();
  127. drawing.save(out, xmlOptions);
  128. out.close();
  129. }
  130. @Override
  131. public XSSFClientAnchor createAnchor(int dx1, int dy1, int dx2, int dy2,
  132. int col1, int row1, int col2, int row2) {
  133. return new XSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2);
  134. }
  135. /**
  136. * Constructs a textbox under the drawing.
  137. *
  138. * @param anchor the client anchor describes how this group is attached
  139. * to the sheet.
  140. * @return the newly created textbox.
  141. */
  142. public XSSFTextBox createTextbox(XSSFClientAnchor anchor){
  143. long shapeId = newShapeId();
  144. CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
  145. CTShape ctShape = ctAnchor.addNewSp();
  146. ctShape.set(XSSFSimpleShape.prototype());
  147. ctShape.getNvSpPr().getCNvPr().setId(shapeId);
  148. XSSFTextBox shape = new XSSFTextBox(this, ctShape);
  149. shape.anchor = anchor;
  150. return shape;
  151. }
  152. /**
  153. * Creates a picture.
  154. *
  155. * @param anchor the client anchor describes how this picture is attached to the sheet.
  156. * @param pictureIndex the index of the picture in the workbook collection of pictures,
  157. * {@link org.apache.poi.xssf.usermodel.XSSFWorkbook#getAllPictures()} .
  158. *
  159. * @return the newly created picture shape.
  160. */
  161. public XSSFPicture createPicture(XSSFClientAnchor anchor, int pictureIndex)
  162. {
  163. PackageRelationship rel = addPictureReference(pictureIndex);
  164. long shapeId = newShapeId();
  165. CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
  166. CTPicture ctShape = ctAnchor.addNewPic();
  167. ctShape.set(XSSFPicture.prototype());
  168. ctShape.getNvPicPr().getCNvPr().setId(shapeId);
  169. XSSFPicture shape = new XSSFPicture(this, ctShape);
  170. shape.anchor = anchor;
  171. shape.setPictureReference(rel);
  172. ctShape.getSpPr().setXfrm(createXfrm(anchor));
  173. return shape;
  174. }
  175. @Override
  176. public XSSFPicture createPicture(ClientAnchor anchor, int pictureIndex){
  177. return createPicture((XSSFClientAnchor)anchor, pictureIndex);
  178. }
  179. /**
  180. * Creates a chart.
  181. * @param anchor the client anchor describes how this chart is attached to
  182. * the sheet.
  183. * @return the newly created chart
  184. * @see org.apache.poi.xssf.usermodel.XSSFDrawing#createChart(ClientAnchor)
  185. */
  186. public XSSFChart createChart(XSSFClientAnchor anchor) {
  187. int chartNumber = getPackagePart().getPackage().
  188. getPartsByContentType(XSSFRelation.CHART.getContentType()).size() + 1;
  189. RelationPart rp = createRelationship(
  190. XSSFRelation.CHART, XSSFFactory.getInstance(), chartNumber, false);
  191. XSSFChart chart = rp.getDocumentPart();
  192. String chartRelId = rp.getRelationship().getId();
  193. XSSFGraphicFrame frame = createGraphicFrame(anchor);
  194. frame.setChart(chart, chartRelId);
  195. frame.getCTGraphicalObjectFrame().setXfrm(createXfrm(anchor));
  196. return chart;
  197. }
  198. @Override
  199. public XSSFChart createChart(ClientAnchor anchor) {
  200. return createChart((XSSFClientAnchor)anchor);
  201. }
  202. /**
  203. * Add the indexed picture to this drawing relations
  204. *
  205. * @param pictureIndex the index of the picture in the workbook collection of pictures,
  206. * {@link org.apache.poi.xssf.usermodel.XSSFWorkbook#getAllPictures()} .
  207. */
  208. @SuppressWarnings("resource")
  209. protected PackageRelationship addPictureReference(int pictureIndex){
  210. XSSFWorkbook wb = (XSSFWorkbook)getParent().getParent();
  211. XSSFPictureData data = wb.getAllPictures().get(pictureIndex);
  212. XSSFPictureData pic = new XSSFPictureData(data.getPackagePart());
  213. RelationPart rp = addRelation(null, XSSFRelation.IMAGES, pic);
  214. return rp.getRelationship();
  215. }
  216. /**
  217. * Creates a simple shape. This includes such shapes as lines, rectangles,
  218. * and ovals.
  219. *
  220. * @param anchor the client anchor describes how this group is attached
  221. * to the sheet.
  222. * @return the newly created shape.
  223. */
  224. public XSSFSimpleShape createSimpleShape(XSSFClientAnchor anchor)
  225. {
  226. long shapeId = newShapeId();
  227. CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
  228. CTShape ctShape = ctAnchor.addNewSp();
  229. ctShape.set(XSSFSimpleShape.prototype());
  230. ctShape.getNvSpPr().getCNvPr().setId(shapeId);
  231. ctShape.getSpPr().setXfrm(createXfrm(anchor));
  232. XSSFSimpleShape shape = new XSSFSimpleShape(this, ctShape);
  233. shape.anchor = anchor;
  234. return shape;
  235. }
  236. /**
  237. * Creates a simple shape. This includes such shapes as lines, rectangles,
  238. * and ovals.
  239. *
  240. * @param anchor the client anchor describes how this group is attached
  241. * to the sheet.
  242. * @return the newly created shape.
  243. */
  244. public XSSFConnector createConnector(XSSFClientAnchor anchor)
  245. {
  246. CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
  247. CTConnector ctShape = ctAnchor.addNewCxnSp();
  248. ctShape.set(XSSFConnector.prototype());
  249. XSSFConnector shape = new XSSFConnector(this, ctShape);
  250. shape.anchor = anchor;
  251. return shape;
  252. }
  253. /**
  254. * Creates a simple shape. This includes such shapes as lines, rectangles,
  255. * and ovals.
  256. *
  257. * @param anchor the client anchor describes how this group is attached
  258. * to the sheet.
  259. * @return the newly created shape.
  260. */
  261. public XSSFShapeGroup createGroup(XSSFClientAnchor anchor)
  262. {
  263. CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
  264. CTGroupShape ctGroup = ctAnchor.addNewGrpSp();
  265. ctGroup.set(XSSFShapeGroup.prototype());
  266. CTTransform2D xfrm = createXfrm(anchor);
  267. CTGroupTransform2D grpXfrm =ctGroup.getGrpSpPr().getXfrm();
  268. grpXfrm.setOff(xfrm.getOff());
  269. grpXfrm.setExt(xfrm.getExt());
  270. grpXfrm.setChExt(xfrm.getExt());
  271. XSSFShapeGroup shape = new XSSFShapeGroup(this, ctGroup);
  272. shape.anchor = anchor;
  273. return shape;
  274. }
  275. /**
  276. * Creates a comment.
  277. * @param anchor the client anchor describes how this comment is attached
  278. * to the sheet.
  279. * @return the newly created comment.
  280. */
  281. @Override
  282. public XSSFComment createCellComment(ClientAnchor anchor) {
  283. XSSFClientAnchor ca = (XSSFClientAnchor)anchor;
  284. XSSFSheet sheet = (XSSFSheet)getParent();
  285. //create comments and vmlDrawing parts if they don't exist
  286. CommentsTable comments = sheet.getCommentsTable(true);
  287. XSSFVMLDrawing vml = sheet.getVMLDrawing(true);
  288. com.microsoft.schemas.vml.CTShape vmlShape = vml.newCommentShape();
  289. if(ca.isSet()){
  290. // convert offsets from emus to pixels since we get a DrawingML-anchor
  291. // but create a VML Drawing
  292. int dx1Pixels = ca.getDx1()/Units.EMU_PER_PIXEL;
  293. int dy1Pixels = ca.getDy1()/Units.EMU_PER_PIXEL;
  294. int dx2Pixels = ca.getDx2()/Units.EMU_PER_PIXEL;
  295. int dy2Pixels = ca.getDy2()/Units.EMU_PER_PIXEL;
  296. String position =
  297. ca.getCol1() + ", " + dx1Pixels + ", " +
  298. ca.getRow1() + ", " + dy1Pixels + ", " +
  299. ca.getCol2() + ", " + dx2Pixels + ", " +
  300. ca.getRow2() + ", " + dy2Pixels;
  301. vmlShape.getClientDataArray(0).setAnchorArray(0, position);
  302. }
  303. CellAddress ref = new CellAddress(ca.getRow1(), ca.getCol1());
  304. if(comments.findCellComment(ref) != null) {
  305. throw new IllegalArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref);
  306. }
  307. return new XSSFComment(comments, comments.newComment(ref), vmlShape);
  308. }
  309. /**
  310. * Creates a new graphic frame.
  311. *
  312. * @param anchor the client anchor describes how this frame is attached
  313. * to the sheet
  314. * @return the newly created graphic frame
  315. */
  316. private XSSFGraphicFrame createGraphicFrame(XSSFClientAnchor anchor) {
  317. CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
  318. CTGraphicalObjectFrame ctGraphicFrame = ctAnchor.addNewGraphicFrame();
  319. ctGraphicFrame.set(XSSFGraphicFrame.prototype());
  320. ctGraphicFrame.setXfrm(createXfrm(anchor));
  321. long frameId = numOfGraphicFrames++;
  322. XSSFGraphicFrame graphicFrame = new XSSFGraphicFrame(this, ctGraphicFrame);
  323. graphicFrame.setAnchor(anchor);
  324. graphicFrame.setId(frameId);
  325. graphicFrame.setName("Diagramm" + frameId);
  326. return graphicFrame;
  327. }
  328. /**
  329. * Returns all charts in this drawing.
  330. */
  331. public List<XSSFChart> getCharts() {
  332. List<XSSFChart> charts = new ArrayList<XSSFChart>();
  333. for(POIXMLDocumentPart part : getRelations()) {
  334. if(part instanceof XSSFChart) {
  335. charts.add((XSSFChart)part);
  336. }
  337. }
  338. return charts;
  339. }
  340. /**
  341. * Create and initialize a CTTwoCellAnchor that anchors a shape against top-left and bottom-right cells.
  342. *
  343. * @return a new CTTwoCellAnchor
  344. */
  345. private CTTwoCellAnchor createTwoCellAnchor(XSSFClientAnchor anchor) {
  346. CTTwoCellAnchor ctAnchor = drawing.addNewTwoCellAnchor();
  347. ctAnchor.setFrom(anchor.getFrom());
  348. ctAnchor.setTo(anchor.getTo());
  349. ctAnchor.addNewClientData();
  350. anchor.setTo(ctAnchor.getTo());
  351. anchor.setFrom(ctAnchor.getFrom());
  352. STEditAs.Enum aditAs;
  353. switch(anchor.getAnchorType()) {
  354. case DONT_MOVE_AND_RESIZE: aditAs = STEditAs.ABSOLUTE; break;
  355. case MOVE_AND_RESIZE: aditAs = STEditAs.TWO_CELL; break;
  356. case MOVE_DONT_RESIZE: aditAs = STEditAs.ONE_CELL; break;
  357. default: aditAs = STEditAs.ONE_CELL;
  358. }
  359. ctAnchor.setEditAs(aditAs);
  360. return ctAnchor;
  361. }
  362. private CTTransform2D createXfrm(XSSFClientAnchor anchor) {
  363. CTTransform2D xfrm = CTTransform2D.Factory.newInstance();
  364. CTPoint2D off = xfrm.addNewOff();
  365. off.setX(anchor.getDx1());
  366. off.setY(anchor.getDy1());
  367. XSSFSheet sheet = (XSSFSheet)getParent();
  368. double widthPx = 0;
  369. for (int col=anchor.getCol1(); col<anchor.getCol2(); col++) {
  370. widthPx += sheet.getColumnWidthInPixels(col);
  371. }
  372. double heightPx = 0;
  373. for (int row=anchor.getRow1(); row<anchor.getRow2(); row++) {
  374. heightPx += ImageUtils.getRowHeightInPixels(sheet, row);
  375. }
  376. int width = Units.pixelToEMU((int)widthPx);
  377. int height = Units.pixelToEMU((int)heightPx);
  378. CTPositiveSize2D ext = xfrm.addNewExt();
  379. ext.setCx(width - anchor.getDx1() + anchor.getDx2());
  380. ext.setCy(height - anchor.getDy1() + anchor.getDy2());
  381. // TODO: handle vflip/hflip
  382. return xfrm;
  383. }
  384. private long newShapeId(){
  385. return drawing.sizeOfTwoCellAnchorArray() + 1;
  386. }
  387. /**
  388. * @return list of shapes in this drawing
  389. */
  390. public List<XSSFShape> getShapes(){
  391. List<XSSFShape> lst = new ArrayList<XSSFShape>();
  392. XmlCursor cur = drawing.newCursor();
  393. try {
  394. if (cur.toFirstChild()) {
  395. addShapes(cur, lst);
  396. }
  397. } finally {
  398. cur.dispose();
  399. }
  400. return lst;
  401. }
  402. /**
  403. * @return list of shapes in this shape group
  404. */
  405. public List<XSSFShape> getShapes(XSSFShapeGroup groupshape){
  406. List<XSSFShape> lst = new ArrayList<XSSFShape>();
  407. XmlCursor cur = groupshape.getCTGroupShape().newCursor();
  408. try {
  409. addShapes(cur, lst);
  410. } finally {
  411. cur.dispose();
  412. }
  413. return lst;
  414. }
  415. private void addShapes(XmlCursor cur, List<XSSFShape> lst) {
  416. try {
  417. do {
  418. cur.push();
  419. if (cur.toFirstChild()) {
  420. do {
  421. XmlObject obj = cur.getObject();
  422. XSSFShape shape;
  423. if (obj instanceof CTMarker) {
  424. // ignore anchor elements
  425. continue;
  426. } else if (obj instanceof CTPicture) {
  427. shape = new XSSFPicture(this, (CTPicture)obj) ;
  428. } else if(obj instanceof CTConnector) {
  429. shape = new XSSFConnector(this, (CTConnector)obj) ;
  430. } else if(obj instanceof CTShape) {
  431. shape = hasOleLink(obj)
  432. ? new XSSFObjectData(this, (CTShape)obj)
  433. : new XSSFSimpleShape(this, (CTShape)obj) ;
  434. } else if(obj instanceof CTGraphicalObjectFrame) {
  435. shape = new XSSFGraphicFrame(this, (CTGraphicalObjectFrame)obj) ;
  436. } else if(obj instanceof CTGroupShape) {
  437. shape = new XSSFShapeGroup(this, (CTGroupShape)obj) ;
  438. } else if(obj instanceof XmlAnyTypeImpl) {
  439. LOG.log(POILogger.WARN, "trying to parse AlternateContent, "
  440. + "this unlinks the returned Shapes from the underlying xml content, "
  441. + "so those shapes can't be used to modify the drawing, "
  442. + "i.e. modifications will be ignored!");
  443. // XmlAnyTypeImpl is returned for AlternateContent parts, which might contain a CTDrawing
  444. cur.push();
  445. cur.toFirstChild();
  446. XmlCursor cur2 = null;
  447. try {
  448. // need to parse AlternateContent again, otherwise the child elements aren't typed,
  449. // but also XmlAnyTypes
  450. CTDrawing alterWS = CTDrawing.Factory.parse(cur.newXMLStreamReader());
  451. cur2 = alterWS.newCursor();
  452. if (cur2.toFirstChild()) {
  453. addShapes(cur2, lst);
  454. }
  455. } catch (XmlException e) {
  456. LOG.log(POILogger.WARN, "unable to parse CTDrawing in alternate content.", e);
  457. } finally {
  458. if (cur2 != null) {
  459. cur2.dispose();
  460. }
  461. cur.pop();
  462. }
  463. continue;
  464. } else {
  465. // ignore anything else
  466. continue;
  467. }
  468. assert(shape != null);
  469. shape.anchor = getAnchorFromParent(obj);
  470. lst.add(shape);
  471. } while (cur.toNextSibling());
  472. }
  473. cur.pop();
  474. } while (cur.toNextSibling());
  475. } finally {
  476. cur.dispose();
  477. }
  478. }
  479. private boolean hasOleLink(XmlObject shape) {
  480. QName uriName = new QName(null, "uri");
  481. String xquery = "declare namespace a='"+XSSFRelation.NS_DRAWINGML+"' .//a:extLst/a:ext";
  482. XmlCursor cur = shape.newCursor();
  483. cur.selectPath(xquery);
  484. try {
  485. while (cur.toNextSelection()) {
  486. String uri = cur.getAttributeText(uriName);
  487. if ("{63B3BB69-23CF-44E3-9099-C40C66FF867C}".equals(uri)) {
  488. return true;
  489. }
  490. }
  491. } finally {
  492. cur.dispose();
  493. }
  494. return false;
  495. }
  496. private XSSFAnchor getAnchorFromParent(XmlObject obj){
  497. XSSFAnchor anchor = null;
  498. XmlObject parentXbean = null;
  499. XmlCursor cursor = obj.newCursor();
  500. if(cursor.toParent()) {
  501. parentXbean = cursor.getObject();
  502. }
  503. cursor.dispose();
  504. if(parentXbean != null){
  505. if (parentXbean instanceof CTTwoCellAnchor) {
  506. CTTwoCellAnchor ct = (CTTwoCellAnchor)parentXbean;
  507. anchor = new XSSFClientAnchor(ct.getFrom(), ct.getTo());
  508. } else if (parentXbean instanceof CTOneCellAnchor) {
  509. CTOneCellAnchor ct = (CTOneCellAnchor)parentXbean;
  510. anchor = new XSSFClientAnchor(ct.getFrom(), CTMarker.Factory.newInstance());
  511. }
  512. }
  513. return anchor;
  514. }
  515. @Override
  516. public Iterator<XSSFShape> iterator() {
  517. return getShapes().iterator();
  518. }
  519. }