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

Last change on this file since 10865 was 10865, checked in by Don-vip, 8 years ago

see #13232 - remove unused classes from metadata-extractor and SvgSalamander

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