net.sf.saxon.s9api.XdmNode Java Examples
The following examples show how to use
net.sf.saxon.s9api.XdmNode.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XSLTBuilder.java From kite with Apache License 2.0 | 6 votes |
@Override protected boolean doProcess2(Record inputRecord, InputStream stream) throws SaxonApiException, XMLStreamException { incrementNumRecords(); for (Fragment fragment : fragments) { Record outputRecord = inputRecord.copy(); removeAttachments(outputRecord); XdmNode document = parseXmlDocument(stream); LOG.trace("XSLT input document: {}", document); XsltTransformer evaluator = fragment.transformer; evaluator.setInitialContextNode(document); XMLStreamWriter morphlineWriter = new MorphlineXMLStreamWriter(getChild(), outputRecord); evaluator.setDestination(new XMLStreamWriterDestination(morphlineWriter)); evaluator.transform(); // run the query and push into child via RecordXMLStreamWriter } return true; }
Example #2
Source File: CheckAssertionAction.java From validator with Apache License 2.0 | 6 votes |
@Override public void check(Bag results) { log.info("Checking assertions for {}", results.getInput().getName()); final List<AssertionType> toCheck = findAssertions(results.getName()); final List<String> errors = new ArrayList<>(); if (toCheck != null && !toCheck.isEmpty()) { final XdmNode node = results.getReport(); toCheck.forEach(a -> { if (!check(node, a)) { log.error("Assertion mismatch: {}", a.getValue()); errors.add(a.getValue()); } }); if (errors.isEmpty()) { log.info("{} assertions successfully verified for {}", toCheck.size(), results.getName()); } else { log.warn("{} assertion of {} failed while checking {}", errors.size(), toCheck.size(), results.getName()); } results.setAssertionResult(new Result<>(toCheck.size(), errors)); } else { log.warn("Can not find assertions for {}", results.getName()); } }
Example #3
Source File: EvaluateXQuery.java From localization_nifi with Apache License 2.0 | 6 votes |
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)); } } }
Example #4
Source File: InputFactoryTest.java From validator with Apache License 2.0 | 6 votes |
@Test public void testDomSource() throws SaxonApiException, SAXException, IOException { final DocumentBuilder builder = TestObjectFactory.createProcessor().newDocumentBuilder(); final BuildingContentHandler handler = builder.newBuildingContentHandler(); handler.startDocument(); handler.startElement("http://some.ns", "mynode", "mynode", new AttributesImpl()); final Document dom = NodeOverNodeInfo.wrap(handler.getDocumentNode().getUnderlyingNode()).getOwnerDocument(); final Input domInput = InputFactory.read(new DOMSource(dom), "MD5", "id".getBytes()); assertThat(domInput).isNotNull(); final Source source = domInput.getSource(); assertThat(source).isNotNull(); final Result<XdmNode, XMLSyntaxError> parsed = Helper.parseDocument(domInput); assertThat(parsed.isValid()).isTrue(); // read twice assertThat(Helper.parseDocument(domInput).getObject()).isNotNull(); }
Example #5
Source File: HtmlExtractor.java From validator with Apache License 2.0 | 6 votes |
private String convertToString(final XdmNode element) { try { final StringWriter writer = new StringWriter(); final Serializer serializer = this.processor.newSerializer(writer); serializer.serializeNode(element); return writer.toString(); } catch (final SaxonApiException e) { throw new IllegalStateException("Can not convert to string", e); } }
Example #6
Source File: XPathProcessor.java From GeoTriples with Apache License 2.0 | 6 votes |
public String execute(Node node, String expression) throws SaxonApiException { Processor proc = new Processor(false); XPathCompiler xpath = proc.newXPathCompiler(); DocumentBuilder builder = proc.newDocumentBuilder(); String fileName = getClass().getResource( map.getLogicalSource().getIdentifier()).getFile(); XdmNode doc = builder.build(new File(fileName)); String expre = replace(node, expression); expression = expression.replaceAll( "\\{" + expression.split("\\{")[1].split("\\}")[0] + "\\}", "'" + expre + "'"); XPathSelector selector = xpath.compile(expression).load(); selector.setContextItem(doc); // Evaluate the expression. Object result = selector.evaluate(); return result.toString(); }
Example #7
Source File: EvaluateXQuery.java From nifi with Apache License 2.0 | 6 votes |
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)); } } }
Example #8
Source File: ScenarioRepository.java From validator with Apache License 2.0 | 6 votes |
/** * Ermittelt für das gegebene Dokument das passende Szenario. * * @param document das Eingabedokument * @return ein Ergebnis-Objekt zur weiteren Verarbeitung */ public Result<Scenario, String> selectScenario(final XdmNode document) { final Result<Scenario, String> result; final List<Scenario> collect = getScenarios().stream().filter(s -> match(document, s)) .collect(Collectors.toList()); if (collect.size() == 1) { result = new Result<>(collect.get(0)); } else if (collect.isEmpty()) { result = new Result<>(getFallbackScenario(), Collections.singleton("None of the loaded scenarios matches the specified document")); } else { result = new Result<>(getFallbackScenario(), Collections.singleton("More than on scenario matches the specified document")); } return result; }
Example #9
Source File: DocumentParseAction.java From validator with Apache License 2.0 | 6 votes |
/** * Parsed und überprüft ein übergebenes Dokument darauf ob es well-formed ist. Dies stellt den ersten * Verarbeitungsschritt des Prüf-Tools dar. Diese Funktion verzichtet explizit auf die Validierung gegenüber einem * Schema. * * @param content ein Dokument * @return Ergebnis des Parsings inklusive etwaiger Fehler */ public Result<XdmNode, XMLSyntaxError> parseDocument(final Input content) { if (content == null) { throw new IllegalArgumentException("Input may not be null"); } Result<XdmNode, XMLSyntaxError> result; try { final DocumentBuilder builder = this.processor.newDocumentBuilder(); builder.setLineNumbering(true); final XdmNode doc = builder.build(content.getSource()); result = new Result<>(doc, Collections.emptyList()); } catch (final SaxonApiException | IOException e) { log.debug("Exception while parsing {}", content.getName(), e); final XMLSyntaxError error = new XMLSyntaxError(); error.setSeverityCode(XMLSyntaxErrorSeverity.SEVERITY_FATAL_ERROR); error.setMessage(String.format("IOException while reading resource %s: %s", content.getName(), e.getMessage())); result = new Result<>(Collections.singleton(error)); } return result; }
Example #10
Source File: TECore.java From teamengine with Apache License 2.0 | 6 votes |
String getAssertionValue(String text, XdmNode paramsVar) { if (text.indexOf("$") < 0) { return text; } String newText = text; XdmNode params = (XdmNode) paramsVar.axisIterator(Axis.CHILD).next(); XdmSequenceIterator it = params.axisIterator(Axis.CHILD); while (it.hasNext()) { XdmNode n = (XdmNode) it.next(); QName qname = n.getNodeName(); if (qname != null) { String tagname = qname.getLocalName(); if (tagname.equals("param")) { String name = n.getAttributeValue(LOCALNAME_QNAME); String label = getLabel(n); newText = StringUtils.replaceAll(newText, "{$" + name + "}", label); } } } newText = StringUtils.replaceAll(newText, "{$context}", contextLabel); return newText; }
Example #11
Source File: TECore.java From teamengine with Apache License 2.0 | 6 votes |
public int execute_test(String testName, List<String> params, XdmNode contextNode) throws Exception { if (LOGR.isLoggable( FINE)) { String logMsg = String.format( "Preparing test %s for execution, using params:%n %s", testName, params); LOGR.fine(logMsg); } TestEntry test = index.getTest(testName); if (test == null) { throw new Exception("Error: Test " + testName + " not found."); } XdmNode paramsNode = engine.getBuilder().build( new StreamSource(new StringReader(getParamsXML(params)))); if (contextNode == null && test.usesContext()) { String contextNodeXML = "<context><value>" + test.getContext() + "</value></context>"; contextNode = engine.getBuilder().build( new StreamSource(new StringReader(contextNodeXML))); } XPathContext context = getXPathContext(test, opts.getSourcesName(), contextNode); return executeTest(test, paramsNode, context); }
Example #12
Source File: Helper.java From validator with Apache License 2.0 | 5 votes |
public static String serialize(final XdmNode node) { try ( final StringWriter writer = new StringWriter() ) { final Processor processor = Helper.getTestProcessor(); final Serializer serializer = processor.newSerializer(writer); serializer.serializeNode(node); return writer.toString(); } catch (final SaxonApiException | IOException e) { throw new IllegalStateException("Can not serialize document", e); } }
Example #13
Source File: CheckAssertionAction.java From validator with Apache License 2.0 | 5 votes |
private boolean check(XdmNode document, AssertionType assertion) { try { final XPathSelector selector = createSelector(assertion); selector.setContextItem(document); return selector.effectiveBooleanValue(); } catch (SaxonApiException e) { log.error("Error evaluating assertion {} for {}", assertion.getTest(), assertion.getReportDoc(), e); } return false; }
Example #14
Source File: XQueryBuilder.java From kite with Apache License 2.0 | 5 votes |
private boolean addRecordValues(XdmNode node, Axis axis, XdmNodeKind nodeTest, Record record) { boolean isEmpty = true; XdmSequenceIterator iter = node.axisIterator(axis); while (iter.hasNext()) { XdmNode child = (XdmNode) iter.next(); if (child.getNodeKind() == nodeTest) { String strValue = child.getStringValue(); if (strValue.length() > 0) { record.put(child.getNodeName().getLocalName(), strValue); isEmpty = false; } } } return !isEmpty; }
Example #15
Source File: XQueryBuilder.java From kite with Apache License 2.0 | 5 votes |
@Override protected boolean doProcess2(Record inputRecord, InputStream stream) throws SaxonApiException, XMLStreamException { incrementNumRecords(); for (Fragment fragment : fragments) { Record template = inputRecord.copy(); removeAttachments(template); XdmNode document = parseXmlDocument(stream); LOG.trace("XQuery input document: {}", document); XQueryEvaluator evaluator = fragment.xQueryEvaluator; evaluator.setContextItem(document); int i = 0; for (XdmItem item : evaluator) { i++; if (LOG.isTraceEnabled()) { LOG.trace("XQuery result sequence item #{} is of class: {} with value: {}", new Object[] { i, item.getUnderlyingValue().getClass().getName(), item }); } if (item.isAtomicValue()) { LOG.debug("Ignoring atomic value in result sequence: {}", item); continue; } XdmNode node = (XdmNode) item; Record outputRecord = template.copy(); boolean isNonEmpty = addRecordValues(node, Axis.SELF, XdmNodeKind.ATTRIBUTE, outputRecord); isNonEmpty = addRecordValues(node, Axis.ATTRIBUTE, XdmNodeKind.ATTRIBUTE, outputRecord) || isNonEmpty; isNonEmpty = addRecordValues(node, Axis.CHILD, XdmNodeKind.ELEMENT, outputRecord) || isNonEmpty; if (isNonEmpty) { // pass record to next command in chain if (!getChild().process(outputRecord)) { return false; } } } } return true; }
Example #16
Source File: DocumentParseActionTest.java From validator with Apache License 2.0 | 5 votes |
@Test public void testSimple() { final Result<XdmNode, XMLSyntaxError> result = this.action.parseDocument(read(Simple.SIMPLE_VALID)); assertThat(result).isNotNull(); assertThat(result.getObject()).isNotNull(); assertThat(result.getErrors()).isEmpty(); assertThat(result.isValid()).isTrue(); }
Example #17
Source File: DocumentParseActionTest.java From validator with Apache License 2.0 | 5 votes |
@Test public void testIllformed() { final Result<XdmNode, XMLSyntaxError> result = this.action.parseDocument(read(Simple.NOT_WELLFORMED)); assertThat(result).isNotNull(); assertThat(result.getErrors()).isNotEmpty(); assertThat(result.getObject()).isNull(); assertThat(result.isValid()).isFalse(); }
Example #18
Source File: SaxonSecurityTest.java From validator with Apache License 2.0 | 5 votes |
@Test public void testXxe() { final URL resource = SaxonSecurityTest.class.getResource("/evil/xxe.xml"); final Result<XdmNode, XMLSyntaxError> result = Helper.parseDocument(InputFactory.read(resource)); assertThat(result.isValid()).isFalse(); assertThat(result.getObject()).isNull(); assertThat(result.getErrors().stream().map(XMLSyntaxError::getMessage).collect(Collectors.joining())) .contains("http://apache.org/xml/features/disallow-doctype-dec"); }
Example #19
Source File: SaxonCommand.java From kite with Apache License 2.0 | 5 votes |
protected XdmNode parseXmlDocument(InputStream stream) throws XMLStreamException, SaxonApiException { XMLStreamReader reader = inputFactory.createXMLStreamReader(null, stream); BuildingStreamWriterImpl writer = documentBuilder.newBuildingStreamWriter(); new XMLStreamCopier(reader, writer).copy(false); // push XML into Saxon and build TinyTree reader.close(); writer.close(); XdmNode document = writer.getDocumentNode(); return document; }
Example #20
Source File: Helper.java From validator with Apache License 2.0 | 5 votes |
/** * Lädt ein XML-Dokument von der gegebenen URL * * @param url die url die geladen werden soll * @return ein result objekt mit Dokument */ public static XdmNode load(final URL url) { try ( final InputStream input = url.openStream() ) { return TestObjectFactory.createProcessor().newDocumentBuilder().build(new StreamSource(input)); } catch (final SaxonApiException | IOException e) { throw new IllegalStateException("Fehler beim Laden der XML-Datei", e); } }
Example #21
Source File: LinkCountHDFS.java From marklogic-contentpump with Apache License 2.0 | 5 votes |
@Override public void initialize(InputSplit inSplit, TaskAttemptContext context) throws IOException, InterruptedException { Path file = ((FileSplit)inSplit).getPath(); FileSystem fs = file.getFileSystem(context.getConfiguration()); FSDataInputStream fileIn = fs.open(file); DocumentBuilder docBuilder = builderLocal.get(); try { Document document = docBuilder.parse(fileIn); net.sf.saxon.s9api.DocumentBuilder db = saxonBuilderLocal.get(); XdmNode xdmDoc = db.wrap(document); XPathCompiler xpath = proc.newXPathCompiler(); xpath.declareNamespace("wp", "http://www.mediawiki.org/xml/export-0.4/"); XPathSelector selector = xpath.compile(PATH_EXPRESSION).load(); selector.setContextItem(xdmDoc); items = new ArrayList<XdmItem>(); for (XdmItem item : selector) { items.add(item); } } catch (SAXException ex) { ex.printStackTrace(); throw new IOException(ex); } catch (SaxonApiException e) { e.printStackTrace(); } finally { if (fileIn != null) { fileIn.close(); } } }
Example #22
Source File: TECore.java From teamengine with Apache License 2.0 | 5 votes |
public NodeInfo executeXSLFunction(XPathContext context, FunctionEntry fe, NodeInfo params) throws Exception { String oldFnPath = fnPath; CRC32 crc = new CRC32(); crc.update((fe.getPrefix() + fe.getId()).getBytes()); fnPath += Long.toHexString(crc.getValue()) + "/"; XdmNode n = executeTemplate(fe, S9APIUtils.makeNode(params), context); fnPath = oldFnPath; if (n == null) { return null; } return n.getUnderlyingNode(); }
Example #23
Source File: TECore.java From teamengine with Apache License 2.0 | 5 votes |
static String getLabel(XdmNode n) { String label = n.getAttributeValue(LABEL_QNAME); if (label == null) { XdmNode value = (XdmNode) n.axisIterator(Axis.CHILD).next(); XdmItem childItem = null; try { childItem = value.axisIterator(Axis.CHILD).next(); } catch (Exception e) { // Not an error } if (childItem == null) { XdmSequenceIterator it = value.axisIterator(Axis.ATTRIBUTE); if (it.hasNext()) { label = it.next().getStringValue(); } else { label = ""; } } else if (childItem.isAtomicValue()) { label = childItem.getStringValue(); } else if (childItem instanceof XdmNode) { XdmNode n2 = (XdmNode) childItem; if (n2.getNodeKind() == XdmNodeKind.ELEMENT) { label = "<" + n2.getNodeName().toString() + ">"; } else { label = n2.toString(); } } } return label; }
Example #24
Source File: LogUtils.java From teamengine with Apache License 2.0 | 5 votes |
public static XdmNode getParamsFromLog( net.sf.saxon.s9api.DocumentBuilder builder, Document log) throws Exception { Element starttest = (Element) log.getElementsByTagName("starttest") .item(0); NodeList nl = starttest.getElementsByTagName("params"); if (nl == null || nl.getLength() == 0) { return null; } else { Document doc = DomUtils.createDocument(nl.item(0)); return builder.build(new DOMSource(doc)); } }
Example #25
Source File: LogUtils.java From teamengine with Apache License 2.0 | 5 votes |
public static XdmNode getContextFromLog( net.sf.saxon.s9api.DocumentBuilder builder, Document log) throws Exception { Element starttest = (Element) log.getElementsByTagName("starttest") .item(0); NodeList nl = starttest.getElementsByTagName("context"); if (nl == null || nl.getLength() == 0) { return null; } else { Element context = (Element) nl.item(0); Element value = (Element) context.getElementsByTagName("value") .item(0); nl = value.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); if (n.getNodeType() == Node.ATTRIBUTE_NODE) { String s = DomUtils.serializeNode(value); XdmNode xn = builder.build(new StreamSource( new CharArrayReader(s.toCharArray()))); return (XdmNode) xn.axisIterator(Axis.ATTRIBUTE).next(); } else if (n.getNodeType() == Node.ELEMENT_NODE) { Document doc = DomUtils.createDocument(n); return builder.build(new DOMSource(doc)); } } } return null; }
Example #26
Source File: TECore.java From teamengine with Apache License 2.0 | 5 votes |
XPathContext getXPathContext(TestEntry test, String sourcesName, XdmNode contextNode) throws Exception { XPathContext context = null; if (test.usesContext()) { XsltExecutable xe = engine.loadExecutable(test, sourcesName); Executable ex = xe.getUnderlyingCompiledStylesheet() .getExecutable(); context = new XPathContextMajor(contextNode.getUnderlyingNode(), ex); } return context; }
Example #27
Source File: TECore.java From teamengine with Apache License 2.0 | 5 votes |
public XdmNode executeTemplate(TemplateEntry template, XdmNode params, XPathContext context) throws Exception { if (stop) { throw new Exception("Execution was stopped by the user."); } XsltExecutable executable = engine.loadExecutable(template, opts.getSourcesName()); XsltTransformer xt = executable.load(); XdmDestination dest = new XdmDestination(); xt.setDestination(dest); if (template.usesContext() && context != null) { xt.setSource((NodeInfo) context.getContextItem()); } else { xt.setSource(new StreamSource(new StringReader("<nil/>"))); } xt.setParameter(TECORE_QNAME, new ObjValue(this)); if (params != null) { xt.setParameter(TEPARAMS_QNAME, params); } // test may set global verdict, e.g. by calling ctl:fail if (LOGR.isLoggable( FINE)) { LOGR.log( FINE, "Executing TemplateEntry {0}" + template.getQName()); } xt.transform(); XdmNode ret = dest.getXdmNode(); if (ret != null && LOGR.isLoggable( FINE)) { LOGR.log( FINE, "Output:\n" + ret.toString()); } return ret; }
Example #28
Source File: CollectingErrorEventHandler.java From validator with Apache License 2.0 | 5 votes |
@Override public void message(final XdmNode content, final boolean terminate, final SourceLocator locator) { final XMLSyntaxError e = new XMLSyntaxError(); if (locator != null) { e.setColumnNumber(locator.getColumnNumber()); e.setRowNumber(locator.getLineNumber()); } e.setMessage("Error procesing" + content.getStringValue()); e.setSeverityCode(terminate ? XMLSyntaxErrorSeverity.SEVERITY_FATAL_ERROR : XMLSyntaxErrorSeverity.SEVERITY_WARNING); }
Example #29
Source File: ConfigurationLoader.java From validator with Apache License 2.0 | 5 votes |
private static void checkVersion(final URI scenarioDefinition, final Processor processor) { try { final Result<XdmNode, XMLSyntaxError> result = new DocumentParseAction(processor) .parseDocument(InputFactory.read(scenarioDefinition.toURL())); if (result.isValid() && !isSupportedDocument(result.getObject())) { throw new IllegalStateException(String.format( "Specified scenario configuration %s is not supported.%nThis version only supports definitions of '%s'", scenarioDefinition, SUPPORTED_MAJOR_VERSION_SCHEMA)); } } catch (final MalformedURLException e) { throw new IllegalStateException("Error reading definition file"); } }
Example #30
Source File: ConfigurationLoader.java From validator with Apache License 2.0 | 5 votes |
private static XdmNode findRoot(final XdmNode doc) { for (final XdmNode node : doc.children()) { if (node.getNodeKind() == XdmNodeKind.ELEMENT) { return node; } } throw new IllegalArgumentException("Kein root element gefunden"); }