Created
February 14, 2012 00:54
-
-
Save schmmd/1822050 to your computer and use it in GitHub Desktop.
dot2svg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def svg2xml(svgString: String) = { | |
import org.apache.batik.dom.svg.SVGDOMImplementation; | |
import org.apache.batik.util.XMLResourceDescriptor | |
import org.apache.batik.dom.svg.SAXSVGDocumentFactory | |
val uri = SVGDOMImplementation.SVG_NAMESPACE_URI; | |
val doc = using(new java.io.StringReader(svgString)) { reader => | |
val parser = XMLResourceDescriptor.getXMLParserClassName(); | |
val f = new SAXSVGDocumentFactory(parser); | |
f.createSVGDocument(uri, reader); | |
} | |
val gs = doc.getElementsByTagNameNS(uri, "g") | |
for (i <- 0 until gs.getLength) { | |
val g = gs.item(i) | |
val attributes = g.getAttributes | |
val clazz = attributes.getNamedItem("class").getNodeValue | |
if (clazz == "node") { | |
val children = g.getChildNodes | |
for (j <- 0 until children.getLength) { | |
val child = children.item(j) | |
if (child.getNodeName == "title") { | |
val text = child.getFirstChild.getNodeValue | |
import org.w3c.dom.events._ | |
g.asInstanceOf[EventTarget].addEventListener("click", | |
new EventListener() { def handleEvent(e: Event) { println("click: " + e) } }, | |
true); | |
} | |
} | |
} | |
} | |
doc | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment