Search in sources :

Example 26 with PackagePartName

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

the class TestContentTypeManager method testContentTypeAddition.

/**
     * Test the addition of several default and override content types.
     */
@Test
public void testContentTypeAddition() throws Exception {
    ContentTypeManager ctm = new ZipContentTypeManager(null, null);
    PackagePartName name1 = PackagingURIHelper.createPartName("/foo/foo.XML");
    PackagePartName name2 = PackagingURIHelper.createPartName("/foo/foo2.xml");
    PackagePartName name3 = PackagingURIHelper.createPartName("/foo/doc.rels");
    PackagePartName name4 = PackagingURIHelper.createPartName("/foo/doc.RELS");
    // Add content types
    ctm.addContentType(name1, "foo-type1");
    ctm.addContentType(name2, "foo-type2");
    ctm.addContentType(name3, "text/xml+rel");
    ctm.addContentType(name4, "text/xml+rel");
    assertEquals(ctm.getContentType(name1), "foo-type1");
    assertEquals(ctm.getContentType(name2), "foo-type2");
    assertEquals(ctm.getContentType(name3), "text/xml+rel");
    assertEquals(ctm.getContentType(name3), "text/xml+rel");
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) Test(org.junit.Test)

Example 27 with PackagePartName

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

the class TestContentTypeManager method testContentTypeRemoval.

/**
     * Test the addition then removal of content types.
     */
@Test
public void testContentTypeRemoval() throws Exception {
    ContentTypeManager ctm = new ZipContentTypeManager(null, null);
    PackagePartName name1 = PackagingURIHelper.createPartName("/foo/foo.xml");
    PackagePartName name2 = PackagingURIHelper.createPartName("/foo/foo2.xml");
    PackagePartName name3 = PackagingURIHelper.createPartName("/foo/doc.rels");
    PackagePartName name4 = PackagingURIHelper.createPartName("/foo/doc.RELS");
    // Add content types
    ctm.addContentType(name1, "foo-type1");
    ctm.addContentType(name2, "foo-type2");
    ctm.addContentType(name3, "text/xml+rel");
    ctm.addContentType(name4, "text/xml+rel");
    ctm.removeContentType(name2);
    ctm.removeContentType(name3);
    assertEquals(ctm.getContentType(name1), "foo-type1");
    assertEquals(ctm.getContentType(name2), "foo-type1");
    assertEquals(ctm.getContentType(name3), null);
    ctm.removeContentType(name1);
    assertEquals(ctm.getContentType(name1), null);
    assertEquals(ctm.getContentType(name2), null);
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) Test(org.junit.Test)

Example 28 with PackagePartName

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

the class TestOPCCompliancePartName method testPartNameComparaison.

/**
     * Part name equivalence is determined by comparing part names as
     * case-insensitive ASCII strings. [M1.12]
     */
@Test
public void testPartNameComparaison() throws Exception {
    String[] partName1 = { "/word/document.xml", "/docProps/core.xml", "/rels/.rels" };
    String[] partName2 = { "/WORD/DocUment.XML", "/docProps/core.xml", "/rels/.rels" };
    for (int i = 0; i < partName1.length || i < partName2.length; ++i) {
        PackagePartName p1 = PackagingURIHelper.createPartName(partName1[i]);
        PackagePartName p2 = PackagingURIHelper.createPartName(partName2[i]);
        assertTrue(p1.equals(p2));
        assertTrue(p1.compareTo(p2) == 0);
        assertTrue(p1.hashCode() == p2.hashCode());
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) Test(org.junit.Test)

Example 29 with PackagePartName

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

the class TestOPCCompliancePackageModel method testPartNameDerivationAdditionFailure.

/**
     * A package implementer shall neither create nor recognize a part with a
     * part name derived from another part name by appending segments to it.
     * [M1.11]
     */
@Test
public void testPartNameDerivationAdditionFailure() {
    OPCPackage pkg = OPCPackage.create("TODELETEIFEXIST.docx");
    try {
        PackagePartName name = PackagingURIHelper.createPartName("/word/document.xml");
        PackagePartName nameDerived = PackagingURIHelper.createPartName("/word/document.xml/image1.gif");
        pkg.createPart(name, ContentTypes.XML);
        pkg.createPart(nameDerived, ContentTypes.EXTENSION_GIF);
    } catch (InvalidOperationException e) {
        pkg.revert();
        return;
    } catch (InvalidFormatException e) {
        fail(e.getMessage());
    }
    fail("A package implementer shall neither create nor recognize a part with a" + " part name derived from another part name by appending segments to it." + " [M1.11]");
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Test(org.junit.Test)

Example 30 with PackagePartName

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

the class TestOPCCompliancePackageModel method testAddPackageAlreadyAddFailure2.

/**
     * Rule M1.12 : Packages shall not contain equivalent part names and package
     * implementers shall neither create nor recognize packages with equivalent
     * part names.
     */
@Test
public void testAddPackageAlreadyAddFailure2() throws Exception {
    OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
    PackagePartName partName = null;
    try {
        partName = PackagingURIHelper.createPartName("/word/document.xml");
    } catch (InvalidFormatException e) {
        throw new Exception(e.getMessage());
    }
    pkg.createPart(partName, ContentTypes.XML);
    try {
        pkg.createPart(partName, ContentTypes.XML);
    } catch (InvalidOperationException e) {
        return;
    }
    fail("Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names. [M1.12]");
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) IOException(java.io.IOException) InvalidOperationException(org.apache.poi.openxml4j.exceptions.InvalidOperationException) PartAlreadyExistsException(org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException) Test(org.junit.Test)

Aggregations

PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)37 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)19 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)19 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)16 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)13 Test (org.junit.Test)10 IOException (java.io.IOException)9 OutputStream (java.io.OutputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 POIXMLException (org.apache.poi.POIXMLException)5 ArrayList (java.util.ArrayList)4 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)4 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)4 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)4 URI (java.net.URI)3 PartAlreadyExistsException (org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException)3 XmlException (org.apache.xmlbeans.XmlException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 NoSuchElementException (java.util.NoSuchElementException)2