001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * Created on April 17, 2004, 1:52 PM
017: */
018: package org.geotools.renderer.lite;
019:
020: import java.awt.image.BufferedImage;
021: import java.net.URL;
022: import java.util.Hashtable;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025:
026: import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
027: import org.apache.batik.transcoder.TranscoderInput;
028: import org.apache.batik.util.XMLResourceDescriptor;
029: import org.w3c.dom.Document;
030:
031: /**
032: * Turns SVG vector drawings into buffered images geotools can use for rendering
033: *
034: * @author James
035: * @source $URL:
036: * http://svn.geotools.org/geotools/trunk/gt/module/render/src/org/geotools/renderer/lite/SVGGlyphRenderer.java $
037: */
038: public class SVGGlyphRenderer implements GlyphRenderer {
039: private static Hashtable cache = new Hashtable(10);
040:
041: private static final java.util.List formats = java.util.Collections
042: .unmodifiableList(java.util.Arrays
043: .asList(new String[] { "image/svg" }));
044: /** The logger for the rendering module. */
045: private static final Logger LOGGER = org.geotools.util.logging.Logging
046: .getLogger("org.geotools.rendering");
047:
048: // static {
049: // do register our xml reader wrapper against batik so that we can use
050: // jaxp instead of the hard-coded xerces implementation
051: //XMLResourceDescriptor.setXMLParserClassName(BatikXMLReader.class.getName());
052: // }
053:
054: /**
055: * mini cache to stop re-loading SVG files.
056: * Dont know how effective this is, but...
057: */
058: private Document getDocument(URL url) throws Exception {
059: if (cache.contains(url))
060: return (Document) cache.get(url);
061:
062: String parser = XMLResourceDescriptor.getXMLParserClassName();
063: SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
064:
065: Document doc = f.createDocument(url.toString());
066: cache.put(url, doc);
067: return doc;
068: }
069:
070: /** Creates a new instance of SVGGlyphRenderer */
071: public SVGGlyphRenderer() {
072: }
073:
074: public boolean canRender(String format) {
075: return (format.toLowerCase().equals("image/svg+xml"));
076: }
077:
078: public java.util.List getFormats() {
079: return formats;
080: }
081:
082: public java.awt.image.BufferedImage render(
083: org.geotools.styling.Graphic graphic,
084: org.geotools.styling.ExternalGraphic eg, Object feature,
085: int height) {
086: try {
087: BufferedImage img;
088: URL svgfile = eg.getLocation();
089: InternalTranscoder magic = new InternalTranscoder();
090:
091: if (height > 0)
092: magic.addTranscodingHint(InternalTranscoder.KEY_HEIGHT,
093: new Float(height));
094:
095: Document inputDoc = getDocument(svgfile);
096: // TranscoderInput in = new TranscoderInput(svgfile .openStream());
097: magic.transcode(inputDoc);
098: img = magic.getImage();
099: return img;
100: } catch (java.io.IOException mue) {
101: LOGGER.log(Level.WARNING,
102: "Unable to load external svg file", mue);
103: return null;
104: } catch (Exception te) {
105: LOGGER.log(Level.WARNING,
106: "Unable to load external svg file", te);
107: return null;
108: }
109: }
110:
111: }
|