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]");
}
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);
}
}
Aggregations