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

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

Introduction

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

Prototype

public PackagePart getPart(PackageRelationship partRel) 

Source Link

Document

Get the target part from the specified relationship.

Usage

From source file:com.hp.autonomy.frontend.reports.powerpoint.PowerPointServiceImpl.java

License:MIT License

/**
 * Utility function to generate a new unique package part name within a PowerPoint zip file, given a base name
 *   which has a number before the file extension.
 * @param opcPackage the PowerPoint zip package.
 * @param baseName the original name./* w  w  w . j ava 2 s  .c o m*/
 * @return a new unique package part name with an incremented number if the old name had a number before the file extension,
 *         or the old name otherwise.
 * @throws InvalidFormatException if there was an exception while generating the new name.
 */
private static PackagePartName generateNewName(final OPCPackage opcPackage, final String baseName)
        throws InvalidFormatException {
    final Pattern pattern = Pattern.compile("(.*?)(\\d+)(\\.\\w+)?$");

    final Matcher matcher = pattern.matcher(baseName);

    if (matcher.find()) {
        int num = Integer.parseInt(matcher.group(2));

        for (int ii = num + 1; ii < Integer.MAX_VALUE; ++ii) {
            final PackagePartName testName = PackagingURIHelper
                    .createPartName(matcher.group(1) + ii + matcher.group(3));

            if (opcPackage.getPart(testName) == null) {
                return testName;
            }
        }
    }

    // If the document doesn't have a numeric extension, just return it
    return PackagingURIHelper.createPartName(baseName);
}

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  . java 2s.  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());/*from www  .j  av a 2  s.c  om*/
    }

    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 www  .ja va 2s.  co m*/
        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 /*  ww  w  .java2 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);
}