| 1 | /*
|
|---|
| 2 | * SVG Salamander
|
|---|
| 3 | * Copyright (c) 2004, Mark McKay
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or
|
|---|
| 7 | * without modification, are permitted provided that the following
|
|---|
| 8 | * conditions are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above
|
|---|
| 11 | * copyright notice, this list of conditions and the following
|
|---|
| 12 | * disclaimer.
|
|---|
| 13 | * - Redistributions in binary form must reproduce the above
|
|---|
| 14 | * copyright notice, this list of conditions and the following
|
|---|
| 15 | * disclaimer in the documentation and/or other materials
|
|---|
| 16 | * provided with the distribution.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|---|
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|---|
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|---|
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|---|
| 22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|---|
| 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|---|
| 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|---|
| 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|---|
| 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|---|
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|---|
| 29 | * OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 30 | *
|
|---|
| 31 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
|---|
| 32 | * projects can be found at http://www.kitfox.com
|
|---|
| 33 | *
|
|---|
| 34 | * Created on February 18, 2004, 5:09 PM
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | package com.kitfox.svg;
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | import java.util.*;
|
|---|
| 41 | import java.net.*;
|
|---|
| 42 | import org.xml.sax.*;
|
|---|
| 43 | import org.xml.sax.helpers.DefaultHandler;
|
|---|
| 44 |
|
|---|
| 45 | import java.util.logging.Level;
|
|---|
| 46 | import java.util.logging.Logger;
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * @author Mark McKay
|
|---|
| 50 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
|---|
| 51 | */
|
|---|
| 52 | public class SVGLoader extends DefaultHandler
|
|---|
| 53 | {
|
|---|
| 54 | final HashMap nodeClasses = new HashMap();
|
|---|
| 55 | //final HashMap attribClasses = new HashMap();
|
|---|
| 56 | final LinkedList buildStack = new LinkedList();
|
|---|
| 57 |
|
|---|
| 58 | final HashSet ignoreClasses = new HashSet();
|
|---|
| 59 |
|
|---|
| 60 | final SVGLoaderHelper helper;
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * The diagram that represents the base of this SVG document we're loading.
|
|---|
| 64 | * Will be augmented to include node indexing info and other useful stuff.
|
|---|
| 65 | */
|
|---|
| 66 | final SVGDiagram diagram;
|
|---|
| 67 |
|
|---|
| 68 | // SVGElement loadRoot;
|
|---|
| 69 |
|
|---|
| 70 | //Used to keep track of document elements that are not part of the SVG namespace
|
|---|
| 71 | int skipNonSVGTagDepth = 0;
|
|---|
| 72 | int indent = 0;
|
|---|
| 73 |
|
|---|
| 74 | final boolean verbose;
|
|---|
| 75 |
|
|---|
| 76 | /** Creates a new instance of SVGLoader */
|
|---|
| 77 | public SVGLoader(URI xmlBase, SVGUniverse universe)
|
|---|
| 78 | {
|
|---|
| 79 | this(xmlBase, universe, false);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | public SVGLoader(URI xmlBase, SVGUniverse universe, boolean verbose)
|
|---|
| 83 | {
|
|---|
| 84 | this.verbose = verbose;
|
|---|
| 85 |
|
|---|
| 86 | diagram = new SVGDiagram(xmlBase, universe);
|
|---|
| 87 |
|
|---|
| 88 | //Compile a list of important builder classes
|
|---|
| 89 | nodeClasses.put("a", A.class);
|
|---|
| 90 | nodeClasses.put("circle", Circle.class);
|
|---|
| 91 | nodeClasses.put("clippath", ClipPath.class);
|
|---|
| 92 | nodeClasses.put("defs", Defs.class);
|
|---|
| 93 | nodeClasses.put("desc", Desc.class);
|
|---|
| 94 | nodeClasses.put("ellipse", Ellipse.class);
|
|---|
| 95 | nodeClasses.put("filter", Filter.class);
|
|---|
| 96 | nodeClasses.put("font", Font.class);
|
|---|
| 97 | nodeClasses.put("font-face", FontFace.class);
|
|---|
| 98 | nodeClasses.put("g", Group.class);
|
|---|
| 99 | nodeClasses.put("glyph", Glyph.class);
|
|---|
| 100 | nodeClasses.put("hkern", Hkern.class);
|
|---|
| 101 | nodeClasses.put("image", ImageSVG.class);
|
|---|
| 102 | nodeClasses.put("line", Line.class);
|
|---|
| 103 | nodeClasses.put("lineargradient", LinearGradient.class);
|
|---|
| 104 | nodeClasses.put("marker", Marker.class);
|
|---|
| 105 | nodeClasses.put("metadata", Metadata.class);
|
|---|
| 106 | nodeClasses.put("missing-glyph", MissingGlyph.class);
|
|---|
| 107 | nodeClasses.put("path", Path.class);
|
|---|
| 108 | nodeClasses.put("pattern", PatternSVG.class);
|
|---|
| 109 | nodeClasses.put("polygon", Polygon.class);
|
|---|
| 110 | nodeClasses.put("polyline", Polyline.class);
|
|---|
| 111 | nodeClasses.put("radialgradient", RadialGradient.class);
|
|---|
| 112 | nodeClasses.put("rect", Rect.class);
|
|---|
| 113 | nodeClasses.put("shape", ShapeElement.class);
|
|---|
| 114 | nodeClasses.put("stop", Stop.class);
|
|---|
| 115 | nodeClasses.put("style", Style.class);
|
|---|
| 116 | nodeClasses.put("svg", SVGRoot.class);
|
|---|
| 117 | nodeClasses.put("symbol", Symbol.class);
|
|---|
| 118 | nodeClasses.put("text", Text.class);
|
|---|
| 119 | nodeClasses.put("title", Title.class);
|
|---|
| 120 | nodeClasses.put("tspan", Tspan.class);
|
|---|
| 121 | nodeClasses.put("use", Use.class);
|
|---|
| 122 |
|
|---|
| 123 | ignoreClasses.add("midpointstop");
|
|---|
| 124 |
|
|---|
| 125 | //attribClasses.put("clip-path", StyleUrl.class);
|
|---|
| 126 | //attribClasses.put("color", StyleColor.class);
|
|---|
| 127 |
|
|---|
| 128 | helper = new SVGLoaderHelper(xmlBase, universe, diagram);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | private String printIndent(int indent, String indentStrn)
|
|---|
| 132 | {
|
|---|
| 133 | StringBuffer sb = new StringBuffer();
|
|---|
| 134 | for (int i = 0; i < indent; i++)
|
|---|
| 135 | {
|
|---|
| 136 | sb.append(indentStrn);
|
|---|
| 137 | }
|
|---|
| 138 | return sb.toString();
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | public void startDocument() throws SAXException
|
|---|
| 142 | {
|
|---|
| 143 | // System.err.println("Start doc");
|
|---|
| 144 |
|
|---|
| 145 | // buildStack.clear();
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | public void endDocument() throws SAXException
|
|---|
| 149 | {
|
|---|
| 150 | // System.err.println("End doc");
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | public void startElement(String namespaceURI, String sName, String qName, Attributes attrs) throws SAXException
|
|---|
| 154 | {
|
|---|
| 155 | if (verbose)
|
|---|
| 156 | {
|
|---|
| 157 | System.err.println(printIndent(indent, " ") + "Starting parse of tag " + sName+ ": " + namespaceURI);
|
|---|
| 158 | }
|
|---|
| 159 | indent++;
|
|---|
| 160 |
|
|---|
| 161 | if (skipNonSVGTagDepth != 0 || (!namespaceURI.equals("") && !namespaceURI.equals(SVGElement.SVG_NS)))
|
|---|
| 162 | {
|
|---|
| 163 | skipNonSVGTagDepth++;
|
|---|
| 164 | return;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | sName = sName.toLowerCase();
|
|---|
| 168 |
|
|---|
| 169 | //javax.swing.JOptionPane.showMessageDialog(null, sName);
|
|---|
| 170 |
|
|---|
| 171 | Object obj = nodeClasses.get(sName);
|
|---|
| 172 | if (obj == null)
|
|---|
| 173 | {
|
|---|
| 174 | if (!ignoreClasses.contains(sName))
|
|---|
| 175 | {
|
|---|
| 176 | if (verbose)
|
|---|
| 177 | {
|
|---|
| 178 | System.err.println("SVGLoader: Could not identify tag '" + sName + "'");
|
|---|
| 179 | }
|
|---|
| 180 | }
|
|---|
| 181 | return;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | //Debug info tag depth
|
|---|
| 185 | //for (int i = 0; i < buildStack.size(); i++) System.err.print(" ");
|
|---|
| 186 | //System.err.println("+" + sName);
|
|---|
| 187 |
|
|---|
| 188 | try {
|
|---|
| 189 | Class cls = (Class)obj;
|
|---|
| 190 | SVGElement svgEle = (SVGElement)cls.newInstance();
|
|---|
| 191 |
|
|---|
| 192 | SVGElement parent = null;
|
|---|
| 193 | if (buildStack.size() != 0) parent = (SVGElement)buildStack.getLast();
|
|---|
| 194 | svgEle.loaderStartElement(helper, attrs, parent);
|
|---|
| 195 |
|
|---|
| 196 | buildStack.addLast(svgEle);
|
|---|
| 197 | }
|
|---|
| 198 | catch (Exception e)
|
|---|
| 199 | {
|
|---|
| 200 | Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
|
|---|
| 201 | "Could not load", e);
|
|---|
| 202 | throw new SAXException(e);
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | public void endElement(String namespaceURI, String sName, String qName)
|
|---|
| 208 | throws SAXException
|
|---|
| 209 | {
|
|---|
| 210 | indent--;
|
|---|
| 211 | if (verbose)
|
|---|
| 212 | {
|
|---|
| 213 | System.err.println(printIndent(indent, " ") + "Ending parse of tag " + sName+ ": " + namespaceURI);
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | if (skipNonSVGTagDepth != 0)
|
|---|
| 217 | {
|
|---|
| 218 | skipNonSVGTagDepth--;
|
|---|
| 219 | return;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | sName = sName.toLowerCase();
|
|---|
| 223 |
|
|---|
| 224 | Object obj = nodeClasses.get(sName);
|
|---|
| 225 | if (obj == null) return;
|
|---|
| 226 |
|
|---|
| 227 | //Debug info tag depth
|
|---|
| 228 | //for (int i = 0; i < buildStack.size(); i++) System.err.print(" ");
|
|---|
| 229 | //System.err.println("-" + sName);
|
|---|
| 230 |
|
|---|
| 231 | try {
|
|---|
| 232 | SVGElement svgEle = (SVGElement)buildStack.removeLast();
|
|---|
| 233 |
|
|---|
| 234 | svgEle.loaderEndElement(helper);
|
|---|
| 235 |
|
|---|
| 236 | SVGElement parent = null;
|
|---|
| 237 | if (buildStack.size() != 0)
|
|---|
| 238 | {
|
|---|
| 239 | parent = (SVGElement)buildStack.getLast();
|
|---|
| 240 | }
|
|---|
| 241 | //else loadRoot = (SVGElement)svgEle;
|
|---|
| 242 |
|
|---|
| 243 | if (parent != null)
|
|---|
| 244 | {
|
|---|
| 245 | parent.loaderAddChild(helper, svgEle);
|
|---|
| 246 | }
|
|---|
| 247 | else
|
|---|
| 248 | {
|
|---|
| 249 | diagram.setRoot((SVGRoot)svgEle);
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | }
|
|---|
| 253 | catch (Exception e)
|
|---|
| 254 | {
|
|---|
| 255 | Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
|
|---|
| 256 | "Could not parse", e);
|
|---|
| 257 | throw new SAXException(e);
|
|---|
| 258 | }
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | public void characters(char buf[], int offset, int len)
|
|---|
| 262 | throws SAXException
|
|---|
| 263 | {
|
|---|
| 264 | if (skipNonSVGTagDepth != 0)
|
|---|
| 265 | {
|
|---|
| 266 | return;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | if (buildStack.size() != 0)
|
|---|
| 270 | {
|
|---|
| 271 | SVGElement parent = (SVGElement)buildStack.getLast();
|
|---|
| 272 | String s = new String(buf, offset, len);
|
|---|
| 273 | parent.loaderAddText(helper, s);
|
|---|
| 274 | }
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | public void processingInstruction(String target, String data)
|
|---|
| 278 | throws SAXException
|
|---|
| 279 | {
|
|---|
| 280 | //Check for external style sheet
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | // public SVGElement getLoadRoot() { return loadRoot; }
|
|---|
| 284 | public SVGDiagram getLoadedDiagram() { return diagram; }
|
|---|
| 285 | }
|
|---|