Example usage for org.apache.poi.openxml4j.opc OPCPackage getRelationshipsByType

List of usage examples for org.apache.poi.openxml4j.opc OPCPackage getRelationshipsByType

Introduction

In this page you can find the example usage for org.apache.poi.openxml4j.opc OPCPackage getRelationshipsByType.

Prototype

@Override
public PackageRelationshipCollection getRelationshipsByType(String relationshipType) 

Source Link

Document

Retrieves all relationships with the specified type.

Usage

From source file:mj.ocraptor.extraction.tika.parser.pkg.ZipContainerDetector.java

License:Apache License

/**
 * Detects the type of an OfficeOpenXML (OOXML) file from
 *  opened Package //from   w w w  .j  a  va 2 s  . co m
 */
public static MediaType detectOfficeOpenXML(OPCPackage pkg) {
    PackageRelationshipCollection core = pkg.getRelationshipsByType(ExtractorFactory.CORE_DOCUMENT_REL);
    if (core.size() != 1) {
        // Invalid OOXML Package received
        return null;
    }

    // Get the type of the core document part
    PackagePart corePart = pkg.getPart(core.getRelationship(0));
    String coreType = corePart.getContentType();

    // Turn that into the type of the overall document
    String docType = coreType.substring(0, coreType.lastIndexOf('.'));

    // The Macro Enabled formats are a little special
    if (docType.toLowerCase().endsWith("macroenabled")) {
        docType = docType.toLowerCase() + ".12";
    }

    if (docType.toLowerCase().endsWith("macroenabledtemplate")) {
        docType = MACRO_TEMPLATE_PATTERN.matcher(docType).replaceAll("macroenabled.12");
    }

    // Build the MediaType object and return
    return MediaType.parse(docType);
}

From source file:org.alfresco.repo.content.transform.OOXMLThumbnailContentTransformer.java

License:Open Source License

@Override
protected void transformInternal(ContentReader reader, ContentWriter writer, TransformationOptions options)
        throws Exception {
    final String sourceMimetype = reader.getMimetype();
    final String sourceExtension = getMimetypeService().getExtension(sourceMimetype);
    final String targetMimetype = writer.getMimetype();

    if (log.isDebugEnabled()) {
        StringBuilder msg = new StringBuilder();
        msg.append("Transforming from ").append(sourceMimetype).append(" to ").append(targetMimetype);
        log.debug(msg.toString());/*  www .j  a  v  a  2s.  co  m*/
    }

    OPCPackage pkg = null;
    try {
        File ooxmlTempFile = TempFileProvider.createTempFile(this.getClass().getSimpleName() + "_ooxml",
                sourceExtension);
        reader.getContent(ooxmlTempFile);

        // Load the file
        pkg = OPCPackage.open(ooxmlTempFile.getPath());

        // Does it have a thumbnail?
        PackageRelationshipCollection rels = pkg.getRelationshipsByType(PackageRelationshipTypes.THUMBNAIL);
        if (rels.size() > 0) {
            // Get the thumbnail part
            PackageRelationship tRel = rels.getRelationship(0);
            PackagePart tPart = pkg.getPart(tRel);

            // Write it to the target
            InputStream tStream = tPart.getInputStream();
            writer.putContent(tStream);
            tStream.close();
        } else {
            log.debug("No thumbnail present in " + reader.toString());
            throw new UnimportantTransformException(NO_THUMBNAIL_PRESENT_IN_FILE + targetMimetype);
        }
    } catch (IOException e) {
        throw new AlfrescoRuntimeException("Unable to transform " + sourceExtension + " file.", e);
    } finally {
        if (pkg != null) {
            pkg.close();
        }
    }
}

From source file:org.apache.tika.parser.microsoft.ooxml.AbstractOOXMLExtractor.java

License:Apache License

private void handleThumbnail(ContentHandler handler) {
    try {/*from   w  w w.j  av a 2 s  . c om*/
        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) {

    }
}

From source file:org.apache.tika.parser.pkg.ZipContainerDetector.java

License:Apache License

/**
 * Detects the type of an OfficeOpenXML (OOXML) file from
 *  opened Package // w  w  w  .ja  va  2s . c o  m
 */
public static MediaType detectOfficeOpenXML(OPCPackage pkg) {
    // Check for the normal Office core document
    PackageRelationshipCollection core = pkg.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT);
    // Otherwise check for some other Office core document types
    if (core.size() == 0) {
        core = pkg.getRelationshipsByType(STRICT_CORE_DOCUMENT);
    }
    if (core.size() == 0) {
        core = pkg.getRelationshipsByType(VISIO_DOCUMENT);
    }

    // If we didn't find a single core document of any type, skip detection
    if (core.size() != 1) {
        // Invalid OOXML Package received
        return null;
    }

    // Get the type of the core document part
    PackagePart corePart = pkg.getPart(core.getRelationship(0));
    String coreType = corePart.getContentType();

    // Turn that into the type of the overall document
    String docType = coreType.substring(0, coreType.lastIndexOf('.'));

    // The Macro Enabled formats are a little special
    if (docType.toLowerCase(Locale.ROOT).endsWith("macroenabled")) {
        docType = docType.toLowerCase(Locale.ROOT) + ".12";
    }

    if (docType.toLowerCase(Locale.ROOT).endsWith("macroenabledtemplate")) {
        docType = MACRO_TEMPLATE_PATTERN.matcher(docType).replaceAll("macroenabled.12");
    }

    // Build the MediaType object and return
    return MediaType.parse(docType);
}

From source file:org.apache.tika.parser.pkg.ZipContainerDetector.java

License:Apache License

/**
 * Detects Open XML Paper Specification (XPS)
 *//*from   w w  w . j  a v a  2s  .c o m*/
private static MediaType detectXPSOPC(OPCPackage pkg) {
    PackageRelationshipCollection xps = pkg
            .getRelationshipsByType("http://schemas.microsoft.com/xps/2005/06/fixedrepresentation");
    if (xps.size() == 1) {
        return MediaType.application("vnd.ms-xpsdocument");
    } else {
        // Non-XPS Package received
        return null;
    }
}

From source file:org.apache.tika.parser.pkg.ZipContainerDetector.java

License:Apache License

/**
 * Detects AutoCAD formats that live in OPC packaging
 *//*from  ww  w . j  a  va  2 s  .co  m*/
private static MediaType detectAutoCADOPC(OPCPackage pkg) {
    PackageRelationshipCollection dwfxSeq = pkg
            .getRelationshipsByType("http://schemas.autodesk.com/dwfx/2007/relationships/documentsequence");
    if (dwfxSeq.size() == 1) {
        return MediaType.parse("model/vnd.dwfx+xps");
    } else {
        // Non-AutoCAD Package received
        return null;
    }
}