@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); } } });
void writeformattedItem(XdmItem item, ProcessContext context, OutputStream out) throws 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)); } } }
@Override public ValidationResult validate(final String subject, final String input, final ValidationContext validationContext) { try { final Processor proc = new Processor(false); final XQueryCompiler comp = proc.newXQueryCompiler(); String error = null; try { comp.compile(input); } catch (final Exception e) { error = e.toString(); } return new ValidationResult.Builder().input(input).subject(subject).valid(error == null).explanation(error).build(); } catch (final Exception e) { return new ValidationResult.Builder().input(input).subject(subject).valid(false) .explanation("Unable to initialize XQuery engine due to " + e.toString()).build(); } } }
final Processor proc = new Processor(false); final XMLReader xmlReader; final XQueryCompiler comp = proc.newXQueryCompiler(); exp = comp.compile(entry.getValue()); attributeToXQueryMap.put(entry.getKey().getName(), exp); } catch (SaxonApiException e) { slashExpression = comp.compile("/"); } catch (SaxonApiException e) { logger.error("unable to compile XQuery expression due to {}", new Object[]{e}); XQueryEvaluator qe = entry.getValue().load(); qe.setContextItem(sourceRef.get()); XdmValue result = qe.evaluate(); String value = formatItem(item, context); String attributeName = entry.getKey(); if (result.size() > 1) { attributeName += "." + index++; 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() {
/** * Set the source document for the transformation. This method is equivalent to building * a document from the supplied source object, and then supplying the document node of * the resulting document as the initial context node. * @param source the principal source document for the transformation */ public void setSource(Source source) throws SaxonApiException { setInitialContextNode(processor.newDocumentBuilder().build(source)); }
/** * Set the source document for the query. This method is equivalent to building * a document from the supplied source object, and then supplying the document node of * the resulting document as the initial context node. * @param source the principal source document for the transformation */ public void setSource(Source source) throws SaxonApiException { setContextItem(processor.newDocumentBuilder().build(source)); }
@Override public XdmValue call(XdmValue[] arguments) throws SaxonApiException { StringBuilder buf = new StringBuilder(); for (XdmValue argument : arguments) { XdmSequenceIterator iter = argument.iterator(); while (iter.hasNext()) { buf.append(iter.next().getStringValue()); } } return new XdmAtomicValue(buf.toString()); } }
@Override public Stream<? extends XdmItem> apply(XdmItem item) { try { XPathSelector selector = load(); selector.setContextItem(item); XdmSequenceIterator<XdmItem> result = selector.iterator(); return result.stream(); } catch (SaxonApiException e) { throw new SaxonApiUncheckedException(e); } } };
XdmValue matchList = xPathSelector.evaluate(); XdmItem firstItem = matchList.itemAt(0); if (firstItem instanceof XdmNode) { XdmNode xdmNode = (XdmNode) firstItem; XdmNodeKind nodeKind = xdmNode.getNodeKind(); if (nodeKind == XdmNodeKind.ELEMENT) { return xdmNode.toString(); } } return firstItem.getStringValue();
protected static XdmValue wrap(ValueRepresentation value) { if (value == null) { return null; } else if (value instanceof NodeInfo) { return new XdmNode((NodeInfo)value); } else if (value instanceof AtomicValue) { return new XdmAtomicValue((AtomicValue)value); } else if (value instanceof EmptySequence) { return XdmEmptySequence.getInstance(); } else { return new XdmValue(value); } }
/** * Supply an initial template for a transformation. * <p>This is a Saxon extension to the JAXP interface, needed for XSLT 2.0</p> * <p>Changed in 9.9 so it no longer validates the supplied name; an incorrect name will * lead to an error later.</p> * * @param name the name of the initial template, in Clark notation (either a local name, * or "{uri}local") */ public void setInitialTemplate(String name) { xsltTransformer.setInitialTemplate(QName.fromClarkName(name)); }
/** * Get a hash code with semantics corresponding to the equals() method * * @return the hash code * @since 9.5 */ public final int hashCode() { return getItemType().hashCode() ^ (getOccurrenceIndicator().hashCode() << 17); }
/** * Supply an initial mode for a transformation. * <p>This is a Saxon extension to the JAXP interface, needed for XSLT 2.0</p> * * @param name the name of the initial mode, in Clark notation (either a local name, * or "{uri}local") * @throws IllegalArgumentException if the argument is invalid, for example if the * format of the name is incorrect or if there is no mode with this name */ public void setInitialMode(String name) throws IllegalArgumentException { xsltTransformer.setInitialMode(QName.fromClarkName(name)); }
public Stream<? extends XdmItem> apply(XdmItem arg) { try { return XdmFunctionItem.this.call(processor, arg).stream(); } catch (SaxonApiException e) { throw new SaxonApiUncheckedException(e); } } };
/** * Supply an initial mode for a transformation. * <p>This is a Saxon extension to the JAXP interface, needed for XSLT 2.0</p> * * @param name the name of the initial mode, in Clark notation (either a local name, * or "{uri}local") * @throws IllegalArgumentException if the argument is invalid, for example if the * format of the name is incorrect or if there is no mode with this name */ public void setInitialMode(String name) throws IllegalArgumentException { xsltTransformer.setInitialMode(QName.fromClarkName(name)); }
/** * Test whether two SequenceType objects represent the same type * * @param other the other SequenceType object * @return true if the other object is a SequenceType representing the same type * @since 9.5 */ public final boolean equals(Object other) { return other instanceof SequenceType && ((SequenceType) other).getOccurrenceIndicator().equals(getOccurrenceIndicator()) && ((SequenceType) other).getItemType().equals(getItemType()); }
/** * Get the name of the function, as a QName. * <p>This method must be implemented in all subclasses</p> * * @return the function name */ @Override public StructuredQName getFunctionQName() { return function.getName().getStructuredQName(); }
@Override public Stream<? extends XdmItem> apply(XdmItem item) { try { XPathSelector selector = load(); selector.setContextItem(item); XdmSequenceIterator<XdmItem> result = selector.iterator(); return result.stream(); } catch (SaxonApiException e) { throw new SaxonApiUncheckedException(e); } } };
/** * Get a hash code with semantics corresponding to the equals() method * * @return the hash code * @since 9.5 */ public final int hashCode() { return getItemType().hashCode() ^ (getOccurrenceIndicator().hashCode() << 17); }
public Stream<? extends XdmItem> apply(XdmItem arg) { try { return XdmFunctionItem.this.call(processor, arg).stream(); } catch (SaxonApiException e) { throw new SaxonApiUncheckedException(e); } } };