Created
June 17, 2013 10:01
-
-
Save hoehrmann/5795894 to your computer and use it in GitHub Desktop.
Using Batik, loads a SVG document, executes onload scripts, and then serializes the document and prints it, to capture script-generated content. Originally http://esw.w3.org/SOMDump
This file contains hidden or 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
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
import org.apache.batik.bridge.BaseScriptingEnvironment; | |
import org.apache.batik.bridge.BridgeContext; | |
import org.apache.batik.bridge.GVTBuilder; | |
import org.apache.batik.bridge.UserAgentAdapter; | |
import org.apache.batik.dom.svg.SAXSVGDocumentFactory; | |
import org.apache.batik.util.XMLResourceDescriptor; | |
import org.w3c.dom.ls.DOMImplementationLS; | |
import org.w3c.dom.ls.DOMSerializer; | |
import org.apache.xerces.dom3.bootstrap.DOMImplementationRegistry; | |
/** | |
* @author Björn Höhrmann | |
* | |
*/ | |
public class SOMDump | |
{ | |
void run(String[] args) throws Exception | |
{ | |
String parser = XMLResourceDescriptor.getXMLParserClassName(); | |
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser); | |
String uri = args[0]; | |
Document doc = f.createDocument(uri); | |
UserAgentAdapter userAgent = new UserAgentAdapter(); | |
GVTBuilder builder = new GVTBuilder(); | |
BridgeContext ctx = new BridgeContext(userAgent); | |
ctx.setDynamic(true); | |
builder.build(ctx, doc); | |
BaseScriptingEnvironment scriptEnvironment | |
= new BaseScriptingEnvironment(ctx); | |
Element script = null; | |
if (args.length == 2) | |
{ | |
script = doc.createElementNS("http://www.w3.org/2000/svg", "script"); | |
script.setAttributeNS("http://www.w3.org/1999/xlink", "href", args[1]); | |
script.setAttributeNS(null, "type", "text/ecmascript"); | |
doc.getDocumentElement().appendChild(script); | |
} | |
scriptEnvironment.loadScripts(); | |
scriptEnvironment.dispatchSVGLoadEvent(); | |
if (script != null) | |
script.getParentNode().removeChild(script); | |
System.setProperty(DOMImplementationRegistry.PROPERTY, | |
"org.apache.xerces.dom.DOMImplementationSourceImpl"); | |
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); | |
DOMImplementationLS impl = | |
(DOMImplementationLS)registry.getDOMImplementation("LS"); | |
DOMSerializer writer = impl.createDOMSerializer(); | |
System.out.println(writer.writeToString(doc)/*.substring(0,2048)*/); | |
} | |
public static void main(String[] args) throws Exception | |
{ | |
if (args.length < 1) | |
{ | |
System.err.println("Usage: foo bar.svg [ baz.es ]"); | |
return; | |
} | |
(new SOMDump()).run(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment