use of net.sf.saxon.s9api.XdmNode in project nifi by apache.
the class EvaluateXQuery method writeformattedItem.
void writeformattedItem(XdmItem item, ProcessContext context, OutputStream out) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException, IOException {
if (item.isAtomicValue()) {
out.write(item.getStringValue().getBytes(UTF8));
} else {
// item is an XdmNode
XdmNode node = (XdmNode) item;
switch(node.getNodeKind()) {
case DOCUMENT:
case ELEMENT:
Transformer transformer = TransformerFactory.newInstance().newTransformer();
final Properties props = getTransformerProperties(context);
transformer.setOutputProperties(props);
transformer.transform(node.asSource(), new StreamResult(out));
break;
default:
out.write(node.getStringValue().getBytes(UTF8));
}
}
}
use of net.sf.saxon.s9api.XdmNode in project ddf by codice.
the class DynamicSchemaResolver method createTinyBinary.
private byte[] createTinyBinary(String xml) throws XMLStreamException, SaxonApiException {
SaxonDocBuilder builder = new SaxonDocBuilder(processor);
XmlReader xmlReader = new XmlReader();
xmlReader.addHandler(builder);
xmlReader.setStripNamespaces(true);
xmlReader.read(IOUtils.toInputStream(xml));
XdmNode node = builder.getDocument();
TinyTree tinyTree = ((TinyDocumentImpl) node.getUnderlyingNode()).getTree();
TinyBinary tinyBinary = new TinyBinary(tinyTree, StandardCharsets.UTF_8);
return tinyBinary.getBytes();
}
use of net.sf.saxon.s9api.XdmNode in project ddf by codice.
the class XpathFilterCollector method collect.
@Override
public void collect(int docId) throws IOException {
Document doc = this.context.reader().document(docId);
BytesRef binaryValue = doc.getBinaryValue(LUX_XML_FIELD_NAME);
if (binaryValue != null) {
byte[] bytes = binaryValue.bytes;
// Lux update chain
if (bytes.length > 4 && bytes[0] == 'T' && bytes[1] == 'I' && bytes[2] == 'N') {
TinyBinary tb = new TinyBinary(bytes, TinyBinaryField.UTF8);
XdmNode node = new XdmNode(tb.getTinyDocument(config));
try {
selector.setContextItem(node);
XdmItem result = selector.evaluateSingle();
if (result != null && result.size() > 0 && !(result.isAtomicValue() && !((XdmAtomicValue) result).getBooleanValue())) {
super.collect(docId);
}
} catch (SaxonApiException e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unable to evaluate xpath: " + xpath, e);
}
}
}
}
use of net.sf.saxon.s9api.XdmNode in project sirix by sirixdb.
the class TestNodeWrapperS9ApiXSLT method saxonTransform.
/**
* Transform source document with the given stylesheet.
*
* @param xml
* Source xml file.
* @param stylesheet
* Stylesheet to transform sourc xml file.
* @throws SaxonApiException
* Exception from Saxon in case anything goes wrong.
*/
@Ignore("Not a test, utility method only")
public void saxonTransform(final File xml, final File stylesheet) throws SaxonApiException {
final Processor proc = new Processor(false);
final XsltCompiler comp = proc.newXsltCompiler();
final XsltExecutable exp = comp.compile(new StreamSource(stylesheet));
final XdmNode source = proc.newDocumentBuilder().build(new StreamSource(xml));
final Serializer out = new Serializer();
out.setOutputProperty(Serializer.Property.METHOD, "xml");
out.setOutputProperty(Serializer.Property.INDENT, "yes");
out.setOutputFile(new File(TestHelper.PATHS.PATH1.getFile(), "books1.html"));
final XsltTransformer trans = exp.load();
trans.setInitialContextNode(source);
trans.setDestination(out);
trans.transform();
}
use of net.sf.saxon.s9api.XdmNode in project jmeter by apache.
the class XPathUtil method computeAssertionResultUsingSaxon.
/**
* @param result The result of xpath2 assertion
* @param xmlFile XML data
* @param xPathQuery XPath Query
* @param namespaces Space separated set of prefix=namespace
* @param isNegated invert result
* @throws SaxonApiException when the parser has problems with the given xml or xpath query
* @throws FactoryConfigurationError when the parser can not be instantiated
*/
public static void computeAssertionResultUsingSaxon(AssertionResult result, String xmlFile, String xPathQuery, String namespaces, Boolean isNegated) throws SaxonApiException, FactoryConfigurationError {
// generating the cache key
final ImmutablePair<String, String> key = ImmutablePair.of(xPathQuery, namespaces);
// check the cache
XPathExecutable xPathExecutable;
if (StringUtils.isNotEmpty(xPathQuery)) {
xPathExecutable = XPATH_CACHE.get(key);
} else {
log.warn("Error : {}", JMeterUtils.getResString("xpath2_extractor_empty_query"));
return;
}
try (StringReader reader = new StringReader(xmlFile)) {
// We could instantiate it once but might trigger issues in the future
// Sharing of a DocumentBuilder across multiple threads is not recommended.
// However, in the current implementation sharing a DocumentBuilder (once
// initialized)
// will only cause problems if a SchemaValidator is used.
net.sf.saxon.s9api.DocumentBuilder builder = PROCESSOR.newDocumentBuilder();
XdmNode xdmNode = builder.build(new SAXSource(new InputSource(reader)));
if (xPathExecutable != null) {
XPathSelector selector = null;
try {
Document doc;
doc = XPathUtil.makeDocumentBuilder(false, false, false, false).newDocument();
XObject xObject = XPathAPI.eval(doc, xPathQuery, getPrefixResolverForXPath2(doc, namespaces));
selector = xPathExecutable.load();
selector.setContextItem(xdmNode);
XdmValue nodes = selector.evaluate();
boolean resultOfEval = true;
int length = nodes.size();
// In case we need to extract everything
if (length == 0) {
resultOfEval = false;
} else if (xObject.getType() == XObject.CLASS_BOOLEAN) {
resultOfEval = Boolean.parseBoolean(nodes.itemAt(0).getStringValue());
}
result.setFailure(isNegated ? resultOfEval : !resultOfEval);
result.setFailureMessage(isNegated ? "Nodes Matched for " + xPathQuery : "No Nodes Matched for " + xPathQuery);
} catch (ParserConfigurationException | TransformerException e) {
// NOSONAR Exception handled by return
result.setError(true);
result.setFailureMessage("Exception: " + e.getMessage() + " for:" + xPathQuery);
} finally {
if (selector != null) {
try {
selector.getUnderlyingXPathContext().setContextItem(null);
} catch (Exception e) {
// NOSONAR Ignored on purpose
result.setError(true);
result.setFailureMessage("Exception: " + e.getMessage() + " for:" + xPathQuery);
}
}
}
}
}
}
Aggregations