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.

XSLFCommonSlideData.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.xslf.usermodel;
  16. import java.util.ArrayList;
  17. import java.util.Arrays;
  18. import java.util.List;
  19. import org.apache.poi.POIXMLException;
  20. import org.apache.poi.util.Beta;
  21. import org.apache.xmlbeans.XmlCursor;
  22. import org.apache.xmlbeans.XmlException;
  23. import org.apache.xmlbeans.XmlObject;
  24. import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl;
  25. import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData;
  26. import org.openxmlformats.schemas.drawingml.x2006.main.CTTable;
  27. import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
  28. import org.openxmlformats.schemas.presentationml.x2006.main.CTApplicationNonVisualDrawingProps;
  29. import org.openxmlformats.schemas.presentationml.x2006.main.CTCommonSlideData;
  30. import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
  31. import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
  32. import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
  33. @Beta
  34. public class XSLFCommonSlideData {
  35. private final CTCommonSlideData data;
  36. public XSLFCommonSlideData(CTCommonSlideData data) {
  37. this.data = data;
  38. }
  39. @SuppressWarnings("deprecation")
  40. public List<DrawingTextBody> getDrawingText() {
  41. CTGroupShape gs = data.getSpTree();
  42. List<DrawingTextBody> out = new ArrayList<DrawingTextBody>();
  43. processShape(gs, out);
  44. for (CTGroupShape shape : gs.getGrpSpArray()) {
  45. processShape(shape, out);
  46. }
  47. for (CTGraphicalObjectFrame frame: gs.getGraphicFrameArray()) {
  48. CTGraphicalObjectData data = frame.getGraphic().getGraphicData();
  49. XmlCursor c = data.newCursor();
  50. c.selectPath("declare namespace pic='"+CTTable.type.getName().getNamespaceURI()+"' .//pic:tbl");
  51. while (c.toNextSelection()) {
  52. XmlObject o = c.getObject();
  53. if (o instanceof XmlAnyTypeImpl) {
  54. // Pesky XmlBeans bug - see Bugzilla #49934
  55. try {
  56. o = CTTable.Factory.parse(o.toString());
  57. } catch (XmlException e) {
  58. throw new POIXMLException(e);
  59. }
  60. }
  61. if (o instanceof CTTable) {
  62. DrawingTable table = new DrawingTable((CTTable) o);
  63. for (DrawingTableRow row : table.getRows()) {
  64. for (DrawingTableCell cell : row.getCells()) {
  65. DrawingTextBody textBody = cell.getTextBody();
  66. out.add(textBody);
  67. }
  68. }
  69. }
  70. }
  71. c.dispose();
  72. }
  73. return out;
  74. }
  75. public List<DrawingParagraph> getText() {
  76. List<DrawingParagraph> paragraphs = new ArrayList<DrawingParagraph>();
  77. for(DrawingTextBody textBody : getDrawingText()) {
  78. paragraphs.addAll(Arrays.asList(textBody.getParagraphs()));
  79. }
  80. return paragraphs;
  81. }
  82. @SuppressWarnings("deprecation")
  83. private void processShape(CTGroupShape gs, List<DrawingTextBody> out) {
  84. for (CTShape shape : gs.getSpArray()) {
  85. CTTextBody ctTextBody = shape.getTxBody();
  86. if (ctTextBody==null) {
  87. continue;
  88. }
  89. DrawingTextBody textBody;
  90. CTApplicationNonVisualDrawingProps nvpr = shape.getNvSpPr().getNvPr();
  91. if(nvpr.isSetPh()) {
  92. textBody = new DrawingTextPlaceholder(ctTextBody, nvpr.getPh());
  93. } else {
  94. textBody = new DrawingTextBody(ctTextBody);
  95. }
  96. out.add(textBody);
  97. }
  98. }
  99. }