Java源码示例:org.apache.batik.dom.svg.SAXSVGDocumentFactory
示例1
private GraphicsNode createNode(String svgContent) throws GeomajasException {
// batik magic
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
SVGDocument document;
try {
document = f.createSVGDocument("", new StringReader(svgContent));
} catch (IOException e) {
throw new RasterException(e, RasterException.BAD_SVG, "Cannot parse SVG");
}
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
BridgeContext bridgeContext = new BridgeContext(userAgent, loader);
bridgeContext.setDynamic(true);
GVTBuilder builder = new GVTBuilder();
return builder.build(bridgeContext, document);
}
示例2
private void loadDocument() {
transcoder = null;
failedToLoadDocument = true;
if (uri == null) {
return;
}
final SAXSVGDocumentFactory factory = new CustomSAXSVGDocumentFactory();
try {
Document document;
if (documentsMap.containsKey(uri)) {
document = documentsMap.get(uri);
} else {
document = factory.createDocument(uri);
documentsMap.put(uri, document);
}
transcoder = new SimpleImageTranscoder(document);
failedToLoadDocument = false;
} catch (final IOException e) {
Activator.logError("Error loading SVG file", e);
}
}
示例3
public SVGDocument loadMapAsDocument(Content map) throws IOException {
String mapContent;
mapContent = new String(DECODER.decodeBuffer(map.getContent()));
documentFactory = new SAXSVGDocumentFactory(parser);
return (SVGDocument) documentFactory.createDocument(null, new StringReader(mapContent));
}
示例4
/**
* Load the SVG image with the specified name. This operation may either
* create a new graphics node of returned a previously created one.
* @param name Name of the SVG file to load.
* @return GraphicsNode containing SVG image or null if none found.
*/
public static GraphicsNode getSVGImage(String name) {
if (svgCache == null) svgCache = new HashMap<String, GraphicsNode>();
GraphicsNode found = svgCache.get(name);
if (found == null) {
String fileName = SVG_DIR + name;
URL resource = SVGLoader.class.getResource(fileName);
try {
String xmlParser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory df = new SAXSVGDocumentFactory(xmlParser);
SVGDocument doc = df.createSVGDocument(resource.toString());
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
BridgeContext ctx = new BridgeContext(userAgent, loader);
ctx.setDynamicState(BridgeContext.DYNAMIC);
GVTBuilder builder = new GVTBuilder();
found = builder.build(ctx, doc);
svgCache.put(name, found);
}
catch (Exception e) {
System.err.println("getSVGImage error: " + fileName);
e.printStackTrace(System.err);
}
}
return found;
}