xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Cameron McCormack <...@mcc.id.au>
Subject Re: error casting to SVGSVGElement
Date Sat, 05 Jun 2010 06:35:34 GMT
Hi Harry.

Harry Kao:
> I'm trying to embed a stylesheet into an SVG document but the sample
> code isn't working for me:
> http://xmlgraphics.apache.org/batik/using/svg-generator.html#Customizing+the+generated+SVG+style
> If I do this:
>   org.w3c.dom.Document svgDoc =
>       GenericDOMImplementation.getDOMImplementation()
>       .createDocument("http://www.w3.org/2000/svg", "svg", null);
>   System.out.println("el: " + svgDoc.getDocumentElement());
>   System.out.println("ns: " + svgDoc.getDocumentElement().getNamespaceURI());
>   System.out.println("name: " + svgDoc.getDocumentElement().getNodeName());
>   SVGSVGElement s = (SVGSVGElement)svgDoc.getDocumentElement();
> I get the following output:
>   el: org.apache.batik.dom.GenericElementNS@1ac88440
>   ns: http://www.w3.org/2000/svg
>   name: svg
>   Exception in thread "main" java.lang.ClassCastException: org.apache.batik.dom.GenericElementNS
cannot be cast to org.w3c.dom.svg.SVGSVGElement
>     at ReportRenderer.main(ReportRenderer.java:120)
> Is there an obvious reason why this cast is failing?

It fails because the kind of document that is created from the
GenericDOMImplementation isn’t an SVGDocument, it’s a GenericDocument.
If all you need to do to the generated document is to manipulate its
tree a bit and then say serialize it, and not do any SVG DOM specific
stuff, then that’s fine; it just means that the document element will be
of class GenericElementNS rather than something that implements the
SVGSVGElement interface.

(A GenericDocument might be ever so slightly lighter weight than an
SVGDocument.  I wouldn’t think it’s an appreciable different, though.)

If you do need to do some SVG DOM specific stuff on the document (e.g.
you want to build its corresponding graphics tree so that you can call
getBoundingBox() on some elements) then you need to use the
SVGDOMImplementation rather than the GenericDOMImplementation.  That
DOMImplementation will create an SVGDocument whose root element will
implement SVGSVGElement.

Cameron McCormack ≝ http://mcc.id.au/

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

View raw message