2

I'm using the XSLT processor in JDK 1.6 (Xalan) and I make extensive use of the document() function to retrieve data items from documents downloaded from the web. This processing is done as part of the work to render a web page, and it's currently called each time a page is served. I'm aware of a number of ways to optimize multiple evaluations of the same document() from the same XSLT script, but my concern is rather about reducing the hit on the web; that is, I'd like to cache the external documents to retrieve (also because often I get a timeout when trying to retrieve some of them).

I suppose (hope) Xalan has got a pluggable class for retrieving external document, that I could intercept to inject my caching policy, but I can't find it in the docs or browsing sources. Can somebody point me whether it exists and how it can be configured? Thanks.

Perception
  • 79,279
  • 19
  • 185
  • 195
Fabrizio Giudici
  • 532
  • 3
  • 15

1 Answers1

2

Well, after some tweaking with the debugger and source crawling, I've found a pointer in the javadocs, which I didn't find with Google. The class that does the trick is URIResolver, which can be installed to a Transformer by means of:

        import javax.xml.transform.Source;
        import javax.xml.transform.TransformerException;
        import javax.xml.transform.URIResolver;
        import javax.xml.transform.stream.StreamSource;

        public class CachedURIResolver implements URIResolver
          {
            @Override
            public Source resolve (final String href, final String base) 
              throws TransformerException 
              {
                // TODO: caching logic 
                return new StreamSource(href);
              }
          }

        ...

        final Transformer transformer = transformerFactory.newTransformer(transformation); 
        transformer.setURIResolver(new CachedURIResolver());

There should be some finer processing of href and base, I think in case of relative URLs, but it's not needed in my case.

Fabrizio Giudici
  • 532
  • 3
  • 15