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.logging.Level;
061import java.util.logging.Logger;
062import java.util.zip.GZIPInputStream;
063import javax.imageio.ImageIO;
064import javax.xml.parsers.ParserConfigurationException;
065import javax.xml.parsers.SAXParserFactory;
066import org.xml.sax.EntityResolver;
067import org.xml.sax.InputSource;
068import org.xml.sax.SAXException;
069import org.xml.sax.SAXParseException;
070import org.xml.sax.XMLReader;
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<URI, SVGDiagram> loadedDocs = new HashMap<URI, SVGDiagram>();
091    final HashMap<String, Font> loadedFonts = new HashMap<String, Font>();
092    final HashMap<URL, SoftReference<BufferedImage>> loadedImages = new HashMap<URL, SoftReference<BufferedImage>>();
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    //If true, <imageSVG> elements will only load image data that is included using inline data: uris
105    private boolean imageDataInlineOnly = false;
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     * Returns the current animation time in milliseconds.
136     */
137    public double getCurTime()
138    {
139        return curTime;
140    }
141
142    public void setCurTime(double curTime)
143    {
144        double oldTime = this.curTime;
145        this.curTime = curTime;
146        changes.firePropertyChange("curTime", new Double(oldTime), new Double(curTime));
147    }
148
149    /**
150     * Updates all time influenced style and presentation attributes in all SVG
151     * documents in this universe.
152     */
153    public void updateTime() throws SVGException
154    {
155        for (SVGDiagram dia : loadedDocs.values()) {
156            dia.updateTime(curTime);
157        }
158    }
159
160    /**
161     * Called by the Font element to let the universe know that a font has been
162     * loaded and is available.
163     */
164    void registerFont(Font font)
165    {
166        loadedFonts.put(font.getFontFace().getFontFamily(), font);
167    }
168
169    public Font getDefaultFont()
170    {
171        for (Font font : loadedFonts.values()) {
172            return font;
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<BufferedImage> ref = new SoftReference<BufferedImage>(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<BufferedImage> 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<BufferedImage>(img);
264            } else
265            {
266                BufferedImage img = ImageIO.read(imageURL);
267                ref = new SoftReference<BufferedImage>(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<BufferedImage> ref = (SoftReference<BufferedImage>) 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<BufferedImage>(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, ParserConfigurationException
580    {
581        if (cachedReader == null)
582        {
583            SAXParserFactory factory = SAXParserFactory.newInstance();
584            factory.setNamespaceAware(true);
585            cachedReader = factory.newSAXParser().getXMLReader();
586        }
587        return cachedReader;
588    }
589
590    protected URI loadSVG(URI xmlBase, InputSource is)
591    {
592        // Use an instance of ourselves as the SAX event handler
593        SVGLoader handler = new SVGLoader(xmlBase, this, verbose);
594
595        //Place this docment in the universe before it is completely loaded
596        // so that the load process can refer to references within it's current
597        // document
598        loadedDocs.put(xmlBase, handler.getLoadedDiagram());
599
600        try
601        {
602            // Parse the input
603            XMLReader reader = getXMLReaderCached();
604            reader.setEntityResolver(
605                new EntityResolver()
606                {
607                    public InputSource resolveEntity(String publicId, String systemId)
608                    {
609                        //Ignore all DTDs
610                        return new InputSource(new ByteArrayInputStream(new byte[0]));
611                    }
612                });
613            reader.setContentHandler(handler);
614            reader.parse(is);
615
616            handler.getLoadedDiagram().updateTime(curTime);
617            return xmlBase;
618        } catch (SAXParseException sex)
619        {
620            System.err.println("Error processing " + xmlBase);
621            System.err.println(sex.getMessage());
622
623            loadedDocs.remove(xmlBase);
624            return null;
625        } catch (Throwable e)
626        {
627            Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING,
628                "Could not load SVG " + xmlBase, e);
629        }
630
631        return null;
632    }
633
634    /**
635     * Get list of uris of all loaded documents and subdocuments.
636     * @return 
637     */
638    public ArrayList<URI> getLoadedDocumentURIs()
639    {
640        return new ArrayList<URI>(loadedDocs.keySet());
641    }
642    
643    /**
644     * Remove loaded document from cache.
645     * @param uri 
646     */
647    public void removeDocument(URI uri)
648    {
649        loadedDocs.remove(uri);
650    }
651    
652    public boolean isVerbose()
653    {
654        return verbose;
655    }
656
657    public void setVerbose(boolean verbose)
658    {
659        this.verbose = verbose;
660    }
661
662    /**
663     * Uses serialization to duplicate this universe.
664     */
665    public SVGUniverse duplicate() throws IOException, ClassNotFoundException
666    {
667        ByteArrayOutputStream bs = new ByteArrayOutputStream();
668        ObjectOutputStream os = new ObjectOutputStream(bs);
669        os.writeObject(this);
670        os.close();
671
672        ByteArrayInputStream bin = new ByteArrayInputStream(bs.toByteArray());
673        ObjectInputStream is = new ObjectInputStream(bin);
674        SVGUniverse universe = (SVGUniverse) is.readObject();
675        is.close();
676
677        return universe;
678    }
679
680    /**
681     * @return the imageDataInlineOnly
682     */
683    public boolean isImageDataInlineOnly()
684    {
685        return imageDataInlineOnly;
686    }
687
688    /**
689     * @param imageDataInlineOnly the imageDataInlineOnly to set
690     */
691    public void setImageDataInlineOnly(boolean imageDataInlineOnly)
692    {
693        this.imageDataInlineOnly = imageDataInlineOnly;
694    }
695}