summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/kitfox/svg/SVGElement.java
blob: 7398ea9b65f6f17718e465da6215bcf39e59b85b (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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
/*
 * SVGElement.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 January 26, 2004, 1:59 AM
 */

package com.kitfox.svg;

import java.util.*;
import java.util.regex.*;
import java.net.*;
import java.awt.geom.*;

import org.xml.sax.*;
import com.kitfox.svg.animation.*;
import com.kitfox.svg.pathcmd.*;
import com.kitfox.svg.xml.*;
import java.io.Serializable;

/**
 * @author Mark McKay
 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
 */
abstract public class SVGElement implements Serializable
{
    public static final long serialVersionUID = 0;
    
    public static final String SVG_NS = "http://www.w3.org/2000/svg";
    
    protected SVGElement parent = null;
    
    protected final ArrayList children = new ArrayList();
    
    protected String id = null;
    /**
     * CSS class.  Used for applying style sheet information.
     */
    protected String cssClass = null;
    
    /**
     * Styles defined for this elemnt via the <b>style</b> attribute.
     */
    protected final HashMap inlineStyles = new HashMap();
    
    /**
     * Presentation attributes set for this element.  Ie, any attribute other
     * than the <b>style</b> attribute.
     */
    protected final HashMap presAttribs = new HashMap();
    
    /**
     * A list of presentation attributes to not include in the presentation
     * attribute set.
     */
    protected static final Set ignorePresAttrib;
    static
    {
        HashSet set = new HashSet();
//        set.add("id");
//        set.add("class");
//        set.add("style");
//        set.add("xml:base");
        
        ignorePresAttrib = Collections.unmodifiableSet(set);
    }
    
    /**
     * This element may override the URI we resolve against with an
     * xml:base attribute.  If so, a copy is placed here.  Otherwise, we defer
     * to our parent for the reolution base
     */
    protected URI xmlBase = null;
    
    /**
     * The diagram this element belongs to
     */
    protected SVGDiagram diagram;
    /**
     * Link to the universe we reside in
     */
//    protected SVGUniverse universe;
    
    protected final TrackManager trackManager = new TrackManager();

    boolean dirty = true;

//    public static final Matcher adobeId = Pattern.compile("(.*)_1_").matcher("");
//    static final String fpNumRe = "[-+]?((\\d+)|(\\d*\\.\\d+))([-+]?[eE]\\d+)?";
    
    /** Creates a new instance of SVGElement */
    public SVGElement()
    {
        this(null, null, null);
    }
    
    public SVGElement(String id, SVGElement parent)
    {
        this(id, null, parent);
    }
    
    public SVGElement(String id, String cssClass, SVGElement parent)
    {
        this.id = id;
        this.cssClass = cssClass;
        this.parent = parent;
    }
    
    public SVGElement getParent()
    {
        return parent;
    }
    
    void setParent(SVGElement parent)
    {
        this.parent = parent;
    }
    
    /**
     * @return an ordered list of nodes from the root of the tree to this node
     */
    public List getPath(List retVec)
    {
        if (retVec == null) retVec = new ArrayList();
        
        if (parent != null)
        {
            parent.getPath(retVec);
        }
        retVec.add(this);
        
        return retVec;
    }
    
    /**
     * @param retVec - A list to add all children to.  If null, a new list is
     * created and children of this group are added.
     *
     * @return The list containing the children of this group
     */
    public List getChildren(List retVec)
    {
        if (retVec == null) retVec = new ArrayList();
        
        retVec.addAll(children);
        
        return retVec;
    }
    
    /**
     * @param id - Id of svg element to return
     * @return the child of the given id, or null if no such child exists.
     */
    public SVGElement getChild(String id)
    {
        for (Iterator it = children.iterator(); it.hasNext();)
        {
            SVGElement ele = (SVGElement)it.next();
            String eleId = ele.getId();
            if (eleId != null && eleId.equals(id)) return ele;
        }
        
        return null;
    }
    
    /**
     * Searches children for given element.  If found, returns index of child.
     * Otherwise returns -1.
     */
    public int indexOfChild(SVGElement child)
    {
        return children.indexOf(child);
    }
    
    /**
     * Swaps 2 elements in children.
     * @i index of first
     * @j index of second
     *
     * @return true if successful, false otherwise
     */
    public void swapChildren(int i, int j) throws SVGException
    {
        if ((children == null) || (i < 0) || (i >= children.size()) || (j < 0) || (j >= children.size()))
        {
            return;
        }
        
        Object temp = children.get(i);
        children.set(i, children.get(j));
        children.set(j, temp);
        build();
    }
    
    /**
     * Called during SAX load process to notify that this tag has begun the process
     * of being loaded
     * @param attrs - Attributes of this tag
     * @param helper - An object passed to all SVG elements involved in this build
     * process to aid in sharing information.
     */
    public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent) throws SAXException
    {
        //Set identification info
        this.parent = parent;
        this.diagram = helper.diagram;
        
        this.id = attrs.getValue("id");
        if (this.id != null && !this.id.equals(""))
        {
            diagram.setElement(this.id, this);
        }
        
        String className = attrs.getValue("class");
        this.cssClass = (className == null || className.equals("")) ? null : className;
        //docRoot = helper.docRoot;
        //universe = helper.universe;
        
        //Parse style string, if any
        String style = attrs.getValue("style");
        if (style != null)
        {
            HashMap map = XMLParseUtil.parseStyle(style, inlineStyles);
        }
        
        String base = attrs.getValue("xml:base");
        if (base != null && !base.equals(""))
        {
            try
            {
                xmlBase = new URI(base);
            }
            catch (Exception e)
            {
                throw new SAXException(e);
            }
        }
        
        //Place all other attributes into the presentation attribute list
        int numAttrs = attrs.getLength();
        for (int i = 0; i < numAttrs; i++)
        {
            String name = attrs.getQName(i);
            if (ignorePresAttrib.contains(name)) continue;
            String value = attrs.getValue(i);
            
            presAttribs.put(name, new StyleAttribute(name, value));
        }
    }

    public void removeAttribute(String name, int attribType)
    {
        switch (attribType)
        {
            case AnimationElement.AT_CSS:
                inlineStyles.remove(name);
                return;
            case AnimationElement.AT_XML:
                presAttribs.remove(name);
                return;
        }
    }

    public void addAttribute(String name, int attribType, String value) throws SVGElementException
    {
        if (hasAttribute(name, attribType)) throw new SVGElementException(this, "Attribute " + name + "(" + AnimationElement.animationElementToString(attribType) + ") already exists");
        
        //Alter layout for id attribute
        if ("id".equals(name))
        {
            if (diagram != null)
            {
                diagram.removeElement(id);
                diagram.setElement(value, this);
            }
            this.id = value;
        }
        
        switch (attribType)
        {
            case AnimationElement.AT_CSS:
                inlineStyles.put(name, new StyleAttribute(name, value));
                return;
            case AnimationElement.AT_XML:
                presAttribs.put(name, new StyleAttribute(name, value));
                return;
        }
        
        throw new SVGElementException(this, "Invalid attribute type " + attribType);
    }
    
    public boolean hasAttribute(String name, int attribType) throws SVGElementException
    {
        switch (attribType)
        {
            case AnimationElement.AT_CSS:
                return inlineStyles.containsKey(name);
            case AnimationElement.AT_XML:
                return presAttribs.containsKey(name);
            case AnimationElement.AT_AUTO:
                return inlineStyles.containsKey(name) || presAttribs.containsKey(name);
        }
        
        throw new SVGElementException(this, "Invalid attribute type " + attribType);
    }
    
    /**
     * @return a set of Strings that corespond to CSS attributes on this element
     */
    public Set getInlineAttributes()
    {
        return inlineStyles.keySet();
    }
    
    /**
     * @return a set of Strings that corespond to XML attributes on this element
     */
    public Set getPresentationAttributes()
    {
        return presAttribs.keySet();
    }
    
    /**
     * Called after the start element but before the end element to indicate
     * each child tag that has been processed
     */
    public void loaderAddChild(SVGLoaderHelper helper, SVGElement child) throws SVGElementException
    {
        children.add(child);
        child.parent = this;
        child.setDiagram(diagram);
        
        //Add info to track if we've scanned animation element
        if (child instanceof AnimationElement)
        {
            trackManager.addTrackElement((AnimationElement)child);
        }
    }
    
    protected void setDiagram(SVGDiagram diagram)
    {
        this.diagram = diagram;
        diagram.setElement(id, this);
        for (Iterator it = children.iterator(); it.hasNext();)
        {
            SVGElement ele = (SVGElement)it.next();
            ele.setDiagram(diagram);
        }
    }
    
    public void removeChild(SVGElement child) throws SVGElementException
    {
        if (!children.contains(child))
        {
            throw new SVGElementException(this, "Element does not contain child " + child);
        }
        
        children.remove(child);
    }
    
    /**
     * Called during load process to add text scanned within a tag
     */
    public void loaderAddText(SVGLoaderHelper helper, String text)
    {
    }
    
    /**
     * Called to indicate that this tag and the tags it contains have been completely
     * processed, and that it should finish any load processes.
     */
    public void loaderEndElement(SVGLoaderHelper helper) throws SVGParseException
    {
        try
        {
            build();
        }
        catch (SVGException se)
        {
            throw new SVGParseException(se);
        }
    }
    
    /**
     * Called by internal processes to rebuild the geometry of this node
     * from it's presentation attributes, style attributes and animated tracks.
     */
    protected void build() throws SVGException
    {
        StyleAttribute sty = new StyleAttribute();
        
        if (getPres(sty.setName("id")))
        {
            String newId = sty.getStringValue();
            if (!newId.equals(id))
            {
                diagram.removeElement(id);
                id = newId;
                diagram.setElement(this.id, this);
            }
        }
        if (getPres(sty.setName("class"))) cssClass = sty.getStringValue();
        if (getPres(sty.setName("xml:base"))) xmlBase = sty.getURIValue();
    }
    
    public URI getXMLBase()
    {
        return xmlBase != null ? xmlBase :
            (parent != null ? parent.getXMLBase() : diagram.getXMLBase());
    }

    /**
     * @return the id assigned to this node.  Null if no id explicitly set.
     */
    public String getId()
    {
        return id; 
    }
    
    
    LinkedList contexts = new LinkedList();
    
    /**
     * Hack to allow nodes to temporarily change their parents.  The Use tag will
     * need this so it can alter the attributes that a particular node uses.
     */
    protected void pushParentContext(SVGElement context)
    {
        contexts.addLast(context);
    }

    protected SVGElement popParentContext()
    {
        return (SVGElement)contexts.removeLast();
    }
    
    protected SVGElement getParentContext()
    {
        return contexts.isEmpty() ? null : (SVGElement)contexts.getLast();
    }
    
    /*
     * Returns the named style attribute.  Checks for inline styles first, then
     * internal and extranal style sheets, and finally checks for presentation
     * attributes.
     * @param styleName - Name of attribute to return
     * @param recursive - If true and this object does not contain the
     * named style attribute, checks attributes of parents abck to root until
     * one found.
     */
    public boolean getStyle(StyleAttribute attrib) throws SVGException
    {
        return getStyle(attrib, true);
    }
    
    
    public void setAttribute(String name, int attribType, String value) throws SVGElementException
    {
        StyleAttribute styAttr;
        
        
        switch (attribType)
        {
            case AnimationElement.AT_CSS:
            {
                styAttr = (StyleAttribute)inlineStyles.get(name);
                break;
            }
            case AnimationElement.AT_XML:
            {
                styAttr = (StyleAttribute)presAttribs.get(name);
                break;
            }
            case AnimationElement.AT_AUTO:
            {
                styAttr = (StyleAttribute)inlineStyles.get(name);
                
                if (styAttr == null)
                {
                    styAttr = (StyleAttribute)presAttribs.get(name);
                }
                break;
            }
            default:
                throw new SVGElementException(this, "Invalid attribute type " + attribType);
        }
        
        if (styAttr == null)
        {
            throw new SVGElementException(this, "Could not find attribute " + name + "(" + AnimationElement.animationElementToString(attribType) + ").  Make sure to create attribute before setting it.");
        }
        
        //Alter layout for relevant attributes
        if ("id".equals(styAttr.getName()))
        {
            if (diagram != null)
            {
                diagram.removeElement(this.id);
                diagram.setElement(value, this);
            }
            this.id = value;
        }
        
        styAttr.setStringValue(value);
    }
    
    /**
     * Copies the current style into the passed style attribute.  Checks for
     * inline styles first, then internal and extranal style sheets, and
     * finally checks for presentation attributes.  Recursively checks parents.
     * @param attrib - Attribute to write style data to.  Must have it's name
     * set to the name of the style being queried.
     * @param recursive - If true and this object does not contain the
     * named style attribute, checks attributes of parents abck to root until
     * one found.
     */
    public boolean getStyle(StyleAttribute attrib, boolean recursive) throws SVGException
    {
        String styName = attrib.getName();
        
        //Check for local inline styles
        StyleAttribute styAttr = (StyleAttribute)inlineStyles.get(styName);
        
        attrib.setStringValue(styAttr == null ? "" : styAttr.getStringValue());
        
        //Evalutate coresponding track, if one exists
        TrackBase track = trackManager.getTrack(styName, AnimationElement.AT_CSS);
        if (track != null)
        {
            track.getValue(attrib, diagram.getUniverse().getCurTime());
            return true;
        }
        
        //Return if we've found a non animated style
        if (styAttr != null) return true;
        
        
        
        //Implement style sheet lookup later
        
        //Check for presentation attribute
        StyleAttribute presAttr = (StyleAttribute)presAttribs.get(styName);
        
        attrib.setStringValue(presAttr == null ? "" : presAttr.getStringValue());
        
        //Evalutate coresponding track, if one exists
        track = trackManager.getTrack(styName, AnimationElement.AT_XML);
        if (track != null)
        {
            track.getValue(attrib, diagram.getUniverse().getCurTime());
            return true;
        }
        
        //Return if we've found a presentation attribute instead
        if (presAttr != null) return true;
        
        
        //If we're recursive, check parents
        if (recursive)
        {
            SVGElement parentContext = getParentContext();
            if (parentContext != null) return parentContext.getStyle(attrib, true);
            if (parent != null) return parent.getStyle(attrib, true);
        }
        
        //Unsuccessful reading style attribute
        return false;
    }
    
    /**
     * @return the raw style value of this attribute.  Does not take the 
     * presentation value or animation into consideration.  Used by animations 
     * to determine the base to animate from.
     */
    public StyleAttribute getStyleAbsolute(String styName)
    {
        //Check for local inline styles
        return (StyleAttribute)inlineStyles.get(styName);
    }
    
    /**
     * Copies the presentation attribute into the passed one.
     * @return - True if attribute was read successfully
     */
    public boolean getPres(StyleAttribute attrib) throws SVGException
    {
        String presName = attrib.getName();
        
        //Make sure we have a coresponding presentation attribute
        StyleAttribute presAttr = (StyleAttribute)presAttribs.get(presName);
        
        //Copy presentation value directly
        attrib.setStringValue(presAttr == null ? "" : presAttr.getStringValue());
        
        //Evalutate coresponding track, if one exists
        TrackBase track = trackManager.getTrack(presName, AnimationElement.AT_XML);
        if (track != null)
        {
            track.getValue(attrib, diagram.getUniverse().getCurTime());
            return true;
        }
        
        //Return if we found presentation attribute
        if (presAttr != null) return true;
        
        return false;
    }
    
    /**
     * @return the raw presentation value of this attribute.  Ignores any 
     * modifications applied by style attributes or animation.  Used by 
     * animations to determine the starting point to animate from
     */
    public StyleAttribute getPresAbsolute(String styName)
    {
        //Check for local inline styles
        return (StyleAttribute)presAttribs.get(styName);
    }
    
    static protected AffineTransform parseTransform(String val) throws SVGException
    {
        final Matcher matchExpression = Pattern.compile("\\w+\\([^)]*\\)").matcher("");
        
        AffineTransform retXform = new AffineTransform();
        
        matchExpression.reset(val);
        while (matchExpression.find())
        {
            retXform.concatenate(parseSingleTransform(matchExpression.group()));
        }
        
        return retXform;
    }
    
    static public AffineTransform parseSingleTransform(String val) throws SVGException
    {
        final Matcher matchWord = Pattern.compile("[-.\\w]+").matcher("");
        
        AffineTransform retXform = new AffineTransform();
        
        matchWord.reset(val);
        if (!matchWord.find())
        {
            //Return identity transformation if no data present (eg, empty string)
            return retXform;
        }
        
        String function = matchWord.group().toLowerCase();
        
        LinkedList termList = new LinkedList();
        while (matchWord.find())
        {
            termList.add(matchWord.group());
        }
        
        
        double[] terms = new double[termList.size()];
        Iterator it = termList.iterator();
        int count = 0;
        while (it.hasNext())
        {
            terms[count++] = XMLParseUtil.parseDouble((String)it.next());
        }
        
        //Calculate transformation
        if (function.equals("matrix"))
        {
            retXform.setTransform(terms[0], terms[1], terms[2], terms[3], terms[4], terms[5]);
        }
        else if (function.equals("translate"))
        {
            if (terms.length == 1)
            {
                retXform.setToTranslation(terms[0], 0);
            }
            else
            {
                retXform.setToTranslation(terms[0], terms[1]);
            }
        }
        else if (function.equals("scale"))
        {
            if (terms.length > 1)
                retXform.setToScale(terms[0], terms[1]);
            else
                retXform.setToScale(terms[0], terms[0]);
        }
        else if (function.equals("rotate"))
        {
            if (terms.length > 2)
                retXform.setToRotation(Math.toRadians(terms[0]), terms[1], terms[2]);
            else
                retXform.setToRotation(Math.toRadians(terms[0]));
        }
        else if (function.equals("skewx"))
        {
            retXform.setToShear(Math.toRadians(terms[0]), 0.0);
        }
        else if (function.equals("skewy"))
        {
            retXform.setToShear(0.0, Math.toRadians(terms[0]));
        }
        else
        {
            throw new SVGException("Unknown transform type");
        }
        
        return retXform;
    }
    
    static protected float nextFloat(LinkedList l)
    {
        String s = (String)l.removeFirst();
        return Float.parseFloat(s);
    }
    
    static protected PathCommand[] parsePathList(String list)
    {
        final Matcher matchPathCmd = Pattern.compile("([MmLlHhVvAaQqTtCcSsZz])|([-+]?((\\d*\\.\\d+)|(\\d+))([eE][-+]?\\d+)?)").matcher(list);
        
        //Tokenize
        LinkedList tokens = new LinkedList();
        while (matchPathCmd.find())
        {
            tokens.addLast(matchPathCmd.group());
        }
        
        
        boolean defaultRelative = false;
        LinkedList cmdList = new LinkedList();
        char curCmd = 'Z';
        while (tokens.size() != 0)
        {
            String curToken = (String)tokens.removeFirst();
            char initChar = curToken.charAt(0);
            if ((initChar >= 'A' && initChar <='Z') || (initChar >= 'a' && initChar <='z'))
            {
                curCmd = initChar;
            }
            else
            {
                tokens.addFirst(curToken);
            }
            
            PathCommand cmd = null;
            
            switch (curCmd)
            {
                case 'M':
                    cmd = new MoveTo(false, nextFloat(tokens), nextFloat(tokens));
                    curCmd = 'L';
                    break;
                case 'm':
                    cmd = new MoveTo(true, nextFloat(tokens), nextFloat(tokens));
                    curCmd = 'l';
                    break;
                case 'L':
                    cmd = new LineTo(false, nextFloat(tokens), nextFloat(tokens));
                    break;
                case 'l':
                    cmd = new LineTo(true, nextFloat(tokens), nextFloat(tokens));
                    break;
                case 'H':
                    cmd = new Horizontal(false, nextFloat(tokens));
                    break;
                case 'h':
                    cmd = new Horizontal(true, nextFloat(tokens));
                    break;
                case 'V':
                    cmd = new Vertical(false, nextFloat(tokens));
                    break;
                case 'v':
                    cmd = new Vertical(true, nextFloat(tokens));
                    break;
                case 'A':
                    cmd = new Arc(false, nextFloat(tokens), nextFloat(tokens),
                            nextFloat(tokens),
                            nextFloat(tokens) == 1f, nextFloat(tokens) == 1f,
                            nextFloat(tokens), nextFloat(tokens));
                    break;
                case 'a':
                    cmd = new Arc(true, nextFloat(tokens), nextFloat(tokens),
                            nextFloat(tokens),
                            nextFloat(tokens) == 1f, nextFloat(tokens) == 1f,
                            nextFloat(tokens), nextFloat(tokens));
                    break;
                case 'Q':
                    cmd = new Quadratic(false, nextFloat(tokens), nextFloat(tokens),
                            nextFloat(tokens), nextFloat(tokens));
                    break;
                case 'q':
                    cmd = new Quadratic(true, nextFloat(tokens), nextFloat(tokens),
                            nextFloat(tokens), nextFloat(tokens));
                    break;
                case 'T':
                    cmd = new QuadraticSmooth(false, nextFloat(tokens), nextFloat(tokens));
                    break;
                case 't':
                    cmd = new QuadraticSmooth(true, nextFloat(tokens), nextFloat(tokens));
                    break;
                case 'C':
                    cmd = new Cubic(false, nextFloat(tokens), nextFloat(tokens),
                            nextFloat(tokens), nextFloat(tokens),
                            nextFloat(tokens), nextFloat(tokens));
                    break;
                case 'c':
                    cmd = new Cubic(true, nextFloat(tokens), nextFloat(tokens),
                            nextFloat(tokens), nextFloat(tokens),
                            nextFloat(tokens), nextFloat(tokens));
                    break;
                case 'S':
                    cmd = new CubicSmooth(false, nextFloat(tokens), nextFloat(tokens),
                            nextFloat(tokens), nextFloat(tokens));
                    break;
                case 's':
                    cmd = new CubicSmooth(true, nextFloat(tokens), nextFloat(tokens),
                            nextFloat(tokens), nextFloat(tokens));
                    break;
                case 'Z':
                case 'z':
                    cmd = new Terminal();
                    break;
                default:
                    throw new RuntimeException("Invalid path element");
            }
            
            cmdList.add(cmd);
            defaultRelative = cmd.isRelative;
        }
        
        PathCommand[] retArr = new PathCommand[cmdList.size()];
        cmdList.toArray(retArr);
        return retArr;
    }
    
    static protected GeneralPath buildPath(String text, int windingRule)
    {
        PathCommand[] commands = parsePathList(text);
        
        int numKnots = 2;
        for (int i = 0; i < commands.length; i++)
        {
            numKnots += commands[i].getNumKnotsAdded();
        }
        
        
        GeneralPath path = new GeneralPath(windingRule, numKnots);
        
        BuildHistory hist = new BuildHistory();
        
        for (int i = 0; i < commands.length; i++)
        {
            PathCommand cmd = commands[i];
            cmd.appendPath(path, hist);
        }
        
        return path;
    }
    
    
    
    /**
     * Updates all attributes in this diagram associated with a time event.
     * Ie, all attributes with track information.
     * @return - true if this node has changed state as a result of the time
     * update
     */
    abstract public boolean updateTime(double curTime) throws SVGException;

    public int getNumChildren()
    {
        return children.size();
    }

    public SVGElement getChild(int i)
    {
        return (SVGElement)children.get(i);
    }
    
}