Search in sources :

Example 1 with PartAlreadyExistsException

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

the class TestOPCCompliancePackageModel method testAddPackageAlreadyAddFailure.

/**
     * 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 testAddPackageAlreadyAddFailure() throws Exception {
    OPCPackage pkg = OPCPackage.create("DELETEIFEXISTS.docx");
    PackagePartName name1 = null;
    PackagePartName name2 = null;
    try {
        name1 = PackagingURIHelper.createPartName("/word/document.xml");
        name2 = PackagingURIHelper.createPartName("/word/document.xml");
    } catch (InvalidFormatException e) {
        throw new Exception(e.getMessage());
    }
    pkg.createPart(name1, ContentTypes.XML);
    try {
        pkg.createPart(name2, ContentTypes.XML);
    } catch (PartAlreadyExistsException 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) 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) PartAlreadyExistsException(org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException) Test(org.junit.Test)

Example 2 with PartAlreadyExistsException

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

the class POIXMLDocumentPart method createRelationship.

/**
     * Create a new child POIXMLDocumentPart
     *
     * @param descriptor the part descriptor
     * @param factory the factory that will create an instance of the requested relation
     * @param idx part number
     * @param noRelation if true, then no relationship is added.
     * @return the created child POIXMLDocumentPart
     * @throws PartAlreadyExistsException
     *             If rule M1.12 is not verified : Packages shall not contain
     *             equivalent part names and package implementers shall neither
     *             create nor recognize packages with equivalent part names.
     */
protected final RelationPart createRelationship(POIXMLRelation descriptor, POIXMLFactory factory, int idx, boolean noRelation) {
    try {
        PackagePartName ppName = PackagingURIHelper.createPartName(descriptor.getFileName(idx));
        PackageRelationship rel = null;
        PackagePart part = packagePart.getPackage().createPart(ppName, descriptor.getContentType());
        if (!noRelation) {
            /* only add to relations, if according relationship is being created. */
            rel = packagePart.addRelationship(ppName, TargetMode.INTERNAL, descriptor.getRelation());
        }
        POIXMLDocumentPart doc = factory.newDocumentPart(descriptor);
        doc.packagePart = part;
        doc.parent = this;
        if (!noRelation) {
            /* only add to relations, if according relationship is being created. */
            addRelation(rel, doc);
        }
        return new RelationPart(rel, doc);
    } catch (PartAlreadyExistsException pae) {
        //  that the name is already taken
        throw pae;
    } catch (Exception e) {
        // Give a general wrapped exception for the problem
        throw new POIXMLException(e);
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) IOException(java.io.IOException) PartAlreadyExistsException(org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException) PartAlreadyExistsException(org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException)

Aggregations

IOException (java.io.IOException)2 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)2 PartAlreadyExistsException (org.apache.poi.openxml4j.exceptions.PartAlreadyExistsException)2 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)2 InvalidOperationException (org.apache.poi.openxml4j.exceptions.InvalidOperationException)1 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)1 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)1 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)1 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)1 Test (org.junit.Test)1