123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 |
- /* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ==================================================================== */
-
- package org.apache.poi.xssf.extractor;
-
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.util.Date;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
-
- import junit.framework.TestCase;
-
- import org.apache.poi.POIXMLDocumentPart;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.xssf.XSSFTestDataSamples;
- import org.apache.poi.xssf.model.MapInfo;
- import org.apache.poi.xssf.usermodel.XSSFCell;
- import org.apache.poi.xssf.usermodel.XSSFMap;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- import org.junit.Test;
- import org.xml.sax.EntityResolver;
- import org.xml.sax.InputSource;
- import org.xml.sax.SAXException;
-
- /**
- * @author Roberto Manicardi
- */
- public final class TestXSSFExportToXML extends TestCase {
- public void testExportToXML() throws Exception {
-
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");
-
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
-
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(1);
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xml = os.toString("UTF-8");
-
- assertNotNull(xml);
- assertFalse(xml.equals(""));
-
- String docente = xml.split("<DOCENTE>")[1].split("</DOCENTE>")[0].trim();
- String nome = xml.split("<NOME>")[1].split("</NOME>")[0].trim();
- String tutor = xml.split("<TUTOR>")[1].split("</TUTOR>")[0].trim();
- String cdl = xml.split("<CDL>")[1].split("</CDL>")[0].trim();
- String durata = xml.split("<DURATA>")[1].split("</DURATA>")[0].trim();
- String argomento = xml.split("<ARGOMENTO>")[1].split("</ARGOMENTO>")[0].trim();
- String progetto = xml.split("<PROGETTO>")[1].split("</PROGETTO>")[0].trim();
- String crediti = xml.split("<CREDITI>")[1].split("</CREDITI>")[0].trim();
-
- assertEquals("ro", docente);
- assertEquals("ro", nome);
- assertEquals("ds", tutor);
- assertEquals("gs", cdl);
- assertEquals("g", durata);
- assertEquals("gvvv", argomento);
- assertEquals("aaaa", progetto);
- assertEquals("aa", crediti);
-
- parseXML(xml);
-
- found = true;
- }
- assertTrue(found);
- }
-
- public void testExportToXMLInverseOrder() throws Exception {
-
- XSSFWorkbook wb = XSSFTestDataSamples
- .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx");
-
- MapInfo mapInfo = null;
-
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
-
- if (!(p instanceof MapInfo)) {
- continue;
- }
- mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(1);
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xml = os.toString("UTF-8");
-
- assertNotNull(xml);
- assertFalse(xml.equals(""));
-
- String docente = xml.split("<DOCENTE>")[1].split("</DOCENTE>")[0].trim();
- String nome = xml.split("<NOME>")[1].split("</NOME>")[0].trim();
- String tutor = xml.split("<TUTOR>")[1].split("</TUTOR>")[0].trim();
- String cdl = xml.split("<CDL>")[1].split("</CDL>")[0].trim();
- String durata = xml.split("<DURATA>")[1].split("</DURATA>")[0].trim();
- String argomento = xml.split("<ARGOMENTO>")[1].split("</ARGOMENTO>")[0].trim();
- String progetto = xml.split("<PROGETTO>")[1].split("</PROGETTO>")[0].trim();
- String crediti = xml.split("<CREDITI>")[1].split("</CREDITI>")[0].trim();
-
- assertEquals("aa", nome);
- assertEquals("aaaa", docente);
- assertEquals("gvvv", tutor);
- assertEquals("g", cdl);
- assertEquals("gs", durata);
- assertEquals("ds", argomento);
- assertEquals("ro", progetto);
- assertEquals("ro", crediti);
-
- parseXML(xml);
-
- found = true;
- }
- assertTrue(found);
- }
-
- public void testXPathOrdering() {
-
- XSSFWorkbook wb = XSSFTestDataSamples
- .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx");
-
- MapInfo mapInfo = null;
-
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
-
- if (p instanceof MapInfo) {
- mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(1);
- XSSFExportToXml exporter = new XSSFExportToXml(map);
-
- assertEquals(1, exporter.compare("/CORSO/DOCENTE", "/CORSO/NOME"));
- assertEquals(-1, exporter.compare("/CORSO/NOME", "/CORSO/DOCENTE"));
- }
-
- found = true;
- }
- assertTrue(found);
- }
-
- public void testMultiTable() throws Exception {
-
- XSSFWorkbook wb = XSSFTestDataSamples
- .openSampleWorkbook("CustomXMLMappings-complex-type.xlsx");
-
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
-
- if (p instanceof MapInfo) {
- MapInfo mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(2);
-
- assertNotNull(map);
-
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xml = os.toString("UTF-8");
-
- assertNotNull(xml);
-
- String[] regexConditions = {
- "<MapInfo", "</MapInfo>",
- "<Schema ID=\"1\" Namespace=\"\" SchemaRef=\"\"/>",
- "<Schema ID=\"4\" Namespace=\"\" SchemaRef=\"\"/>",
- "DataBinding",
- "Map Append=\"false\" AutoFit=\"false\" ID=\"1\"",
- "Map Append=\"false\" AutoFit=\"false\" ID=\"5\"",
- };
-
- for (String condition : regexConditions) {
- Pattern pattern = Pattern.compile(condition);
- Matcher matcher = pattern.matcher(xml);
- assertTrue(matcher.find());
- }
- }
-
- found = true;
- }
- assertTrue(found);
- }
-
- public void test55850ComplexXmlExport() throws Exception {
-
- XSSFWorkbook wb = XSSFTestDataSamples
- .openSampleWorkbook("55850.xlsx");
-
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
-
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(2);
-
- assertNotNull("XSSFMap is null", map);
-
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xmlData = os.toString("UTF-8");
-
- assertNotNull(xmlData);
- assertFalse(xmlData.equals(""));
-
- String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
- String b = a.split("<B>")[1].split("</B>")[0].trim();
- String c = b.split("<C>")[1].split("</C>")[0].trim();
- String d = c.split("<D>")[1].split("</Dd>")[0].trim();
- String e = d.split("<E>")[1].split("</EA>")[0].trim();
-
- String euro = e.split("<EUR>")[1].split("</EUR>")[0].trim();
- String chf = e.split("<CHF>")[1].split("</CHF>")[0].trim();
-
- assertEquals("15", euro);
- assertEquals("19", chf);
-
- parseXML(xmlData);
-
- found = true;
- }
- assertTrue(found);
- }
-
- public void testFormulaCells_Bugzilla_55927() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx");
-
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
-
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(1);
-
- assertNotNull("XSSFMap is null", map);
-
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xmlData = os.toString("UTF-8");
-
- assertNotNull(xmlData);
- assertFalse(xmlData.equals(""));
-
- String date = xmlData.split("<DATE>")[1].split("</DATE>")[0].trim();
- assertEquals("2012-01-13", date);
-
- parseXML(xmlData);
-
- found = true;
- }
- assertTrue(found);
- }
-
- public void testFormulaCells_Bugzilla_55926() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.xlsx");
-
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
-
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(1);
-
- assertNotNull("XSSFMap is null", map);
-
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xmlData = os.toString("UTF-8");
-
- assertNotNull(xmlData);
- assertFalse(xmlData.equals(""));
-
- String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
- String doubleValue = a.split("<DOUBLE>")[1].split("</DOUBLE>")[0].trim();
- String stringValue = a.split("<STRING>")[1].split("</STRING>")[0].trim();
-
- assertEquals("Hello World", stringValue);
- assertEquals("5.1", doubleValue);
-
- parseXML(xmlData);
-
- found = true;
- }
- assertTrue(found);
- }
-
- @Test
- public void testXmlExportIgnoresEmptyCells_Bugzilla_55924() throws Exception {
-
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx");
-
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
-
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(1);
-
- assertNotNull("XSSFMap is null", map);
-
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xmlData = os.toString("UTF-8");
-
- assertNotNull(xmlData);
- assertFalse(xmlData.equals(""));
-
- String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
- String euro = a.split("<EUR>")[1].split("</EUR>")[0].trim();
- assertEquals("1",euro);
-
- parseXML(xmlData);
-
- found = true;
- }
- assertTrue(found);
- }
-
- public void testXmlExportCompare_Bug_55923() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
-
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
-
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(4);
-
- assertNotNull("XSSFMap is null", map);
-
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- assertEquals(0, exporter.compare("", ""));
- assertEquals(0, exporter.compare("/", "/"));
- assertEquals(0, exporter.compare("//", "//"));
- assertEquals(0, exporter.compare("/a/", "/b/"));
-
- assertEquals(-1, exporter.compare("/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:EUR",
- "/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:CHF"));
-
- found = true;
- }
- assertTrue(found);
- }
-
- public void testXmlExportSchemaOrderingBug_Bugzilla_55923() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
-
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
-
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(4);
-
- assertNotNull("XSSFMap is null", map);
-
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xmlData = os.toString("UTF-8");
-
- assertNotNull(xmlData);
- assertFalse(xmlData.equals(""));
-
- String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
- String a_b = a.split("<B>")[1].split("</B>")[0].trim();
- String a_b_c = a_b.split("<C>")[1].split("</C>")[0].trim();
- String a_b_c_e = a_b_c.split("<E>")[1].split("</EA>")[0].trim();
- String a_b_c_e_euro = a_b_c_e.split("<EUR>")[1].split("</EUR>")[0].trim();
- String a_b_c_e_chf = a_b_c_e.split("<CHF>")[1].split("</CHF>")[0].trim();
-
- assertEquals("1",a_b_c_e_euro);
- assertEquals("2",a_b_c_e_chf);
-
- String a_b_d = a_b.split("<D>")[1].split("</Dd>")[0].trim();
- String a_b_d_e = a_b_d.split("<E>")[1].split("</EA>")[0].trim();
-
- String a_b_d_e_euro = a_b_d_e.split("<EUR>")[1].split("</EUR>")[0].trim();
- String a_b_d_e_chf = a_b_d_e.split("<CHF>")[1].split("</CHF>")[0].trim();
-
- assertEquals("3",a_b_d_e_euro);
- assertEquals("4",a_b_d_e_chf);
-
- found = true;
- }
- assertTrue(found);
- }
-
- private void parseXML(String xmlData) throws IOException, SAXException, ParserConfigurationException {
- DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
- docBuilderFactory.setNamespaceAware(true);
- docBuilderFactory.setValidating(false);
- DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
- docBuilder.setEntityResolver(new DummyEntityResolver());
-
- docBuilder.parse(new ByteArrayInputStream(xmlData.getBytes("UTF-8")));
- }
-
- private static class DummyEntityResolver implements EntityResolver
- {
- @Override
- public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
- {
- return null;
- }
- }
-
- public void testExportDataTypes() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
-
- Sheet sheet = wb.getSheetAt(0);
- Row row = sheet.getRow(0);
-
- Cell cString = row.createCell(0);
- cString.setCellValue("somestring");
- cString.setCellType(XSSFCell.CELL_TYPE_STRING);
-
- Cell cBoolean = row.createCell(1);
- cBoolean.setCellValue(true);
- cBoolean.setCellType(XSSFCell.CELL_TYPE_BOOLEAN);
-
- Cell cError = row.createCell(2);
- cError.setCellType(XSSFCell.CELL_TYPE_ERROR);
-
- Cell cFormulaString = row.createCell(3);
- cFormulaString.setCellFormula("A1");
- cFormulaString.setCellType(XSSFCell.CELL_TYPE_FORMULA);
-
- Cell cFormulaNumeric = row.createCell(4);
- cFormulaNumeric.setCellFormula("F1");
- cFormulaNumeric.setCellType(XSSFCell.CELL_TYPE_FORMULA);
-
- Cell cNumeric = row.createCell(5);
- cNumeric.setCellValue(1.2);
- cNumeric.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
-
- Cell cDate = row.createCell(6);
- cDate.setCellValue(new Date());
- cDate.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
-
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
-
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(4);
-
- assertNotNull("XSSFMap is null", map);
-
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, true);
- String xmlData = os.toString("UTF-8");
-
- assertNotNull(xmlData);
- assertFalse(xmlData.equals(""));
-
- parseXML(xmlData);
-
- found = true;
- }
- assertTrue(found);
- }
-
- public void testValidateFalse() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
-
- boolean found = false;
- for (POIXMLDocumentPart p : wb.getRelations()) {
-
- if (!(p instanceof MapInfo)) {
- continue;
- }
- MapInfo mapInfo = (MapInfo) p;
-
- XSSFMap map = mapInfo.getXSSFMapById(4);
-
- assertNotNull("XSSFMap is null", map);
-
- XSSFExportToXml exporter = new XSSFExportToXml(map);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- exporter.exportToXML(os, false);
- String xmlData = os.toString("UTF-8");
-
- assertNotNull(xmlData);
- assertFalse(xmlData.equals(""));
-
- parseXML(xmlData);
-
- found = true;
- }
- assertTrue(found);
- }
- }
|