package javax.xml.transform; public interface URIResolver { public Source resolve(String href, String base) throws TransformerException; }
The resolve()
method should return
a Source
object if it successfully resolves the URL.
Otherwise it should return null to indicate that the default
URL resolution mechanism should be used.
A URIResolver class
import javax.xml.transform.*; import javax.xml.transform.stream.StreamSource; import java.util.zip.GZIPInputStream; import java.net.URL; import java.io.InputStream; public class GZipURIResolver implements URIResolver { public Source resolve(String href, String base) { try { href = href + ".gz"; URL context = new URL(base); URL u = new URL(context, href); InputStream in = u.openStream(); GZIPInputStream gin = new GZIPInputStream(in); return new StreamSource(gin, u.toString()); } catch (Exception e) { // If anything goes wrong, just return null and let // the default resolver try. } return null; } }
The following two methods in
TransformerFactory
set and get the URIResolver
that Transformer
objects created by
this factory will use to resolve URIs:
public
abstract void setURIResolver(URIResolver resolver);
public abstract URIResolver getURIResolver();
For example,
URIResolver resolver = new GZipURIResolver(); factory.setURIResolver(resolver);