use of org.exist.contentextraction.ContentExtraction in project exist by eXist-db.
the class ContentFunctions method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
// is argument the empty sequence?
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
ContentExtraction ce = new ContentExtraction();
if (isCalledAs("stream-content")) {
/* binary content */
BinaryValue binary = (BinaryValue) args[0].itemAt(0);
/* callback function */
FunctionReference ref = (FunctionReference) args[2].itemAt(0);
Map<String, String> mappings = new HashMap<>();
if (args[3].hasOne()) {
NodeValue namespaces = (NodeValue) args[3].itemAt(0);
parseMappings(namespaces, mappings);
}
return streamContent(ce, binary, args[1], ref, mappings, args[4]);
} else {
try {
if (isCalledAs("get-metadata")) {
context.pushDocumentContext();
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
builder.startElement(new QName("html", XHTML_NS), null);
builder.startElement(new QName("head", XHTML_NS), null);
final QName qnMeta = new QName("meta", XHTML_NS);
final Metadata metadata = ce.extractMetadata((BinaryValue) args[0].itemAt(0));
for (final String name : metadata.names()) {
for (final String value : metadata.getValues(name)) {
final AttributesImpl attributes = new AttributesImpl();
attributes.addAttribute("", "name", "name", "string", name);
attributes.addAttribute("", "content", "content", "string", value);
builder.startElement(qnMeta, attributes);
builder.endElement();
}
}
builder.endElement();
builder.endElement();
builder.endDocument();
return builder.getDocument();
} finally {
context.popDocumentContext();
}
} else {
final DocumentBuilderReceiver builder = new DocumentBuilderReceiver();
builder.setSuppressWhitespace(false);
final Metadata metadata = ce.extractContentAndMetadata((BinaryValue) args[0].itemAt(0), (ContentHandler) builder);
return (NodeValue) builder.getDocument();
}
} catch (IOException | SAXException | ContentExtractionException ex) {
LOG.error(ex.getMessage(), ex);
throw new XPathException(this, ex.getMessage(), ex);
}
}
}
Aggregations