Search in sources :

Example 26 with PackageRelationshipCollection

use of org.apache.poi.openxml4j.opc.PackageRelationshipCollection in project tika by apache.

the class SXWPFWordExtractorDecorator method loadNumbering.

private XWPFNumbering loadNumbering(PackagePart packagePart) {
    try {
        PackageRelationshipCollection numberingParts = packagePart.getRelationshipsByType(XWPFRelation.NUMBERING.getRelation());
        if (numberingParts.size() > 0) {
            PackageRelationship numberingRelationShip = numberingParts.getRelationship(0);
            if (numberingRelationShip == null) {
                return null;
            }
            PackagePart numberingPart = packagePart.getRelatedPart(numberingRelationShip);
            if (numberingPart == null) {
                return null;
            }
            return new XWPFNumberingShim(numberingPart);
        }
    } catch (IOException | OpenXML4JException e) {
    //swallow
    }
    return null;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) OpenXML4JException(org.apache.poi.openxml4j.exceptions.OpenXML4JException) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) IOException(java.io.IOException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) XWPFNumberingShim(org.apache.tika.parser.microsoft.ooxml.xwpf.XWPFNumberingShim)

Example 27 with PackageRelationshipCollection

use of org.apache.poi.openxml4j.opc.PackageRelationshipCollection in project tika by apache.

the class SXWPFWordExtractorDecorator method loadStyles.

private XWPFStylesShim loadStyles(PackagePart packagePart) throws InvalidFormatException, TikaException, IOException, SAXException {
    PackageRelationshipCollection stylesParts = packagePart.getRelationshipsByType(XWPFRelation.STYLES.getRelation());
    if (stylesParts.size() > 0) {
        PackageRelationship stylesRelationShip = stylesParts.getRelationship(0);
        if (stylesRelationShip == null) {
            return null;
        }
        PackagePart stylesPart = packagePart.getRelatedPart(stylesRelationShip);
        if (stylesPart == null) {
            return null;
        }
        return new XWPFStylesShim(stylesPart, context);
    }
    return null;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) XWPFStylesShim(org.apache.tika.parser.microsoft.ooxml.xwpf.XWPFStylesShim) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 28 with PackageRelationshipCollection

use of org.apache.poi.openxml4j.opc.PackageRelationshipCollection in project poi by apache.

the class POIXMLDocument method getRelatedByType.

/**
     * Retrieves all the PackageParts which are defined as relationships of the base document with the
     * specified content type.
     * 
     * @param contentType the content type
     * 
     * @return all the base document PackageParts which match the content type
     * 
     * @throws InvalidFormatException when the relationships or the parts contain errors
     * 
     * @see org.apache.poi.xssf.usermodel.XSSFRelation
     * @see org.apache.poi.xslf.usermodel.XSLFRelation
     * @see org.apache.poi.xwpf.usermodel.XWPFRelation
     * @see org.apache.poi.xdgf.usermodel.XDGFRelation
     */
protected PackagePart[] getRelatedByType(String contentType) throws InvalidFormatException {
    PackageRelationshipCollection partsC = getPackagePart().getRelationshipsByType(contentType);
    PackagePart[] parts = new PackagePart[partsC.size()];
    int count = 0;
    for (PackageRelationship rel : partsC) {
        parts[count] = getPackagePart().getRelatedPart(rel);
        count++;
    }
    return parts;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 29 with PackageRelationshipCollection

use of org.apache.poi.openxml4j.opc.PackageRelationshipCollection in project poi by apache.

the class TestContentTypeManager method testContentType.

/**
     * Test the properties part content parsing.
     */
@Test
public void testContentType() throws Exception {
    String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
    // Retrieves core properties part
    OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ);
    try {
        PackageRelationshipCollection rels = p.getRelationshipsByType(PackageRelationshipTypes.CORE_PROPERTIES);
        PackageRelationship corePropertiesRelationship = rels.getRelationship(0);
        PackagePart coreDocument = p.getPart(corePropertiesRelationship);
        assertEquals("application/vnd.openxmlformats-package.core-properties+xml", coreDocument.getContentType());
        // TODO - finish writing this test
        assumeTrue("finish writing this test", false);
        ContentTypeManager ctm = new ZipContentTypeManager(coreDocument.getInputStream(), p);
        assertNotNull(ctm);
    } finally {
        p.close();
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Aggregations

PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)29 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)23 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)15 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)14 IOException (java.io.IOException)8 XmlException (org.apache.xmlbeans.XmlException)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)4 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)4 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)4 XWPFRelation (org.apache.poi.xwpf.usermodel.XWPFRelation)4 TikaException (org.apache.tika.exception.TikaException)4 SAXException (org.xml.sax.SAXException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 OutputStream (java.io.OutputStream)2 URI (java.net.URI)2 ZipException (java.util.zip.ZipException)2 XMLSignatureException (javax.xml.crypto.dsig.XMLSignatureException)2