use of org.apache.poi.openxml4j.opc.PackageRelationship in project poi by apache.
the class POIXMLFactory method createDocumentPart.
/**
* Create a POIXMLDocumentPart from existing package part and relation. This method is called
* from {@link POIXMLDocument#load(POIXMLFactory)} when parsing a document
*
* @param parent parent part
* @param part the PackagePart representing the created instance
* @return A new instance of a POIXMLDocumentPart.
*
* @since by POI 3.14-Beta1
*/
public POIXMLDocumentPart createDocumentPart(POIXMLDocumentPart parent, PackagePart part) {
PackageRelationship rel = getPackageRelationship(parent, part);
POIXMLRelation descriptor = getDescriptor(rel.getRelationshipType());
if (descriptor == null || descriptor.getRelationClass() == null) {
LOGGER.log(POILogger.DEBUG, "using default POIXMLDocumentPart for " + rel.getRelationshipType());
return new POIXMLDocumentPart(parent, part);
}
Class<? extends POIXMLDocumentPart> cls = descriptor.getRelationClass();
try {
try {
return createDocumentPart(cls, PARENT_PART, new Object[] { parent, part });
} catch (NoSuchMethodException e) {
return createDocumentPart(cls, ORPHAN_PART, new Object[] { part });
}
} catch (Exception e) {
throw new POIXMLException(e);
}
}
use of org.apache.poi.openxml4j.opc.PackageRelationship in project poi by apache.
the class POIXMLDocumentPart method addRelation.
/**
* Add a new child POIXMLDocumentPart
*
* @param relId the preferred relation id, when null the next free relation id will be used
* @param relationshipType the package relationship type
* @param part the child to add
*
* @return the new RelationPart
*
* @since 3.14-Beta1
*/
public final RelationPart addRelation(String relId, POIXMLRelation relationshipType, POIXMLDocumentPart part) {
PackageRelationship pr = this.packagePart.findExistingRelation(part.getPackagePart());
if (pr == null) {
PackagePartName ppn = part.getPackagePart().getPartName();
String relType = relationshipType.getRelation();
pr = packagePart.addRelationship(ppn, TargetMode.INTERNAL, relType, relId);
}
addRelation(pr, part);
return new RelationPart(pr, part);
}
use of org.apache.poi.openxml4j.opc.PackageRelationship in project tika by apache.
the class AbstractOOXMLExtractor method loadLinkedRelationships.
/**
* This is used by the SAX docx and pptx decorators to load hyperlinks and
* other linked objects
*
* @param bodyPart
* @return
*/
protected Map<String, String> loadLinkedRelationships(PackagePart bodyPart, boolean includeInternal, Metadata metadata) {
Map<String, String> linkedRelationships = new HashMap<>();
try {
PackageRelationshipCollection prc = bodyPart.getRelationshipsByType(XWPFRelation.HYPERLINK.getRelation());
for (int i = 0; i < prc.size(); i++) {
PackageRelationship pr = prc.getRelationship(i);
if (pr == null) {
continue;
}
if (!includeInternal && TargetMode.INTERNAL.equals(pr.getTargetMode())) {
continue;
}
String id = pr.getId();
String url = (pr.getTargetURI() == null) ? null : pr.getTargetURI().toString();
if (id != null && url != null) {
linkedRelationships.put(id, url);
}
}
for (String rel : EMBEDDED_RELATIONSHIPS) {
prc = bodyPart.getRelationshipsByType(rel);
for (int i = 0; i < prc.size(); i++) {
PackageRelationship pr = prc.getRelationship(i);
if (pr == null) {
continue;
}
String id = pr.getId();
String uriString = (pr.getTargetURI() == null) ? null : pr.getTargetURI().toString();
String fileName = uriString;
if (pr.getTargetURI() != null) {
try {
fileName = FileHelper.getFilename(new File(fileName));
} catch (Exception e) {
fileName = uriString;
}
}
if (id != null) {
fileName = (fileName == null) ? "" : fileName;
linkedRelationships.put(id, fileName);
}
}
}
} catch (InvalidFormatException e) {
EmbeddedDocumentUtil.recordEmbeddedStreamException(e, metadata);
}
return linkedRelationships;
}
use of org.apache.poi.openxml4j.opc.PackageRelationship in project tika by apache.
the class AbstractOOXMLExtractor method handleThumbnail.
private void handleThumbnail(ContentHandler handler) {
try {
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) {
}
}
use of org.apache.poi.openxml4j.opc.PackageRelationship in project tika by apache.
the class XSSFBExcelExtractorDecorator 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