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

Last change on this file since 8024 was 7676, checked in by stoecker, 12 years ago

update SVG code to current SVN (fix line endings), see #10479

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