Search in sources :

Example 6 with URIResolver

use of javax.xml.transform.URIResolver in project mycore by MyCoRe-Org.

the class MCRURIResolver method resolve.

/**
 * URI Resolver that resolves XSL document() or xsl:include calls.
 *
 * @see javax.xml.transform.URIResolver
 */
public Source resolve(String href, String base) throws TransformerException {
    if (LOGGER.isDebugEnabled()) {
        if (base != null) {
            LOGGER.debug("Including {} from {}", href, base);
            addDebugInfo(href, base);
        } else {
            LOGGER.debug("Including {}", href);
            addDebugInfo(href, null);
        }
    }
    if (!href.contains(":")) {
        return tryResolveXSL(href, base);
    }
    String scheme = getScheme(href, base);
    URIResolver uriResolver = SUPPORTED_SCHEMES.get(scheme);
    if (uriResolver != null) {
        return uriResolver.resolve(href, base);
    } else {
        // try to handle as URL, use default resolver for file:// and
        try {
            InputSource entity = MCREntityResolver.instance().resolveEntity(null, href);
            if (entity != null) {
                LOGGER.debug("Resolved via EntityResolver: {}", entity.getSystemId());
                return new MCRLazyStreamSource(entity::getByteStream, entity.getSystemId());
            }
        } catch (SAXException | IOException e) {
            LOGGER.debug("Error while resolving uri: {}", href);
        }
        // http://
        if (href.endsWith("/") && scheme.equals("file")) {
            // cannot stream directories
            return null;
        }
        StreamSource streamSource = new StreamSource();
        streamSource.setSystemId(href);
        return streamSource;
    }
}
Also used : InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) MCRLazyStreamSource(org.mycore.common.xsl.MCRLazyStreamSource) URIResolver(javax.xml.transform.URIResolver) IOException(java.io.IOException) MCRLazyStreamSource(org.mycore.common.xsl.MCRLazyStreamSource) SAXException(org.xml.sax.SAXException)

Example 7 with URIResolver

use of javax.xml.transform.URIResolver in project camel by apache.

the class XsltCustomizeURIResolverTest method createRegistry.

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry registry = super.createRegistry();
    URIResolver customURIResolver = getCustomURIResolver();
    registry.bind("customURIResolver", customURIResolver);
    return registry;
}
Also used : JndiRegistry(org.apache.camel.impl.JndiRegistry) URIResolver(javax.xml.transform.URIResolver)

Example 8 with URIResolver

use of javax.xml.transform.URIResolver in project camel by apache.

the class XsltComponent method createEndpoint.

@Override
protected Endpoint createEndpoint(String uri, final String remaining, Map<String, Object> parameters) throws Exception {
    XsltEndpoint endpoint = new XsltEndpoint(uri, this);
    endpoint.setConverter(getXmlConverter());
    endpoint.setContentCache(isContentCache());
    endpoint.setSaxon(isSaxon());
    endpoint.setSaxonConfiguration(saxonConfiguration);
    endpoint.setSaxonConfigurationProperties(saxonConfigurationProperties);
    endpoint.setSaxonExtensionFunctions(saxonExtensionFunctions);
    String resourceUri = remaining;
    // if its a http uri, then append additional parameters as they are part of the uri
    if (ResourceHelper.isHttpUri(resourceUri)) {
        resourceUri = ResourceHelper.appendParameters(resourceUri, parameters);
    }
    LOG.debug("{} using schema resource: {}", this, resourceUri);
    endpoint.setResourceUri(resourceUri);
    // lookup custom resolver to use
    URIResolver resolver = resolveAndRemoveReferenceParameter(parameters, "uriResolver", URIResolver.class);
    if (resolver == null) {
        // not in endpoint then use component specific resolver
        resolver = getUriResolver();
    }
    if (resolver == null) {
        // lookup custom resolver factory to use
        XsltUriResolverFactory resolverFactory = resolveAndRemoveReferenceParameter(parameters, "uriResolverFactory", XsltUriResolverFactory.class);
        if (resolverFactory == null) {
            // not in endpoint then use component specific resolver factory
            resolverFactory = getUriResolverFactory();
        }
        if (resolverFactory == null) {
            // fallback to use the Default URI resolver factory
            resolverFactory = new DefaultXsltUriResolverFactory();
        }
        resolver = resolverFactory.createUriResolver(getCamelContext(), remaining);
    }
    endpoint.setUriResolver(resolver);
    setProperties(endpoint, parameters);
    if (!parameters.isEmpty()) {
        // additional parameters need to be stored on endpoint as they can be used to configure xslt builder additionally
        endpoint.setParameters(parameters);
    }
    return endpoint;
}
Also used : URIResolver(javax.xml.transform.URIResolver)

Example 9 with URIResolver

use of javax.xml.transform.URIResolver in project robovm by robovm.

the class ProcessorInclude method getSourceFromUriResolver.

/**
   * Get the Source object for the included or imported stylesheet module
   * obtained from the user's URIResolver, if there is no user provided 
   * URIResolver null is returned.
   */
private Source getSourceFromUriResolver(StylesheetHandler handler) throws TransformerException {
    Source s = null;
    TransformerFactoryImpl processor = handler.getStylesheetProcessor();
    URIResolver uriresolver = processor.getURIResolver();
    if (uriresolver != null) {
        String href = getHref();
        String base = handler.getBaseIdentifier();
        s = uriresolver.resolve(href, base);
    }
    return s;
}
Also used : URIResolver(javax.xml.transform.URIResolver) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource)

Example 10 with URIResolver

use of javax.xml.transform.URIResolver in project jdk8u_jdk by JetBrains.

the class XSLTFunctionsTest method testDocument.

/**
     * bug 8062518
     * Verifies that a reference to the DTM created by XSLT document function is
     * actually read from the DTM by an extension function.
     * @param xml Content of xml file to process
     * @param xsl stylesheet content that loads external document {@code externalDoc}
     *        with XSLT 'document' function and then reads it with
     *        DocumentExtFunc.test() function
     * @param externalDoc Content of the external xml document
     * @param expectedResult Expected transformation result
     **/
@Test(dataProvider = "document")
public void testDocument(final String xml, final String xsl, final String externalDoc, final String expectedResult) throws Exception {
    // Prepare sources for transormation
    Source src = new StreamSource(new StringReader(xml));
    Source xslsrc = new StreamSource(new StringReader(xsl));
    // Create factory and transformer
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer(xslsrc);
    t.setErrorListener(tf.getErrorListener());
    // Set URI Resolver to return the newly constructed xml
    // stream source object from xml test string
    t.setURIResolver(new URIResolver() {

        @Override
        public Source resolve(String href, String base) throws TransformerException {
            if (href.contains("externalDoc")) {
                return new StreamSource(new StringReader(externalDoc));
            } else {
                return new StreamSource(new StringReader(xml));
            }
        }
    });
    // Prepare output stream
    StringWriter xmlResultString = new StringWriter();
    StreamResult xmlResultStream = new StreamResult(xmlResultString);
    //Transform the xml
    t.transform(src, xmlResultStream);
    // If the document can't be accessed and the bug is in place then
    // reported exception will be thrown during transformation
    System.out.println("Transformation result:" + xmlResultString.toString().trim());
    // Check the result - it should contain two (node name, node values) entries -
    // one for original document, another for a document created with
    // call to 'document' function
    assertEquals(xmlResultString.toString().trim(), expectedResult);
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) URIResolver(javax.xml.transform.URIResolver) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) Test(org.testng.annotations.Test)

Aggregations

URIResolver (javax.xml.transform.URIResolver)13 Source (javax.xml.transform.Source)8 StreamSource (javax.xml.transform.stream.StreamSource)6 InputSource (org.xml.sax.InputSource)6 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)5 SAXSource (javax.xml.transform.sax.SAXSource)5 IOException (java.io.IOException)4 Transformer (javax.xml.transform.Transformer)4 TransformerFactory (javax.xml.transform.TransformerFactory)4 DOMSource (javax.xml.transform.dom.DOMSource)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 TransformerException (javax.xml.transform.TransformerException)3 ConfigurationException (org.eclipse.wst.xsl.jaxp.debug.invoker.internal.ConfigurationException)3 Field (java.lang.reflect.Field)2 URL (java.net.URL)2 Properties (java.util.Properties)2 ErrorListener (javax.xml.transform.ErrorListener)2 Templates (javax.xml.transform.Templates)2 TreeWalker (org.apache.xml.utils.TreeWalker)2 Node (org.w3c.dom.Node)2