summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/kitfox/svg/xml/XMLParseUtil.java
blob: c8900b907d74f24ae3e3b5d4d4aa762791ad6518 (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
/*
 * SVG Salamander
 * Copyright (c) 2004, Mark McKay
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or 
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 *   - Redistributions of source code must retain the above 
 *     copyright notice, this list of conditions and the following
 *     disclaimer.
 *   - Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials 
 *     provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 * 
 * 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, 1:49 PM
 */

package com.kitfox.svg.xml;

import org.w3c.dom.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;
import java.lang.reflect.*;

/**
 * @author Mark McKay
 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
 */
public class XMLParseUtil
{
    static final Matcher fpMatch = Pattern.compile("([-+]?((\\d*\\.\\d+)|(\\d+))([eE][+-]?\\d+)?)(\\%|in|cm|mm|pt|pc|px|em|ex)?").matcher("");
    static final Matcher intMatch = Pattern.compile("[-+]?\\d+").matcher("");

    /** Creates a new instance of XMLParseUtil */
    private XMLParseUtil()
    {
    }

    /**
     * Scans the tag's children and returns the first text element found
     */
    public static String getTagText(Element ele)
    {
        NodeList nl = ele.getChildNodes();
        int size = nl.getLength();

        Node node = null;
        int i = 0;
        for (; i < size; i++)
        {
            node = nl.item(i);
            if (node instanceof Text) break;
        }
        if (i == size || node == null) return null;

        return ((Text)node).getData();
    }

    /**
     * Returns the first node that is a direct child of root with the coresponding
     * name.  Does not search children of children.
     */
    public static Element getFirstChild(Element root, String name)
    {
        NodeList nl = root.getChildNodes();
        int size = nl.getLength();
        for (int i = 0; i < size; i++)
        {
            Node node = nl.item(i);
            if (!(node instanceof Element)) continue;
            Element ele = (Element)node;
            if (ele.getTagName().equals(name)) return ele;
        }

        return null;
    }

    public static String[] parseStringList(String list)
    {
//        final Pattern patWs = Pattern.compile("\\s+");
        final Matcher matchWs = Pattern.compile("[^\\s]+").matcher("");
        matchWs.reset(list);

        LinkedList matchList = new LinkedList();
        while (matchWs.find())
        {
            matchList.add(matchWs.group());
        }

        String[] retArr = new String[matchList.size()];
        return (String[])matchList.toArray(retArr);
    }

    public static boolean isDouble(String val)
    {
        fpMatch.reset(val);
        return fpMatch.matches();
    }
    
    public static double parseDouble(String val)
    {
        /*
        if (val == null) return 0.0;

        double retVal = 0.0;
        try
        { retVal = Double.parseDouble(val); }
        catch (Exception e)
        {}
        return retVal;
         */
        return findDouble(val);
    }

    /**
     * Searches the given string for the first floating point number it contains,
     * parses and returns it.
     */
    public synchronized static double findDouble(String val)
    {
        if (val == null) return 0;

        fpMatch.reset(val);
        try
        {
            if (!fpMatch.find()) return 0;
        }
        catch (StringIndexOutOfBoundsException e)
        {
            System.err.println("XMLParseUtil: regex parse problem: '" + val + "'");
            e.printStackTrace();
        }

        val = fpMatch.group(1);
        //System.err.println("Parsing " + val);

        double retVal = 0;
        try
        { 
            retVal = Double.parseDouble(val); 
            
            float pixPerInch;
            try {
                pixPerInch = (float)Toolkit.getDefaultToolkit().getScreenResolution();
            }
            catch (NoClassDefFoundError err)
            {
                //Default value for headless X servers
                pixPerInch = 72;
            }
            final float inchesPerCm = .3936f;
            final String units = fpMatch.group(6);
            
            if ("%".equals(units)) retVal /= 100;
            else if ("in".equals(units))
            {
                retVal *= pixPerInch;
            }
            else if ("cm".equals(units))
            {
                retVal *= inchesPerCm * pixPerInch;
            }
            else if ("mm".equals(units))
            {
                retVal *= inchesPerCm * pixPerInch * .1f;
            }
            else if ("pt".equals(units))
            {
                retVal *= (1f / 72f) * pixPerInch;
            }
            else if ("pc".equals(units))
            {
                retVal *= (1f / 6f) * pixPerInch;
            }
        }
        catch (Exception e)
        {}
        return retVal;
    }

    /**
     * Scans an input string for double values.  For each value found, places
     * in a list.  This method regards any characters not part of a floating
     * point value to be seperators.  Thus this will parse whitespace seperated,
     * comma seperated, and many other separation schemes correctly.
     */
    public synchronized static double[] parseDoubleList(String list)
    {
        if (list == null) return null;

        fpMatch.reset(list);

        LinkedList doubList = new LinkedList();
        while (fpMatch.find())
        {
            String val = fpMatch.group(1);
            doubList.add(Double.valueOf(val));
        }

        double[] retArr = new double[doubList.size()];
        Iterator it = doubList.iterator();
        int idx = 0;
        while (it.hasNext())
        {
            retArr[idx++] = ((Double)it.next()).doubleValue();
        }

        return retArr;
    }

    public static float parseFloat(String val)
    {
        /*
        if (val == null) return 0f;

        float retVal = 0f;
        try
        { retVal = Float.parseFloat(val); }
        catch (Exception e)
        {}
        return retVal;
         */
        return findFloat(val);
    }

    /**
     * Searches the given string for the first floating point number it contains,
     * parses and returns it.
     */
    public synchronized static float findFloat(String val)
    {
        if (val == null) return 0f;

        fpMatch.reset(val);
        if (!fpMatch.find()) return 0f;

        val = fpMatch.group(1);
        //System.err.println("Parsing " + val);

        float retVal = 0f;
        try
        {
            retVal = Float.parseFloat(val);
            String units = fpMatch.group(6);
            if ("%".equals(units)) retVal /= 100;
        }
        catch (Exception e)
        {}
        return retVal;
    }

    public synchronized static float[] parseFloatList(String list)
    {
        if (list == null) return null;

        fpMatch.reset(list);

        LinkedList floatList = new LinkedList();
        while (fpMatch.find())
        {
            String val = fpMatch.group(1);
            floatList.add(Float.valueOf(val));
        }

        float[] retArr = new float[floatList.size()];
        Iterator it = floatList.iterator();
        int idx = 0;
        while (it.hasNext())
        {
            retArr[idx++] = ((Float)it.next()).floatValue();
        }

        return retArr;
    }

    public static int parseInt(String val)
    {
        if (val == null) return 0;

        int retVal = 0;
        try
        { retVal = Integer.parseInt(val); }
        catch (Exception e)
        {}
        return retVal;
    }

    /**
     * Searches the given string for the first integer point number it contains,
     * parses and returns it.
     */
    public static int findInt(String val)
    {
        if (val == null) return 0;

        intMatch.reset(val);
        if (!intMatch.find()) return 0;

        val = intMatch.group();
        //System.err.println("Parsing " + val);

        int retVal = 0;
        try
        { retVal = Integer.parseInt(val); }
        catch (Exception e)
        {}
        return retVal;
    }

    public static int[] parseIntList(String list)
    {
        if (list == null) return null;

        intMatch.reset(list);

        LinkedList intList = new LinkedList();
        while (intMatch.find())
        {
            String val = intMatch.group();
            intList.add(Integer.valueOf(val));
        }

        int[] retArr = new int[intList.size()];
        Iterator it = intList.iterator();
        int idx = 0;
        while (it.hasNext())
        {
            retArr[idx++] = ((Integer)it.next()).intValue();
        }

        return retArr;
    }
/*
    public static int parseHex(String val)
    {
        int retVal = 0;
        
        for (int i = 0; i < val.length(); i++)
        {
            retVal <<= 4;
            
            char ch = val.charAt(i);
            if (ch >= '0' && ch <= '9')
            {
                retVal |= ch - '0';
            }
            else if (ch >= 'a' && ch <= 'z')
            {
                retVal |= ch - 'a' + 10;
            }
            else if (ch >= 'A' && ch <= 'Z')
            {
                retVal |= ch - 'A' + 10;
            }
            else throw new RuntimeException();
        }
        
        return retVal;
    }
*/
    /**
     * The input string represents a ratio.  Can either be specified as a
     * double number on the range of [0.0 1.0] or as a percentage [0% 100%]
     */
    public static double parseRatio(String val)
    {
        if (val == null || val.equals("")) return 0.0;

        if (val.charAt(val.length() - 1) == '%')
        {
            parseDouble(val.substring(0, val.length() - 1));
        }
        return parseDouble(val);
    }

    public static NumberWithUnits parseNumberWithUnits(String val)
    {
        if (val == null) return null;

        return new NumberWithUnits(val);
    }
/*
    public static Color parseColor(String val)
    {
        Color retVal = null;

        if (val.charAt(0) == '#')
        {
            String hexStrn = val.substring(1);
            
            if (hexStrn.length() == 3)
            {
                hexStrn = "" + hexStrn.charAt(0) + hexStrn.charAt(0) + hexStrn.charAt(1) + hexStrn.charAt(1) + hexStrn.charAt(2) + hexStrn.charAt(2);
            }
            int hexVal = parseHex(hexStrn);

            retVal = new Color(hexVal);
        }
        else
        {
            final Matcher rgbMatch = Pattern.compile("rgb\\((\\d+),(\\d+),(\\d+)\\)", Pattern.CASE_INSENSITIVE).matcher("");

            rgbMatch.reset(val);
            if (rgbMatch.matches())
            {
                int r = Integer.parseInt(rgbMatch.group(1));
                int g = Integer.parseInt(rgbMatch.group(2));
                int b = Integer.parseInt(rgbMatch.group(3));
                retVal = new Color(r, g, b);
            }
            else
            {
                Color lookupCol = ColorTable.instance().lookupColor(val);
                if (lookupCol != null) retVal = lookupCol;
            }
        }

        return retVal;
    }
*/
    /**
     * Parses the given attribute of this tag and returns it as a String.
     */
    public static String getAttribString(Element ele, String name)
    {
        return ele.getAttribute(name);
    }

    /**
     * Parses the given attribute of this tag and returns it as an int.
     */
    public static int getAttribInt(Element ele, String name)
    {
        String sval = ele.getAttribute(name);
        int val = 0;
        try { val = Integer.parseInt(sval); } catch (Exception e) {}

        return val;
    }

    /**
     * Parses the given attribute of this tag as a hexadecimal encoded string and
     * returns it as an int
     */
    public static int getAttribIntHex(Element ele, String name)
    {
        String sval = ele.getAttribute(name);
        int val = 0;
        try { val = Integer.parseInt(sval, 16); } catch (Exception e) {}

        return val;
    }

    /**
     * Parses the given attribute of this tag and returns it as a float
     */
    public static float getAttribFloat(Element ele, String name)
    {
        String sval = ele.getAttribute(name);
        float val = 0.0f;
        try { val = Float.parseFloat(sval); } catch (Exception e) {}

        return val;
    }

    /**
     * Parses the given attribute of this tag and returns it as a double.
     */
    public static double getAttribDouble(Element ele, String name)
    {
        String sval = ele.getAttribute(name);
        double val = 0.0;
        try { val = Double.parseDouble(sval); } catch (Exception e) {}

        return val;
    }

    /**
     * Parses the given attribute of this tag and returns it as a boolean.
     * Essentially compares the lower case textual value to the string "true"
     */
    public static boolean getAttribBoolean(Element ele, String name)
    {
        String sval = ele.getAttribute(name);

        return sval.toLowerCase().equals("true");
    }

    public static URL getAttribURL(Element ele, String name, URL docRoot)
    {
        String sval = ele.getAttribute(name);

        URL url;
        try
        {
            return new URL(docRoot, sval);
        }
        catch (Exception e)
        {
            return null;
        }
    }

    /**
     * Returns the first ReadableXMLElement with the given name
     */
    public static ReadableXMLElement getElement(Class classType, Element root, String name, URL docRoot)
    {
        if (root == null) return null;

        //Do not process if not a LoadableObject
        if (!ReadableXMLElement.class.isAssignableFrom(classType))
        {
            return null;
        }

        NodeList nl = root.getChildNodes();
        int size = nl.getLength();
        for (int i = 0; i < size; i++)
        {
            Node node = nl.item(i);
            if (!(node instanceof Element)) continue;
            Element ele = (Element)node;
            if (!ele.getTagName().equals(name)) continue;

            ReadableXMLElement newObj = null;
            try { newObj = (ReadableXMLElement)classType.newInstance(); }
            catch (Exception e) { e.printStackTrace(); continue; }
            newObj.read(ele, docRoot);

            if (newObj == null) continue;

            return newObj;
        }

        return null;
    }

    /**
     * Returns a HashMap of nodes that are children of root.  All nodes will
     * be of class classType and have a tag name of 'name'.  'key' is
     * an attribute of tag 'name' who's string value will be used as the key
     * in the HashMap
     */
    public static HashMap getElementHashMap(Class classType, Element root, String name, String key, URL docRoot)
    {
        if (root == null) return null;

        //Do not process if not a LoadableObject
        if (!ReadableXMLElement.class.isAssignableFrom(classType))
        {
            return null;
        }

        HashMap retMap = new HashMap();

/*
        Class[] params = {Element.class, URL.class};
        Method loadMethod = null;
        try { loadMethod = classType.getMethod("load", params); }
        catch (Exception e) { e.printStackTrace(); return null; }

 */
        NodeList nl = root.getChildNodes();
        int size = nl.getLength();
        for (int i = 0; i < size; i++)
        {
            Node node = nl.item(i);
            if (!(node instanceof Element)) continue;
            Element ele = (Element)node;
            if (!ele.getTagName().equals(name)) continue;

            ReadableXMLElement newObj = null;
            try { newObj = (ReadableXMLElement)classType.newInstance(); }
            catch (Exception e) { e.printStackTrace(); continue; }
            newObj.read(ele, docRoot);
/*
            Object[] args = {ele, source};
            Object obj = null;
            try { obj = loadMethod.invoke(null, args); }
            catch (Exception e) { e.printStackTrace(); }

 */
            if (newObj == null) continue;

            String keyVal = getAttribString(ele, key);
            retMap.put(keyVal, newObj);
        }

        return retMap;
    }

    public static HashSet getElementHashSet(Class classType, Element root, String name, URL docRoot)
    {
        if (root == null) return null;

        //Do not process if not a LoadableObject
        if (!ReadableXMLElement.class.isAssignableFrom(classType))
        {
            return null;
        }

        HashSet retSet = new HashSet();

        /*
        Class[] params = {Element.class, URL.class};
        Method loadMethod = null;
        try { loadMethod = classType.getMethod("load", params); }
        catch (Exception e) { e.printStackTrace(); return null; }
        */

        NodeList nl = root.getChildNodes();
        int size = nl.getLength();
        for (int i = 0; i < size; i++)
        {
            Node node = nl.item(i);
            if (!(node instanceof Element)) continue;
            Element ele = (Element)node;
            if (!ele.getTagName().equals(name)) continue;

            ReadableXMLElement newObj = null;
            try { newObj = (ReadableXMLElement)classType.newInstance(); }
            catch (Exception e) { e.printStackTrace(); continue; }
            newObj.read(ele, docRoot);
            /*
            Object[] args = {ele, source};
            Object obj = null;
            try { obj = loadMethod.invoke(null, args); }
            catch (Exception e) { e.printStackTrace(); }
             */

            if (newObj == null) continue;

            retSet.add(newObj);
        }

        return retSet;
    }


    public static LinkedList getElementLinkedList(Class classType, Element root, String name, URL docRoot)
    {
        if (root == null) return null;

        //Do not process if not a LoadableObject
        if (!ReadableXMLElement.class.isAssignableFrom(classType))
        {
            return null;
        }

        NodeList nl = root.getChildNodes();
        LinkedList elementCache = new LinkedList();
        int size = nl.getLength();
        for (int i = 0; i < size; i++)
        {
            Node node = nl.item(i);
            if (!(node instanceof Element)) continue;
            Element ele = (Element)node;
            if (!ele.getTagName().equals(name)) continue;

            ReadableXMLElement newObj = null;
            try { newObj = (ReadableXMLElement)classType.newInstance(); }
            catch (Exception e) { e.printStackTrace(); continue; }
            newObj.read(ele, docRoot);

            elementCache.addLast(newObj);
        }

        return elementCache;
    }

    public static Object[] getElementArray(Class classType, Element root, String name, URL docRoot)
    {
        if (root == null) return null;

        //Do not process if not a LoadableObject
        if (!ReadableXMLElement.class.isAssignableFrom(classType))
        {
            return null;
        }

        LinkedList elementCache = getElementLinkedList(classType, root, name, docRoot);

        Object[] retArr = (Object[])Array.newInstance(classType, elementCache.size());
        return elementCache.toArray(retArr);
    }

    /**
     * Takes a number of tags of name 'name' that are children of 'root', and
     * looks for attributes of 'attrib' on them.  Converts attributes to an
     * int and returns in an array.
     */
    public static int[] getElementArrayInt(Element root, String name, String attrib)
    {
        if (root == null) return null;

        NodeList nl = root.getChildNodes();
        LinkedList elementCache = new LinkedList();
        int size = nl.getLength();

        for (int i = 0; i < size; i++)
        {
            Node node = nl.item(i);
            if (!(node instanceof Element)) continue;
            Element ele = (Element)node;
            if (!ele.getTagName().equals(name)) continue;

            String valS = ele.getAttribute(attrib);
            int eleVal = 0;
            try { eleVal = Integer.parseInt(valS); }
            catch (Exception e) {}

            elementCache.addLast(new Integer(eleVal));
        }

        int[] retArr = new int[elementCache.size()];
        Iterator it = elementCache.iterator();
        int idx = 0;
        while (it.hasNext())
        {
            retArr[idx++] = ((Integer)it.next()).intValue();
        }

        return retArr;
    }

    /**
     * Takes a number of tags of name 'name' that are children of 'root', and
     * looks for attributes of 'attrib' on them.  Converts attributes to an
     * int and returns in an array.
     */
    public static String[] getElementArrayString(Element root, String name, String attrib)
    {
        if (root == null) return null;

        NodeList nl = root.getChildNodes();
        LinkedList elementCache = new LinkedList();
        int size = nl.getLength();

        for (int i = 0; i < size; i++)
        {
            Node node = nl.item(i);
            if (!(node instanceof Element)) continue;
            Element ele = (Element)node;
            if (!ele.getTagName().equals(name)) continue;

            String valS = ele.getAttribute(attrib);

            elementCache.addLast(valS);
        }

        String[] retArr = new String[elementCache.size()];
        Iterator it = elementCache.iterator();
        int idx = 0;
        while (it.hasNext())
        {
            retArr[idx++] = (String)it.next();
        }

        return retArr;
    }

    /**
     * Takes a CSS style string and retursn a hash of them.
     * @param styleString - A CSS formatted string of styles.  Eg,
     *     "font-size:12;fill:#d32c27;fill-rule:evenodd;stroke-width:1pt;"
     */
    public static HashMap parseStyle(String styleString) {
        return parseStyle(styleString, new HashMap());
    }

    /**
     * Takes a CSS style string and returns a hash of them.
     * @param styleString - A CSS formatted string of styles.  Eg,
     *     "font-size:12;fill:#d32c27;fill-rule:evenodd;stroke-width:1pt;"
     * @param map - A map to which these styles will be added
     */
    public static HashMap parseStyle(String styleString, HashMap map) {
        final Pattern patSemi = Pattern.compile(";");

        String[] styles = patSemi.split(styleString);

        for (int i = 0; i < styles.length; i++)
        {
            if (styles[i].length() == 0)
            {
                continue;
            }

            int colon = styles[i].indexOf(':');
            if (colon == -1)
            {
                continue;
            }

            String key = styles[i].substring(0, colon).trim();
            String value = styles[i].substring(colon + 1).trim();

            map.put(key, new StyleAttribute(key, value));
        }

        return map;
    }
}