use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class POIXMLDocumentPart method read.
/**
* Iterate through the underlying PackagePart and create child POIXMLFactory instances
* using the specified factory
*
* @param factory the factory object that creates POIXMLFactory instances
* @param context context map containing already visited noted keyed by targetURI
*
* @throws OpenXML4JException thrown when a related part can't be read
*/
protected void read(POIXMLFactory factory, Map<PackagePart, POIXMLDocumentPart> context) throws OpenXML4JException {
PackagePart pp = getPackagePart();
// add mapping a second time, in case of initial caller hasn't done so
POIXMLDocumentPart otherChild = context.put(pp, this);
if (otherChild != null && otherChild != this) {
throw new POIXMLException("Unique PackagePart-POIXMLDocumentPart relation broken!");
}
if (!pp.hasRelationships())
return;
PackageRelationshipCollection rels = packagePart.getRelationships();
List<POIXMLDocumentPart> readLater = new ArrayList<POIXMLDocumentPart>();
// scan breadth-first, so parent-relations are hopefully the shallowest element
for (PackageRelationship rel : rels) {
if (rel.getTargetMode() == TargetMode.INTERNAL) {
URI uri = rel.getTargetURI();
// check for internal references (e.g. '#Sheet1!A1')
PackagePartName relName;
if (uri.getRawFragment() != null) {
relName = PackagingURIHelper.createPartName(uri.getPath());
} else {
relName = PackagingURIHelper.createPartName(uri);
}
final PackagePart p = packagePart.getPackage().getPart(relName);
if (p == null) {
logger.log(POILogger.ERROR, "Skipped invalid entry " + rel.getTargetURI());
continue;
}
POIXMLDocumentPart childPart = context.get(p);
if (childPart == null) {
childPart = factory.createDocumentPart(this, p);
childPart.parent = this;
// already add child to context, so other children can reference it
context.put(p, childPart);
readLater.add(childPart);
}
addRelation(rel, childPart);
}
}
for (POIXMLDocumentPart childPart : readLater) {
childPart.read(factory, context);
}
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class XWPFSettings method commit.
@Override
protected void commit() throws IOException {
if (ctSettings == null) {
throw new IllegalStateException("Unable to write out settings that were never read in!");
}
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTSettings.type.getName().getNamespaceURI(), "settings"));
PackagePart part = getPackagePart();
OutputStream out = part.getOutputStream();
ctSettings.save(out, xmlOptions);
out.close();
}
use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.
the class XWPFFootnotes method commit.
@Override
protected void commit() throws IOException {
XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTFootnotes.type.getName().getNamespaceURI(), "footnotes"));
PackagePart part = getPackagePart();
OutputStream out = part.getOutputStream();
ctFootnotes.save(out, xmlOptions);
out.close();
}
use of org.apache.poi.openxml4j.opc.PackagePart in project tika by apache.
the class AbstractOOXMLExtractor method handleEmbeddedPart.
private void handleEmbeddedPart(PackagePart source, PackageRelationship rel, ContentHandler handler, Metadata parentMetadata, Set<String> seen) throws IOException, SAXException, TikaException, InvalidFormatException {
URI targetURI = rel.getTargetURI();
if (targetURI != null) {
if (seen.contains(targetURI.toString())) {
return;
}
seen.add(targetURI.toString());
}
URI sourceURI = rel.getSourceURI();
String sourceDesc;
if (sourceURI != null) {
sourceDesc = getJustFileName(sourceURI.getPath());
if (sourceDesc.startsWith("slide")) {
sourceDesc += "_";
} else {
sourceDesc = "";
}
} else {
sourceDesc = "";
}
if (rel.getTargetMode() != TargetMode.INTERNAL) {
return;
}
PackagePart target;
try {
target = source.getRelatedPart(rel);
} catch (IllegalArgumentException ex) {
return;
}
String type = rel.getRelationshipType();
if (RELATION_OLE_OBJECT.equals(type) && TYPE_OLE_OBJECT.equals(target.getContentType())) {
handleEmbeddedOLE(target, handler, sourceDesc + rel.getId(), parentMetadata);
} else if (RELATION_AUDIO.equals(type) || RELATION_IMAGE.equals(type) || RELATION_PACKAGE.equals(type) || RELATION_OLE_OBJECT.equals(type)) {
handleEmbeddedFile(target, handler, sourceDesc + rel.getId());
} else if (RELATION_MACRO.equals(type)) {
handleMacros(target, handler);
}
}
use of org.apache.poi.openxml4j.opc.PackagePart 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) {
}
}
Aggregations