Use external stylesheet from resource with Batik in Java

46 Views Asked by At

I'm using Batik in Java to display and dynamically alter SVG. Say I have the following SVG:

<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
    <rect id="item" width="100" height="100" x="50" y="50" />
</svg>

I would like to be able to add an external stylesheet reference, either in the file or programatically in Java, so I can apply a fill as follows:

SVGDocument document = ... // load it
Element element = document.getRootElement().getElementById("item");
element.setAttribute("class", "blue");

I've done it using SVG attributes (e.g. fill="#FFFFFF") and in-file CSS (e.g. <style type="text/css">...), but I am looking to make it work with external CSS.

I've tried adding this to the start of the SVG file:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<?xml-stylesheet type="text/css" href="styles.css" ?>

But it appears to not register the file, or not find the file. Both the SVG and the CSS file are located in a resources package in my Java project. How can I make this work?

I'm open to moving the CSS-reference to the Java code instead of it being in the SVG, but ideally they should remain as resource files.

0

There are 0 best solutions below