net.sf.saxon.s9api.XsltCompiler Java Examples

The following examples show how to use net.sf.saxon.s9api.XsltCompiler. 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: 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 #2
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 #3
Source File: Engine.java    From teamengine with Apache License 2.0 4 votes vote down vote up
public XsltCompiler getCompiler() {
    return compiler;
}