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

Last change on this file since 17311 was 14328, checked in by Don-vip, 6 years ago

see #14319, see #16838 - update to svgSalamander 1.1.2

  • Property svn:eol-style set to native
File size: 9.0 KB
Line 
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
37package com.kitfox.svg;
38
39
40import java.net.URI;
41import java.util.HashMap;
42import java.util.HashSet;
43import java.util.LinkedList;
44import java.util.logging.Level;
45import java.util.logging.Logger;
46
47import org.xml.sax.Attributes;
48import org.xml.sax.SAXException;
49import org.xml.sax.helpers.DefaultHandler;
50
51/**
52 * @author Mark McKay
53 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
54 */
55public class SVGLoader extends DefaultHandler
56{
57 final HashMap<String, Class<?>> nodeClasses = new HashMap<>();
58 //final HashMap attribClasses = new HashMap();
59 final LinkedList<SVGElement> buildStack = new LinkedList<>();
60
61 final HashSet<String> ignoreClasses = new HashSet<>();
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;
78
79 /**
80 * Creates a new instance of SVGLoader
81 * @param xmlBase
82 * @param universe
83 */
84 public SVGLoader(URI xmlBase, SVGUniverse universe)
85 {
86 this(xmlBase, universe, false);
87 }
88
89 public SVGLoader(URI xmlBase, SVGUniverse universe, boolean verbose)
90 {
91 this.verbose = verbose;
92
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 }
147
148 @Override
149 public void startDocument() throws SAXException
150 {
151// System.err.println("Start doc");
152
153// buildStack.clear();
154 }
155
156 @Override
157 public void endDocument() throws SAXException
158 {
159// System.err.println("End doc");
160 }
161
162 @Override
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++;
170
171 if (skipNonSVGTagDepth != 0 || (!namespaceURI.equals("") && !namespaceURI.equals(SVGElement.SVG_NS)))
172 {
173 skipNonSVGTagDepth++;
174 return;
175 }
176
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 {
199 Class<?> cls = (Class<?>)obj;
200 SVGElement svgEle = (SVGElement)cls.newInstance();
201
202 SVGElement parent = null;
203 if (buildStack.size() != 0) parent = buildStack.getLast();
204 svgEle.loaderStartElement(helper, attrs, parent);
205
206 buildStack.addLast(svgEle);
207 }
208 catch (Exception e)
209 {
210 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
211 "Could not load", e);
212 throw new SAXException(e);
213 }
214
215 }
216
217 @Override
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 }
226
227 if (skipNonSVGTagDepth != 0)
228 {
229 skipNonSVGTagDepth--;
230 return;
231 }
232
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 {
243 SVGElement svgEle = buildStack.removeLast();
244
245 svgEle.loaderEndElement(helper);
246
247 SVGElement parent = null;
248 if (buildStack.size() != 0)
249 {
250 parent = buildStack.getLast();
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 {
266 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
267 "Could not parse", e);
268 throw new SAXException(e);
269 }
270 }
271
272 @Override
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 {
283 SVGElement parent = buildStack.getLast();
284 String s = new String(buf, offset, len);
285 parent.loaderAddText(helper, s);
286 }
287 }
288
289 @Override
290 public void processingInstruction(String target, String data)
291 throws SAXException
292 {
293 //Check for external style sheet
294 }
295
296// public SVGElement getLoadRoot() { return loadRoot; }
297 public SVGDiagram getLoadedDiagram() { return diagram; }
298}
Note: See TracBrowser for help on using the repository browser.