source: josm/trunk/src/com/kitfox/svg/SVGLoader.java@ 4656

Last change on this file since 4656 was 4256, checked in by bastiK, 13 years ago

see #6560 - basic svg support, includes kitfox svgsalamander, r 98, patched

File size: 8.2 KB
Line 
1/*
2 * SVGLoader.java
3 *
4 *
5 * The Salamander Project - 2D and 3D graphics libraries in Java
6 * Copyright (C) 2004 Mark McKay
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
23 * projects can be found at http://www.kitfox.com
24 *
25 * Created on February 18, 2004, 5:09 PM
26 */
27
28package com.kitfox.svg;
29
30
31import java.util.*;
32import java.net.*;
33import org.xml.sax.*;
34import org.xml.sax.helpers.DefaultHandler;
35
36/**
37 * @author Mark McKay
38 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
39 */
40public class SVGLoader extends DefaultHandler
41{
42 final HashMap nodeClasses = new HashMap();
43 //final HashMap attribClasses = new HashMap();
44 final LinkedList buildStack = new LinkedList();
45
46 final HashSet ignoreClasses = new HashSet();
47
48 final SVGLoaderHelper helper;
49
50 /**
51 * The diagram that represents the base of this SVG document we're loading.
52 * Will be augmented to include node indexing info and other useful stuff.
53 */
54 final SVGDiagram diagram;
55
56// SVGElement loadRoot;
57
58 //Used to keep track of document elements that are not part of the SVG namespace
59 int skipNonSVGTagDepth = 0;
60 int indent = 0;
61
62 final boolean verbose;
63
64 /** Creates a new instance of SVGLoader */
65 public SVGLoader(URI xmlBase, SVGUniverse universe)
66 {
67 this(xmlBase, universe, false);
68 }
69
70 public SVGLoader(URI xmlBase, SVGUniverse universe, boolean verbose)
71 {
72 this.verbose = verbose;
73
74 diagram = new SVGDiagram(xmlBase, universe);
75
76 //Compile a list of important builder classes
77 nodeClasses.put("a", A.class);
78 nodeClasses.put("circle", Circle.class);
79 nodeClasses.put("clippath", ClipPath.class);
80 nodeClasses.put("defs", Defs.class);
81 nodeClasses.put("desc", Desc.class);
82 nodeClasses.put("ellipse", Ellipse.class);
83 nodeClasses.put("filter", Filter.class);
84 nodeClasses.put("font", Font.class);
85 nodeClasses.put("font-face", FontFace.class);
86 nodeClasses.put("g", Group.class);
87 nodeClasses.put("glyph", Glyph.class);
88 nodeClasses.put("hkern", Hkern.class);
89 nodeClasses.put("image", ImageSVG.class);
90 nodeClasses.put("line", Line.class);
91 nodeClasses.put("lineargradient", LinearGradient.class);
92 nodeClasses.put("marker", Marker.class);
93 nodeClasses.put("metadata", Metadata.class);
94 nodeClasses.put("missing-glyph", MissingGlyph.class);
95 nodeClasses.put("path", Path.class);
96 nodeClasses.put("pattern", PatternSVG.class);
97 nodeClasses.put("polygon", Polygon.class);
98 nodeClasses.put("polyline", Polyline.class);
99 nodeClasses.put("radialgradient", RadialGradient.class);
100 nodeClasses.put("rect", Rect.class);
101 nodeClasses.put("shape", ShapeElement.class);
102 nodeClasses.put("stop", Stop.class);
103 nodeClasses.put("style", Style.class);
104 nodeClasses.put("svg", SVGRoot.class);
105 nodeClasses.put("symbol", Symbol.class);
106 nodeClasses.put("text", Text.class);
107 nodeClasses.put("title", Title.class);
108 nodeClasses.put("tspan", Tspan.class);
109 nodeClasses.put("use", Use.class);
110
111 ignoreClasses.add("midpointstop");
112
113 //attribClasses.put("clip-path", StyleUrl.class);
114 //attribClasses.put("color", StyleColor.class);
115
116 helper = new SVGLoaderHelper(xmlBase, universe, diagram);
117 }
118
119 private String printIndent(int indent, String indentStrn)
120 {
121 StringBuffer sb = new StringBuffer();
122 for (int i = 0; i < indent; i++)
123 {
124 sb.append(indentStrn);
125 }
126 return sb.toString();
127 }
128
129 public void startDocument() throws SAXException
130 {
131// System.err.println("Start doc");
132
133// buildStack.clear();
134 }
135
136 public void endDocument() throws SAXException
137 {
138// System.err.println("End doc");
139 }
140
141 public void startElement(String namespaceURI, String sName, String qName, Attributes attrs) throws SAXException
142 {
143 if (verbose)
144 {
145 System.err.println(printIndent(indent, " ") + "Starting parse of tag " + sName+ ": " + namespaceURI);
146 }
147 indent++;
148
149 if (skipNonSVGTagDepth != 0 || (!namespaceURI.equals("") && !namespaceURI.equals(SVGElement.SVG_NS)))
150 {
151 skipNonSVGTagDepth++;
152 return;
153 }
154
155 sName = sName.toLowerCase();
156
157//javax.swing.JOptionPane.showMessageDialog(null, sName);
158
159 Object obj = nodeClasses.get(sName);
160 if (obj == null)
161 {
162 if (!ignoreClasses.contains(sName))
163 {
164 if (verbose)
165 {
166 System.err.println("SVGLoader: Could not identify tag '" + sName + "'");
167 }
168 }
169 return;
170 }
171
172//Debug info tag depth
173//for (int i = 0; i < buildStack.size(); i++) System.err.print(" ");
174//System.err.println("+" + sName);
175
176 try {
177 Class cls = (Class)obj;
178 SVGElement svgEle = (SVGElement)cls.newInstance();
179
180 SVGElement parent = null;
181 if (buildStack.size() != 0) parent = (SVGElement)buildStack.getLast();
182 svgEle.loaderStartElement(helper, attrs, parent);
183
184 buildStack.addLast(svgEle);
185 }
186 catch (Exception e)
187 {
188 e.printStackTrace();
189 throw new SAXException(e);
190 }
191
192 }
193
194 public void endElement(String namespaceURI, String sName, String qName)
195 throws SAXException
196 {
197 indent--;
198 if (verbose)
199 {
200 System.err.println(printIndent(indent, " ") + "Ending parse of tag " + sName+ ": " + namespaceURI);
201 }
202
203 if (skipNonSVGTagDepth != 0)
204 {
205 skipNonSVGTagDepth--;
206 return;
207 }
208
209 sName = sName.toLowerCase();
210
211 Object obj = nodeClasses.get(sName);
212 if (obj == null) return;
213
214//Debug info tag depth
215//for (int i = 0; i < buildStack.size(); i++) System.err.print(" ");
216//System.err.println("-" + sName);
217
218 try {
219 SVGElement svgEle = (SVGElement)buildStack.removeLast();
220
221 svgEle.loaderEndElement(helper);
222
223 SVGElement parent = null;
224 if (buildStack.size() != 0) parent = (SVGElement)buildStack.getLast();
225 //else loadRoot = (SVGElement)svgEle;
226
227 if (parent != null) parent.loaderAddChild(helper, svgEle);
228 else diagram.setRoot((SVGRoot)svgEle);
229
230 }
231 catch (Exception e)
232 {
233e.printStackTrace();
234 throw new SAXException(e);
235 }
236 }
237
238 public void characters(char buf[], int offset, int len)
239 throws SAXException
240 {
241 if (skipNonSVGTagDepth != 0)
242 {
243 return;
244 }
245
246 if (buildStack.size() != 0)
247 {
248 SVGElement parent = (SVGElement)buildStack.getLast();
249 String s = new String(buf, offset, len);
250 parent.loaderAddText(helper, s);
251 }
252 }
253
254 public void processingInstruction(String target, String data)
255 throws SAXException
256 {
257 //Check for external style sheet
258 }
259
260// public SVGElement getLoadRoot() { return loadRoot; }
261 public SVGDiagram getLoadedDiagram() { return diagram; }
262}
Note: See TracBrowser for help on using the repository browser.