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.

XSSFVMLDrawing.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import java.math.BigInteger;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import java.util.regex.Matcher;
  25. import java.util.regex.Pattern;
  26. import javax.xml.namespace.QName;
  27. import com.microsoft.schemas.office.excel.CTClientData;
  28. import com.microsoft.schemas.office.excel.STObjectType;
  29. import com.microsoft.schemas.office.office.CTIdMap;
  30. import com.microsoft.schemas.office.office.CTShapeLayout;
  31. import com.microsoft.schemas.office.office.STConnectType;
  32. import com.microsoft.schemas.office.office.STInsetMode;
  33. import com.microsoft.schemas.office.office.ShapelayoutDocument;
  34. import com.microsoft.schemas.vml.CTGroup;
  35. import com.microsoft.schemas.vml.CTPath;
  36. import com.microsoft.schemas.vml.CTShadow;
  37. import com.microsoft.schemas.vml.CTShape;
  38. import com.microsoft.schemas.vml.CTShapetype;
  39. import com.microsoft.schemas.vml.STExt;
  40. import com.microsoft.schemas.vml.STStrokeJoinStyle;
  41. import com.microsoft.schemas.vml.STTrueFalse;
  42. import org.apache.poi.ooxml.POIXMLDocumentPart;
  43. import org.apache.poi.ooxml.util.DocumentHelper;
  44. import org.apache.poi.openxml4j.opc.PackagePart;
  45. import org.apache.poi.schemas.vmldrawing.XmlDocument;
  46. import org.apache.poi.util.ReplacingInputStream;
  47. import org.apache.xmlbeans.XmlCursor;
  48. import org.apache.xmlbeans.XmlException;
  49. import org.apache.xmlbeans.XmlObject;
  50. import org.apache.xmlbeans.XmlOptions;
  51. import org.w3c.dom.Document;
  52. import org.xml.sax.SAXException;
  53. /**
  54. * Represents a SpreadsheetML VML drawing.
  55. *
  56. * <p>
  57. * In Excel 2007 VML drawings are used to describe properties of cell comments,
  58. * although the spec says that VML is deprecated:
  59. * </p>
  60. * <p>
  61. * The VML format is a legacy format originally introduced with Office 2000 and is included and fully defined
  62. * in this Standard for backwards compatibility reasons. The DrawingML format is a newer and richer format
  63. * created with the goal of eventually replacing any uses of VML in the Office Open XML formats. VML should be
  64. * considered a deprecated format included in Office Open XML for legacy reasons only and new applications that
  65. * need a file format for drawings are strongly encouraged to use preferentially DrawingML
  66. * </p>
  67. *
  68. * <p>
  69. * Warning - Excel is known to put invalid XML into these files!
  70. * For example, &gt;br&lt; without being closed or escaped crops up.
  71. * </p>
  72. *
  73. * See 6.4 VML - SpreadsheetML Drawing in Office Open XML Part 4 - Markup Language Reference.pdf
  74. */
  75. public final class XSSFVMLDrawing extends POIXMLDocumentPart {
  76. // this ID value seems to have significance to Excel >= 2010;
  77. // see https://issues.apache.org/bugzilla/show_bug.cgi?id=55409
  78. private static final String COMMENT_SHAPE_TYPE_ID = "_x0000_t202";
  79. /**
  80. * to actually process the namespace-less vmldrawing, we've introduced a proxy namespace.
  81. * this namespace is active in-memory, but will be removed on saving to the file
  82. */
  83. public static final QName QNAME_VMLDRAWING = new QName("urn:schemas-poi-apache-org:vmldrawing", "xml");
  84. /**
  85. * regexp to parse shape ids, in VML they have weird form of id="_x0000_s1026"
  86. */
  87. private static final Pattern ptrn_shapeId = Pattern.compile("_x0000_s(\\d+)");
  88. private XmlDocument root;
  89. private String _shapeTypeId;
  90. private int _shapeId = 1024;
  91. /**
  92. * Create a new SpreadsheetML drawing
  93. *
  94. * @see XSSFSheet#createDrawingPatriarch()
  95. */
  96. protected XSSFVMLDrawing() {
  97. super();
  98. newDrawing();
  99. }
  100. /**
  101. * Construct a SpreadsheetML drawing from a package part
  102. *
  103. * @param part the package part holding the drawing data,
  104. * the content type must be <code>application/vnd.openxmlformats-officedocument.drawing+xml</code>
  105. *
  106. * @since POI 3.14-Beta1
  107. */
  108. protected XSSFVMLDrawing(PackagePart part) throws IOException, XmlException {
  109. super(part);
  110. read(getPackagePart().getInputStream());
  111. }
  112. public XmlDocument getDocument() {
  113. return root;
  114. }
  115. protected void read(InputStream is) throws IOException, XmlException {
  116. Document doc;
  117. try {
  118. /*
  119. * This is a seriously sick fix for the fact that some .xlsx files contain raw bits
  120. * of HTML, without being escaped or properly turned into XML.
  121. * The result is that they contain things like &gt;br&lt;, which breaks the XML parsing.
  122. * This very sick InputStream wrapper attempts to spot these go past, and fix them.
  123. */
  124. doc = DocumentHelper.readDocument(new ReplacingInputStream(is, "<br>", "<br/>"));
  125. } catch (SAXException e) {
  126. throw new XmlException(e.getMessage(), e);
  127. }
  128. XmlOptions xopt = new XmlOptions(DEFAULT_XML_OPTIONS);
  129. xopt.setLoadSubstituteNamespaces(Collections.singletonMap("", QNAME_VMLDRAWING.getNamespaceURI()));
  130. root = XmlDocument.Factory.parse(doc, xopt);
  131. XmlCursor cur = root.getXml().newCursor();
  132. try {
  133. for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) {
  134. XmlObject xo = cur.getObject();
  135. if (xo instanceof CTShapetype) {
  136. _shapeTypeId = ((CTShapetype)xo).getId();
  137. } else if (xo instanceof CTShape) {
  138. CTShape shape = (CTShape)xo;
  139. String id = shape.getId();
  140. if(id != null) {
  141. Matcher m = ptrn_shapeId.matcher(id);
  142. if(m.find()) {
  143. _shapeId = Math.max(_shapeId, Integer.parseInt(m.group(1)));
  144. }
  145. }
  146. }
  147. }
  148. } finally {
  149. cur.dispose();
  150. }
  151. }
  152. protected List<XmlObject> getItems(){
  153. List<XmlObject> items = new ArrayList<>();
  154. XmlCursor cur = root.getXml().newCursor();
  155. try {
  156. for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) {
  157. items.add(cur.getObject());
  158. }
  159. } finally {
  160. cur.dispose();
  161. }
  162. return items;
  163. }
  164. protected void write(OutputStream out) throws IOException {
  165. XmlOptions xopt = new XmlOptions(DEFAULT_XML_OPTIONS);
  166. xopt.setSaveImplicitNamespaces(Collections.singletonMap("", QNAME_VMLDRAWING.getNamespaceURI()));
  167. root.save(out, xopt);
  168. }
  169. @Override
  170. protected void commit() throws IOException {
  171. PackagePart part = getPackagePart();
  172. try (OutputStream out = part.getOutputStream()) {
  173. write(out);
  174. }
  175. }
  176. /**
  177. * Initialize a new Speadsheet VML drawing
  178. */
  179. private void newDrawing(){
  180. root = XmlDocument.Factory.newInstance();
  181. XmlCursor xml = root.addNewXml().newCursor();
  182. ShapelayoutDocument layDoc = ShapelayoutDocument.Factory.newInstance();
  183. CTShapeLayout layout = layDoc.addNewShapelayout();
  184. layout.setExt(STExt.EDIT);
  185. CTIdMap idmap = layout.addNewIdmap();
  186. idmap.setExt(STExt.EDIT);
  187. idmap.setData("1");
  188. xml.toEndToken();
  189. XmlCursor layCur = layDoc.newCursor();
  190. layCur.copyXmlContents(xml);
  191. layCur.dispose();
  192. CTGroup grp = CTGroup.Factory.newInstance();
  193. CTShapetype shapetype = grp.addNewShapetype();
  194. _shapeTypeId = COMMENT_SHAPE_TYPE_ID;
  195. shapetype.setId(_shapeTypeId);
  196. shapetype.setCoordsize("21600,21600");
  197. shapetype.setSpt(202);
  198. shapetype.setPath2("m,l,21600r21600,l21600,xe");
  199. shapetype.addNewStroke().setJoinstyle(STStrokeJoinStyle.MITER);
  200. CTPath path = shapetype.addNewPath();
  201. path.setGradientshapeok(STTrueFalse.T);
  202. path.setConnecttype(STConnectType.RECT);
  203. xml.toEndToken();
  204. XmlCursor grpCur = grp.newCursor();
  205. grpCur.copyXmlContents(xml);
  206. grpCur.dispose();
  207. }
  208. protected CTShape newCommentShape(){
  209. CTGroup grp = CTGroup.Factory.newInstance();
  210. CTShape shape = grp.addNewShape();
  211. shape.setId("_x0000_s" + (++_shapeId));
  212. shape.setType("#" + _shapeTypeId);
  213. shape.setStyle("position:absolute; visibility:hidden");
  214. shape.setFillcolor("#ffffe1");
  215. shape.setInsetmode(STInsetMode.AUTO);
  216. shape.addNewFill().setColor("#ffffe1");
  217. CTShadow shadow = shape.addNewShadow();
  218. shadow.setOn(STTrueFalse.T);
  219. shadow.setColor("black");
  220. shadow.setObscured(STTrueFalse.T);
  221. shape.addNewPath().setConnecttype(STConnectType.NONE);
  222. shape.addNewTextbox().setStyle("mso-direction-alt:auto");
  223. CTClientData cldata = shape.addNewClientData();
  224. cldata.setObjectType(STObjectType.NOTE);
  225. cldata.addNewMoveWithCells();
  226. cldata.addNewSizeWithCells();
  227. cldata.addNewAnchor().setStringValue("1, 15, 0, 2, 3, 15, 3, 16");
  228. cldata.addNewAutoFill().setStringValue("False");
  229. cldata.addNewRow().setBigIntegerValue(BigInteger.valueOf(0));
  230. cldata.addNewColumn().setBigIntegerValue(BigInteger.valueOf(0));
  231. XmlCursor xml = root.getXml().newCursor();
  232. xml.toEndToken();
  233. XmlCursor grpCur = grp.newCursor();
  234. grpCur.copyXmlContents(xml);
  235. xml.toPrevSibling();
  236. shape = (CTShape)xml.getObject();
  237. grpCur.dispose();
  238. xml.dispose();
  239. return shape;
  240. }
  241. /**
  242. * Find a shape with ClientData of type "NOTE" and the specified row and column
  243. *
  244. * @return the comment shape or <code>null</code>
  245. */
  246. public CTShape findCommentShape(int row, int col){
  247. XmlCursor cur = root.getXml().newCursor();
  248. for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) {
  249. XmlObject itm = cur.getObject();
  250. if (matchCommentShape(itm, row, col)) {
  251. return (CTShape)itm;
  252. }
  253. }
  254. return null;
  255. }
  256. private boolean matchCommentShape(XmlObject itm, int row, int col) {
  257. if (!(itm instanceof CTShape)) {
  258. return false;
  259. }
  260. CTShape sh = (CTShape)itm;
  261. if (sh.sizeOfClientDataArray() == 0) {
  262. return false;
  263. }
  264. CTClientData cldata = sh.getClientDataArray(0);
  265. if(cldata.getObjectType() != STObjectType.NOTE) {
  266. return false;
  267. }
  268. int crow = cldata.getRowArray(0).intValue();
  269. int ccol = cldata.getColumnArray(0).intValue();
  270. return (crow == row && ccol == col);
  271. }
  272. protected boolean removeCommentShape(int row, int col){
  273. XmlCursor cur = root.getXml().newCursor();
  274. for (boolean found = cur.toFirstChild(); found; found = cur.toNextSibling()) {
  275. XmlObject itm = cur.getObject();
  276. if (matchCommentShape(itm, row, col)) {
  277. cur.removeXml();
  278. return true;
  279. }
  280. }
  281. return false;
  282. }
  283. }