Search in sources :

Example 6 with PackageRelationshipCollection

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

the class TestXSSFWorkbook method testSetVBAProject.

/**
     * Tests that we can save a workbook with macros and reload it.
     */
@Test
public void testSetVBAProject() throws Exception {
    File file;
    final byte[] allBytes = new byte[256];
    for (int i = 0; i < 256; i++) {
        allBytes[i] = (byte) (i - 128);
    }
    XSSFWorkbook wb1 = new XSSFWorkbook();
    wb1.createSheet();
    wb1.setVBAProject(new ByteArrayInputStream(allBytes));
    file = TempFile.createTempFile("poi-", ".xlsm");
    OutputStream out = new FileOutputStream(file);
    wb1.write(out);
    out.close();
    wb1.close();
    // Check the package contains what we'd expect it to
    OPCPackage pkg = OPCPackage.open(file.toString());
    PackagePart wbPart = pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
    assertTrue(wbPart.hasRelationships());
    final PackageRelationshipCollection relationships = wbPart.getRelationships().getRelationships(XSSFRelation.VBA_MACROS.getRelation());
    assertEquals(1, relationships.size());
    assertEquals(XSSFRelation.VBA_MACROS.getDefaultFileName(), relationships.getRelationship(0).getTargetURI().toString());
    PackagePart vbaPart = pkg.getPart(PackagingURIHelper.createPartName(XSSFRelation.VBA_MACROS.getDefaultFileName()));
    assertNotNull(vbaPart);
    assertFalse(vbaPart.isRelationshipPart());
    assertEquals(XSSFRelation.VBA_MACROS.getContentType(), vbaPart.getContentType());
    final byte[] fromFile = IOUtils.toByteArray(vbaPart.getInputStream());
    assertArrayEquals(allBytes, fromFile);
    // Load back the XSSFWorkbook just to check nothing explodes
    @SuppressWarnings("resource") XSSFWorkbook wb2 = new XSSFWorkbook(pkg);
    assertEquals(1, wb2.getNumberOfSheets());
    assertEquals(XSSFWorkbookType.XLSM, wb2.getWorkbookType());
    pkg.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) MemoryPackagePart(org.apache.poi.openxml4j.opc.internal.MemoryPackagePart) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) TempFile(org.apache.poi.util.TempFile) File(java.io.File) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 7 with PackageRelationshipCollection

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

the class ZipContainerDetector method detectOfficeOpenXML.

/**
     * Detects the type of an OfficeOpenXML (OOXML) file from
     *  opened Package 
     */
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);
}
Also used : PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 8 with PackageRelationshipCollection

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

the class OOXMLExtractorFactory method trySXSLF.

private static POIXMLTextExtractor trySXSLF(OPCPackage pkg) throws XmlException, OpenXML4JException, IOException {
    PackageRelationshipCollection packageRelationshipCollection = pkg.getRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument");
    if (packageRelationshipCollection.size() == 0) {
        packageRelationshipCollection = pkg.getRelationshipsByType("http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument");
    }
    if (packageRelationshipCollection.size() == 0) {
        return null;
    }
    PackagePart corePart = pkg.getPart(packageRelationshipCollection.getRelationship(0));
    String targetContentType = corePart.getContentType();
    XSLFRelation[] xslfRelations = org.apache.poi.xslf.extractor.XSLFPowerPointExtractor.SUPPORTED_TYPES;
    for (int i = 0; i < xslfRelations.length; i++) {
        XSLFRelation xslfRelation = xslfRelations[i];
        if (xslfRelation.getContentType().equals(targetContentType)) {
            return new XSLFEventBasedPowerPointExtractor(pkg);
        }
    }
    if (XSLFRelation.THEME_MANAGER.getContentType().equals(targetContentType)) {
        return new XSLFEventBasedPowerPointExtractor(pkg);
    }
    return null;
}
Also used : PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) XSLFRelation(org.apache.poi.xslf.usermodel.XSLFRelation) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) XSLFEventBasedPowerPointExtractor(org.apache.tika.parser.microsoft.ooxml.xslf.XSLFEventBasedPowerPointExtractor)

Example 9 with PackageRelationshipCollection

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

the class SXSLFPowerPointExtractorDecorator method handleBasicRelatedParts.

/**
     * This should handle the comments, master, notes, etc
     *
     * @param contentType
     * @param xhtmlClassLabel
     * @param parentPart
     * @param contentHandler
     */
private void handleBasicRelatedParts(String contentType, String xhtmlClassLabel, PackagePart parentPart, ContentHandler contentHandler) throws SAXException {
    PackageRelationshipCollection relatedPartPRC = null;
    try {
        relatedPartPRC = parentPart.getRelationshipsByType(contentType);
    } catch (InvalidFormatException e) {
        metadata.add(TikaCoreProperties.TIKA_META_EXCEPTION_WARNING, ExceptionUtils.getStackTrace(e));
    }
    if (relatedPartPRC != null && relatedPartPRC.size() > 0) {
        AttributesImpl attributes = new AttributesImpl();
        attributes.addAttribute("", "class", "class", "CDATA", xhtmlClassLabel);
        contentHandler.startElement("", "div", "div", attributes);
        for (int i = 0; i < relatedPartPRC.size(); i++) {
            PackageRelationship relatedPartPackageRelationship = relatedPartPRC.getRelationship(i);
            try {
                PackagePart relatedPartPart = parentPart.getRelatedPart(relatedPartPackageRelationship);
                try (InputStream stream = relatedPartPart.getInputStream()) {
                    context.getSAXParser().parse(stream, new OfflineContentHandler(new EmbeddedContentHandler(contentHandler)));
                } catch (IOException | TikaException e) {
                    metadata.add(TikaCoreProperties.TIKA_META_EXCEPTION_WARNING, ExceptionUtils.getStackTrace(e));
                }
            } catch (InvalidFormatException e) {
                metadata.add(TikaCoreProperties.TIKA_META_EXCEPTION_WARNING, ExceptionUtils.getStackTrace(e));
            }
        }
        contentHandler.endElement("", "div", "div");
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) AttributesImpl(org.xml.sax.helpers.AttributesImpl) OfflineContentHandler(org.apache.tika.sax.OfflineContentHandler) TikaException(org.apache.tika.exception.TikaException) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) InputStream(java.io.InputStream) EmbeddedContentHandler(org.apache.tika.sax.EmbeddedContentHandler) IOException(java.io.IOException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 10 with PackageRelationshipCollection

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

the class SXWPFWordExtractorDecorator method addRelatedParts.

private void addRelatedParts(PackagePart documentPart, List<PackagePart> relatedParts) {
    for (String relation : MAIN_PART_RELATIONS) {
        PackageRelationshipCollection prc = null;
        try {
            prc = documentPart.getRelationshipsByType(relation);
            if (prc != null) {
                for (int i = 0; i < prc.size(); i++) {
                    PackagePart packagePart = documentPart.getRelatedPart(prc.getRelationship(i));
                    relatedParts.add(packagePart);
                }
            }
        } catch (InvalidFormatException e) {
        }
    }
}
Also used : PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Aggregations

PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)29 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)23 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)15 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)14 IOException (java.io.IOException)8 XmlException (org.apache.xmlbeans.XmlException)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)4 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)4 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)4 XWPFRelation (org.apache.poi.xwpf.usermodel.XWPFRelation)4 TikaException (org.apache.tika.exception.TikaException)4 SAXException (org.xml.sax.SAXException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 OutputStream (java.io.OutputStream)2 URI (java.net.URI)2 ZipException (java.util.zip.ZipException)2 XMLSignatureException (javax.xml.crypto.dsig.XMLSignatureException)2