Search in sources :

Example 61 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.

the class TestOPCComplianceCoreProperties method extractInvalidFormatMessage.

private static String extractInvalidFormatMessage(String sampleNameSuffix) {
    InputStream is = OpenXML4JTestDataSamples.openComplianceSampleStream("OPCCompliance_CoreProperties_" + sampleNameSuffix);
    OPCPackage pkg;
    try {
        pkg = OPCPackage.open(is);
    } catch (InvalidFormatException e) {
        // no longer required for successful test
        return e.getMessage();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    pkg.revert();
    throw new AssertionFailedError("expected OPC compliance exception was not thrown");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) AssertionFailedError(junit.framework.AssertionFailedError) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 62 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException 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 63 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException 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)

Example 64 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.

the class TestOPCCompliancePackageModel method testAddRelationshipRelationshipsPartFailure.

/**
     * Try to add a relationship to a relationship part.
     *
     * Check rule M1.25: The Relationships part shall not have relationships to
     * any other part. Package implementers shall enforce this requirement upon
     * the attempt to create such a relationship and shall treat any such
     * relationship as invalid.
     */
@Test
public void testAddRelationshipRelationshipsPartFailure() {
    OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
    PackagePartName name1 = null;
    try {
        name1 = PackagingURIHelper.createPartName("/test/_rels/document.xml.rels");
    } catch (InvalidFormatException e) {
        fail("This exception should never happen !");
    }
    try {
        pkg.addRelationship(name1, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
    } catch (InvalidOperationException e) {
        return;
    }
    fail("Fail test -> M1.25: The Relationships part shall not have relationships to any other part");
}
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 65 with InvalidFormatException

use of org.apache.poi.openxml4j.exceptions.InvalidFormatException in project poi by apache.

the class TestOPCCompliancePartName method testPartNameStartsWithAForwardSlashFailure.

/**
     * A part name shall start with a forward slash ('/') character. [M1.4]
     */
@Test
public void testPartNameStartsWithAForwardSlashFailure() throws URISyntaxException {
    try {
        PackagingURIHelper.createPartName(new URI("document.xml"));
        fail("A part name shall start with a forward slash ('/') character. [M1.4]");
    } catch (InvalidFormatException e) {
    // Normal behaviour
    }
}
Also used : URI(java.net.URI) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Test(org.junit.Test)

Aggregations

InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)72 IOException (java.io.IOException)24 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)22 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)18 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)17 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)16 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)15 InputStream (java.io.InputStream)12 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)11 Test (org.junit.Test)10 URI (java.net.URI)9 POIXMLException (org.apache.poi.POIXMLException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ArrayList (java.util.ArrayList)7 TikaException (org.apache.tika.exception.TikaException)7 XmlException (org.apache.xmlbeans.XmlException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 FileNotFoundException (java.io.FileNotFoundException)6 HashMap (java.util.HashMap)6 Workbook (org.apache.poi.ss.usermodel.Workbook)6