Search in sources :

Example 76 with PackagePart

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

the class POIXMLDocumentPart method read.

/**
     * Iterate through the underlying PackagePart and create child POIXMLFactory instances
     * using the specified factory
     *
     * @param factory   the factory object that creates POIXMLFactory instances
     * @param context   context map containing already visited noted keyed by targetURI
     * 
     * @throws OpenXML4JException thrown when a related part can't be read
     */
protected void read(POIXMLFactory factory, Map<PackagePart, POIXMLDocumentPart> context) throws OpenXML4JException {
    PackagePart pp = getPackagePart();
    // add mapping a second time, in case of initial caller hasn't done so
    POIXMLDocumentPart otherChild = context.put(pp, this);
    if (otherChild != null && otherChild != this) {
        throw new POIXMLException("Unique PackagePart-POIXMLDocumentPart relation broken!");
    }
    if (!pp.hasRelationships())
        return;
    PackageRelationshipCollection rels = packagePart.getRelationships();
    List<POIXMLDocumentPart> readLater = new ArrayList<POIXMLDocumentPart>();
    // scan breadth-first, so parent-relations are hopefully the shallowest element
    for (PackageRelationship rel : rels) {
        if (rel.getTargetMode() == TargetMode.INTERNAL) {
            URI uri = rel.getTargetURI();
            // check for internal references (e.g. '#Sheet1!A1')
            PackagePartName relName;
            if (uri.getRawFragment() != null) {
                relName = PackagingURIHelper.createPartName(uri.getPath());
            } else {
                relName = PackagingURIHelper.createPartName(uri);
            }
            final PackagePart p = packagePart.getPackage().getPart(relName);
            if (p == null) {
                logger.log(POILogger.ERROR, "Skipped invalid entry " + rel.getTargetURI());
                continue;
            }
            POIXMLDocumentPart childPart = context.get(p);
            if (childPart == null) {
                childPart = factory.createDocumentPart(this, p);
                childPart.parent = this;
                // already add child to context, so other children can reference it
                context.put(p, childPart);
                readLater.add(childPart);
            }
            addRelation(rel, childPart);
        }
    }
    for (POIXMLDocumentPart childPart : readLater) {
        childPart.read(factory, context);
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) ArrayList(java.util.ArrayList) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) URI(java.net.URI)

Example 77 with PackagePart

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

the class XWPFSettings method commit.

@Override
protected void commit() throws IOException {
    if (ctSettings == null) {
        throw new IllegalStateException("Unable to write out settings that were never read in!");
    }
    XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
    xmlOptions.setSaveSyntheticDocumentElement(new QName(CTSettings.type.getName().getNamespaceURI(), "settings"));
    PackagePart part = getPackagePart();
    OutputStream out = part.getOutputStream();
    ctSettings.save(out, xmlOptions);
    out.close();
}
Also used : QName(javax.xml.namespace.QName) XmlOptions(org.apache.xmlbeans.XmlOptions) OutputStream(java.io.OutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 78 with PackagePart

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

the class XWPFFootnotes method commit.

@Override
protected void commit() throws IOException {
    XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
    xmlOptions.setSaveSyntheticDocumentElement(new QName(CTFootnotes.type.getName().getNamespaceURI(), "footnotes"));
    PackagePart part = getPackagePart();
    OutputStream out = part.getOutputStream();
    ctFootnotes.save(out, xmlOptions);
    out.close();
}
Also used : QName(javax.xml.namespace.QName) XmlOptions(org.apache.xmlbeans.XmlOptions) OutputStream(java.io.OutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 79 with PackagePart

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

the class AbstractOOXMLExtractor method handleEmbeddedPart.

private void handleEmbeddedPart(PackagePart source, PackageRelationship rel, ContentHandler handler, Metadata parentMetadata, Set<String> seen) throws IOException, SAXException, TikaException, InvalidFormatException {
    URI targetURI = rel.getTargetURI();
    if (targetURI != null) {
        if (seen.contains(targetURI.toString())) {
            return;
        }
        seen.add(targetURI.toString());
    }
    URI sourceURI = rel.getSourceURI();
    String sourceDesc;
    if (sourceURI != null) {
        sourceDesc = getJustFileName(sourceURI.getPath());
        if (sourceDesc.startsWith("slide")) {
            sourceDesc += "_";
        } else {
            sourceDesc = "";
        }
    } else {
        sourceDesc = "";
    }
    if (rel.getTargetMode() != TargetMode.INTERNAL) {
        return;
    }
    PackagePart target;
    try {
        target = source.getRelatedPart(rel);
    } catch (IllegalArgumentException ex) {
        return;
    }
    String type = rel.getRelationshipType();
    if (RELATION_OLE_OBJECT.equals(type) && TYPE_OLE_OBJECT.equals(target.getContentType())) {
        handleEmbeddedOLE(target, handler, sourceDesc + rel.getId(), parentMetadata);
    } else if (RELATION_AUDIO.equals(type) || RELATION_IMAGE.equals(type) || RELATION_PACKAGE.equals(type) || RELATION_OLE_OBJECT.equals(type)) {
        handleEmbeddedFile(target, handler, sourceDesc + rel.getId());
    } else if (RELATION_MACRO.equals(type)) {
        handleMacros(target, handler);
    }
}
Also used : PackagePart(org.apache.poi.openxml4j.opc.PackagePart) URI(java.net.URI)

Example 80 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart 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)

Aggregations

PackagePart (org.apache.poi.openxml4j.opc.PackagePart)118 OutputStream (java.io.OutputStream)38 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)27 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)25 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)24 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)23 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)19 QName (javax.xml.namespace.QName)18 IOException (java.io.IOException)17 XmlOptions (org.apache.xmlbeans.XmlOptions)17 InputStream (java.io.InputStream)11 Test (org.junit.Test)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 POIXMLException (org.apache.poi.POIXMLException)8 XmlException (org.apache.xmlbeans.XmlException)8 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)7 ArrayList (java.util.ArrayList)6 TikaException (org.apache.tika.exception.TikaException)6 URI (java.net.URI)5 SAXException (org.xml.sax.SAXException)5