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.

TestXSSFExportToXML.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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.ByteArrayInputStream;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.IOException;
  19. import java.util.Date;
  20. import java.util.regex.Matcher;
  21. import java.util.regex.Pattern;
  22. import javax.xml.parsers.DocumentBuilder;
  23. import javax.xml.parsers.DocumentBuilderFactory;
  24. import javax.xml.parsers.ParserConfigurationException;
  25. import junit.framework.TestCase;
  26. import org.apache.poi.POIXMLDocumentPart;
  27. import org.apache.poi.ss.usermodel.Cell;
  28. import org.apache.poi.ss.usermodel.Row;
  29. import org.apache.poi.ss.usermodel.Sheet;
  30. import org.apache.poi.xssf.XSSFTestDataSamples;
  31. import org.apache.poi.xssf.model.MapInfo;
  32. import org.apache.poi.xssf.usermodel.XSSFCell;
  33. import org.apache.poi.xssf.usermodel.XSSFMap;
  34. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  35. import org.junit.Test;
  36. import org.xml.sax.EntityResolver;
  37. import org.xml.sax.InputSource;
  38. import org.xml.sax.SAXException;
  39. /**
  40. * @author Roberto Manicardi
  41. */
  42. public final class TestXSSFExportToXML extends TestCase {
  43. public void testExportToXML() throws Exception {
  44. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");
  45. boolean found = false;
  46. for (POIXMLDocumentPart p : wb.getRelations()) {
  47. if (!(p instanceof MapInfo)) {
  48. continue;
  49. }
  50. MapInfo mapInfo = (MapInfo) p;
  51. XSSFMap map = mapInfo.getXSSFMapById(1);
  52. XSSFExportToXml exporter = new XSSFExportToXml(map);
  53. ByteArrayOutputStream os = new ByteArrayOutputStream();
  54. exporter.exportToXML(os, true);
  55. String xml = os.toString("UTF-8");
  56. assertNotNull(xml);
  57. assertFalse(xml.equals(""));
  58. String docente = xml.split("<DOCENTE>")[1].split("</DOCENTE>")[0].trim();
  59. String nome = xml.split("<NOME>")[1].split("</NOME>")[0].trim();
  60. String tutor = xml.split("<TUTOR>")[1].split("</TUTOR>")[0].trim();
  61. String cdl = xml.split("<CDL>")[1].split("</CDL>")[0].trim();
  62. String durata = xml.split("<DURATA>")[1].split("</DURATA>")[0].trim();
  63. String argomento = xml.split("<ARGOMENTO>")[1].split("</ARGOMENTO>")[0].trim();
  64. String progetto = xml.split("<PROGETTO>")[1].split("</PROGETTO>")[0].trim();
  65. String crediti = xml.split("<CREDITI>")[1].split("</CREDITI>")[0].trim();
  66. assertEquals("ro", docente);
  67. assertEquals("ro", nome);
  68. assertEquals("ds", tutor);
  69. assertEquals("gs", cdl);
  70. assertEquals("g", durata);
  71. assertEquals("gvvv", argomento);
  72. assertEquals("aaaa", progetto);
  73. assertEquals("aa", crediti);
  74. parseXML(xml);
  75. found = true;
  76. }
  77. assertTrue(found);
  78. }
  79. public void testExportToXMLInverseOrder() throws Exception {
  80. XSSFWorkbook wb = XSSFTestDataSamples
  81. .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx");
  82. MapInfo mapInfo = null;
  83. boolean found = false;
  84. for (POIXMLDocumentPart p : wb.getRelations()) {
  85. if (!(p instanceof MapInfo)) {
  86. continue;
  87. }
  88. mapInfo = (MapInfo) p;
  89. XSSFMap map = mapInfo.getXSSFMapById(1);
  90. XSSFExportToXml exporter = new XSSFExportToXml(map);
  91. ByteArrayOutputStream os = new ByteArrayOutputStream();
  92. exporter.exportToXML(os, true);
  93. String xml = os.toString("UTF-8");
  94. assertNotNull(xml);
  95. assertFalse(xml.equals(""));
  96. String docente = xml.split("<DOCENTE>")[1].split("</DOCENTE>")[0].trim();
  97. String nome = xml.split("<NOME>")[1].split("</NOME>")[0].trim();
  98. String tutor = xml.split("<TUTOR>")[1].split("</TUTOR>")[0].trim();
  99. String cdl = xml.split("<CDL>")[1].split("</CDL>")[0].trim();
  100. String durata = xml.split("<DURATA>")[1].split("</DURATA>")[0].trim();
  101. String argomento = xml.split("<ARGOMENTO>")[1].split("</ARGOMENTO>")[0].trim();
  102. String progetto = xml.split("<PROGETTO>")[1].split("</PROGETTO>")[0].trim();
  103. String crediti = xml.split("<CREDITI>")[1].split("</CREDITI>")[0].trim();
  104. assertEquals("aa", nome);
  105. assertEquals("aaaa", docente);
  106. assertEquals("gvvv", tutor);
  107. assertEquals("g", cdl);
  108. assertEquals("gs", durata);
  109. assertEquals("ds", argomento);
  110. assertEquals("ro", progetto);
  111. assertEquals("ro", crediti);
  112. parseXML(xml);
  113. found = true;
  114. }
  115. assertTrue(found);
  116. }
  117. public void testXPathOrdering() {
  118. XSSFWorkbook wb = XSSFTestDataSamples
  119. .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx");
  120. MapInfo mapInfo = null;
  121. boolean found = false;
  122. for (POIXMLDocumentPart p : wb.getRelations()) {
  123. if (p instanceof MapInfo) {
  124. mapInfo = (MapInfo) p;
  125. XSSFMap map = mapInfo.getXSSFMapById(1);
  126. XSSFExportToXml exporter = new XSSFExportToXml(map);
  127. assertEquals(1, exporter.compare("/CORSO/DOCENTE", "/CORSO/NOME"));
  128. assertEquals(-1, exporter.compare("/CORSO/NOME", "/CORSO/DOCENTE"));
  129. }
  130. found = true;
  131. }
  132. assertTrue(found);
  133. }
  134. public void testMultiTable() throws Exception {
  135. XSSFWorkbook wb = XSSFTestDataSamples
  136. .openSampleWorkbook("CustomXMLMappings-complex-type.xlsx");
  137. boolean found = false;
  138. for (POIXMLDocumentPart p : wb.getRelations()) {
  139. if (p instanceof MapInfo) {
  140. MapInfo mapInfo = (MapInfo) p;
  141. XSSFMap map = mapInfo.getXSSFMapById(2);
  142. assertNotNull(map);
  143. XSSFExportToXml exporter = new XSSFExportToXml(map);
  144. ByteArrayOutputStream os = new ByteArrayOutputStream();
  145. exporter.exportToXML(os, true);
  146. String xml = os.toString("UTF-8");
  147. assertNotNull(xml);
  148. String[] regexConditions = {
  149. "<MapInfo", "</MapInfo>",
  150. "<Schema ID=\"1\" Namespace=\"\" SchemaRef=\"\"/>",
  151. "<Schema ID=\"4\" Namespace=\"\" SchemaRef=\"\"/>",
  152. "DataBinding",
  153. "Map Append=\"false\" AutoFit=\"false\" ID=\"1\"",
  154. "Map Append=\"false\" AutoFit=\"false\" ID=\"5\"",
  155. };
  156. for (String condition : regexConditions) {
  157. Pattern pattern = Pattern.compile(condition);
  158. Matcher matcher = pattern.matcher(xml);
  159. assertTrue(matcher.find());
  160. }
  161. }
  162. found = true;
  163. }
  164. assertTrue(found);
  165. }
  166. public void test55850ComplexXmlExport() throws Exception {
  167. XSSFWorkbook wb = XSSFTestDataSamples
  168. .openSampleWorkbook("55850.xlsx");
  169. boolean found = false;
  170. for (POIXMLDocumentPart p : wb.getRelations()) {
  171. if (!(p instanceof MapInfo)) {
  172. continue;
  173. }
  174. MapInfo mapInfo = (MapInfo) p;
  175. XSSFMap map = mapInfo.getXSSFMapById(2);
  176. assertNotNull("XSSFMap is null", map);
  177. XSSFExportToXml exporter = new XSSFExportToXml(map);
  178. ByteArrayOutputStream os = new ByteArrayOutputStream();
  179. exporter.exportToXML(os, true);
  180. String xmlData = os.toString("UTF-8");
  181. assertNotNull(xmlData);
  182. assertFalse(xmlData.equals(""));
  183. String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
  184. String b = a.split("<B>")[1].split("</B>")[0].trim();
  185. String c = b.split("<C>")[1].split("</C>")[0].trim();
  186. String d = c.split("<D>")[1].split("</Dd>")[0].trim();
  187. String e = d.split("<E>")[1].split("</EA>")[0].trim();
  188. String euro = e.split("<EUR>")[1].split("</EUR>")[0].trim();
  189. String chf = e.split("<CHF>")[1].split("</CHF>")[0].trim();
  190. assertEquals("15", euro);
  191. assertEquals("19", chf);
  192. parseXML(xmlData);
  193. found = true;
  194. }
  195. assertTrue(found);
  196. }
  197. public void testFormulaCells_Bugzilla_55927() throws Exception {
  198. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx");
  199. boolean found = false;
  200. for (POIXMLDocumentPart p : wb.getRelations()) {
  201. if (!(p instanceof MapInfo)) {
  202. continue;
  203. }
  204. MapInfo mapInfo = (MapInfo) p;
  205. XSSFMap map = mapInfo.getXSSFMapById(1);
  206. assertNotNull("XSSFMap is null", map);
  207. XSSFExportToXml exporter = new XSSFExportToXml(map);
  208. ByteArrayOutputStream os = new ByteArrayOutputStream();
  209. exporter.exportToXML(os, true);
  210. String xmlData = os.toString("UTF-8");
  211. assertNotNull(xmlData);
  212. assertFalse(xmlData.equals(""));
  213. String date = xmlData.split("<DATE>")[1].split("</DATE>")[0].trim();
  214. assertEquals("2012-01-13", date);
  215. parseXML(xmlData);
  216. found = true;
  217. }
  218. assertTrue(found);
  219. }
  220. public void testFormulaCells_Bugzilla_55926() throws Exception {
  221. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.xlsx");
  222. boolean found = false;
  223. for (POIXMLDocumentPart p : wb.getRelations()) {
  224. if (!(p instanceof MapInfo)) {
  225. continue;
  226. }
  227. MapInfo mapInfo = (MapInfo) p;
  228. XSSFMap map = mapInfo.getXSSFMapById(1);
  229. assertNotNull("XSSFMap is null", map);
  230. XSSFExportToXml exporter = new XSSFExportToXml(map);
  231. ByteArrayOutputStream os = new ByteArrayOutputStream();
  232. exporter.exportToXML(os, true);
  233. String xmlData = os.toString("UTF-8");
  234. assertNotNull(xmlData);
  235. assertFalse(xmlData.equals(""));
  236. String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
  237. String doubleValue = a.split("<DOUBLE>")[1].split("</DOUBLE>")[0].trim();
  238. String stringValue = a.split("<STRING>")[1].split("</STRING>")[0].trim();
  239. assertEquals("Hello World", stringValue);
  240. assertEquals("5.1", doubleValue);
  241. parseXML(xmlData);
  242. found = true;
  243. }
  244. assertTrue(found);
  245. }
  246. @Test
  247. public void testXmlExportIgnoresEmptyCells_Bugzilla_55924() throws Exception {
  248. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx");
  249. boolean found = false;
  250. for (POIXMLDocumentPart p : wb.getRelations()) {
  251. if (!(p instanceof MapInfo)) {
  252. continue;
  253. }
  254. MapInfo mapInfo = (MapInfo) p;
  255. XSSFMap map = mapInfo.getXSSFMapById(1);
  256. assertNotNull("XSSFMap is null", map);
  257. XSSFExportToXml exporter = new XSSFExportToXml(map);
  258. ByteArrayOutputStream os = new ByteArrayOutputStream();
  259. exporter.exportToXML(os, true);
  260. String xmlData = os.toString("UTF-8");
  261. assertNotNull(xmlData);
  262. assertFalse(xmlData.equals(""));
  263. String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
  264. String euro = a.split("<EUR>")[1].split("</EUR>")[0].trim();
  265. assertEquals("1",euro);
  266. parseXML(xmlData);
  267. found = true;
  268. }
  269. assertTrue(found);
  270. }
  271. public void testXmlExportCompare_Bug_55923() throws Exception {
  272. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
  273. boolean found = false;
  274. for (POIXMLDocumentPart p : wb.getRelations()) {
  275. if (!(p instanceof MapInfo)) {
  276. continue;
  277. }
  278. MapInfo mapInfo = (MapInfo) p;
  279. XSSFMap map = mapInfo.getXSSFMapById(4);
  280. assertNotNull("XSSFMap is null", map);
  281. XSSFExportToXml exporter = new XSSFExportToXml(map);
  282. assertEquals(0, exporter.compare("", ""));
  283. assertEquals(0, exporter.compare("/", "/"));
  284. assertEquals(0, exporter.compare("//", "//"));
  285. assertEquals(0, exporter.compare("/a/", "/b/"));
  286. assertEquals(-1, exporter.compare("/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:EUR",
  287. "/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:CHF"));
  288. found = true;
  289. }
  290. assertTrue(found);
  291. }
  292. public void testXmlExportSchemaOrderingBug_Bugzilla_55923() throws Exception {
  293. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
  294. boolean found = false;
  295. for (POIXMLDocumentPart p : wb.getRelations()) {
  296. if (!(p instanceof MapInfo)) {
  297. continue;
  298. }
  299. MapInfo mapInfo = (MapInfo) p;
  300. XSSFMap map = mapInfo.getXSSFMapById(4);
  301. assertNotNull("XSSFMap is null", map);
  302. XSSFExportToXml exporter = new XSSFExportToXml(map);
  303. ByteArrayOutputStream os = new ByteArrayOutputStream();
  304. exporter.exportToXML(os, true);
  305. String xmlData = os.toString("UTF-8");
  306. assertNotNull(xmlData);
  307. assertFalse(xmlData.equals(""));
  308. String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
  309. String a_b = a.split("<B>")[1].split("</B>")[0].trim();
  310. String a_b_c = a_b.split("<C>")[1].split("</C>")[0].trim();
  311. String a_b_c_e = a_b_c.split("<E>")[1].split("</EA>")[0].trim();
  312. String a_b_c_e_euro = a_b_c_e.split("<EUR>")[1].split("</EUR>")[0].trim();
  313. String a_b_c_e_chf = a_b_c_e.split("<CHF>")[1].split("</CHF>")[0].trim();
  314. assertEquals("1",a_b_c_e_euro);
  315. assertEquals("2",a_b_c_e_chf);
  316. String a_b_d = a_b.split("<D>")[1].split("</Dd>")[0].trim();
  317. String a_b_d_e = a_b_d.split("<E>")[1].split("</EA>")[0].trim();
  318. String a_b_d_e_euro = a_b_d_e.split("<EUR>")[1].split("</EUR>")[0].trim();
  319. String a_b_d_e_chf = a_b_d_e.split("<CHF>")[1].split("</CHF>")[0].trim();
  320. assertEquals("3",a_b_d_e_euro);
  321. assertEquals("4",a_b_d_e_chf);
  322. found = true;
  323. }
  324. assertTrue(found);
  325. }
  326. private void parseXML(String xmlData) throws IOException, SAXException, ParserConfigurationException {
  327. DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  328. docBuilderFactory.setNamespaceAware(true);
  329. docBuilderFactory.setValidating(false);
  330. DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
  331. docBuilder.setEntityResolver(new DummyEntityResolver());
  332. docBuilder.parse(new ByteArrayInputStream(xmlData.getBytes("UTF-8")));
  333. }
  334. private static class DummyEntityResolver implements EntityResolver
  335. {
  336. @Override
  337. public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
  338. {
  339. return null;
  340. }
  341. }
  342. public void testExportDataTypes() throws Exception {
  343. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
  344. Sheet sheet = wb.getSheetAt(0);
  345. Row row = sheet.getRow(0);
  346. Cell cString = row.createCell(0);
  347. cString.setCellValue("somestring");
  348. cString.setCellType(XSSFCell.CELL_TYPE_STRING);
  349. Cell cBoolean = row.createCell(1);
  350. cBoolean.setCellValue(true);
  351. cBoolean.setCellType(XSSFCell.CELL_TYPE_BOOLEAN);
  352. Cell cError = row.createCell(2);
  353. cError.setCellType(XSSFCell.CELL_TYPE_ERROR);
  354. Cell cFormulaString = row.createCell(3);
  355. cFormulaString.setCellFormula("A1");
  356. cFormulaString.setCellType(XSSFCell.CELL_TYPE_FORMULA);
  357. Cell cFormulaNumeric = row.createCell(4);
  358. cFormulaNumeric.setCellFormula("F1");
  359. cFormulaNumeric.setCellType(XSSFCell.CELL_TYPE_FORMULA);
  360. Cell cNumeric = row.createCell(5);
  361. cNumeric.setCellValue(1.2);
  362. cNumeric.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
  363. Cell cDate = row.createCell(6);
  364. cDate.setCellValue(new Date());
  365. cDate.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
  366. boolean found = false;
  367. for (POIXMLDocumentPart p : wb.getRelations()) {
  368. if (!(p instanceof MapInfo)) {
  369. continue;
  370. }
  371. MapInfo mapInfo = (MapInfo) p;
  372. XSSFMap map = mapInfo.getXSSFMapById(4);
  373. assertNotNull("XSSFMap is null", map);
  374. XSSFExportToXml exporter = new XSSFExportToXml(map);
  375. ByteArrayOutputStream os = new ByteArrayOutputStream();
  376. exporter.exportToXML(os, true);
  377. String xmlData = os.toString("UTF-8");
  378. assertNotNull(xmlData);
  379. assertFalse(xmlData.equals(""));
  380. parseXML(xmlData);
  381. found = true;
  382. }
  383. assertTrue(found);
  384. }
  385. public void testValidateFalse() throws Exception {
  386. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
  387. boolean found = false;
  388. for (POIXMLDocumentPart p : wb.getRelations()) {
  389. if (!(p instanceof MapInfo)) {
  390. continue;
  391. }
  392. MapInfo mapInfo = (MapInfo) p;
  393. XSSFMap map = mapInfo.getXSSFMapById(4);
  394. assertNotNull("XSSFMap is null", map);
  395. XSSFExportToXml exporter = new XSSFExportToXml(map);
  396. ByteArrayOutputStream os = new ByteArrayOutputStream();
  397. exporter.exportToXML(os, false);
  398. String xmlData = os.toString("UTF-8");
  399. assertNotNull(xmlData);
  400. assertFalse(xmlData.equals(""));
  401. parseXML(xmlData);
  402. found = true;
  403. }
  404. assertTrue(found);
  405. }
  406. }