Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TestXSSFExportToXML.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.extractor;
  16. import java.io.ByteArrayOutputStream;
  17. import java.util.regex.Matcher;
  18. import java.util.regex.Pattern;
  19. import junit.framework.TestCase;
  20. import org.apache.poi.POIXMLDocumentPart;
  21. import org.apache.poi.xssf.XSSFTestDataSamples;
  22. import org.apache.poi.xssf.model.MapInfo;
  23. import org.apache.poi.xssf.usermodel.XSSFMap;
  24. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  25. /**
  26. * @author Roberto Manicardi
  27. */
  28. public final class TestXSSFExportToXML extends TestCase {
  29. public void testExportToXML() throws Exception {
  30. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");
  31. for (POIXMLDocumentPart p : wb.getRelations()) {
  32. if (!(p instanceof MapInfo)) {
  33. continue;
  34. }
  35. MapInfo mapInfo = (MapInfo) p;
  36. XSSFMap map = mapInfo.getXSSFMapById(1);
  37. XSSFExportToXml exporter = new XSSFExportToXml(map);
  38. ByteArrayOutputStream os = new ByteArrayOutputStream();
  39. exporter.exportToXML(os, true);
  40. String xml = os.toString("UTF-8");
  41. assertNotNull(xml);
  42. assertTrue(!xml.equals(""));
  43. String docente = xml.split("<DOCENTE>")[1].split("</DOCENTE>")[0].trim();
  44. String nome = xml.split("<NOME>")[1].split("</NOME>")[0].trim();
  45. String tutor = xml.split("<TUTOR>")[1].split("</TUTOR>")[0].trim();
  46. String cdl = xml.split("<CDL>")[1].split("</CDL>")[0].trim();
  47. String durata = xml.split("<DURATA>")[1].split("</DURATA>")[0].trim();
  48. String argomento = xml.split("<ARGOMENTO>")[1].split("</ARGOMENTO>")[0].trim();
  49. String progetto = xml.split("<PROGETTO>")[1].split("</PROGETTO>")[0].trim();
  50. String crediti = xml.split("<CREDITI>")[1].split("</CREDITI>")[0].trim();
  51. assertEquals("ro", docente);
  52. assertEquals("ro", nome);
  53. assertEquals("ds", tutor);
  54. assertEquals("gs", cdl);
  55. assertEquals("g", durata);
  56. assertEquals("gvvv", argomento);
  57. assertEquals("aaaa", progetto);
  58. assertEquals("aa", crediti);
  59. }
  60. }
  61. public void testExportToXMLInverseOrder() throws Exception {
  62. XSSFWorkbook wb = XSSFTestDataSamples
  63. .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx");
  64. MapInfo mapInfo = null;
  65. for (POIXMLDocumentPart p : wb.getRelations()) {
  66. if (!(p instanceof MapInfo)) {
  67. continue;
  68. }
  69. mapInfo = (MapInfo) p;
  70. XSSFMap map = mapInfo.getXSSFMapById(1);
  71. XSSFExportToXml exporter = new XSSFExportToXml(map);
  72. ByteArrayOutputStream os = new ByteArrayOutputStream();
  73. exporter.exportToXML(os, true);
  74. String xml = os.toString("UTF-8");
  75. assertNotNull(xml);
  76. assertTrue(!xml.equals(""));
  77. String docente = xml.split("<DOCENTE>")[1].split("</DOCENTE>")[0].trim();
  78. String nome = xml.split("<NOME>")[1].split("</NOME>")[0].trim();
  79. String tutor = xml.split("<TUTOR>")[1].split("</TUTOR>")[0].trim();
  80. String cdl = xml.split("<CDL>")[1].split("</CDL>")[0].trim();
  81. String durata = xml.split("<DURATA>")[1].split("</DURATA>")[0].trim();
  82. String argomento = xml.split("<ARGOMENTO>")[1].split("</ARGOMENTO>")[0].trim();
  83. String progetto = xml.split("<PROGETTO>")[1].split("</PROGETTO>")[0].trim();
  84. String crediti = xml.split("<CREDITI>")[1].split("</CREDITI>")[0].trim();
  85. assertEquals("aa", nome);
  86. assertEquals("aaaa", docente);
  87. assertEquals("gvvv", tutor);
  88. assertEquals("g", cdl);
  89. assertEquals("gs", durata);
  90. assertEquals("ds", argomento);
  91. assertEquals("ro", progetto);
  92. assertEquals("ro", crediti);
  93. }
  94. }
  95. public void testXPathOrdering() {
  96. XSSFWorkbook wb = XSSFTestDataSamples
  97. .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx");
  98. MapInfo mapInfo = null;
  99. for (POIXMLDocumentPart p : wb.getRelations()) {
  100. if (p instanceof MapInfo) {
  101. mapInfo = (MapInfo) p;
  102. XSSFMap map = mapInfo.getXSSFMapById(1);
  103. XSSFExportToXml exporter = new XSSFExportToXml(map);
  104. assertEquals(1, exporter.compare("/CORSO/DOCENTE", "/CORSO/NOME"));
  105. assertEquals(-1, exporter.compare("/CORSO/NOME", "/CORSO/DOCENTE"));
  106. }
  107. }
  108. }
  109. public void testMultiTable() throws Exception {
  110. XSSFWorkbook wb = XSSFTestDataSamples
  111. .openSampleWorkbook("CustomXMLMappings-complex-type.xlsx");
  112. for (POIXMLDocumentPart p : wb.getRelations()) {
  113. if (p instanceof MapInfo) {
  114. MapInfo mapInfo = (MapInfo) p;
  115. XSSFMap map = mapInfo.getXSSFMapById(2);
  116. assertNotNull(map);
  117. XSSFExportToXml exporter = new XSSFExportToXml(map);
  118. ByteArrayOutputStream os = new ByteArrayOutputStream();
  119. exporter.exportToXML(os, true);
  120. String xml = os.toString("UTF-8");
  121. assertNotNull(xml);
  122. String[] regexConditions = {
  123. "<MapInfo", "</MapInfo>",
  124. "<Schema ID=\"1\" Namespace=\"\" SchemaRef=\"\"/>",
  125. "<Schema ID=\"4\" Namespace=\"\" SchemaRef=\"\"/>",
  126. "DataBinding",
  127. "Map Append=\"false\" AutoFit=\"false\" ID=\"1\"",
  128. "Map Append=\"false\" AutoFit=\"false\" ID=\"5\"",
  129. };
  130. for (String condition : regexConditions) {
  131. Pattern pattern = Pattern.compile(condition);
  132. Matcher matcher = pattern.matcher(xml);
  133. assertTrue(matcher.find());
  134. }
  135. }
  136. }
  137. }
  138. public void test55850ComplexXmlExport() throws Exception {
  139. XSSFWorkbook wb = XSSFTestDataSamples
  140. .openSampleWorkbook("55850.xlsx");
  141. for (POIXMLDocumentPart p : wb.getRelations()) {
  142. if (!(p instanceof MapInfo)) {
  143. continue;
  144. }
  145. MapInfo mapInfo = (MapInfo) p;
  146. XSSFMap map = mapInfo.getXSSFMapById(2);
  147. assertNotNull("XSSFMap is null", map);
  148. XSSFExportToXml exporter = new XSSFExportToXml(map);
  149. ByteArrayOutputStream os = new ByteArrayOutputStream();
  150. exporter.exportToXML(os, true);
  151. String xmlData = os.toString("UTF-8");
  152. assertNotNull(xmlData);
  153. assertTrue(!xmlData.equals(""));
  154. String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
  155. String b = a.split("<B>")[1].split("</B>")[0].trim();
  156. String c = b.split("<C>")[1].split("</C>")[0].trim();
  157. String d = c.split("<D>")[1].split("</Dd>")[0].trim();
  158. String e = d.split("<E>")[1].split("</EA>")[0].trim();
  159. String euro = e.split("<EUR>")[1].split("</EUR>")[0].trim();
  160. String chf = e.split("<CHF>")[1].split("</CHF>")[0].trim();
  161. assertEquals("15", euro);
  162. assertEquals("19", chf);
  163. }
  164. }
  165. }