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 Harry Kao <ha...@hairycow.name> on 2010/06/05 08:09:31 UTC

error casting to SVGSVGElement

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?

Fri Jun 4 23:09:25 PDT 2010

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


Re: error casting to SVGSVGElement

Posted by Harry Kao <ha...@hairycow.name>.
On Sat, Jun 05, 2010 at 04:35:34PM +1000, Cameron McCormack wrote:
> 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.

Thanks Cameron, that did the trick.

Sat Jun 5 07:50:25 PDT 2010

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


Re: error casting to SVGSVGElement

Posted by Cameron McCormack <ca...@mcc.id.au>.
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