Example usage for org.apache.poi.openxml4j.opc PackageRelationshipTypes CORE_DOCUMENT

List of usage examples for org.apache.poi.openxml4j.opc PackageRelationshipTypes CORE_DOCUMENT

Introduction

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

Prototype

String CORE_DOCUMENT

To view the source code for org.apache.poi.openxml4j.opc PackageRelationshipTypes CORE_DOCUMENT.

Click Source Link

Document

Core document relationship type.

Usage

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

License:Apache License

/**
 * Detects the type of an OfficeOpenXML (OOXML) file from
 *  opened Package // www.  j a  v a2  s  . 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);
}