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 George Armhold <ar...@cs.rutgers.edu> on 2001/09/14 23:21:02 UTC

SVGGraphics2D.getRoot() truncates SVG document?

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


Re: SVGGraphics2D.getRoot() truncates SVG document?

Posted by George Armhold <ar...@cs.rutgers.edu>.
Vincent Hardy wrote:

> George Armhold wrote:
> >
> > 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.
> 
> Right, this is the expected behavior of the SVGGraphics2D. If you'd like
> an example of how to use it for what you are trying to do, you can
> have a look at the org.apache.batik.svggen.SwingPrettyPrint class which
> does manipulate the DOM generated by SVGGraphics2D.
> 
> You can also have a look at the DOMTreeManager and the DOMGroupManager
> for more explanations on how the DOM is managed in SVGGraphics2D.

Actually I'm already trying to follow the model in SwingPrettyPrint
but I'm still baffled (an annotated version of that code would be
really helpful to us mere mortals.) I can't figure out how to get
access to either the <svg> or <defs> tag in the document so I can
append a <script> tag containing some javascript common to the entire
document.  Following the example in SwingPrettyPrint I can get access
to the <g> nodes, but I can't seem to get above that in the document
structure without mangling it.  

Thanks.

--
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


Re: SVGGraphics2D.getRoot() truncates SVG document?

Posted by Vincent Hardy <vi...@sun.com>.
George,

George Armhold wrote:
> 
> 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.  

Right, this is the expected behavior of the SVGGraphics2D. If you'd like
an example of how to use it for what you are trying to do, you can 
have a look at the org.apache.batik.svggen.SwingPrettyPrint class which
does manipulate the DOM generated by SVGGraphics2D. 

You can also have a look at the DOMTreeManager and the DOMGroupManager 
for more explanations on how the DOM is managed in SVGGraphics2D.

I hope this helps.
Vincent.

> 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

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