Search in sources :

Example 36 with PackagePart

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

the class TestPOIXMLDocument method assertReadWrite.

public void assertReadWrite(OPCPackage pkg1) throws Exception {
    OPCParser doc = new OPCParser(pkg1);
    doc.parse(new TestFactory());
    traverse(doc);
    File tmp = TempFile.createTempFile("poi-ooxml", ".tmp");
    FileOutputStream out = new FileOutputStream(tmp);
    doc.write(out);
    out.close();
    // Should not be able to write to an output stream that has been closed
    try {
        doc.write(out);
        fail("Should not be able to write to an output stream that has been closed.");
    } catch (final OpenXML4JRuntimeException e) {
        // see {@link org.apache.poi.openxml4j.opc.ZipPackage#saveImpl(java.io.OutputStream)}
        if (e.getMessage().matches("Fail to save: an error occurs while saving the package : The part .+ failed to be saved in the stream with marshaller .+")) {
        // expected
        } else {
            throw e;
        }
    }
    // Should not be able to write a document that has been closed
    doc.close();
    try {
        doc.write(new NullOutputStream());
        fail("Should not be able to write a document that has been closed.");
    } catch (final IOException e) {
        if (e.getMessage().equals("Cannot write data, document seems to have been closed already")) {
        // expected
        } else {
            throw e;
        }
    }
    // Should be able to close a document multiple times, though subsequent closes will have no effect.
    doc.close();
    @SuppressWarnings("resource") OPCPackage pkg2 = OPCPackage.open(tmp.getAbsolutePath());
    doc = new OPCParser(pkg1);
    try {
        doc.parse(new TestFactory());
        traverse(doc);
        assertEquals(pkg1.getRelationships().size(), pkg2.getRelationships().size());
        ArrayList<PackagePart> l1 = pkg1.getParts();
        ArrayList<PackagePart> l2 = pkg2.getParts();
        assertEquals(l1.size(), l2.size());
        for (int i = 0; i < l1.size(); i++) {
            PackagePart p1 = l1.get(i);
            PackagePart p2 = l2.get(i);
            assertEquals(p1.getContentType(), p2.getContentType());
            assertEquals(p1.hasRelationships(), p2.hasRelationships());
            if (p1.hasRelationships()) {
                assertEquals(p1.getRelationships().size(), p2.getRelationships().size());
            }
            assertEquals(p1.getPartName(), p2.getPartName());
        }
    } finally {
        doc.close();
        pkg1.close();
        pkg2.close();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) TempFile(org.apache.poi.util.TempFile) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) OpenXML4JRuntimeException(org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException) NullOutputStream(org.apache.poi.util.NullOutputStream)

Example 37 with PackagePart

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

the class TestXSLFSlideShow method testContainsMainContentType.

@Test
public void testContainsMainContentType() throws Exception {
    boolean found = false;
    for (PackagePart part : pack.getParts()) {
        if (part.getContentType().equals(XSLFRelation.MAIN.getContentType())) {
            found = true;
        }
    }
    assertTrue(found);
}
Also used : PackagePart(org.apache.poi.openxml4j.opc.PackagePart) Test(org.junit.Test)

Example 38 with PackagePart

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

the class TestXMLSlideShow method testContainsMainContentType.

@Test
public void testContainsMainContentType() throws Exception {
    boolean found = false;
    for (PackagePart part : pack.getParts()) {
        if (part.getContentType().equals(XSLFRelation.MAIN.getContentType())) {
            found = true;
        }
    }
    assertTrue(found);
}
Also used : PackagePart(org.apache.poi.openxml4j.opc.PackagePart) Test(org.junit.Test)

Example 39 with PackagePart

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

the class TestXSSFWorkbook method bug47668.

/**
     * Verify that the attached test data was not modified. If this test method
     * fails, the test data is not working properly.
     */
@Test
public void bug47668() throws Exception {
    XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("47668.xlsx");
    List<XSSFPictureData> allPictures = workbook.getAllPictures();
    assertEquals(1, allPictures.size());
    PackagePartName imagePartName = PackagingURIHelper.createPartName("/xl/media/image1.jpeg");
    PackagePart imagePart = workbook.getPackage().getPart(imagePartName);
    assertNotNull(imagePart);
    for (XSSFPictureData pictureData : allPictures) {
        PackagePart picturePart = pictureData.getPackagePart();
        assertSame(imagePart, picturePart);
    }
    XSSFSheet sheet0 = workbook.getSheetAt(0);
    XSSFDrawing drawing0 = sheet0.createDrawingPatriarch();
    XSSFPictureData pictureData0 = (XSSFPictureData) drawing0.getRelations().get(0);
    byte[] data0 = pictureData0.getData();
    CRC32 crc0 = new CRC32();
    crc0.update(data0);
    XSSFSheet sheet1 = workbook.getSheetAt(1);
    XSSFDrawing drawing1 = sheet1.createDrawingPatriarch();
    XSSFPictureData pictureData1 = (XSSFPictureData) drawing1.getRelations().get(0);
    byte[] data1 = pictureData1.getData();
    CRC32 crc1 = new CRC32();
    crc1.update(data1);
    assertEquals(crc0.getValue(), crc1.getValue());
    workbook.close();
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) CRC32(java.util.zip.CRC32) MemoryPackagePart(org.apache.poi.openxml4j.opc.internal.MemoryPackagePart) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) Test(org.junit.Test)

Example 40 with PackagePart

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

the class TestXWPFDocument method testContainsMainContentType.

@Test
public void testContainsMainContentType() throws Exception {
    XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
    OPCPackage pack = doc.getPackage();
    boolean found = false;
    for (PackagePart part : pack.getParts()) {
        if (part.getContentType().equals(XWPFRelation.DOCUMENT.getContentType())) {
            found = true;
        }
    //            if (false) {
    //                // successful tests should be silent
    //                System.out.println(part);
    //            }
    }
    assertTrue(found);
    pack.close();
    doc.close();
}
Also used : PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Aggregations

PackagePart (org.apache.poi.openxml4j.opc.PackagePart)118 OutputStream (java.io.OutputStream)38 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)27 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)25 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)24 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)23 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)19 QName (javax.xml.namespace.QName)18 IOException (java.io.IOException)17 XmlOptions (org.apache.xmlbeans.XmlOptions)17 InputStream (java.io.InputStream)11 Test (org.junit.Test)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 POIXMLException (org.apache.poi.POIXMLException)8 XmlException (org.apache.xmlbeans.XmlException)8 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)7 ArrayList (java.util.ArrayList)6 TikaException (org.apache.tika.exception.TikaException)6 URI (java.net.URI)5 SAXException (org.xml.sax.SAXException)5