source: josm/trunk/src/com/kitfox/svg/SVGUniverse.java@ 14082

Last change on this file since 14082 was 11525, checked in by Don-vip, 7 years ago

see #14319 - update to latest version of svgSalamander (2017-01-07, patched)

  • Property svn:eol-style set to native
File size: 20.2 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, 11:43 PM
35 */
36package com.kitfox.svg;
37
38import java.awt.Graphics2D;
39import java.awt.image.BufferedImage;
40import java.beans.PropertyChangeListener;
41import java.beans.PropertyChangeSupport;
42import java.io.BufferedInputStream;
43import java.io.ByteArrayInputStream;
44import java.io.ByteArrayOutputStream;
45import java.io.IOException;
46import java.io.InputStream;
47import java.io.ObjectInputStream;
48import java.io.ObjectOutputStream;
49import java.io.Reader;
50import java.io.Serializable;
51import java.lang.ref.SoftReference;
52import java.net.MalformedURLException;
53import java.net.URI;
54import java.net.URISyntaxException;
55import java.net.URL;
56import java.util.ArrayList;
57import java.util.Base64;
58import java.util.HashMap;
59import java.util.logging.Level;
60import java.util.logging.Logger;
61import java.util.zip.GZIPInputStream;
62
63import javax.imageio.ImageIO;
64import javax.xml.parsers.ParserConfigurationException;
65import javax.xml.parsers.SAXParserFactory;
66
67import org.xml.sax.EntityResolver;
68import org.xml.sax.InputSource;
69import org.xml.sax.SAXException;
70import org.xml.sax.SAXParseException;
71import org.xml.sax.XMLReader;
72
73import com.kitfox.svg.app.beans.SVGIcon;
74
75/**
76 * Many SVG files can be loaded at one time. These files will quite likely need
77 * to reference one another. The SVG universe provides a container for all these
78 * files and the means for them to relate to each other.
79 *
80 * @author Mark McKay
81 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
82 */
83public class SVGUniverse implements Serializable
84{
85
86 public static final long serialVersionUID = 0;
87 transient private PropertyChangeSupport changes = new PropertyChangeSupport(this);
88 /**
89 * Maps document URIs to their loaded SVG diagrams. Note that URIs for
90 * documents loaded from URLs will reflect their URLs and URIs for documents
91 * initiated from streams will have the scheme <i>svgSalamander</i>.
92 */
93 final HashMap<URI, SVGDiagram> loadedDocs = new HashMap<>();
94 final HashMap<String, Font> loadedFonts = new HashMap<>();
95 final HashMap<URL, SoftReference<BufferedImage>> loadedImages = new HashMap<>();
96 public static final String INPUTSTREAM_SCHEME = "svgSalamander";
97 /**
98 * Current time in this universe. Used for resolving attributes that are
99 * influenced by track information. Time is in milliseconds. Time 0
100 * coresponds to the time of 0 in each member diagram.
101 */
102 protected double curTime = 0.0;
103 private boolean verbose = false;
104 //Cache reader for efficiency
105 XMLReader cachedReader;
106
107 /**
108 * Creates a new instance of SVGUniverse
109 */
110 public SVGUniverse()
111 {
112 }
113
114 public void addPropertyChangeListener(PropertyChangeListener l)
115 {
116 changes.addPropertyChangeListener(l);
117 }
118
119 public void removePropertyChangeListener(PropertyChangeListener l)
120 {
121 changes.removePropertyChangeListener(l);
122 }
123
124 /**
125 * Release all loaded SVG document from memory
126 */
127 public void clear()
128 {
129 loadedDocs.clear();
130 loadedFonts.clear();
131 loadedImages.clear();
132 }
133
134 /**
135 * Called by the Font element to let the universe know that a font has been
136 * loaded and is available.
137 */
138 void registerFont(Font font)
139 {
140 loadedFonts.put(font.getFontFace().getFontFamily(), font);
141 }
142
143 public Font getDefaultFont()
144 {
145 for (Font font : loadedFonts.values()) {
146 return font;
147 }
148 return null;
149 }
150
151 public Font getFont(String fontName)
152 {
153 return loadedFonts.get(fontName);
154 }
155
156 URL registerImage(URI imageURI)
157 {
158 String scheme = imageURI.getScheme();
159 if (scheme.equals("data"))
160 {
161 String path = imageURI.getRawSchemeSpecificPart();
162 int idx = path.indexOf(';');
163 String mime = path.substring(0, idx);
164 String content = path.substring(idx + 1);
165
166 if (content.startsWith("base64"))
167 {
168 try
169 {
170 byte[] buf = Base64.getDecoder().decode(content.substring(6));
171 ByteArrayInputStream bais = new ByteArrayInputStream(buf);
172 BufferedImage img = ImageIO.read(bais);
173
174 URL url;
175 int urlIdx = 0;
176 while (true)
177 {
178 url = new URL("inlineImage", "localhost", "img" + urlIdx);
179 if (!loadedImages.containsKey(url))
180 {
181 break;
182 }
183 urlIdx++;
184 }
185
186 SoftReference<BufferedImage> ref = new SoftReference<>(img);
187 loadedImages.put(url, ref);
188
189 return url;
190 }
191 catch (IOException | IllegalArgumentException ex)
192 {
193 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
194 "Could not decode inline image", ex);
195 }
196 }
197 return null;
198 } else
199 {
200 try
201 {
202 URL url = imageURI.toURL();
203 registerImage(url);
204 return url;
205 } catch (MalformedURLException ex)
206 {
207 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
208 "Bad url", ex);
209 }
210 return null;
211 }
212 }
213
214 void registerImage(URL imageURL)
215 {
216 if (loadedImages.containsKey(imageURL))
217 {
218 return;
219 }
220
221 SoftReference<BufferedImage> ref;
222 try
223 {
224 String fileName = imageURL.getFile();
225 if (".svg".equals(fileName.substring(fileName.length() - 4).toLowerCase()))
226 {
227 SVGIcon icon = new SVGIcon();
228 icon.setSvgURI(imageURL.toURI());
229
230 BufferedImage img = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
231 Graphics2D g = img.createGraphics();
232 icon.paintIcon(null, g, 0, 0);
233 g.dispose();
234 ref = new SoftReference<>(img);
235 } else
236 {
237 BufferedImage img = ImageIO.read(imageURL);
238 ref = new SoftReference<>(img);
239 }
240 loadedImages.put(imageURL, ref);
241 } catch (Exception e)
242 {
243 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
244 "Could not load image: " + imageURL, e);
245 }
246 }
247
248 BufferedImage getImage(URL imageURL)
249 {
250 SoftReference<BufferedImage> ref = loadedImages.get(imageURL);
251 if (ref == null)
252 {
253 return null;
254 }
255
256 BufferedImage img = ref.get();
257 //If image was cleared from memory, reload it
258 if (img == null)
259 {
260 try
261 {
262 img = ImageIO.read(imageURL);
263 } catch (Exception e)
264 {
265 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
266 "Could not load image", e);
267 }
268 ref = new SoftReference<>(img);
269 loadedImages.put(imageURL, ref);
270 }
271
272 return img;
273 }
274
275 /**
276 * Returns the element of the document at the given URI. If the document is
277 * not already loaded, it will be.
278 */
279 public SVGElement getElement(URI path)
280 {
281 return getElement(path, true);
282 }
283
284 public SVGElement getElement(URL path)
285 {
286 try
287 {
288 URI uri = new URI(path.toString());
289 return getElement(uri, true);
290 } catch (Exception e)
291 {
292 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
293 "Could not parse url " + path, e);
294 }
295 return null;
296 }
297
298 /**
299 * Looks up a href within our universe. If the href refers to a document
300 * that is not loaded, it will be loaded. The URL #target will then be
301 * checked against the SVG diagram's index and the coresponding element
302 * returned. If there is no coresponding index, null is returned.
303 */
304 public SVGElement getElement(URI path, boolean loadIfAbsent)
305 {
306 try
307 {
308 //Strip fragment from URI
309 URI xmlBase = new URI(path.getScheme(), path.getSchemeSpecificPart(), null);
310
311 SVGDiagram dia = loadedDocs.get(xmlBase);
312 if (dia == null && loadIfAbsent)
313 {
314//System.err.println("SVGUnivserse: " + xmlBase.toString());
315//javax.swing.JOptionPane.showMessageDialog(null, xmlBase.toString());
316 URL url = xmlBase.toURL();
317
318 loadSVG(url, false);
319 dia = loadedDocs.get(xmlBase);
320 if (dia == null)
321 {
322 return null;
323 }
324 }
325
326 String fragment = path.getFragment();
327 return fragment == null ? dia.getRoot() : dia.getElement(fragment);
328 } catch (Exception e)
329 {
330 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
331 "Could not parse path " + path, e);
332 return null;
333 }
334 }
335
336 public SVGDiagram getDiagram(URI xmlBase)
337 {
338 return getDiagram(xmlBase, true);
339 }
340
341 /**
342 * Returns the diagram that has been loaded from this root. If diagram is
343 * not already loaded, returns null.
344 */
345 public SVGDiagram getDiagram(URI xmlBase, boolean loadIfAbsent)
346 {
347 if (xmlBase == null)
348 {
349 return null;
350 }
351
352 SVGDiagram dia = loadedDocs.get(xmlBase);
353 if (dia != null || !loadIfAbsent)
354 {
355 return dia;
356 }
357
358 //Load missing diagram
359 try
360 {
361 URL url;
362 if ("jar".equals(xmlBase.getScheme()) && xmlBase.getPath() != null && !xmlBase.getPath().contains("!/"))
363 {
364 //Workaround for resources stored in jars loaded by Webstart.
365 //http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6753651
366 url = SVGUniverse.class.getResource("xmlBase.getPath()");
367 }
368 else
369 {
370 url = xmlBase.toURL();
371 }
372
373
374 loadSVG(url, false);
375 dia = loadedDocs.get(xmlBase);
376 return dia;
377 } catch (Exception e)
378 {
379 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
380 "Could not parse", e);
381 }
382
383 return null;
384 }
385
386 /**
387 * Wraps input stream in a BufferedInputStream. If it is detected that this
388 * input stream is GZIPped, also wraps in a GZIPInputStream for inflation.
389 *
390 * @param is Raw input stream
391 * @return Uncompressed stream of SVG data
392 * @throws java.io.IOException
393 */
394 private InputStream createDocumentInputStream(InputStream is) throws IOException
395 {
396 BufferedInputStream bin = new BufferedInputStream(is);
397 bin.mark(2);
398 int b0 = bin.read();
399 int b1 = bin.read();
400 bin.reset();
401
402 //Check for gzip magic number
403 if ((b1 << 8 | b0) == GZIPInputStream.GZIP_MAGIC)
404 {
405 GZIPInputStream iis = new GZIPInputStream(bin);
406 return iis;
407 } else
408 {
409 //Plain text
410 return bin;
411 }
412 }
413
414 public URI loadSVG(URL docRoot)
415 {
416 return loadSVG(docRoot, false);
417 }
418
419 /**
420 * Loads an SVG file and all the files it references from the URL provided.
421 * If a referenced file already exists in the SVG universe, it is not
422 * reloaded.
423 *
424 * @param docRoot - URL to the location where this SVG file can be found.
425 * @param forceLoad - if true, ignore cached diagram and reload
426 * @return - The URI that refers to the loaded document
427 */
428 public URI loadSVG(URL docRoot, boolean forceLoad)
429 {
430 try
431 {
432 URI uri = new URI(docRoot.toString());
433 if (loadedDocs.containsKey(uri) && !forceLoad)
434 {
435 return uri;
436 }
437
438 InputStream is = docRoot.openStream();
439 return loadSVG(uri, new InputSource(createDocumentInputStream(is)));
440 } catch (URISyntaxException ex)
441 {
442 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
443 "Could not parse", ex);
444 } catch (IOException e)
445 {
446 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
447 "Could not parse", e);
448 }
449
450 return null;
451 }
452
453 public URI loadSVG(InputStream is, String name) throws IOException
454 {
455 return loadSVG(is, name, false);
456 }
457
458 public URI loadSVG(InputStream is, String name, boolean forceLoad) throws IOException
459 {
460 URI uri = getStreamBuiltURI(name);
461 if (uri == null)
462 {
463 return null;
464 }
465 if (loadedDocs.containsKey(uri) && !forceLoad)
466 {
467 return uri;
468 }
469
470 return loadSVG(uri, new InputSource(createDocumentInputStream(is)));
471 }
472
473 public URI loadSVG(Reader reader, String name)
474 {
475 return loadSVG(reader, name, false);
476 }
477
478 /**
479 * This routine allows you to create SVG documents from data streams that
480 * may not necessarily have a URL to load from. Since every SVG document
481 * must be identified by a unique URL, Salamander provides a method to fake
482 * this for streams by defining it's own protocol - svgSalamander - for SVG
483 * documents without a formal URL.
484 *
485 * @param reader - A stream containing a valid SVG document
486 * @param name - <p>A unique name for this document. It will be used to
487 * construct a unique URI to refer to this document and perform resolution
488 * with relative URIs within this document.</p> <p>For example, a name of
489 * "/myScene" will produce the URI svgSalamander:/myScene.
490 * "/maps/canada/toronto" will produce svgSalamander:/maps/canada/toronto.
491 * If this second document then contained the href "../uk/london", it would
492 * resolve by default to svgSalamander:/maps/uk/london. That is, SVG
493 * Salamander defines the URI scheme svgSalamander for it's own internal use
494 * and uses it for uniquely identfying documents loaded by stream.</p> <p>If
495 * you need to link to documents outside of this scheme, you can either
496 * supply full hrefs (eg, href="url(http://www.kitfox.com/index.html)") or
497 * put the xml:base attribute in a tag to change the defaultbase URIs are
498 * resolved against</p> <p>If a name does not start with the character '/',
499 * it will be automatically prefixed to it.</p>
500 * @param forceLoad - if true, ignore cached diagram and reload
501 *
502 * @return - The URI that refers to the loaded document
503 */
504 public URI loadSVG(Reader reader, String name, boolean forceLoad)
505 {
506//System.err.println(url.toString());
507 //Synthesize URI for this stream
508 URI uri = getStreamBuiltURI(name);
509 if (uri == null)
510 {
511 return null;
512 }
513 if (loadedDocs.containsKey(uri) && !forceLoad)
514 {
515 return uri;
516 }
517
518 return loadSVG(uri, new InputSource(reader));
519 }
520
521 /**
522 * Synthesize a URI for an SVGDiagram constructed from a stream.
523 *
524 * @param name - Name given the document constructed from a stream.
525 */
526 public URI getStreamBuiltURI(String name)
527 {
528 if (name == null || name.length() == 0)
529 {
530 return null;
531 }
532
533 if (name.charAt(0) != '/')
534 {
535 name = '/' + name;
536 }
537
538 try
539 {
540 //Dummy URL for SVG documents built from image streams
541 return new URI(INPUTSTREAM_SCHEME, name, null);
542 } catch (Exception e)
543 {
544 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
545 "Could not parse", e);
546 return null;
547 }
548 }
549
550 private XMLReader getXMLReaderCached() throws SAXException, ParserConfigurationException
551 {
552 if (cachedReader == null)
553 {
554 SAXParserFactory factory = SAXParserFactory.newInstance();
555 factory.setNamespaceAware(true);
556 cachedReader = factory.newSAXParser().getXMLReader();
557 }
558 return cachedReader;
559 }
560
561 protected URI loadSVG(URI xmlBase, InputSource is)
562 {
563 // Use an instance of ourselves as the SAX event handler
564 SVGLoader handler = new SVGLoader(xmlBase, this, verbose);
565
566 //Place this docment in the universe before it is completely loaded
567 // so that the load process can refer to references within it's current
568 // document
569 loadedDocs.put(xmlBase, handler.getLoadedDiagram());
570
571 try
572 {
573 // Parse the input
574 XMLReader reader = getXMLReaderCached();
575 reader.setEntityResolver(
576 new EntityResolver()
577 {
578 public InputSource resolveEntity(String publicId, String systemId)
579 {
580 //Ignore all DTDs
581 return new InputSource(new ByteArrayInputStream(new byte[0]));
582 }
583 });
584 reader.setContentHandler(handler);
585 reader.parse(is);
586
587 handler.getLoadedDiagram().updateTime(curTime);
588 return xmlBase;
589 } catch (SAXParseException sex)
590 {
591 System.err.println("Error processing " + xmlBase);
592 System.err.println(sex.getMessage());
593
594 loadedDocs.remove(xmlBase);
595 return null;
596 } catch (Throwable e)
597 {
598 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
599 "Could not load SVG " + xmlBase, e);
600 }
601
602 return null;
603 }
604
605 /**
606 * Get list of uris of all loaded documents and subdocuments.
607 * @return
608 */
609 public ArrayList<URI> getLoadedDocumentURIs()
610 {
611 return new ArrayList<>(loadedDocs.keySet());
612 }
613
614 /**
615 * Remove loaded document from cache.
616 * @param uri
617 */
618 public void removeDocument(URI uri)
619 {
620 loadedDocs.remove(uri);
621 }
622
623 public boolean isVerbose()
624 {
625 return verbose;
626 }
627
628 public void setVerbose(boolean verbose)
629 {
630 this.verbose = verbose;
631 }
632
633 /**
634 * Uses serialization to duplicate this universe.
635 */
636 public SVGUniverse duplicate() throws IOException, ClassNotFoundException
637 {
638 ByteArrayOutputStream bs = new ByteArrayOutputStream();
639 ObjectOutputStream os = new ObjectOutputStream(bs);
640 os.writeObject(this);
641 os.close();
642
643 ByteArrayInputStream bin = new ByteArrayInputStream(bs.toByteArray());
644 ObjectInputStream is = new ObjectInputStream(bin);
645 SVGUniverse universe = (SVGUniverse) is.readObject();
646 is.close();
647
648 return universe;
649 }
650}
Note: See TracBrowser for help on using the repository browser.