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 Matiouz Matiouz <ma...@yahoo.fr> on 2008/09/05 18:05:54 UTC

Accessing CSS style of SVG elements from java code

Hello,

I'm using batik 1.7 under windows, and I'm programming in java 5.0.

I'm trying to read the css style of elements (circle, rectangle....) contained in an SVG file.
I can iterate through all these elements but I can't find their style.

Here is the code I'm using:
---------------------------
    private void decodeFile() {
        String mapFile = "D:/maps/rectangle.svg";
        try {
        String parser = XMLResourceDescriptor.getXMLParserClassName();
        SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
        String uri = new File(mapFile).toURI().toString();
        Document doc = f.createDocument(uri);

        SVGDocument svgDocument = (SVGDocument) doc;
        SVGElement root = svgDocument.getRootElement();

        parse(root);

    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
  
    private void parse(Node node) {
        System.out.println("Name: " + node.getNodeName());
        System.out.println();
        
        StyleMap styleMap = null;
        
        if (node instanceof SVGOMRectElement) {
            styleMap = ((SVGOMRectElement) node).getComputedStyleMap(null);
        }

        NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
      Node childnode = (Node) nodeList.item(i);
      parse(childnode);
    }
  }
---------------------------


Here is the SVG file:
---------------------------
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www..w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="10cm" height="5cm" viewBox="0 0 1000 500"
     xmlns="http://www.w3.org/2000/svg" version="1.1">
  <defs>
    <style type="text/css"><![CDATA[
      rect {
        fill: yellow;
        stroke: blue;
        stroke-width: 3
      }
    ]]></style>
  </defs>
  <rect x="200" y="100" width="600" height="300" />
</svg>
---------------------------
The CSS style is defined in the def section and should apply to the whole document (it is displayed correctly in firefox).

When
running the program, the rectangle element is correctly identified as a
SVGOMRectElement but getComputedStyleMap returns null.
Could you tell me how I could retrieve the style?


note 1:
I've also tried to use 
CSSValue s =((SVGStylable)svgElement).getStyle().getPropertyCSSValue("fill");
but I'm getting a null pointer exception at
  at org.apache.batik.dom.svg.SVGStylableElement$StyleDeclaration.<init>(SVGStylableElement.java:645)
    at org..apache.batik.dom.svg.SVGStylableElement.getStyle(SVGStylableElement.java:256)
because cssEngine is null (but I don't know why and I'm not sure this is the real cause of the problem).

note 2:
XMLResourceDescriptor.getXMLParserClassName() returns null so I've tried to hardcode the parser class name
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory("org.apache..xerces.parsers.SAXParser");
but the result is the same.

Any help will be greatly appreciated.


      

Re: Accessing CSS style of SVG elements from java code

Posted by Cameron McCormack <ca...@mcc.id.au>.
Hello Matiouz.

Matiouz Matiouz:
> I'm using batik 1.7 under windows, and I'm programming in java 5.0.
> 
> I'm trying to read the css style of elements (circle, rectangle....) contained in an SVG file.
> I can iterate through all these elements but I can't find their style.

You’ll need to “boot the CSS engine” to get Batik to compute the CSS
information:

  http://wiki.apache.org/xmlgraphics-batik/BootSvgAndCssDom

Once the graphics tree is built (that last builder.build(…) line) calls
to CSS and SVG DOM methods should work.

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