Search in sources :

Example 16 with PackageRelationship

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

the class TestXSSFBugs method bug51470.

@Test
public void bug51470() throws IOException {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51470.xlsx");
    XSSFSheet sh0 = wb.getSheetAt(0);
    XSSFSheet sh1 = wb.cloneSheet(0);
    List<RelationPart> rels0 = sh0.getRelationParts();
    List<RelationPart> rels1 = sh1.getRelationParts();
    assertEquals(1, rels0.size());
    assertEquals(1, rels1.size());
    PackageRelationship pr0 = rels0.get(0).getRelationship();
    PackageRelationship pr1 = rels1.get(0).getRelationship();
    assertEquals(pr0.getTargetMode(), pr1.getTargetMode());
    assertEquals(pr0.getTargetURI(), pr1.getTargetURI());
    POIXMLDocumentPart doc0 = rels0.get(0).getDocumentPart();
    POIXMLDocumentPart doc1 = rels1.get(0).getDocumentPart();
    assertEquals(doc0, doc1);
    wb.close();
}
Also used : RelationPart(org.apache.poi.POIXMLDocumentPart.RelationPart) PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Test(org.junit.Test)

Example 17 with PackageRelationship

use of org.apache.poi.openxml4j.opc.PackageRelationship 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 18 with PackageRelationship

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

the class XSLFPowerPointExtractorDecorator method addSlideParts.

private void addSlideParts(PackagePart slidePart, List<PackagePart> parts) {
    for (String relation : new String[] { XSLFRelation.VML_DRAWING.getRelation(), XSLFRelation.SLIDE_LAYOUT.getRelation(), XSLFRelation.NOTES_MASTER.getRelation(), XSLFRelation.NOTES.getRelation() }) {
        try {
            for (PackageRelationship packageRelationship : slidePart.getRelationshipsByType(relation)) {
                if (packageRelationship.getTargetMode() == TargetMode.INTERNAL) {
                    PackagePartName relName = PackagingURIHelper.createPartName(packageRelationship.getTargetURI());
                    parts.add(packageRelationship.getPackage().getPart(relName));
                }
            }
        } catch (InvalidFormatException e) {
        }
    }
    //and slide of course
    parts.add(slidePart);
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 19 with PackageRelationship

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

the class XSSFExcelExtractorDecorator method getMainDocumentParts.

/**
     * In Excel files, sheets have things embedded in them,
     * and sheet drawings which have the images
     */
@Override
protected List<PackagePart> getMainDocumentParts() throws TikaException {
    List<PackagePart> parts = new ArrayList<PackagePart>();
    for (PackagePart part : sheetParts) {
        // Add the sheet
        parts.add(part);
        // If it has drawings, return those too
        try {
            for (PackageRelationship rel : part.getRelationshipsByType(XSSFRelation.DRAWINGS.getRelation())) {
                if (rel.getTargetMode() == TargetMode.INTERNAL) {
                    PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
                    parts.add(rel.getPackage().getPart(relName));
                }
            }
            for (PackageRelationship rel : part.getRelationshipsByType(XSSFRelation.VML_DRAWINGS.getRelation())) {
                if (rel.getTargetMode() == TargetMode.INTERNAL) {
                    PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
                    parts.add(rel.getPackage().getPart(relName));
                }
            }
        } catch (InvalidFormatException e) {
            throw new TikaException("Broken OOXML file", e);
        }
    }
    //by AbstractOOXMLExtractor
    for (PackagePart part : extractor.getPackage().getPartsByRelationshipType(RELATION_OFFICE_DOCUMENT)) {
        parts.add(part);
    }
    return parts;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) TikaException(org.apache.tika.exception.TikaException) ArrayList(java.util.ArrayList) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 20 with PackageRelationship

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

the class XSSFExcelExtractorDecorator method extractHyperLinks.

private void extractHyperLinks(PackagePart sheetPart, XHTMLContentHandler xhtml) throws SAXException {
    try {
        for (PackageRelationship rel : sheetPart.getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation())) {
            xhtml.startElement("a", "href", rel.getTargetURI().toString());
            xhtml.characters(rel.getTargetURI().toString());
            xhtml.endElement("a");
        }
    } catch (InvalidFormatException e) {
    //swallow
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Aggregations

PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)50 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)28 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)21 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)15 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)13 IOException (java.io.IOException)11 POIXMLException (org.apache.poi.POIXMLException)8 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)5 TikaException (org.apache.tika.exception.TikaException)5 ArrayList (java.util.ArrayList)4 XmlException (org.apache.xmlbeans.XmlException)4 Test (org.junit.Test)4 InputStream (java.io.InputStream)3 URI (java.net.URI)3 HashMap (java.util.HashMap)3 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)3 SAXException (org.xml.sax.SAXException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2