Search in sources :

Example 31 with PackageRelationship

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

the class POIXMLFactory method createDocumentPart.

/**
     * Create a POIXMLDocumentPart from existing package part and relation. This method is called
     * from {@link POIXMLDocument#load(POIXMLFactory)} when parsing a document
     *
     * @param parent parent part
     * @param part  the PackagePart representing the created instance
     * @return A new instance of a POIXMLDocumentPart.
     * 
     * @since by POI 3.14-Beta1
     */
public POIXMLDocumentPart createDocumentPart(POIXMLDocumentPart parent, PackagePart part) {
    PackageRelationship rel = getPackageRelationship(parent, part);
    POIXMLRelation descriptor = getDescriptor(rel.getRelationshipType());
    if (descriptor == null || descriptor.getRelationClass() == null) {
        LOGGER.log(POILogger.DEBUG, "using default POIXMLDocumentPart for " + rel.getRelationshipType());
        return new POIXMLDocumentPart(parent, part);
    }
    Class<? extends POIXMLDocumentPart> cls = descriptor.getRelationClass();
    try {
        try {
            return createDocumentPart(cls, PARENT_PART, new Object[] { parent, part });
        } catch (NoSuchMethodException e) {
            return createDocumentPart(cls, ORPHAN_PART, new Object[] { part });
        }
    } catch (Exception e) {
        throw new POIXMLException(e);
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 32 with PackageRelationship

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

the class POIXMLDocumentPart method addRelation.

/**
     * Add a new child POIXMLDocumentPart
     *
     * @param relId the preferred relation id, when null the next free relation id will be used
     * @param relationshipType the package relationship type
     * @param part the child to add
     * 
     * @return the new RelationPart
     *
     * @since 3.14-Beta1
     */
public final RelationPart addRelation(String relId, POIXMLRelation relationshipType, POIXMLDocumentPart part) {
    PackageRelationship pr = this.packagePart.findExistingRelation(part.getPackagePart());
    if (pr == null) {
        PackagePartName ppn = part.getPackagePart().getPartName();
        String relType = relationshipType.getRelation();
        pr = packagePart.addRelationship(ppn, TargetMode.INTERNAL, relType, relId);
    }
    addRelation(pr, part);
    return new RelationPart(pr, part);
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName)

Example 33 with PackageRelationship

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

the class AbstractOOXMLExtractor method loadLinkedRelationships.

/**
     * This is used by the SAX docx and pptx decorators to load hyperlinks and
     * other linked objects
     *
     * @param bodyPart
     * @return
     */
protected Map<String, String> loadLinkedRelationships(PackagePart bodyPart, boolean includeInternal, Metadata metadata) {
    Map<String, String> linkedRelationships = new HashMap<>();
    try {
        PackageRelationshipCollection prc = bodyPart.getRelationshipsByType(XWPFRelation.HYPERLINK.getRelation());
        for (int i = 0; i < prc.size(); i++) {
            PackageRelationship pr = prc.getRelationship(i);
            if (pr == null) {
                continue;
            }
            if (!includeInternal && TargetMode.INTERNAL.equals(pr.getTargetMode())) {
                continue;
            }
            String id = pr.getId();
            String url = (pr.getTargetURI() == null) ? null : pr.getTargetURI().toString();
            if (id != null && url != null) {
                linkedRelationships.put(id, url);
            }
        }
        for (String rel : EMBEDDED_RELATIONSHIPS) {
            prc = bodyPart.getRelationshipsByType(rel);
            for (int i = 0; i < prc.size(); i++) {
                PackageRelationship pr = prc.getRelationship(i);
                if (pr == null) {
                    continue;
                }
                String id = pr.getId();
                String uriString = (pr.getTargetURI() == null) ? null : pr.getTargetURI().toString();
                String fileName = uriString;
                if (pr.getTargetURI() != null) {
                    try {
                        fileName = FileHelper.getFilename(new File(fileName));
                    } catch (Exception e) {
                        fileName = uriString;
                    }
                }
                if (id != null) {
                    fileName = (fileName == null) ? "" : fileName;
                    linkedRelationships.put(id, fileName);
                }
            }
        }
    } catch (InvalidFormatException e) {
        EmbeddedDocumentUtil.recordEmbeddedStreamException(e, metadata);
    }
    return linkedRelationships;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) HashMap(java.util.HashMap) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) File(java.io.File) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) Ole10NativeException(org.apache.poi.poifs.filesystem.Ole10NativeException) TikaException(org.apache.tika.exception.TikaException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XmlException(org.apache.xmlbeans.XmlException) SAXException(org.xml.sax.SAXException)

Example 34 with PackageRelationship

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

the class AbstractOOXMLExtractor method handleThumbnail.

private void handleThumbnail(ContentHandler handler) {
    try {
        OPCPackage opcPackage = extractor.getPackage();
        for (PackageRelationship rel : opcPackage.getRelationshipsByType(PackageRelationshipTypes.THUMBNAIL)) {
            PackagePart tPart = opcPackage.getPart(rel);
            InputStream tStream = tPart.getInputStream();
            Metadata thumbnailMetadata = new Metadata();
            String thumbName = tPart.getPartName().getName();
            thumbnailMetadata.set(Metadata.RESOURCE_NAME_KEY, thumbName);
            AttributesImpl attributes = new AttributesImpl();
            attributes.addAttribute(XHTML, "class", "class", "CDATA", "embedded");
            attributes.addAttribute(XHTML, "id", "id", "CDATA", thumbName);
            handler.startElement(XHTML, "div", "div", attributes);
            handler.endElement(XHTML, "div", "div");
            thumbnailMetadata.set(Metadata.EMBEDDED_RELATIONSHIP_ID, thumbName);
            thumbnailMetadata.set(Metadata.CONTENT_TYPE, tPart.getContentType());
            thumbnailMetadata.set(TikaCoreProperties.TITLE, tPart.getPartName().getName());
            if (embeddedExtractor.shouldParseEmbedded(thumbnailMetadata)) {
                embeddedExtractor.parseEmbedded(TikaInputStream.get(tStream), new EmbeddedContentHandler(handler), thumbnailMetadata, false);
            }
            tStream.close();
        }
    } catch (Exception ex) {
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) AttributesImpl(org.xml.sax.helpers.AttributesImpl) TikaInputStream(org.apache.tika.io.TikaInputStream) InputStream(java.io.InputStream) Metadata(org.apache.tika.metadata.Metadata) EmbeddedContentHandler(org.apache.tika.sax.EmbeddedContentHandler) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Ole10NativeException(org.apache.poi.poifs.filesystem.Ole10NativeException) TikaException(org.apache.tika.exception.TikaException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XmlException(org.apache.xmlbeans.XmlException) SAXException(org.xml.sax.SAXException)

Example 35 with PackageRelationship

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

the class XSSFBExcelExtractorDecorator method extractHyperLinks.

private void extractHyperLinks(PackagePart sheetPart, XHTMLContentHandler xhtml) throws SAXException {
    try {
        for (PackageRelationship rel : sheetPart.getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation())) {
            xhtml.startElement("a", "href", rel.getTargetURI().toString());
            xhtml.characters(rel.getTargetURI().toString());
            xhtml.endElement("a");
        }
    } catch (InvalidFormatException e) {
    //swallow
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Aggregations

PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)50 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)28 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)21 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)15 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)13 IOException (java.io.IOException)11 POIXMLException (org.apache.poi.POIXMLException)8 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)5 TikaException (org.apache.tika.exception.TikaException)5 ArrayList (java.util.ArrayList)4 XmlException (org.apache.xmlbeans.XmlException)4 Test (org.junit.Test)4 InputStream (java.io.InputStream)3 URI (java.net.URI)3 HashMap (java.util.HashMap)3 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)3 SAXException (org.xml.sax.SAXException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2