xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From George Armhold <armh...@cs.rutgers.edu>
Subject SVGGraphics2D.getRoot() truncates SVG document?
Date Fri, 14 Sep 2001 21:21:02 GMT
I'm trying to manipulate the DOM generated by an SVGGraphics2D in order
to add some javascript.  I find that if I call SVGGraphics.getRoot(), it
wipes out any existing tags in the DOM.  The example below will
demonstrate this.  If you comment out the two lines starting with
g.getRoot(), it'll generate an SVG doc with some simple drawing code. 
When the getRoot() line is active, the drawing code gets truncated.    I
get this behaviour with Batik 1.0 as well as 1.1 RC2.  Am I supposed to
somehow add the node returned from getRoot() back into the DOM? If so,
how?

Any explanations appreciated.


import javax.xml.parsers.*; 
import java.io.*;
import org.w3c.dom.*;
import java.awt.*;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.dom.svg.SVGDOMImplementation;

public class Simple {

  public static void main(String[] args) {
    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    DOMImplementation impl =
SVGDOMImplementation.getDOMImplementation();
    Document doc = impl.createDocument(svgNS, "svg", null);

    // Create an instance of the SVG Generator
    SVGGraphics2D g = new SVGGraphics2D(doc);

    // define the size of the SVG
    Dimension d = new Dimension(1024, 1024);
    g.setSVGCanvasSize(d);

    // set background color
    g.setColor(Color.white);
    g.fill(new Rectangle(0, 0, d.width, d.height));

    // now do our drawing
    Rectangle rect = new Rectangle(0, 0, 100, 100);
    g.setColor(Color.blue);
    g.fill(rect);
    
    Element scriptElement = doc.createElement("script");
    scriptElement.setAttribute("type", "text/ecmascript");
    scriptElement.setAttribute("xlink:href",
"http://pablo.rutgers.edu/~armhold/animate.js");

    // this call to getRoot() appears to corrupt the SVG document:
    Element svgRoot = g.getRoot();
    svgRoot.appendChild(scriptElement);

    try {
      PrintWriter writer = new PrintWriter(System.out);
      g.stream(writer, false);
      writer.flush();
    } catch (Exception e) {
      e.printStackTrace();
    }

    System.exit(0);
  }
}




--
George Armhold
Rutgers University
Bioinformatics Initiative

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


Mime
View raw message