net.sf.saxon.s9api.SaxonApiException Java Examples

The following examples show how to use net.sf.saxon.s9api.SaxonApiException. 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: InputFactoryTest.java    From validator with Apache License 2.0 6 votes vote down vote up
@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 #2
Source File: XSLTBuilder.java    From kite with Apache License 2.0 6 votes vote down vote up
@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 #3
Source File: SchemaValidationAction.java    From validator with Apache License 2.0 6 votes vote down vote up
private Result<Boolean, XMLSyntaxError> validate(final Bag results, final Scenario scenario) {
    log.debug("Validating document using scenario {}", scenario.getConfiguration().getName());
    final CollectingErrorEventHandler errorHandler = new CollectingErrorEventHandler();
    try ( final SourceProvider validateInput = resolveSource(results) ) {

        final Validator validator = this.factory.createValidator(scenario.getSchema());
        validator.setErrorHandler(errorHandler);
        validator.validate(validateInput.getSource());
        return new Result<>(!errorHandler.hasErrors(), errorHandler.getErrors());
    } catch (final SAXException | SaxonApiException | IOException e) {
        final String msg = String.format("Error processing schema validation for scenario %s", scenario.getConfiguration().getName());
        log.error(msg, e);
        results.addProcessingError(msg);
        return new Result<>(Boolean.FALSE);
    }
}
 
Example #4
Source File: XQueryEvaluator.java    From yawl with GNU Lesser General Public License v3.0 6 votes vote down vote up
private String evaluateQuery(String query, Document dataDoc)
           throws CodeletExecutionException {

    try {
        String result = SaxonUtil.evaluateQuery(query, dataDoc);
        if (result != null)
            return result;
        else
            throw new CodeletExecutionException("No data produced for query: '" +
                                                 query + "'.");
    }
    catch (SaxonApiException sapie) {
        throw new CodeletExecutionException(
                "Invalid query: '" + query + "'.\n" +
                "Message from parser: [" + sapie.getMessage() + "]");
    }
    catch (Exception e) {
        throw new CodeletExecutionException(
                "Exception in query evaluation: '" + query + "'.");
    }
}
 
Example #5
Source File: DocumentParseAction.java    From validator with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #6
Source File: YTask.java    From yawl with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected Element evaluateTreeQuery(String query, Document document)
        throws YQueryException {

    try {
        logger.debug("Evaluating XQuery: " + query);
        return SaxonUtil.evaluateTreeQuery(query, document);
    } catch (SaxonApiException e) {
        YQueryException qe = new YQueryException(
                "Something Wrong with Process Specification:\n" +
                        "The engine failed to parse an invalid query.\n" +
                        "Please check task:\n\t" +
                        "id[ " + getID() + " ]\n\t" +
                        "query: \n\t" + query + ".\n" +
                        "Message from parser: [" + e.getMessage() + "]");
        qe.setStackTrace(e.getStackTrace());
        throw qe;
    }
}
 
Example #7
Source File: YTask.java    From yawl with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void checkXQuery(String xQuery, String param, YVerificationHandler handler) {
    if (!StringUtil.isNullOrEmpty(xQuery)) {
        if (ExternalDataGatewayFactory.isExternalDataMappingExpression(xQuery)) {
            checkExternalMapping(xQuery, handler);
        } else {
            try {
                SaxonUtil.compileXQuery(xQuery);
            } catch (SaxonApiException e) {
                handler.error(this, this + " [id= " + this.getID() +
                        "] the XQuery could not be successfully" +
                        " parsed [" + e.getMessage() + "]");
            }
        }
    } else handler.error(this, this + " [id= " + this.getID() +
            "] the XQuery for param [" + param +
            "] cannot be equal to null or the empty string.");
}
 
Example #8
Source File: XPathProcessor.java    From GeoTriples with Apache License 2.0 6 votes vote down vote up
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 #9
Source File: ContentRepository.java    From validator with Apache License 2.0 6 votes vote down vote up
/**
 * Lädt ein XSL von der angegebenen URI
 *
 * @param uri die URI der XSL Definition
 * @return ein XSLT Executable
 */
public XsltExecutable loadXsltScript(final URI uri) {
    log.info("Loading XSLT script from  {}", uri);
    final XsltCompiler xsltCompiler = getProcessor().newXsltCompiler();
    final CollectingErrorEventHandler listener = new CollectingErrorEventHandler();
    try {
        xsltCompiler.setErrorListener(listener);
        if (getResolver() != null) {
            // otherwise use default resolver
            xsltCompiler.setURIResolver(getResolver());
        }

        return xsltCompiler.compile(resolveInRepository(uri));
    } catch (final SaxonApiException e) {
        listener.getErrors().forEach(event -> event.log(log));
        throw new IllegalStateException("Can not compile xslt executable for uri " + uri, e);
    } finally {
        if (!listener.hasErrors() && listener.hasEvents()) {
            log.warn("Received warnings or errors while loading a xslt script {}", uri);
            listener.getErrors().forEach(e -> e.log(log));
        }
    }
}
 
Example #10
Source File: YTask.java    From yawl with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean evaluateSplitQuery(String query, YIdentifier tokenToSend)
        throws YQueryException {

    // check for timer predicates first
    if (isTimerPredicate(query)) {
        return evaluateTimerPredicate(query, tokenToSend);
    }

    // next check for plugin evaluator expressions and have the evaluators replace
    // them with simple values
    query = PredicateEvaluatorCache.process(getDecompositionPrototype(), query,
            tokenToSend);

    // now we have a standard query - check if it evaluates to true
    String xquery = "boolean(" + query + ")";
    try {
        logger.debug("Evaluating XQuery: " + xquery);
        String result = SaxonUtil.evaluateQuery(xquery, _net.getInternalDataDocument());

        if (result != null) {
            if (result.equalsIgnoreCase("true")) {
                logger.debug("XQuery evaluated TRUE.");
                return true;
            } else if (result.equalsIgnoreCase("false")) {
                logger.debug("XQuery evaluated FALSE.");
                return false;
            }
        }

        // either result is null or result is not a boolean string
        logger.error("Evaluated XQuery did not return a singular boolean result.");
        throw new YQueryException("Evaluated XQuery did not return a singular " +
                "boolean result. Evaluated: '" + xquery + "'");
    } catch (SaxonApiException e) {
        logger.error("Invalid XQuery expression (" + xquery + ").", e);
        throw new YQueryException("Invalid XQuery expression (" + xquery + ").");
    }
}
 
Example #11
Source File: SchematronValidationActionTest.java    From validator with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcessingError() throws IOException, SaxonApiException {
    final CheckAction.Bag bag = createBag(InputFactory.read(Simple.SIMPLE_VALID.toURL()), true);

    final Scenario scenario = bag.getScenarioSelectionResult().getObject();
    final XsltExecutable exec = mock(XsltExecutable.class);
    final XsltTransformer transformer = mock(XsltTransformer.class);
    doThrow(new SaxonApiException("invalid")).when(transformer).transform();
    when(exec.load()).thenReturn(transformer);
    final ResourceType resourceType = new ResourceType();
    resourceType.setName("invalid internal");
    scenario.setSchematronValidations(Collections.singletonList(new Transformation(exec, resourceType)));
    this.action.check(bag);
    assertThat(bag.getReportInput().getProcessingError().getError()).isNotEmpty();
}
 
Example #12
Source File: SaxonSecurityTest.java    From validator with Apache License 2.0 5 votes vote down vote up
@Test
public void testEvilStylesheets() throws IOException {
    final Processor p = TestObjectFactory.createProcessor();
    for (int i = 1; i <= 5; i++) {
        try {
            final URL resource = SaxonSecurityTest.class.getResource(String.format("/evil/evil%s.xsl", i));
            final XsltCompiler compiler = p.newXsltCompiler();
            final RelativeUriResolver resolver = new RelativeUriResolver(Simple.REPOSITORY_URI);
            compiler.setURIResolver(resolver);
            final XsltExecutable executable = compiler.compile(new StreamSource(resource.openStream()));
            final XsltTransformer transformer = executable.load();
            final Source document = InputFactory.read("<root/>".getBytes(), "dummy").getSource();
            // transformer.getUnderlyingController().setUnparsedTextURIResolver(resolver);
            transformer.setURIResolver(resolver);
            transformer.setSource(document);
            final XdmDestination result = new XdmDestination();
            transformer.setDestination(result);
            transformer.transform();

            // wenn der Punkt erreicht wird, sollte wenigstens, das Element evil nicht mit 'bösen' Inhalten gefüllt sein!
            if (StringUtils.isNotBlank(result.getXdmNode().getStringValue())) {
                fail(String.format("Saxon configuration should prevent expansion within %s", resource));
            }

        } catch (final SaxonApiException | RuntimeException e) {
            log.info("Expected exception detected {}", e.getMessage(), e);
        }
    }
}
 
Example #13
Source File: Helper.java    From validator with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #14
Source File: Helper.java    From validator with Apache License 2.0 5 votes vote down vote up
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 #15
Source File: LinkCountHDFS.java    From marklogic-contentpump with Apache License 2.0 5 votes vote down vote up
@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 #16
Source File: ConditionEvaluator.java    From yawl with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *  PARSING METHODS
 */


private String evaluateXQuery(String expr, Element data) throws RdrConditionException {
    try {
        if (expr.startsWith("{")) expr = deQuote(expr);      // remove braces
        String query = String.format("boolean(%s)", expr);
        return SaxonUtil.evaluateQuery(query, new Document(data.clone()));
    }
    catch (SaxonApiException sae) {
        throw new RdrConditionException("Invalid XPath expression (" + expr + ").");
    }
}
 
Example #17
Source File: CreateReportActionTest.java    From validator with Apache License 2.0 5 votes vote down vote up
@Test
public void testExecutionException() throws SaxonApiException {
    final Processor p = mock(Processor.class);
    final DocumentBuilder documentBuilder = mock(DocumentBuilder.class);
    this.action = new CreateReportAction(p, new ConversionService(), null);

    when(p.newDocumentBuilder()).thenReturn(documentBuilder);
    when(documentBuilder.build(any(Source.class))).thenThrow(new SaxonApiException("mocked"));
    final Bag bag = TestBagBuilder.createBag(InputFactory.read(Simple.SIMPLE_VALID), true);
    this.action.check(bag);
    assertThat(bag.isStopped()).isTrue();

}
 
Example #18
Source File: YTask.java    From yawl with GNU Lesser General Public License v3.0 5 votes vote down vote up
private List evaluateListQuery(String query, Element element)
        throws YQueryException {

    try {
        logger.debug("Evaluating XQuery: " + query);
        return SaxonUtil.evaluateListQuery(query, element);
    } catch (SaxonApiException e) {
        YQueryException de = new YQueryException(e.getMessage());
        de.setStackTrace(e.getStackTrace());
        throw de;
    }
}
 
Example #19
Source File: DynFormUserAttributes.java    From yawl with GNU Lesser General Public License v3.0 5 votes vote down vote up
public boolean isHideIf(String data) {
    boolean hide = false;
    String query = getValue("hideIf");
    if (query != null) {
        try {
            Document dataDoc = JDOMUtil.stringToDocument(data);
            String queryResult = SaxonUtil.evaluateQuery(query, dataDoc);
            hide = queryResult.equalsIgnoreCase("true");
        }
        catch (SaxonApiException sae) {
            // nothing to do, will default to false
        }
    }
    return hide;
}
 
Example #20
Source File: SaxonCommand.java    From kite with Apache License 2.0 5 votes vote down vote up
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 #21
Source File: XQueryBuilder.java    From kite with Apache License 2.0 5 votes vote down vote up
@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 #22
Source File: MyConcatExtensionFunction.java    From kite with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #23
Source File: CreateReportAction.java    From validator with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #24
Source File: SchemaValidationAction.java    From validator with Apache License 2.0 5 votes vote down vote up
@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();
    }
}
 
Example #25
Source File: SchemaValidationAction.java    From validator with Apache License 2.0 5 votes vote down vote up
@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();
    }
}
 
Example #26
Source File: SchemaValidationAction.java    From validator with Apache License 2.0 5 votes vote down vote up
private SourceProvider resolveSource(final Bag results) throws IOException, SaxonApiException {
    final SourceProvider source;
    if (results.getInput() instanceof AbstractInput && (((AbstractInput) results.getInput()).supportsMultipleReads())) {
        source = () -> results.getInput().getSource();
    } else {
        source = serialize(results.getInput(), results.getParserResult().getObject());
    }
    return source;

}
 
Example #27
Source File: SchemaValidationAction.java    From validator with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #28
Source File: ComputeAcceptanceAction.java    From validator with Apache License 2.0 5 votes vote down vote up
private static void evaluateAcceptanceMatch(final Bag results, final XPathSelector selector) {
    try {
        selector.setContextItem(results.getReport());
        results.setAcceptStatus(selector.effectiveBooleanValue() ? AcceptRecommendation.ACCEPTABLE : AcceptRecommendation.REJECT);
    } catch (final SaxonApiException e) {
        final String msg = String.format("Error evaluating accept recommendation: %s", selector.getUnderlyingXPathContext().toString());
        log.error(msg, e);
        results.stopProcessing(msg);
    }
}
 
Example #29
Source File: CheckHandler.java    From validator with Apache License 2.0 5 votes vote down vote up
private byte[] serialize(final Result result) {
    try ( final ByteArrayOutputStream out = new ByteArrayOutputStream() ) {
        final Serializer serializer = this.processor.newSerializer(out);
        serializer.serializeNode(result.getReport());
        return out.toByteArray();
    } catch (final SaxonApiException | IOException e) {
        log.error("Error serializing result", e);
        throw new IllegalStateException("Can not serialize result", e);
    }
}
 
Example #30
Source File: ScenarioRepository.java    From validator with Apache License 2.0 5 votes vote down vote up
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;
}