summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/kitfox/svg/SVGUniverse.java
blob: c68dc7911937ad731865c491dfa21634f2cb7b00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/*
 * SVGUniverse.java
 *
 *
 *  The Salamander Project - 2D and 3D graphics libraries in Java
 *  Copyright (C) 2004 Mark McKay
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *  Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
 *  projects can be found at http://www.kitfox.com
 *
 * Created on February 18, 2004, 11:43 PM
 */

package com.kitfox.svg;

import com.kitfox.svg.app.beans.SVGIcon;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Reader;
import java.io.Serializable;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.HashMap;
import java.util.Iterator;
import java.util.zip.GZIPInputStream;
import javax.imageio.ImageIO;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * Many SVG files can be loaded at one time.  These files will quite likely
 * need to reference one another.  The SVG universe provides a container for
 * all these files and the means for them to relate to each other.
 *
 * @author Mark McKay
 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
 */
public class SVGUniverse implements Serializable
{
    public static final long serialVersionUID = 0;
    
    transient private PropertyChangeSupport changes = new PropertyChangeSupport(this);
    
    /**
     * Maps document URIs to their loaded SVG diagrams.  Note that URIs for
     * documents loaded from URLs will reflect their URLs and URIs for documents
     * initiated from streams will have the scheme <i>svgSalamander</i>.
     */
    final HashMap loadedDocs = new HashMap();
    
    final HashMap loadedFonts = new HashMap();
    
    final HashMap loadedImages = new HashMap();
    
    public static final String INPUTSTREAM_SCHEME = "svgSalamander";
    
    /**
     * Current time in this universe.  Used for resolving attributes that
     * are influenced by track information.  Time is in milliseconds.  Time
     * 0 coresponds to the time of 0 in each member diagram.
     */
    protected double curTime = 0.0;
    
    private boolean verbose = false;

    //Cache reader for efficiency
    XMLReader cachedReader;
    
    /** Creates a new instance of SVGUniverse */
    public SVGUniverse()
    {
    }
    
    public void addPropertyChangeListener(PropertyChangeListener l)
    {
        changes.addPropertyChangeListener(l);
    }
    
    public void removePropertyChangeListener(PropertyChangeListener l)
    {
        changes.removePropertyChangeListener(l);
    }
    
    /**
     * Release all loaded SVG document from memory
     */
    public void clear()
    {
        loadedDocs.clear();
        loadedFonts.clear();
        loadedImages.clear();
    }
    
    /**
     * Returns the current animation time in milliseconds.
     */
    public double getCurTime()
    { 
        return curTime; 
    }
    
    public void setCurTime(double curTime)
    {
        double oldTime = this.curTime;
        this.curTime = curTime; 
        changes.firePropertyChange("curTime", new Double(oldTime), new Double(curTime));
    }
    
    /**
     * Updates all time influenced style and presentation attributes in all SVG
     * documents in this universe.
     */
    public void updateTime() throws SVGException
    {
        for (Iterator it = loadedDocs.values().iterator(); it.hasNext();)
        {
            SVGDiagram dia = (SVGDiagram)it.next();
            dia.updateTime(curTime);
        }
    }
    
    /**
     * Called by the Font element to let the universe know that a font has been
     * loaded and is available.
     */
    void registerFont(Font font)
    {
        loadedFonts.put(font.getFontFace().getFontFamily(), font);
    }
    
    public Font getDefaultFont()
    {
        for (Iterator it = loadedFonts.values().iterator(); it.hasNext();)
        {
            return (Font)it.next();
        }
        return null;
    }
    
    public Font getFont(String fontName)
    {
        return (Font)loadedFonts.get(fontName);
    }

    URL registerImage(URI imageURI)
    {
        String scheme = imageURI.getScheme();
        if (scheme.equals("data"))
        {
            String path = imageURI.getRawSchemeSpecificPart();
            int idx = path.indexOf(';');
            String mime = path.substring(0, idx);
            String content = path.substring(idx + 1);

            if (content.startsWith("base64"))
            {
                content = content.substring(6);
                try {
                    byte[] buf = new sun.misc.BASE64Decoder().decodeBuffer(content);
                    ByteArrayInputStream bais = new ByteArrayInputStream(buf);
                    BufferedImage img = ImageIO.read(bais);

                    URL url;
                    int urlIdx = 0;
                    while (true)
                    {
                        url = new URL("inlineImage", "localhost", "img" + urlIdx);
                        if (!loadedImages.containsKey(url))
                        {
                            break;
                        }
                        urlIdx++;
                    }

                    SoftReference ref = new SoftReference(img);
                    loadedImages.put(url, ref);

                    return url;
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            return null;
        }
        else
        {
            try {
                URL url = imageURI.toURL();
                registerImage(url);
                return url;
            } catch (MalformedURLException ex) {
                ex.printStackTrace();
            }
            return null;
        }
    }

    void registerImage(URL imageURL)
    {
        if (loadedImages.containsKey(imageURL)) return;
        
        SoftReference ref;
        try
        {
            String fileName = imageURL.getFile();
            if (".svg".equals(fileName.substring(fileName.length() - 4).toLowerCase()))
            {
                SVGIcon icon = new SVGIcon();
                icon.setSvgURI(imageURL.toURI());
                
                BufferedImage img = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
                Graphics2D g = img.createGraphics();
                icon.paintIcon(null, g, 0, 0);
                g.dispose();
                ref = new SoftReference(img);
            }
            else
            {
                BufferedImage img = ImageIO.read(imageURL);
                ref = new SoftReference(img);
            }
            loadedImages.put(imageURL, ref);
        }
        catch (Exception e)
        {
            System.err.println("Could not load image: " + imageURL);
            e.printStackTrace();
        }
    }
    
    BufferedImage getImage(URL imageURL)
    {
        SoftReference ref = (SoftReference)loadedImages.get(imageURL);
        if (ref == null) return null;
        
        BufferedImage img = (BufferedImage)ref.get();
        //If image was cleared from memory, reload it
        if (img == null)
        {
            try
            {
                img = ImageIO.read(imageURL);
            }
            catch (Exception e)
            { e.printStackTrace(); }
            ref = new SoftReference(img);
            loadedImages.put(imageURL, ref);
        }
        
        return img;
    }
    
    /**
     * Returns the element of the document at the given URI.  If the document
     * is not already loaded, it will be.
     */
    public SVGElement getElement(URI path)
    {
        return getElement(path, true);
    }
    
    public SVGElement getElement(URL path)
    {
        try
        {
            URI uri = new URI(path.toString());
            return getElement(uri, true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * Looks up a href within our universe.  If the href refers to a document that
     * is not loaded, it will be loaded.  The URL #target will then be checked
     * against the SVG diagram's index and the coresponding element returned.
     * If there is no coresponding index, null is returned.
     */
    public SVGElement getElement(URI path, boolean loadIfAbsent)
    {
        try
        {
            //Strip fragment from URI
            URI xmlBase = new URI(path.getScheme(), path.getSchemeSpecificPart(), null);
            
            SVGDiagram dia = (SVGDiagram)loadedDocs.get(xmlBase);
            if (dia == null && loadIfAbsent)
            {
//System.err.println("SVGUnivserse: " + xmlBase.toString());
//javax.swing.JOptionPane.showMessageDialog(null, xmlBase.toString());
                URL url = xmlBase.toURL();
                
                loadSVG(url, false);
                dia = (SVGDiagram)loadedDocs.get(xmlBase);
                if (dia == null) return null;
            }
            
            String fragment = path.getFragment();
            return fragment == null ? dia.getRoot() : dia.getElement(fragment);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
    
    public SVGDiagram getDiagram(URI xmlBase)
    {
        return getDiagram(xmlBase, true);
    }
    
    /**
     * Returns the diagram that has been loaded from this root.  If diagram is
     * not already loaded, returns null.
     */
    public SVGDiagram getDiagram(URI xmlBase, boolean loadIfAbsent)
    {
        if (xmlBase == null) return null;

        SVGDiagram dia = (SVGDiagram)loadedDocs.get(xmlBase);
        if (dia != null || !loadIfAbsent) return dia;
        
        //Load missing diagram
        try
        {
            URL url;
            if ("jar".equals(xmlBase.getScheme()) && xmlBase.getPath() != null && !xmlBase.getPath().contains("!/"))
            {
                //Workaround for resources stored in jars loaded by Webstart.
                //http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6753651
                url = SVGUniverse.class.getResource("xmlBase.getPath()");
            }
            else
            {
                url = xmlBase.toURL();
            }

            
            loadSVG(url, false);
            dia = (SVGDiagram)loadedDocs.get(xmlBase);
            return dia;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
        return null;
    }
    
    /**
     * Wraps input stream in a BufferedInputStream.  If it is detected that this
     * input stream is GZIPped, also wraps in a GZIPInputStream for inflation.
     * 
     * @param is Raw input stream
     * @return Uncompressed stream of SVG data
     * @throws java.io.IOException
     */
    private InputStream createDocumentInputStream(InputStream is) throws IOException
    {
        BufferedInputStream bin = new BufferedInputStream(is);
        bin.mark(2);
        int b0 = bin.read();
        int b1 = bin.read();
        bin.reset();

        //Check for gzip magic number
        if ((b1 << 8 | b0) == GZIPInputStream.GZIP_MAGIC)
        {
            GZIPInputStream iis = new GZIPInputStream(bin);
            return iis;
        }
        else
        {
            //Plain text
            return bin;
        }
    }
    
    public URI loadSVG(URL docRoot)
    {
        return loadSVG(docRoot, false);
    }
    
    /**
     * Loads an SVG file and all the files it references from the URL provided.
     * If a referenced file already exists in the SVG universe, it is not
     * reloaded.
     * @param docRoot - URL to the location where this SVG file can be found.
     * @param forceLoad - if true, ignore cached diagram and reload
     * @return - The URI that refers to the loaded document
     */
    public URI loadSVG(URL docRoot, boolean forceLoad)
    {
        try
        {
            URI uri = new URI(docRoot.toString());
            if (loadedDocs.containsKey(uri) && !forceLoad)
            {
                return uri;
            }
            
            InputStream is = docRoot.openStream();
            return loadSVG(uri, new InputSource(createDocumentInputStream(is)));
        }
        catch (URISyntaxException ex)
        {
            ex.printStackTrace();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        
        return null;
    }
    
    
    public URI loadSVG(InputStream is, String name) throws IOException
    {
        return loadSVG(is, name, false);
    }
    
    public URI loadSVG(InputStream is, String name, boolean forceLoad) throws IOException
    {
        URI uri = getStreamBuiltURI(name);
        if (uri == null) return null;
        if (loadedDocs.containsKey(uri) && !forceLoad) return uri;
        
        return loadSVG(uri, new InputSource(createDocumentInputStream(is)));
    }
    
    public URI loadSVG(Reader reader, String name)
    {
        return loadSVG(reader, name, false);
    }
    
    /**
     * This routine allows you to create SVG documents from data streams that
     * may not necessarily have a URL to load from.  Since every SVG document
     * must be identified by a unique URL, Salamander provides a method to
     * fake this for streams by defining it's own protocol - svgSalamander -
     * for SVG documents without a formal URL.
     *
     * @param reader - A stream containing a valid SVG document
     * @param name - <p>A unique name for this document.  It will be used to
     * construct a unique URI to refer to this document and perform resolution
     * with relative URIs within this document.</p>
     * <p>For example, a name of "/myScene" will produce the URI
     * svgSalamander:/myScene.  "/maps/canada/toronto" will produce
     * svgSalamander:/maps/canada/toronto.  If this second document then
     * contained the href "../uk/london", it would resolve by default to
     * svgSalamander:/maps/uk/london.  That is, SVG Salamander defines the
     * URI scheme svgSalamander for it's own internal use and uses it
     * for uniquely identfying documents loaded by stream.</p>
     * <p>If you need to link to documents outside of this scheme, you can
     * either supply full hrefs (eg, href="url(http://www.kitfox.com/index.html)")
     * or put the xml:base attribute in a tag to change the defaultbase
     * URIs are resolved against</p>
     * <p>If a name does not start with the character '/', it will be automatically
     * prefixed to it.</p>
     * @param forceLoad - if true, ignore cached diagram and reload
     *
     * @return - The URI that refers to the loaded document
     */
    public URI loadSVG(Reader reader, String name, boolean forceLoad)
    {
//System.err.println(url.toString());
        //Synthesize URI for this stream
        URI uri = getStreamBuiltURI(name);
        if (uri == null) return null;
        if (loadedDocs.containsKey(uri) && !forceLoad) return uri;
        
        return loadSVG(uri, new InputSource(reader));
    }
    
    /**
     * Synthesize a URI for an SVGDiagram constructed from a stream.
     * @param name - Name given the document constructed from a stream.
     */
    public URI getStreamBuiltURI(String name)
    {
        if (name == null || name.length() == 0) return null;
        
        if (name.charAt(0) != '/') name = '/' + name;
        
        try
        {
            //Dummy URL for SVG documents built from image streams
            return new URI(INPUTSTREAM_SCHEME, name, null);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
    
    private XMLReader getXMLReaderCached() throws SAXException
    {
        if (cachedReader == null)
        {
            cachedReader = XMLReaderFactory.createXMLReader();
        }
        return cachedReader;
    }
    
//    protected URI loadSVG(URI xmlBase, InputStream is)
    protected URI loadSVG(URI xmlBase, InputSource is)
    {
        // Use an instance of ourselves as the SAX event handler
        SVGLoader handler = new SVGLoader(xmlBase, this, verbose);
        
        //Place this docment in the universe before it is completely loaded
        // so that the load process can refer to references within it's current
        // document
//System.err.println("SVGUniverse: loading dia " + xmlBase);
        loadedDocs.put(xmlBase, handler.getLoadedDiagram());
        
        // Use the default (non-validating) parser
//        SAXParserFactory factory = SAXParserFactory.newInstance();
//        factory.setValidating(false);
//        factory.setNamespaceAware(true);
        
        try
        {
            // Parse the input
            XMLReader reader = getXMLReaderCached();
            reader.setEntityResolver(
                new EntityResolver()
                {
                    public InputSource resolveEntity(String publicId, String systemId)
                    {
                        //Ignore all DTDs
                        return new InputSource(new ByteArrayInputStream(new byte[0]));
                    }
                }
            );
            reader.setContentHandler(handler);
            reader.parse(is);
            
//            SAXParser saxParser = factory.newSAXParser();
//            saxParser.parse(new InputSource(new BufferedReader(is)), handler);
            handler.getLoadedDiagram().updateTime(curTime);
            return xmlBase;
        }
        catch (SAXParseException sex)
        {
            System.err.println("Error processing " + xmlBase);
            System.err.println(sex.getMessage());
            
            loadedDocs.remove(xmlBase);
            return null;
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
        
        return null;
    }
    
    public static void main(String argv[])
    {
        try
        {
            URL url = new URL("svgSalamander", "localhost", -1, "abc.svg",
                    new URLStreamHandler()
            {
                protected URLConnection openConnection(URL u)
                {
                    return null;
                }
            }
            );
//            URL url2 = new URL("svgSalamander", "localhost", -1, "abc.svg");
            
            //Investigate URI resolution
            URI uriA, uriB, uriC, uriD, uriE;
            
            uriA = new URI("svgSalamander", "/names/mySpecialName", null);
//            uriA = new URI("http://www.kitfox.com/salamander");
//            uriA = new URI("svgSalamander://mySpecialName/grape");
            System.err.println(uriA.toString());
            System.err.println(uriA.getScheme());
            
            uriB = uriA.resolve("#begin");
            System.err.println(uriB.toString());
            
            uriC = uriA.resolve("tree#boing");
            System.err.println(uriC.toString());
            
            uriC = uriA.resolve("../tree#boing");
            System.err.println(uriC.toString());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public boolean isVerbose()
    {
        return verbose;
    }

    public void setVerbose(boolean verbose)
    {
        this.verbose = verbose;
    }
    
    /**
     * Uses serialization to duplicate this universe.
     */
    public SVGUniverse duplicate() throws IOException, ClassNotFoundException
    {
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(bs);
        os.writeObject(this);
        os.close();

        ByteArrayInputStream bin = new ByteArrayInputStream(bs.toByteArray());
        ObjectInputStream is = new ObjectInputStream(bin);
        SVGUniverse universe = (SVGUniverse)is.readObject();
        is.close();

        return universe;
    }
}