use of org.apache.poi.openxml4j.opc.PackagePartName in project poi by apache.
the class XSSFWorkbook method newPackage.
/**
* Create a new SpreadsheetML package and setup the default minimal content
*/
protected static OPCPackage newPackage(XSSFWorkbookType workbookType) {
try {
OPCPackage pkg = OPCPackage.create(new ByteArrayOutputStream());
// Main part
PackagePartName corePartName = PackagingURIHelper.createPartName(XSSFRelation.WORKBOOK.getDefaultFileName());
// Create main part relationship
pkg.addRelationship(corePartName, TargetMode.INTERNAL, PackageRelationshipTypes.CORE_DOCUMENT);
// Create main document part
pkg.createPart(corePartName, workbookType.getContentType());
pkg.getPackageProperties().setCreatorProperty(DOCUMENT_CREATOR);
return pkg;
} catch (Exception e) {
throw new POIXMLException(e);
}
}
use of org.apache.poi.openxml4j.opc.PackagePartName in project poi by apache.
the class TestPackagingURIHelper method testCreatePartNameRelativeString.
/**
* Test createPartName(String, y)
*/
public void testCreatePartNameRelativeString() throws InvalidFormatException {
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("media/image1.gif", partBase);
assertTrue("The part name must be equal to " + partNameToValid.getName(), partNameToValid.equals(relativeName));
pkg.revert();
}
use of org.apache.poi.openxml4j.opc.PackagePartName 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.opc.PackagePartName in project poi by apache.
the class TestOPCCompliancePartName method testPartNameComparaisonFailure.
/**
* Part name equivalence is determined by comparing part names as
* case-insensitive ASCII strings. [M1.12].
*
* All the comparisons MUST FAIL !
*/
@Test
public void testPartNameComparaisonFailure() throws Exception {
String[] partName1 = { "/word/document.xml", "/docProps/core.xml", "/rels/.rels" };
String[] partName2 = { "/WORD/DocUment.XML2", "/docProp/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]);
assertFalse(p1.equals(p2));
assertFalse(p1.compareTo(p2) == 0);
assertFalse(p1.hashCode() == p2.hashCode());
}
}
use of org.apache.poi.openxml4j.opc.PackagePartName in project tika by apache.
the class XSLFPowerPointExtractorDecorator method addSlideParts.
private void addSlideParts(PackagePart slidePart, List<PackagePart> parts) {
for (String relation : new String[] { XSLFRelation.VML_DRAWING.getRelation(), XSLFRelation.SLIDE_LAYOUT.getRelation(), XSLFRelation.NOTES_MASTER.getRelation(), XSLFRelation.NOTES.getRelation() }) {
try {
for (PackageRelationship packageRelationship : slidePart.getRelationshipsByType(relation)) {
if (packageRelationship.getTargetMode() == TargetMode.INTERNAL) {
PackagePartName relName = PackagingURIHelper.createPartName(packageRelationship.getTargetURI());
parts.add(packageRelationship.getPackage().getPart(relName));
}
}
} catch (InvalidFormatException e) {
}
}
//and slide of course
parts.add(slidePart);
}
Aggregations