blackears / svgSalamander

158 stars 57 forks source link

How to avoid MalformedURLException #15

Closed nidi3 closed 7 years ago

nidi3 commented 7 years ago

When creating an SVG from a stream

SVGUniverse universe = new SVGUniverse();
URI uri = universe.loadSVG(new StringReader(svg), "graph");
SVGDiagram diagram = universe.getDiagram(uri);
diagram.render(graphics);

the output is correctly created, but these warnings are emitted:

Mär 26, 2017 5:06:10 PM com.kitfox.svg.SVGUniverse getElement
WARNUNG: Could not parse path svgSalamander:/transparent
java.net.MalformedURLException: unknown protocol: svgsalamander
    at java.net.URL.<init>(URL.java:593)
    at java.net.URL.<init>(URL.java:483)
    at java.net.URL.<init>(URL.java:432)
    at java.net.URI.toURL(URI.java:1089)
    at com.kitfox.svg.SVGUniverse.getElement(SVGUniverse.java:345)
    at com.kitfox.svg.SVGUniverse.getElement(SVGUniverse.java:310)
    at com.kitfox.svg.ShapeElement.renderShape(ShapeElement.java:206)
    at com.kitfox.svg.Polygon.render(Polygon.java:105)
    at com.kitfox.svg.Group.render(Group.java:205)
    at com.kitfox.svg.Group.render(Group.java:205)
    at com.kitfox.svg.SVGRoot.renderToViewport(SVGRoot.java:329)
    at com.kitfox.svg.SVGDiagram.render(SVGDiagram.java:105)

How can this be avoided?

nidi3 commented 7 years ago

Ups, seams to be a problem with my SVG, not with salamander.

DaniRey commented 2 years ago

I hit the same problem and want to share a workaround in hope someone else might profit in the future.

One cause of WARNING: Could not parse path svgSalamander:/transparent can be an invalid color "transparent". The valid color to render transparant would be "none". One reason for an SVG with a color "transparent" might be, that it was generated by graphviz, see https://gitlab.com/graphviz/graphviz/-/issues/1863 As there is no fix for graphviz in sight, I decided to manipulate the SVG with svgSalamander before rendering. I use the following code to do so.

//replace the stroke color "transparent" with "none" to have a valid SVG
//https://gitlab.com/graphviz/graphviz/-/issues/1863
SVGElement graph = diagram.getRoot().getChild(0);
for (int i = 0; i < graph.getNumChildren(); i++) {
   if (graph.getChild(i).hasAttribute("stroke", AT_XML)) {
      graph.getChild(i).setAttribute("stroke", AT_XML, "none");
   }
}