Search in sources :

Example 81 with OPCPackage

use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.

the class TestPOIXMLDocument method testVSDXPart.

@Test
public void testVSDXPart() throws IOException {
    POIDataSamples pds = POIDataSamples.getDiagramInstance();
    OPCPackage open = PackageHelper.open(pds.openResourceAsStream("test.vsdx"));
    POIXMLDocumentPart part = new POIXMLDocumentPart(open, PackageRelationshipTypes.VISIO_CORE_DOCUMENT);
    assertNotNull(part);
    assertEquals(0, part.getRelationCounter());
    open.close();
}
Also used : OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 82 with OPCPackage

use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.

the class TestPOIXMLDocument method testGetNextPartNumber.

@Test
public void testGetNextPartNumber() throws Exception {
    POIDataSamples pds = POIDataSamples.getDocumentInstance();
    @SuppressWarnings("resource") OPCPackage pkg = PackageHelper.open(pds.openResourceAsStream("WordWithAttachments.docx"));
    OPCParser doc = new OPCParser(pkg);
    try {
        doc.parse(new TestFactory());
        // Non-indexed parts: Word is taken, Excel is not
        assertEquals(-1, doc.getNextPartNumber(XWPFRelation.DOCUMENT, 0));
        assertEquals(-1, doc.getNextPartNumber(XWPFRelation.DOCUMENT, -1));
        assertEquals(-1, doc.getNextPartNumber(XWPFRelation.DOCUMENT, 99));
        assertEquals(0, doc.getNextPartNumber(XSSFRelation.WORKBOOK, 0));
        assertEquals(0, doc.getNextPartNumber(XSSFRelation.WORKBOOK, -1));
        assertEquals(0, doc.getNextPartNumber(XSSFRelation.WORKBOOK, 99));
        // Indexed parts:
        // Has 2 headers
        assertEquals(0, doc.getNextPartNumber(XWPFRelation.HEADER, 0));
        assertEquals(3, doc.getNextPartNumber(XWPFRelation.HEADER, -1));
        assertEquals(3, doc.getNextPartNumber(XWPFRelation.HEADER, 1));
        assertEquals(8, doc.getNextPartNumber(XWPFRelation.HEADER, 8));
        // Has no Excel Sheets
        assertEquals(0, doc.getNextPartNumber(XSSFRelation.WORKSHEET, 0));
        assertEquals(1, doc.getNextPartNumber(XSSFRelation.WORKSHEET, -1));
        assertEquals(1, doc.getNextPartNumber(XSSFRelation.WORKSHEET, 1));
    } finally {
        doc.close();
    }
}
Also used : OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 83 with OPCPackage

use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.

the class TestPOIXMLDocument method testVSDX.

@Test
public void testVSDX() throws Exception {
    POIDataSamples pds = POIDataSamples.getDiagramInstance();
    @SuppressWarnings("resource") OPCPackage open = PackageHelper.open(pds.openResourceAsStream("test.vsdx"));
    POIXMLDocument part = new OPCParser(open, PackageRelationshipTypes.VISIO_CORE_DOCUMENT);
    assertNotNull(part);
    assertEquals(0, part.getRelationCounter());
    part.close();
}
Also used : OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 84 with OPCPackage

use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.

the class TestXSSFBugs method bug54764.

@Test
public void bug54764() throws IOException, OpenXML4JException, XmlException {
    OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("54764.xlsx");
    // Check the core properties - will be found but empty, due
    //  to the expansion being too much to be considered valid
    POIXMLProperties props = new POIXMLProperties(pkg);
    assertEquals(null, props.getCoreProperties().getTitle());
    assertEquals(null, props.getCoreProperties().getSubject());
    assertEquals(null, props.getCoreProperties().getDescription());
    // Now check the spreadsheet itself
    try {
        new XSSFWorkbook(pkg).close();
        fail("Should fail as too much expansion occurs");
    } catch (POIXMLException e) {
    // Expected
    }
    pkg.close();
    // Try with one with the entities in the Content Types
    try {
        XSSFTestDataSamples.openSamplePackage("54764-2.xlsx").close();
        fail("Should fail as too much expansion occurs");
    } catch (Exception e) {
    // Expected
    }
    // Check we can still parse valid files after all that
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
    assertEquals(3, wb.getNumberOfSheets());
    wb.close();
}
Also used : POIXMLProperties(org.apache.poi.POIXMLProperties) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) POIXMLException(org.apache.poi.POIXMLException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) POIXMLException(org.apache.poi.POIXMLException) XLSBUnsupportedException(org.apache.poi.xssf.XLSBUnsupportedException) IOException(java.io.IOException) XmlException(org.apache.xmlbeans.XmlException) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Test(org.junit.Test)

Example 85 with OPCPackage

use of org.apache.poi.openxml4j.opc.OPCPackage in project poi by apache.

the class TestXSSFBugs method bug57482.

/**
     * A .xlsx file with no Shared Strings table should open fine
     * in read-only mode
     */
@SuppressWarnings("resource")
@Test
public void bug57482() throws IOException, InvalidFormatException {
    for (PackageAccess access : new PackageAccess[] { PackageAccess.READ_WRITE, PackageAccess.READ }) {
        File file = HSSFTestDataSamples.getSampleFile("57482-OnlyNumeric.xlsx");
        OPCPackage pkg = OPCPackage.open(file, access);
        try {
            // Try to open it and read the contents
            XSSFWorkbook wb1 = new XSSFWorkbook(pkg);
            assertNotNull(wb1.getSharedStringSource());
            assertEquals(0, wb1.getSharedStringSource().getCount());
            DataFormatter fmt = new DataFormatter();
            XSSFSheet s = wb1.getSheetAt(0);
            assertEquals("1", fmt.formatCellValue(s.getRow(0).getCell(0)));
            assertEquals("11", fmt.formatCellValue(s.getRow(0).getCell(1)));
            assertEquals("5", fmt.formatCellValue(s.getRow(4).getCell(0)));
            // Add a text cell
            s.getRow(0).createCell(3).setCellValue("Testing");
            assertEquals("Testing", fmt.formatCellValue(s.getRow(0).getCell(3)));
            // Try to write-out and read again, should only work
            //  in read-write mode, not read-only mode
            XSSFWorkbook wb2 = null;
            try {
                wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
                if (access == PackageAccess.READ) {
                    fail("Shouln't be able to write from read-only mode");
                }
                // Check again
                s = wb2.getSheetAt(0);
                assertEquals("1", fmt.formatCellValue(s.getRow(0).getCell(0)));
                assertEquals("11", fmt.formatCellValue(s.getRow(0).getCell(1)));
                assertEquals("5", fmt.formatCellValue(s.getRow(4).getCell(0)));
                assertEquals("Testing", fmt.formatCellValue(s.getRow(0).getCell(3)));
            } catch (InvalidOperationException e) {
                if (access == PackageAccess.READ_WRITE) {
                    // Shouldn't occur in write-mode
                    throw e;
                }
            } finally {
                if (wb2 != null) {
                    wb2.getPackage().revert();
                }
            }
            wb1.getPackage().revert();
        } finally {
            pkg.revert();
        }
    }
}
Also used : PackageAccess(org.apache.poi.openxml4j.opc.PackageAccess) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) TempFile(org.apache.poi.util.TempFile) ZipSecureFile(org.apache.poi.openxml4j.util.ZipSecureFile) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Aggregations

OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)116 Test (org.junit.Test)54 InputStream (java.io.InputStream)29 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)25 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)21 File (java.io.File)19 IOException (java.io.IOException)17 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)13 SignatureConfig (org.apache.poi.poifs.crypt.dsig.SignatureConfig)10 SignatureInfo (org.apache.poi.poifs.crypt.dsig.SignatureInfo)10 XSSFReader (org.apache.poi.xssf.eventusermodel.XSSFReader)10 OutputStream (java.io.OutputStream)9 ArrayList (java.util.ArrayList)9 FileOutputStream (java.io.FileOutputStream)8 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)8 XmlException (org.apache.xmlbeans.XmlException)8 FileInputStream (java.io.FileInputStream)7