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

Last change on this file since 7253 was 6617, checked in by Don-vip, 12 years ago

Replace non-working Sun bugtracker links to new OpenJDK one

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