Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TestXSSFVMLDrawing.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 static org.apache.poi.xssf.usermodel.XSSFVMLDrawing.QNAME_VMLDRAWING;
  18. import static org.junit.jupiter.api.Assertions.assertEquals;
  19. import static org.junit.jupiter.api.Assertions.assertNotNull;
  20. import static org.junit.jupiter.api.Assertions.assertNull;
  21. import static org.junit.jupiter.api.Assertions.assertSame;
  22. import static org.junit.jupiter.api.Assertions.assertTrue;
  23. import java.io.ByteArrayInputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.math.BigInteger;
  27. import java.nio.charset.StandardCharsets;
  28. import java.util.Collections;
  29. import java.util.List;
  30. import java.util.regex.Pattern;
  31. import com.microsoft.schemas.office.excel.CTClientData;
  32. import com.microsoft.schemas.office.excel.STObjectType;
  33. import com.microsoft.schemas.office.office.CTShapeLayout;
  34. import com.microsoft.schemas.office.office.STConnectType;
  35. import com.microsoft.schemas.office.office.STInsetMode;
  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.impl.CTShapetypeImpl;
  42. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  43. import org.apache.poi.POIDataSamples;
  44. import org.apache.xmlbeans.XmlException;
  45. import org.apache.xmlbeans.XmlObject;
  46. import org.apache.xmlbeans.XmlOptions;
  47. import org.junit.jupiter.api.Test;
  48. import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STTrueFalse;
  49. import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STTrueFalseBlank;
  50. class TestXSSFVMLDrawing {
  51. @Test
  52. void testNew() throws IOException, XmlException {
  53. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  54. List<XmlObject> items = vml.getItems();
  55. assertEquals(2, items.size());
  56. assertTrue(items.get(0) instanceof CTShapeLayout);
  57. CTShapeLayout layout = (CTShapeLayout)items.get(0);
  58. assertSame(STExt.EDIT, layout.getExt());
  59. assertSame(STExt.EDIT, layout.getIdmap().getExt());
  60. assertEquals("1", layout.getIdmap().getData());
  61. assertTrue(items.get(1) instanceof CTShapetype);
  62. CTShapetype type = (CTShapetype)items.get(1);
  63. assertEquals("21600,21600", type.getCoordsize());
  64. assertEquals(202.0f, type.getSpt(), 0);
  65. assertEquals("m,l,21600r21600,l21600,xe", type.getPath2());
  66. assertEquals("_x0000_t202", type.getId());
  67. assertSame(STTrueFalse.T, type.getPathArray(0).getGradientshapeok());
  68. assertSame(STConnectType.RECT, type.getPathArray(0).getConnecttype());
  69. CTShape shape = vml.newCommentShape();
  70. items = vml.getItems();
  71. assertEquals(3, items.size());
  72. assertSame(items.get(2), shape);
  73. assertEquals("#_x0000_t202", shape.getType());
  74. assertEquals("position:absolute; visibility:hidden", shape.getStyle());
  75. assertEquals("#ffffe1", shape.getFillcolor());
  76. assertSame(STInsetMode.AUTO, shape.getInsetmode());
  77. assertEquals("#ffffe1", shape.getFillArray(0).getColor());
  78. CTShadow shadow = shape.getShadowArray(0);
  79. assertSame(STTrueFalse.T, shadow.getOn());
  80. assertEquals("black", shadow.getColor());
  81. assertSame(STTrueFalse.T, shadow.getObscured());
  82. assertSame(STConnectType.NONE, shape.getPathArray(0).getConnecttype());
  83. assertEquals("mso-direction-alt:auto", shape.getTextboxArray(0).getStyle());
  84. CTClientData cldata = shape.getClientDataArray(0);
  85. assertSame(STObjectType.NOTE, cldata.getObjectType());
  86. assertEquals(1, cldata.sizeOfMoveWithCellsArray());
  87. assertEquals(1, cldata.sizeOfSizeWithCellsArray());
  88. assertEquals("1, 15, 0, 2, 3, 15, 3, 16", cldata.getAnchorArray(0));
  89. assertEquals("False", cldata.getAutoFillArray(0).toString());
  90. assertEquals(0, cldata.getRowArray(0).intValue());
  91. assertEquals(0, cldata.getColumnArray(0).intValue());
  92. assertEquals("[]", cldata.getVisibleList().toString());
  93. cldata.setVisibleArray(new STTrueFalseBlank.Enum[] { STTrueFalseBlank.Enum.forString("True") });
  94. assertEquals("[True]", cldata.getVisibleList().toString());
  95. //serialize and read again
  96. UnsynchronizedByteArrayOutputStream out = new UnsynchronizedByteArrayOutputStream();
  97. vml.write(out);
  98. XSSFVMLDrawing vml2 = new XSSFVMLDrawing();
  99. vml2.read(out.toInputStream());
  100. List<XmlObject> items2 = vml2.getItems();
  101. assertEquals(3, items2.size());
  102. assertTrue(items2.get(0) instanceof CTShapeLayout);
  103. assertTrue(items2.get(1) instanceof CTShapetype);
  104. assertTrue(items2.get(2) instanceof CTShape);
  105. }
  106. @Test
  107. void testFindCommentShape() throws IOException, XmlException {
  108. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  109. try (InputStream stream = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("vmlDrawing1.vml")) {
  110. vml.read(stream);
  111. }
  112. CTShape sh_a1 = vml.findCommentShape(0, 0);
  113. assertNotNull(sh_a1);
  114. assertEquals("_x0000_s1025", sh_a1.getId());
  115. CTShape sh_b1 = vml.findCommentShape(0, 1);
  116. assertNotNull(sh_b1);
  117. assertEquals("_x0000_s1026", sh_b1.getId());
  118. CTShape sh_c1 = vml.findCommentShape(0, 2);
  119. assertNull(sh_c1);
  120. CTShape sh_d1 = vml.newCommentShape();
  121. assertEquals("_x0000_s1027", sh_d1.getId());
  122. sh_d1.getClientDataArray(0).setRowArray(0, new BigInteger("0"));
  123. sh_d1.getClientDataArray(0).setColumnArray(0, new BigInteger("3"));
  124. assertSame(sh_d1, vml.findCommentShape(0, 3));
  125. //newly created drawing
  126. XSSFVMLDrawing newVml = new XSSFVMLDrawing();
  127. assertNull(newVml.findCommentShape(0, 0));
  128. sh_a1 = newVml.newCommentShape();
  129. assertEquals("_x0000_s1025", sh_a1.getId());
  130. sh_a1.getClientDataArray(0).setRowArray(0, new BigInteger("0"));
  131. sh_a1.getClientDataArray(0).setColumnArray(0, new BigInteger("1"));
  132. assertSame(sh_a1, newVml.findCommentShape(0, 1));
  133. }
  134. @Test
  135. void testRemoveCommentShape() throws IOException, XmlException {
  136. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  137. try (InputStream stream = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("vmlDrawing1.vml")) {
  138. vml.read(stream);
  139. }
  140. CTShape sh_a1 = vml.findCommentShape(0, 0);
  141. assertNotNull(sh_a1);
  142. assertTrue(vml.removeCommentShape(0, 0));
  143. assertNull(vml.findCommentShape(0, 0));
  144. }
  145. @Test
  146. void testEvilUnclosedBRFixing() throws IOException, XmlException {
  147. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  148. try (InputStream stream = POIDataSamples.getOpenXML4JInstance().openResourceAsStream("bug-60626.vml")) {
  149. vml.read(stream);
  150. }
  151. XmlOptions xopt = new XmlOptions(DEFAULT_XML_OPTIONS);
  152. xopt.setSaveImplicitNamespaces(Collections.singletonMap("", QNAME_VMLDRAWING.getNamespaceURI()));
  153. Pattern p = Pattern.compile("<br/>");
  154. int count = 0;
  155. for (XmlObject xo : vml.getItems()) {
  156. String[] split = p.split(xo.xmlText(xopt));
  157. count += split.length-1;
  158. }
  159. assertEquals(16, count);
  160. }
  161. @Test
  162. void bug65061_InvalidXmlns() throws IOException, XmlException {
  163. // input hasn't no <?xml... declaration - as in the sample file
  164. String input =
  165. "<xml xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\">\n" +
  166. "<v:shapetype id=\"_x0000_t202\" coordsize=\"21600,21600\" path=\"m,l,21600r21600,l21600,xe\" o:spt=\"202\">\n" +
  167. "<v:stroke joinstyle=\"miter\"/>\n" +
  168. "<v:path o:connecttype=\"rect\" gradientshapeok=\"t\"/>\n" +
  169. "</v:shapetype>\n" +
  170. "</xml>";
  171. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  172. vml.read(new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)));
  173. // check that the xml beans parsed correctly
  174. assertNotNull(vml.getDocument().getXml());
  175. // check the parsed child
  176. List<XmlObject> objs = vml.getItems();
  177. assertEquals(1, objs.size());
  178. XmlObject xst = objs.get(0);
  179. assertTrue(xst instanceof CTShapetypeImpl);
  180. CTShapetype st = (CTShapetype)xst;
  181. assertSame(STStrokeJoinStyle.MITER, st.getStrokeArray(0).getJoinstyle());
  182. }
  183. }