~ubuntu-branches/ubuntu/precise/scilab/precise

« back to all changes in this revision

Viewing changes to modules/xcos/src/java/org/scilab/modules/xcos/utils/StyleMap.java

  • Committer: Bazaar Package Importer
  • Author(s): Sylvestre Ledru
  • Date: 2010-04-16 15:57:24 UTC
  • mfrom: (1.1.9 upstream) (4.4.1 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100416155724-lx1sdku0oidl0ffn
Tags: 5.2.2-1
* New upstream release
* better-feedback-on-lib-error.diff, full_support.diff, renesas-sh.diff,
  s390javadetection.diff, sparc64.diff, test_run_permission.diff,
  z_cpudetection.diff removed (applied upstream)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3
 
 * Copyright (C) 2009 - DIGITEO - Clément DAVID
4
 
 *
5
 
 * This file must be used under the terms of the CeCILL.
6
 
 * This source file is licensed as described in the file COPYING, which
7
 
 * you should have received as part of this distribution.  The terms
8
 
 * are also available at
9
 
 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10
 
 *
11
 
 */
12
 
 
13
 
package org.scilab.modules.xcos.utils;
14
 
 
15
 
import java.util.HashMap;
16
 
import java.util.Map;
17
 
import java.util.regex.Matcher;
18
 
import java.util.regex.Pattern;
19
 
 
20
 
/**
21
 
 * Perform useful conversions between a style string and key/value based map.
22
 
 */
23
 
public final class StyleMap extends HashMap<String, String> {
24
 
        
25
 
        private static final Pattern P = Pattern.compile("(\\w+)(=(\\w+))?;?");
26
 
        private static final int KEY_GROUP = 1;
27
 
        private static final int VALUE_GROUP = 3;
28
 
        
29
 
        /**
30
 
         * Create a hashmap from a style string
31
 
         * @param style The string which contains key=value list 
32
 
         */
33
 
        public StyleMap(String style) {
34
 
                super();
35
 
                Matcher m = P.matcher(style);
36
 
 
37
 
                while (m.find()) {
38
 
                        String key = m.group(KEY_GROUP);
39
 
                        String value = m.group(VALUE_GROUP);
40
 
                        put(key, value);
41
 
                }
42
 
        }
43
 
        
44
 
        /**
45
 
         * Export to a key=value; string
46
 
         * @return formatted string
47
 
         */
48
 
        @Override
49
 
        public String toString() {
50
 
                StringBuilder str = new StringBuilder();
51
 
                String valueRef = null;
52
 
                
53
 
                for (Map.Entry<String, String> entry : entrySet()) {
54
 
                        str.append(entry.getKey());
55
 
                        
56
 
                        valueRef = entry.getValue();
57
 
                        if (valueRef != null) {
58
 
                                str.append("=");
59
 
                                str.append(valueRef);
60
 
                        }
61
 
                        str.append(";");
62
 
                }
63
 
                
64
 
                return str.toString();
65
 
        }
66
 
}