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();
}
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");
}
}
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);
}
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;
}
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
}
}
Aggregations