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;
}
}
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;
}
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;
}
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;
}
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);
}
Aggregations