Java源码示例:net.sf.saxon.s9api.XdmNode

示例1
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));
        }
    }
}
 
示例2
/**
 * 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;
}
 
示例3
/**
 * 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;

}
 
示例4
@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());
    }
}
 
示例5
@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();
}
 
示例6
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();

}
 
示例7
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));
        }
    }
}
 
示例8
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);
}
 
示例9
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;
}
 
示例10
@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;
}
 
示例11
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");
    }
}
 
示例12
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");
}
 
示例13
@Override
public void serialize(final XdmNode node) throws SaxonApiException, IOException {
    try ( final ByteArrayOutputStream out = new ByteArrayOutputStream() ) {
        final Serializer serializer = this.processor.newSerializer();
        serializer.setOutputStream(out);
        serializer.serializeNode(node);
        serializer.close();
        this.bytes = out.toByteArray();
    }
}
 
示例14
@Override
public void serialize(final XdmNode node) throws SaxonApiException, IOException {
    try ( final OutputStream out = Files.newOutputStream(this.file) ) {
        final Serializer serializer = this.processor.newSerializer();
        serializer.setOutputStream(out);
        serializer.serializeNode(node);
        serializer.close();
    }
}
 
示例15
private SerializedDocument serialize(final Input input, final XdmNode object) throws IOException, SaxonApiException {
    final SerializedDocument doc;
    if (input instanceof AbstractInput && ((AbstractInput) input).getLength() < getInMemoryLimit()) {
        doc = new ByteArraySerializedDocument(this.processor);
    } else {
        doc = new FileSerializedDocument(this.processor);
    }
    doc.serialize(object);
    return doc;
}
 
示例16
@Override
public void check(final Bag results) {
    final Result<XdmNode, XMLSyntaxError> parserResult = parseDocument(results.getInput());
    final ValidationResultsWellformedness v = new ValidationResultsWellformedness();
    results.setParserResult(parserResult);
    v.getXmlSyntaxError().addAll(parserResult.getErrors());
    results.getReportInput().setValidationResultsWellformedness(v);
    if (parserResult.isInvalid()) {
        results.stopProcessing(parserResult.getErrors().stream().map(XMLSyntaxError::getMessage).collect(Collectors.toList()));
    }
}
 
示例17
private XdmNode createErrorInformation(final Collection<XMLSyntaxError> errors) throws SaxonApiException, SAXException {
    final BuildingContentHandler contentHandler = this.processor.newDocumentBuilder().newBuildingContentHandler();
    contentHandler.startDocument();
    contentHandler.startElement(EngineInformation.getFrameworkNamespace(), ERROR_MESSAGE_ELEMENT, ERROR_MESSAGE_ELEMENT,
            new AttributesImpl());
    final String message = errors.stream().map(XMLSyntaxError::getMessage).collect(Collectors.joining());
    contentHandler.characters(message.toCharArray(), 0, message.length());
    contentHandler.endElement(EngineInformation.getFrameworkNamespace(), ERROR_MESSAGE_ELEMENT, ERROR_MESSAGE_ELEMENT);
    return contentHandler.getDocumentNode();
}
 
示例18
private Result<Scenario, String> determineScenario(final XdmNode document) {
    final Result<Scenario, String> result = this.repository.selectScenario(document);
    if (result.isInvalid()) {
        return new Result<>(this.repository.getFallbackScenario());
    }
    return result;
}
 
示例19
private static boolean match(final XdmNode document, final Scenario scenario) {
    try {
        final XPathSelector selector = scenario.getMatchSelector();
        selector.setContextItem(document);
        return selector.effectiveBooleanValue();
    } catch (final SaxonApiException e) {
        log.error("Error evaluating xpath expression", e);
    }
    return false;
}
 
示例20
public List<XdmNode> extract(final XdmNode xdmSource) {
    try {
        final XPathSelector selector = getSelector();
        selector.setContextItem(xdmSource);
        return selector.stream().map(HtmlExtractor::castToNode).collect(Collectors.toList());

    } catch (final SaxonApiException e) {
        throw new IllegalStateException("Can not extract html content", e);
    }
}
 
示例21
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);
    }
}
 
示例22
@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);
}
 
示例23
private void print(final String origName, final XdmItem xdmItem) {
    final XdmNode node = (XdmNode) xdmItem;
    final String name = origName + "-" + node.getAttributeValue(NAME_ATTRIBUTE);
    final Path file = this.outputDirectory.resolve(name + ".html");
    final Serializer serializer = this.processor.newSerializer(file.toFile());
    try {
        log.info("Writing report html '{}' to {}", name, file.toAbsolutePath());
        serializer.serializeNode(node);
    } catch (final SaxonApiException e) {
        log.error("Error extracting html content to {}", file.toAbsolutePath(), e);
    }
}
 
示例24
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;

}
 
示例25
@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();
}
 
示例26
@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();
}
 
示例27
@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");
}
 
示例28
/**
 * 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);

    }

}
 
示例29
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);
    }
}
 
示例30
@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();
        }
    }
}