Search in sources :

Example 11 with PackagePartName

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);
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) ByteArrayOutputStream(java.io.ByteArrayOutputStream) POIXMLException(org.apache.poi.POIXMLException) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) NoSuchElementException(java.util.NoSuchElementException) POIXMLException(org.apache.poi.POIXMLException) XLSBUnsupportedException(org.apache.poi.xssf.XLSBUnsupportedException) IOException(java.io.IOException) XmlException(org.apache.xmlbeans.XmlException)

Example 12 with PackagePartName

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();
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 13 with PackagePartName

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]");
}
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 14 with PackagePartName

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());
    }
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) Test(org.junit.Test)

Example 15 with PackagePartName

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);
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

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