You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Joel Uckelman <uc...@nomic.net> on 2007/02/02 13:27:16 UTC

Re: URIResolver and Batik

Thus spake Steven Vetzal:
> 
> Hi All,
> 
> I want to make local graphics from our JSR170 repository available  
> using the <svg:image> tag. While I can do this neatly using Apache  
> FOP using my own URIResolver, I can't find any way to do this same  
> thing with Batik.
> 
> Has anyone out there attempted attaching a custom URIResolver or  
> otherwise hooked into the URI lookups in Batik? What approach did you  
> take?
> 
> Thanks!
> 
> Steve

I had to do something similar. We have <use> elements which reference
svg files which are stored in a zip archive. In order to help Batik
find these, here's what I did:

class SVGManagerDocumentLoader extends DocumentLoader {
   public SVGManagerDocumentLoader(UserAgent userAgent) {
      super(userAgent);
   }
 
   public Document loadDocument(String uri)
    throws MalformedURLException, IOException {
      // doctor the uri
      String file = DataArchive.IMAGE_DIR +
         (new File((new URL(uri)).getPath())).getName();
      // call the *real* document loader from the base class, using
      // the filestream from the zip archive
      return loadDocument(uri, archive.getFileStream(file));
   }
}

This is what Batik uses to load documents, so I'm intercepting the uri
and doctoring it as is appropriate for our app.

Then, I extended SVGAbstractTranscoder to use my custom DocumentLoader:

protected class BufferedImageTranscoder extends SVGAbstractTranscoder {
   // lots of irrelevant stuff omitted

   protected DocumentLoader documentLoader;

   public BufferedImageTranscoder() {
      documentLoader = new SVGManagerDocumentLoader(userAgent);
   }

   protected BridgeContext createBridgeContext() {
      return new BridgeContext(userAgent, documentLoader);
   }
}

I have no idea if this is the Right Way To Do It, but it works for me.
(I'd like to hear suggestions if there's a less hackish way to do it.)
Perhaps you could adapt this (or something like it) to your problem.
In particular, I'm not sure whether image loading goes through
loadDocument(), so this might not be what you want.

-- 
J.

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org