The SVG and CSS DOM Interfaces

When you first create an SVG Document from Java it will only support the DOM Core calls (basically traversal of the tree and accessing attributes).

The really interesting CSS and SVG specific DOM interfaces will not work properly until you initialize them. The code below is the simplest example of how to initialize them.

This defaults a lot of behaviour and in particular for real uses most people will want to provide a custom implementation of the UserAgent class.

If you are using the JSVGCanvas it does this when you associate an SVGDocument with the canvas. It will do this for you asynchronously you can be notified when it is done by registering a GVT!TreeBuilderListener with the canvas before setting an SVGDocument (or URL) on the canvas.

Example

   1 import org.apache.batik.bridge.UserAgent;
   2 import org.apache.batik.bridge.UserAgentAdapter;
   3 import org.apache.batik.bridge.DocumentLoader;
   4 import org.apache.batik.bridge.BridgeContext;
   5 import org.apache.batik.bridge.GVTBuilder;
   6 
   7 import org.apache.batik.gvt.GraphicsNode;
   8 
   9 /* ..... */
  10         SVGDocument    svgDoc;
  11         UserAgent      userAgent;
  12         DocumentLoader loader;
  13         BridgeContext  ctx;
  14         GVTBuilder     builder;
  15         GraphicsNode   rootGN;
  16         
  17         userAgent = new UserAgentAdapter();
  18         loader    = new DocumentLoader(userAgent);
  19         ctx       = new BridgeContext(userAgent, loader);
  20         ctx.setDynamicState(BridgeContext.DYNAMIC);
  21         builder   = new GVTBuilder();
  22         rootGN    = builder.build(ctx, svgDoc);

BootSvgAndCssDom (last edited 2012-06-22 14:05:31 by drool)