use of net.sf.saxon.s9api.Processor in project sirix by sirixdb.
the class TestXSLTTransformation method testTransform.
@Test
public void testTransform() throws Exception {
final Processor proc = new Processor(false);
final XsltCompiler compiler = proc.newXsltCompiler();
try {
final XsltExecutable exec = compiler.compile(new StreamSource(new File(STYLESHEET)));
final XsltTransformer transform = exec.load();
transform.setSource(new StreamSource(new FileInputStream(INPUT)));
final Serializer serializer = new Serializer();
final OutputStream out = new ByteArrayOutputStream();
serializer.setOutputStream(out);
transform.setDestination(serializer);
transform.transform();
final StringBuilder expected = TestHelper.readFile(new File(EXPECTED), false);
assertEquals("XML files match", expected.toString(), new StringBuilder("<root>").append(out.toString()).append("</root>").toString());
} catch (final SaxonApiException e) {
LOGWRAPPER.error(e);
}
}
use of net.sf.saxon.s9api.Processor in project nifi by apache.
the class EvaluateXQuery method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
final List<FlowFile> flowFileBatch = session.get(50);
if (flowFileBatch.isEmpty()) {
return;
}
final ComponentLog logger = getLogger();
final Map<String, XQueryExecutable> attributeToXQueryMap = new HashMap<>();
final Processor proc = new Processor(false);
final XMLReader xmlReader;
try {
xmlReader = XMLReaderFactory.createXMLReader();
} catch (SAXException e) {
logger.error("Error while constructing XMLReader {}", new Object[] { e });
throw new ProcessException(e.getMessage());
}
if (!context.getProperty(VALIDATE_DTD).asBoolean()) {
xmlReader.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(new StringReader(""));
}
});
}
final XQueryCompiler comp = proc.newXQueryCompiler();
for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) {
if (!entry.getKey().isDynamic()) {
continue;
}
final XQueryExecutable exp;
try {
exp = comp.compile(entry.getValue());
attributeToXQueryMap.put(entry.getKey().getName(), exp);
} catch (SaxonApiException e) {
// should not happen because we've already validated the XQuery (in XQueryValidator)
throw new ProcessException(e);
}
}
final XQueryExecutable slashExpression;
try {
slashExpression = comp.compile("/");
} catch (SaxonApiException e) {
logger.error("unable to compile XQuery expression due to {}", new Object[] { e });
session.transfer(flowFileBatch, REL_FAILURE);
return;
}
final String destination = context.getProperty(DESTINATION).getValue();
flowFileLoop: for (FlowFile flowFile : flowFileBatch) {
if (!isScheduled()) {
session.rollback();
return;
}
final AtomicReference<Throwable> error = new AtomicReference<>(null);
final AtomicReference<XdmNode> sourceRef = new AtomicReference<>(null);
session.read(flowFile, new InputStreamCallback() {
@Override
public void process(final InputStream rawIn) throws IOException {
try (final InputStream in = new BufferedInputStream(rawIn)) {
XQueryEvaluator qe = slashExpression.load();
qe.setSource(new SAXSource(xmlReader, new InputSource(in)));
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
dfactory.setNamespaceAware(true);
Document dom = dfactory.newDocumentBuilder().newDocument();
qe.run(new DOMDestination(dom));
XdmNode rootNode = proc.newDocumentBuilder().wrap(dom);
sourceRef.set(rootNode);
} catch (final Exception e) {
error.set(e);
}
}
});
if (error.get() != null) {
logger.error("unable to evaluate XQuery against {} due to {}; routing to 'failure'", new Object[] { flowFile, error.get() });
session.transfer(flowFile, REL_FAILURE);
continue;
}
final Map<String, String> xQueryResults = new HashMap<>();
List<FlowFile> childrenFlowFiles = new ArrayList<>();
for (final Map.Entry<String, XQueryExecutable> entry : attributeToXQueryMap.entrySet()) {
try {
XQueryEvaluator qe = entry.getValue().load();
qe.setContextItem(sourceRef.get());
XdmValue result = qe.evaluate();
if (DESTINATION_ATTRIBUTE.equals(destination)) {
int index = 1;
for (XdmItem item : result) {
String value = formatItem(item, context);
String attributeName = entry.getKey();
if (result.size() > 1) {
attributeName += "." + index++;
}
xQueryResults.put(attributeName, value);
}
} else {
// if (DESTINATION_CONTENT.equals(destination)){
if (result.size() == 0) {
logger.info("Routing {} to 'unmatched'", new Object[] { flowFile });
session.transfer(flowFile, REL_NO_MATCH);
continue flowFileLoop;
} else if (result.size() == 1) {
final XdmItem item = result.itemAt(0);
flowFile = session.write(flowFile, new OutputStreamCallback() {
@Override
public void process(final OutputStream rawOut) throws IOException {
try (final OutputStream out = new BufferedOutputStream(rawOut)) {
writeformattedItem(item, context, out);
} catch (TransformerFactoryConfigurationError | TransformerException e) {
throw new IOException(e);
}
}
});
} else {
for (final XdmItem item : result) {
FlowFile ff = session.clone(flowFile);
ff = session.write(ff, new OutputStreamCallback() {
@Override
public void process(final OutputStream rawOut) throws IOException {
try (final OutputStream out = new BufferedOutputStream(rawOut)) {
try {
writeformattedItem(item, context, out);
} catch (TransformerFactoryConfigurationError | TransformerException e) {
throw new IOException(e);
}
}
}
});
childrenFlowFiles.add(ff);
}
}
}
} catch (final SaxonApiException e) {
logger.error("failed to evaluate XQuery for {} for Property {} due to {}; routing to failure", new Object[] { flowFile, entry.getKey(), e });
session.transfer(flowFile, REL_FAILURE);
session.remove(childrenFlowFiles);
continue flowFileLoop;
} catch (TransformerFactoryConfigurationError | TransformerException | IOException e) {
logger.error("Failed to write XQuery result for {} due to {}; routing original to 'failure'", new Object[] { flowFile, error.get() });
session.transfer(flowFile, REL_FAILURE);
session.remove(childrenFlowFiles);
continue flowFileLoop;
}
}
if (DESTINATION_ATTRIBUTE.equals(destination)) {
flowFile = session.putAllAttributes(flowFile, xQueryResults);
final Relationship destRel = xQueryResults.isEmpty() ? REL_NO_MATCH : REL_MATCH;
logger.info("Successfully evaluated XQueries against {} and found {} matches; routing to {}", new Object[] { flowFile, xQueryResults.size(), destRel.getName() });
session.transfer(flowFile, destRel);
session.getProvenanceReporter().modifyAttributes(flowFile);
} else {
// if (DESTINATION_CONTENT.equals(destination)) {
if (!childrenFlowFiles.isEmpty()) {
logger.info("Successfully created {} new FlowFiles from {}; routing all to 'matched'", new Object[] { childrenFlowFiles.size(), flowFile });
session.transfer(childrenFlowFiles, REL_MATCH);
session.remove(flowFile);
} else {
logger.info("Successfully updated content for {}; routing to 'matched'", new Object[] { flowFile });
session.transfer(flowFile, REL_MATCH);
session.getProvenanceReporter().modifyContent(flowFile);
}
}
}
// end flowFileLoop
}
use of net.sf.saxon.s9api.Processor in project sirix by sirixdb.
the class TestNodeWrapper method testCompareOrder.
@Test
public void testCompareOrder() throws XPathException, SirixException {
final Processor proc = new Processor(false);
final Configuration config = proc.getUnderlyingConfiguration();
final Session session = generateSession();
final NodeWriteTrx trx = session.beginNodeWriteTrx();
trx.commit();
trx.close();
// Not the same document.
NodeInfo node = new DocumentWrapper(session, config);
NodeInfo other = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 3);
try {
node.compareOrder(other);
fail();
} catch (final IllegalStateException e) {
}
// Before.
node = new DocumentWrapper(mHolder.getSession(), config);
other = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 3);
assertEquals(-1, node.compareOrder(other));
// After.
node = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 3);
other = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 0);
assertEquals(1, node.compareOrder(other));
// Same.
node = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 3);
other = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), config), 3);
assertEquals(0, node.compareOrder(other));
session.close();
mDatabase.close();
}
use of net.sf.saxon.s9api.Processor in project sirix by sirixdb.
the class TestNodeWrapper method testGetAttributeValue.
@Test
public void testGetAttributeValue() throws SirixException {
final Processor proc = new Processor(false);
node = new NodeWrapper(new DocumentWrapper(mHolder.getSession(), proc.getUnderlyingConfiguration()), 1);
final AxisIterator iterator = node.iterateAxis(Axis.ATTRIBUTE);
final NodeInfo attribute = (NodeInfo) iterator.next();
node.getNamePool().allocate(attribute.getPrefix(), attribute.getURI(), attribute.getLocalPart());
// Only supported on element nodes.
// node = (NodeWrapper) node.getParent();
assertEquals("j", node.getAttributeValue(attribute.getFingerprint()));
}
use of net.sf.saxon.s9api.Processor in project sirix by sirixdb.
the class TestNodeWrapper method beforeMethod.
@Before
public void beforeMethod() throws SirixException {
Databases.truncateDatabase(DB_CONFIG);
Databases.createDatabase(DB_CONFIG);
TestHelper.createTestDocument();
mHolder = Holder.generateRtx();
final Processor proc = new Processor(false);
final Configuration config = proc.getUnderlyingConfiguration();
node = new DocumentWrapper(mHolder.getSession(), config).getNodeWrapper();
}
Aggregations