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