Search in sources :

Example 86 with OPCPackage

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

the class TestContentTypeManager method testContentType.

/**
     * Test the properties part content parsing.
     */
@Test
public void testContentType() throws Exception {
    String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
    // Retrieves core properties part
    OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ);
    try {
        PackageRelationshipCollection rels = p.getRelationshipsByType(PackageRelationshipTypes.CORE_PROPERTIES);
        PackageRelationship corePropertiesRelationship = rels.getRelationship(0);
        PackagePart coreDocument = p.getPart(corePropertiesRelationship);
        assertEquals("application/vnd.openxmlformats-package.core-properties+xml", coreDocument.getContentType());
        // TODO - finish writing this test
        assumeTrue("finish writing this test", false);
        ContentTypeManager ctm = new ZipContentTypeManager(coreDocument.getInputStream(), p);
        assertNotNull(ctm);
    } finally {
        p.close();
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 87 with OPCPackage

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

the class TestPackagingURIHelper method testCreatePartNameRelativeURI.

/**
	 * Test createPartName(URI, y)
	 */
public void testCreatePartNameRelativeURI() throws Exception {
    PackagePartName partNameToValid = PackagingURIHelper.createPartName("/word/media/image1.gif");
    OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
    // Base part
    PackagePartName nameBase = PackagingURIHelper.createPartName("/word/document.xml");
    PackagePart partBase = pkg.createPart(nameBase, ContentTypes.XML);
    // Relative part name
    PackagePartName relativeName = PackagingURIHelper.createPartName(new URI("media/image1.gif"), partBase);
    assertTrue("The part name must be equal to " + partNameToValid.getName(), partNameToValid.equals(relativeName));
    pkg.revert();
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) URI(java.net.URI)

Example 88 with OPCPackage

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

the class TestOPCComplianceCoreProperties method testOnlyOneCorePropertiesPart.

/**
     * Test M4.1 rule.
     */
@Test
public void testOnlyOneCorePropertiesPart() throws Exception {
    // We have relaxed this check, so we can read the file anyway
    try {
        extractInvalidFormatMessage("OnlyOneCorePropertiesPartFAIL.docx");
        fail("M4.1 should be being relaxed");
    } catch (AssertionFailedError e) {
    // expected here
    }
    // We will use the first core properties, and ignore the others
    InputStream is = OpenXML4JTestDataSamples.openSampleStream("MultipleCoreProperties.docx");
    OPCPackage pkg = OPCPackage.open(is);
    // We can see 2 by type
    assertEquals(2, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
    // But only the first one by relationship
    assertEquals(1, pkg.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES).size());
    // It should be core.xml not the older core1.xml
    assertEquals("/docProps/core.xml", pkg.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES).get(0).getPartName().toString());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AssertionFailedError(junit.framework.AssertionFailedError) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 89 with OPCPackage

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

the class TestOPCComplianceCoreProperties method testCorePropertiesPart.

@Test
public void testCorePropertiesPart() {
    OPCPackage pkg;
    try {
        InputStream is = OpenXML4JTestDataSamples.openComplianceSampleStream("OPCCompliance_CoreProperties_OnlyOneCorePropertiesPart.docx");
        pkg = OPCPackage.open(is);
    } catch (InvalidFormatException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    pkg.revert();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Test(org.junit.Test)

Example 90 with OPCPackage

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

the class TestSignatureInfo method testManipulation.

@Test
public void testManipulation() throws Exception {
    // sign & validate
    String testFile = "hello-world-unsigned.xlsx";
    @SuppressWarnings("resource") OPCPackage pkg = OPCPackage.open(copy(testdata.getFile(testFile)), PackageAccess.READ_WRITE);
    sign(pkg, "Test", "CN=Test", 1);
    // manipulate
    XSSFWorkbook wb = new XSSFWorkbook(pkg);
    wb.setSheetName(0, "manipulated");
    // ... I don't know, why commit is protected ...
    POITestCase.callMethod(XSSFWorkbook.class, wb, Void.class, "commit", new Class[0], new Object[0]);
    // todo: test a manipulation on a package part, which is not signed
    // ... maybe in combination with #56164 
    // validate
    SignatureConfig sic = new SignatureConfig();
    sic.setOpcPackage(pkg);
    SignatureInfo si = new SignatureInfo();
    si.setSignatureConfig(sic);
    boolean b = si.verifySignature();
    assertFalse("signature should be broken", b);
    wb.close();
}
Also used : SignatureInfo(org.apache.poi.poifs.crypt.dsig.SignatureInfo) SignatureConfig(org.apache.poi.poifs.crypt.dsig.SignatureConfig) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) 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