001/* 002 * SVG Salamander 003 * Copyright (c) 2004, Mark McKay 004 * All rights reserved. 005 * 006 * Redistribution and use in source and binary forms, with or 007 * without modification, are permitted provided that the following 008 * conditions are met: 009 * 010 * - Redistributions of source code must retain the above 011 * copyright notice, this list of conditions and the following 012 * disclaimer. 013 * - Redistributions in binary form must reproduce the above 014 * copyright notice, this list of conditions and the following 015 * disclaimer in the documentation and/or other materials 016 * provided with the distribution. 017 * 018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 022 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 027 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 029 * OF THE POSSIBILITY OF SUCH DAMAGE. 030 * 031 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other 032 * projects can be found at http://www.kitfox.com 033 * 034 * Created on February 18, 2004, 11:43 PM 035 */ 036package com.kitfox.svg; 037 038import com.kitfox.svg.app.beans.SVGIcon; 039import com.kitfox.svg.util.Base64InputStream; 040import java.awt.Graphics2D; 041import java.awt.image.BufferedImage; 042import java.beans.PropertyChangeListener; 043import java.beans.PropertyChangeSupport; 044import java.io.BufferedInputStream; 045import java.io.ByteArrayInputStream; 046import java.io.ByteArrayOutputStream; 047import java.io.IOException; 048import java.io.InputStream; 049import java.io.ObjectInputStream; 050import java.io.ObjectOutputStream; 051import java.io.Reader; 052import java.io.Serializable; 053import java.lang.ref.SoftReference; 054import java.net.MalformedURLException; 055import java.net.URI; 056import java.net.URISyntaxException; 057import java.net.URL; 058import java.util.ArrayList; 059import java.util.HashMap; 060import java.util.Iterator; 061import java.util.logging.Level; 062import java.util.logging.Logger; 063import java.util.zip.GZIPInputStream; 064import javax.imageio.ImageIO; 065import org.xml.sax.EntityResolver; 066import org.xml.sax.InputSource; 067import org.xml.sax.SAXException; 068import org.xml.sax.SAXParseException; 069import org.xml.sax.XMLReader; 070import org.xml.sax.helpers.XMLReaderFactory; 071 072/** 073 * Many SVG files can be loaded at one time. These files will quite likely need 074 * to reference one another. The SVG universe provides a container for all these 075 * files and the means for them to relate to each other. 076 * 077 * @author Mark McKay 078 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a> 079 */ 080public class SVGUniverse implements Serializable 081{ 082 083 public static final long serialVersionUID = 0; 084 transient private PropertyChangeSupport changes = new PropertyChangeSupport(this); 085 /** 086 * Maps document URIs to their loaded SVG diagrams. Note that URIs for 087 * documents loaded from URLs will reflect their URLs and URIs for documents 088 * initiated from streams will have the scheme <i>svgSalamander</i>. 089 */ 090 final HashMap loadedDocs = new HashMap(); 091 final HashMap loadedFonts = new HashMap(); 092 final HashMap loadedImages = new HashMap(); 093 public static final String INPUTSTREAM_SCHEME = "svgSalamander"; 094 /** 095 * Current time in this universe. Used for resolving attributes that are 096 * influenced by track information. Time is in milliseconds. Time 0 097 * coresponds to the time of 0 in each member diagram. 098 */ 099 protected double curTime = 0.0; 100 private boolean verbose = false; 101 //Cache reader for efficiency 102 XMLReader cachedReader; 103 104 /** 105 * Creates a new instance of SVGUniverse 106 */ 107 public SVGUniverse() 108 { 109 } 110 111 public void addPropertyChangeListener(PropertyChangeListener l) 112 { 113 changes.addPropertyChangeListener(l); 114 } 115 116 public void removePropertyChangeListener(PropertyChangeListener l) 117 { 118 changes.removePropertyChangeListener(l); 119 } 120 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 } 130 131 /** 132 * Returns the current animation time in milliseconds. 133 */ 134 public double getCurTime() 135 { 136 return curTime; 137 } 138 139 public void setCurTime(double curTime) 140 { 141 double oldTime = this.curTime; 142 this.curTime = curTime; 143 changes.firePropertyChange("curTime", new Double(oldTime), new Double(curTime)); 144 } 145 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 { 154 SVGDiagram dia = (SVGDiagram) it.next(); 155 dia.updateTime(curTime); 156 } 157 } 158 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 } 167 168 public Font getDefaultFont() 169 { 170 for (Iterator it = loadedFonts.values().iterator(); it.hasNext();) 171 { 172 return (Font) it.next(); 173 } 174 return null; 175 } 176 177 public Font getFont(String fontName) 178 { 179 return (Font) loadedFonts.get(fontName); 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); 195 try 196 { 197// byte[] buf = new sun.misc.BASE64Decoder().decodeBuffer(content); 198// ByteArrayInputStream bais = new ByteArrayInputStream(buf); 199 ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes()); 200 Base64InputStream bais = new Base64InputStream(bis); 201 202 BufferedImage img = ImageIO.read(bais); 203 204 URL url; 205 int urlIdx = 0; 206 while (true) 207 { 208 url = new URL("inlineImage", "localhost", "img" + urlIdx); 209 if (!loadedImages.containsKey(url)) 210 { 211 break; 212 } 213 urlIdx++; 214 } 215 216 SoftReference ref = new SoftReference(img); 217 loadedImages.put(url, ref); 218 219 return url; 220 } catch (IOException ex) 221 { 222 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 223 "Could not decode inline image", ex); 224 } 225 } 226 return null; 227 } else 228 { 229 try 230 { 231 URL url = imageURI.toURL(); 232 registerImage(url); 233 return url; 234 } catch (MalformedURLException ex) 235 { 236 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 237 "Bad url", ex); 238 } 239 return null; 240 } 241 } 242 243 void registerImage(URL imageURL) 244 { 245 if (loadedImages.containsKey(imageURL)) 246 { 247 return; 248 } 249 250 SoftReference ref; 251 try 252 { 253 String fileName = imageURL.getFile(); 254 if (".svg".equals(fileName.substring(fileName.length() - 4).toLowerCase())) 255 { 256 SVGIcon icon = new SVGIcon(); 257 icon.setSvgURI(imageURL.toURI()); 258 259 BufferedImage img = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB); 260 Graphics2D g = img.createGraphics(); 261 icon.paintIcon(null, g, 0, 0); 262 g.dispose(); 263 ref = new SoftReference(img); 264 } else 265 { 266 BufferedImage img = ImageIO.read(imageURL); 267 ref = new SoftReference(img); 268 } 269 loadedImages.put(imageURL, ref); 270 } catch (Exception e) 271 { 272 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 273 "Could not load image: " + imageURL, e); 274 } 275 } 276 277 BufferedImage getImage(URL imageURL) 278 { 279 SoftReference ref = (SoftReference) loadedImages.get(imageURL); 280 if (ref == null) 281 { 282 return null; 283 } 284 285 BufferedImage img = (BufferedImage) ref.get(); 286 //If image was cleared from memory, reload it 287 if (img == null) 288 { 289 try 290 { 291 img = ImageIO.read(imageURL); 292 } catch (Exception e) 293 { 294 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 295 "Could not load image", e); 296 } 297 ref = new SoftReference(img); 298 loadedImages.put(imageURL, ref); 299 } 300 301 return img; 302 } 303 304 /** 305 * Returns the element of the document at the given URI. If the document is 306 * not already loaded, it will be. 307 */ 308 public SVGElement getElement(URI path) 309 { 310 return getElement(path, true); 311 } 312 313 public SVGElement getElement(URL path) 314 { 315 try 316 { 317 URI uri = new URI(path.toString()); 318 return getElement(uri, true); 319 } catch (Exception e) 320 { 321 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 322 "Could not parse url " + path, e); 323 } 324 return null; 325 } 326 327 /** 328 * Looks up a href within our universe. If the href refers to a document 329 * that is not loaded, it will be loaded. The URL #target will then be 330 * checked against the SVG diagram's index and the coresponding element 331 * returned. If there is no coresponding index, null is returned. 332 */ 333 public SVGElement getElement(URI path, boolean loadIfAbsent) 334 { 335 try 336 { 337 //Strip fragment from URI 338 URI xmlBase = new URI(path.getScheme(), path.getSchemeSpecificPart(), null); 339 340 SVGDiagram dia = (SVGDiagram) loadedDocs.get(xmlBase); 341 if (dia == null && loadIfAbsent) 342 { 343//System.err.println("SVGUnivserse: " + xmlBase.toString()); 344//javax.swing.JOptionPane.showMessageDialog(null, xmlBase.toString()); 345 URL url = xmlBase.toURL(); 346 347 loadSVG(url, false); 348 dia = (SVGDiagram) loadedDocs.get(xmlBase); 349 if (dia == null) 350 { 351 return null; 352 } 353 } 354 355 String fragment = path.getFragment(); 356 return fragment == null ? dia.getRoot() : dia.getElement(fragment); 357 } catch (Exception e) 358 { 359 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 360 "Could not parse path " + path, e); 361 return null; 362 } 363 } 364 365 public SVGDiagram getDiagram(URI xmlBase) 366 { 367 return getDiagram(xmlBase, true); 368 } 369 370 /** 371 * Returns the diagram that has been loaded from this root. If diagram is 372 * not already loaded, returns null. 373 */ 374 public SVGDiagram getDiagram(URI xmlBase, boolean loadIfAbsent) 375 { 376 if (xmlBase == null) 377 { 378 return null; 379 } 380 381 SVGDiagram dia = (SVGDiagram) loadedDocs.get(xmlBase); 382 if (dia != null || !loadIfAbsent) 383 { 384 return dia; 385 } 386 387 //Load missing diagram 388 try 389 { 390 URL url; 391 if ("jar".equals(xmlBase.getScheme()) && xmlBase.getPath() != null && !xmlBase.getPath().contains("!/")) 392 { 393 //Workaround for resources stored in jars loaded by Webstart. 394 //http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6753651 395 url = SVGUniverse.class.getResource("xmlBase.getPath()"); 396 } 397 else 398 { 399 url = xmlBase.toURL(); 400 } 401 402 403 loadSVG(url, false); 404 dia = (SVGDiagram) loadedDocs.get(xmlBase); 405 return dia; 406 } catch (Exception e) 407 { 408 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 409 "Could not parse", e); 410 } 411 412 return null; 413 } 414 415 /** 416 * Wraps input stream in a BufferedInputStream. If it is detected that this 417 * input stream is GZIPped, also wraps in a GZIPInputStream for inflation. 418 * 419 * @param is Raw input stream 420 * @return Uncompressed stream of SVG data 421 * @throws java.io.IOException 422 */ 423 private InputStream createDocumentInputStream(InputStream is) throws IOException 424 { 425 BufferedInputStream bin = new BufferedInputStream(is); 426 bin.mark(2); 427 int b0 = bin.read(); 428 int b1 = bin.read(); 429 bin.reset(); 430 431 //Check for gzip magic number 432 if ((b1 << 8 | b0) == GZIPInputStream.GZIP_MAGIC) 433 { 434 GZIPInputStream iis = new GZIPInputStream(bin); 435 return iis; 436 } else 437 { 438 //Plain text 439 return bin; 440 } 441 } 442 443 public URI loadSVG(URL docRoot) 444 { 445 return loadSVG(docRoot, false); 446 } 447 448 /** 449 * Loads an SVG file and all the files it references from the URL provided. 450 * If a referenced file already exists in the SVG universe, it is not 451 * reloaded. 452 * 453 * @param docRoot - URL to the location where this SVG file can be found. 454 * @param forceLoad - if true, ignore cached diagram and reload 455 * @return - The URI that refers to the loaded document 456 */ 457 public URI loadSVG(URL docRoot, boolean forceLoad) 458 { 459 try 460 { 461 URI uri = new URI(docRoot.toString()); 462 if (loadedDocs.containsKey(uri) && !forceLoad) 463 { 464 return uri; 465 } 466 467 InputStream is = docRoot.openStream(); 468 return loadSVG(uri, new InputSource(createDocumentInputStream(is))); 469 } catch (URISyntaxException ex) 470 { 471 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 472 "Could not parse", ex); 473 } catch (IOException e) 474 { 475 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 476 "Could not parse", e); 477 } 478 479 return null; 480 } 481 482 public URI loadSVG(InputStream is, String name) throws IOException 483 { 484 return loadSVG(is, name, false); 485 } 486 487 public URI loadSVG(InputStream is, String name, boolean forceLoad) throws IOException 488 { 489 URI uri = getStreamBuiltURI(name); 490 if (uri == null) 491 { 492 return null; 493 } 494 if (loadedDocs.containsKey(uri) && !forceLoad) 495 { 496 return uri; 497 } 498 499 return loadSVG(uri, new InputSource(createDocumentInputStream(is))); 500 } 501 502 public URI loadSVG(Reader reader, String name) 503 { 504 return loadSVG(reader, name, false); 505 } 506 507 /** 508 * This routine allows you to create SVG documents from data streams that 509 * may not necessarily have a URL to load from. Since every SVG document 510 * must be identified by a unique URL, Salamander provides a method to fake 511 * this for streams by defining it's own protocol - svgSalamander - for SVG 512 * documents without a formal URL. 513 * 514 * @param reader - A stream containing a valid SVG document 515 * @param name - <p>A unique name for this document. It will be used to 516 * construct a unique URI to refer to this document and perform resolution 517 * with relative URIs within this document.</p> <p>For example, a name of 518 * "/myScene" will produce the URI svgSalamander:/myScene. 519 * "/maps/canada/toronto" will produce svgSalamander:/maps/canada/toronto. 520 * If this second document then contained the href "../uk/london", it would 521 * resolve by default to svgSalamander:/maps/uk/london. That is, SVG 522 * Salamander defines the URI scheme svgSalamander for it's own internal use 523 * and uses it for uniquely identfying documents loaded by stream.</p> <p>If 524 * you need to link to documents outside of this scheme, you can either 525 * supply full hrefs (eg, href="url(http://www.kitfox.com/index.html)") or 526 * put the xml:base attribute in a tag to change the defaultbase URIs are 527 * resolved against</p> <p>If a name does not start with the character '/', 528 * it will be automatically prefixed to it.</p> 529 * @param forceLoad - if true, ignore cached diagram and reload 530 * 531 * @return - The URI that refers to the loaded document 532 */ 533 public URI loadSVG(Reader reader, String name, boolean forceLoad) 534 { 535//System.err.println(url.toString()); 536 //Synthesize URI for this stream 537 URI uri = getStreamBuiltURI(name); 538 if (uri == null) 539 { 540 return null; 541 } 542 if (loadedDocs.containsKey(uri) && !forceLoad) 543 { 544 return uri; 545 } 546 547 return loadSVG(uri, new InputSource(reader)); 548 } 549 550 /** 551 * Synthesize a URI for an SVGDiagram constructed from a stream. 552 * 553 * @param name - Name given the document constructed from a stream. 554 */ 555 public URI getStreamBuiltURI(String name) 556 { 557 if (name == null || name.length() == 0) 558 { 559 return null; 560 } 561 562 if (name.charAt(0) != '/') 563 { 564 name = '/' + name; 565 } 566 567 try 568 { 569 //Dummy URL for SVG documents built from image streams 570 return new URI(INPUTSTREAM_SCHEME, name, null); 571 } catch (Exception e) 572 { 573 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 574 "Could not parse", e); 575 return null; 576 } 577 } 578 579 private XMLReader getXMLReaderCached() throws SAXException 580 { 581 if (cachedReader == null) 582 { 583 cachedReader = XMLReaderFactory.createXMLReader(); 584 } 585 return cachedReader; 586 } 587 588 protected URI loadSVG(URI xmlBase, InputSource is) 589 { 590 // Use an instance of ourselves as the SAX event handler 591 SVGLoader handler = new SVGLoader(xmlBase, this, verbose); 592 593 //Place this docment in the universe before it is completely loaded 594 // so that the load process can refer to references within it's current 595 // document 596 loadedDocs.put(xmlBase, handler.getLoadedDiagram()); 597 598 try 599 { 600 // Parse the input 601 XMLReader reader = getXMLReaderCached(); 602 reader.setEntityResolver( 603 new EntityResolver() 604 { 605 public InputSource resolveEntity(String publicId, String systemId) 606 { 607 //Ignore all DTDs 608 return new InputSource(new ByteArrayInputStream(new byte[0])); 609 } 610 }); 611 reader.setContentHandler(handler); 612 reader.parse(is); 613 614 handler.getLoadedDiagram().updateTime(curTime); 615 return xmlBase; 616 } catch (SAXParseException sex) 617 { 618 System.err.println("Error processing " + xmlBase); 619 System.err.println(sex.getMessage()); 620 621 loadedDocs.remove(xmlBase); 622 return null; 623 } catch (Throwable e) 624 { 625 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, 626 "Could not load SVG " + xmlBase, e); 627 } 628 629 return null; 630 } 631 632 /** 633 * Get list of uris of all loaded documents and subdocuments. 634 * @return 635 */ 636 public ArrayList getLoadedDocumentURIs() 637 { 638 return new ArrayList(loadedDocs.keySet()); 639 } 640 641 /** 642 * Remove loaded document from cache. 643 * @param uri 644 */ 645 public void removeDocument(URI uri) 646 { 647 loadedDocs.remove(uri); 648 } 649 650 public boolean isVerbose() 651 { 652 return verbose; 653 } 654 655 public void setVerbose(boolean verbose) 656 { 657 this.verbose = verbose; 658 } 659 660 /** 661 * Uses serialization to duplicate this universe. 662 */ 663 public SVGUniverse duplicate() throws IOException, ClassNotFoundException 664 { 665 ByteArrayOutputStream bs = new ByteArrayOutputStream(); 666 ObjectOutputStream os = new ObjectOutputStream(bs); 667 os.writeObject(this); 668 os.close(); 669 670 ByteArrayInputStream bin = new ByteArrayInputStream(bs.toByteArray()); 671 ObjectInputStream is = new ObjectInputStream(bin); 672 SVGUniverse universe = (SVGUniverse) is.readObject(); 673 is.close(); 674 675 return universe; 676 } 677}