Search in sources :

Example 6 with MapInfo

use of org.apache.poi.xssf.model.MapInfo in project poi by apache.

the class TestXSSFExportToXML method testXmlExportCompare_Bug_55923.

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);
}
Also used : POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) MapInfo(org.apache.poi.xssf.model.MapInfo)

Example 7 with MapInfo

use of org.apache.poi.xssf.model.MapInfo in project poi by apache.

the class XSSFWorkbook method onDocumentRead.

@Override
protected void onDocumentRead() throws IOException {
    try {
        WorkbookDocument doc = WorkbookDocument.Factory.parse(getPackagePart().getInputStream(), DEFAULT_XML_OPTIONS);
        this.workbook = doc.getWorkbook();
        ThemesTable theme = null;
        Map<String, XSSFSheet> shIdMap = new HashMap<String, XSSFSheet>();
        Map<String, ExternalLinksTable> elIdMap = new HashMap<String, ExternalLinksTable>();
        for (RelationPart rp : getRelationParts()) {
            POIXMLDocumentPart p = rp.getDocumentPart();
            if (p instanceof SharedStringsTable) {
                sharedStringSource = (SharedStringsTable) p;
            } else if (p instanceof StylesTable) {
                stylesSource = (StylesTable) p;
            } else if (p instanceof ThemesTable) {
                theme = (ThemesTable) p;
            } else if (p instanceof CalculationChain) {
                calcChain = (CalculationChain) p;
            } else if (p instanceof MapInfo) {
                mapInfo = (MapInfo) p;
            } else if (p instanceof XSSFSheet) {
                shIdMap.put(rp.getRelationship().getId(), (XSSFSheet) p);
            } else if (p instanceof ExternalLinksTable) {
                elIdMap.put(rp.getRelationship().getId(), (ExternalLinksTable) p);
            }
        }
        boolean packageReadOnly = (getPackage().getPackageAccess() == PackageAccess.READ);
        if (stylesSource == null) {
            // Create Styles if it is missing
            if (packageReadOnly) {
                stylesSource = new StylesTable();
            } else {
                stylesSource = (StylesTable) createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
            }
        }
        stylesSource.setWorkbook(this);
        stylesSource.setTheme(theme);
        if (sharedStringSource == null) {
            // Create SST if it is missing
            if (packageReadOnly) {
                sharedStringSource = new SharedStringsTable();
            } else {
                sharedStringSource = (SharedStringsTable) createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
            }
        }
        // Load individual sheets. The order of sheets is defined by the order
        //  of CTSheet elements in the workbook
        sheets = new ArrayList<XSSFSheet>(shIdMap.size());
        for (CTSheet ctSheet : this.workbook.getSheets().getSheetArray()) {
            parseSheet(shIdMap, ctSheet);
        }
        // Load the external links tables. Their order is defined by the order 
        //  of CTExternalReference elements in the workbook
        externalLinks = new ArrayList<ExternalLinksTable>(elIdMap.size());
        if (this.workbook.isSetExternalReferences()) {
            for (CTExternalReference er : this.workbook.getExternalReferences().getExternalReferenceArray()) {
                ExternalLinksTable el = elIdMap.get(er.getId());
                if (el == null) {
                    logger.log(POILogger.WARN, "ExternalLinksTable with r:id " + er.getId() + " was defined, but didn't exist in package, skipping");
                    continue;
                }
                externalLinks.add(el);
            }
        }
        // Process the named ranges
        reprocessNamedRanges();
    } catch (XmlException e) {
        throw new POIXMLException(e);
    }
}
Also used : WorkbookDocument(org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument) ThemesTable(org.apache.poi.xssf.model.ThemesTable) HashMap(java.util.HashMap) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) CTExternalReference(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTExternalReference) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) StylesTable(org.apache.poi.xssf.model.StylesTable) POIXMLException(org.apache.poi.POIXMLException) CalculationChain(org.apache.poi.xssf.model.CalculationChain) SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) CTSheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet) XmlException(org.apache.xmlbeans.XmlException) MapInfo(org.apache.poi.xssf.model.MapInfo) ExternalLinksTable(org.apache.poi.xssf.model.ExternalLinksTable)

Example 8 with MapInfo

use of org.apache.poi.xssf.model.MapInfo in project poi by apache.

the class TestXSSFExportToXML method testRefElementsInXmlSchema_Bugzilla_56730.

public void testRefElementsInXmlSchema_Bugzilla_56730() throws Exception {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56730.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(""));
        assertEquals("2014-12-31", xmlData.split("<DATE>")[1].split("</DATE>")[0].trim());
        assertEquals("12.5", xmlData.split("<REFELEMENT>")[1].split("</REFELEMENT>")[0].trim());
        parseXML(xmlData);
        found = true;
    }
    assertTrue(found);
}
Also used : POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) MapInfo(org.apache.poi.xssf.model.MapInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 9 with MapInfo

use of org.apache.poi.xssf.model.MapInfo in project poi by apache.

the class TestXSSFExportToXML method testMultiTable.

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);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) MapInfo(org.apache.poi.xssf.model.MapInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 10 with MapInfo

use of org.apache.poi.xssf.model.MapInfo in project poi by apache.

the class TestXSSFExportToXML method testExportToXMLInverseOrder.

public void testExportToXMLInverseOrder() throws Exception {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXmlMappings-inverse-order.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("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);
}
Also used : POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) MapInfo(org.apache.poi.xssf.model.MapInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)14 MapInfo (org.apache.poi.xssf.model.MapInfo)14 XSSFMap (org.apache.poi.xssf.usermodel.XSSFMap)13 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 ArrayListValuedHashMap (org.apache.commons.collections4.multimap.ArrayListValuedHashMap)1 POIXMLException (org.apache.poi.POIXMLException)1 Cell (org.apache.poi.ss.usermodel.Cell)1 Row (org.apache.poi.ss.usermodel.Row)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 CalculationChain (org.apache.poi.xssf.model.CalculationChain)1 ExternalLinksTable (org.apache.poi.xssf.model.ExternalLinksTable)1 SharedStringsTable (org.apache.poi.xssf.model.SharedStringsTable)1 StylesTable (org.apache.poi.xssf.model.StylesTable)1 ThemesTable (org.apache.poi.xssf.model.ThemesTable)1 XmlException (org.apache.xmlbeans.XmlException)1