KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdnc > markup > attr > Decoder


1 /*
2  * $Id: Decoder.java,v 1.2 2004/09/02 00:50:35 aim Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.jdnc.markup.attr;
9
10 import java.awt.Color JavaDoc;
11 import java.awt.Font JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.regex.Pattern JavaDoc;
14
15 import javax.swing.SwingConstants JavaDoc;
16
17 /**
18  * @author Ramesh Gupta
19  */

20
21 public class Decoder {
22     private static HashMap JavaDoc constantsMap;
23     private static HashMap JavaDoc typesMap;
24     private static HashMap JavaDoc svgColorsMap;
25
26     static {
27         constantsMap = new HashMap JavaDoc();
28         constantsMap.put("left", new Integer JavaDoc(SwingConstants.LEFT));
29         constantsMap.put("right", new Integer JavaDoc(SwingConstants.RIGHT));
30         constantsMap.put("center", new Integer JavaDoc(SwingConstants.CENTER));
31         constantsMap.put("leading", new Integer JavaDoc(SwingConstants.LEADING));
32         constantsMap.put("before", new Integer JavaDoc(SwingConstants.LEADING));
33         constantsMap.put("trailing", new Integer JavaDoc(SwingConstants.TRAILING));
34         constantsMap.put("after", new Integer JavaDoc(SwingConstants.TRAILING));
35         constantsMap.put("top", new Integer JavaDoc(SwingConstants.TOP));
36         constantsMap.put("bottom", new Integer JavaDoc(SwingConstants.BOTTOM));
37         // ...
38

39
40         typesMap = new HashMap JavaDoc();
41         typesMap.put("boolean", java.lang.Boolean JavaDoc.class);
42         typesMap.put("date", java.util.Date JavaDoc.class);
43         typesMap.put("double", java.lang.Double JavaDoc.class);
44         typesMap.put("float", java.lang.Float JavaDoc.class);
45         typesMap.put("integer", java.lang.Integer JavaDoc.class);
46         typesMap.put("string", java.lang.String JavaDoc.class);
47         typesMap.put("href", org.jdesktop.swing.data.Link.class);
48         // ...
49

50         svgColorsMap = new HashMap JavaDoc(255);
51         svgColorsMap.put("aliceblue", new Color JavaDoc(240, 248, 255));
52         svgColorsMap.put("antiquewhite", new Color JavaDoc(250, 235, 215));
53         svgColorsMap.put("aqua", new Color JavaDoc( 0, 255, 255));
54         svgColorsMap.put("aquamarine", new Color JavaDoc(127, 255, 212));
55         svgColorsMap.put("azure", new Color JavaDoc(240, 255, 255));
56         svgColorsMap.put("beige", new Color JavaDoc(245, 245, 220));
57         svgColorsMap.put("bisque", new Color JavaDoc(255, 228, 196));
58         svgColorsMap.put("black", new Color JavaDoc( 0, 0, 0));
59         svgColorsMap.put("blanchedalmond", new Color JavaDoc(255, 235, 205));
60         svgColorsMap.put("blue", new Color JavaDoc( 0, 0, 255));
61         svgColorsMap.put("blueviolet", new Color JavaDoc(138, 43, 226));
62         svgColorsMap.put("brown", new Color JavaDoc(165, 42, 42));
63         svgColorsMap.put("burlywood", new Color JavaDoc(222, 184, 135));
64         svgColorsMap.put("cadetblue", new Color JavaDoc( 95, 158, 160));
65         svgColorsMap.put("chartreuse", new Color JavaDoc(127, 255, 0));
66         svgColorsMap.put("chocolate", new Color JavaDoc(210, 105, 30));
67         svgColorsMap.put("coral", new Color JavaDoc(255, 127, 80));
68         svgColorsMap.put("cornflowerblue", new Color JavaDoc(100, 149, 237));
69         svgColorsMap.put("cornsilk", new Color JavaDoc(255, 248, 220));
70         svgColorsMap.put("crimson", new Color JavaDoc(220, 20, 60));
71         svgColorsMap.put("cyan", new Color JavaDoc( 0, 255, 255));
72         svgColorsMap.put("darkblue", new Color JavaDoc( 0, 0, 139));
73         svgColorsMap.put("darkcyan", new Color JavaDoc( 0, 139, 139));
74         svgColorsMap.put("darkgoldenrod", new Color JavaDoc(184, 134, 11));
75         svgColorsMap.put("darkgray", new Color JavaDoc(169, 169, 169));
76         svgColorsMap.put("darkgreen", new Color JavaDoc( 0, 100, 0));
77         svgColorsMap.put("darkgrey", new Color JavaDoc(169, 169, 169));
78         svgColorsMap.put("darkkhaki", new Color JavaDoc(189, 183, 107));
79         svgColorsMap.put("darkmagenta", new Color JavaDoc(139, 0, 139));
80         svgColorsMap.put("darkolivegreen", new Color JavaDoc( 85, 107, 47));
81         svgColorsMap.put("darkorange", new Color JavaDoc(255, 140, 0));
82         svgColorsMap.put("darkorchid", new Color JavaDoc(153, 50, 204));
83         svgColorsMap.put("darkred", new Color JavaDoc(139, 0, 0));
84         svgColorsMap.put("darksalmon", new Color JavaDoc(233, 150, 122));
85         svgColorsMap.put("darkseagreen", new Color JavaDoc(143, 188, 143));
86         svgColorsMap.put("darkslateblue", new Color JavaDoc( 72, 61, 139));
87         svgColorsMap.put("darkslategray", new Color JavaDoc( 47, 79, 79));
88         svgColorsMap.put("darkslategrey", new Color JavaDoc( 47, 79, 79));
89         svgColorsMap.put("darkturquoise", new Color JavaDoc( 0, 206, 209));
90         svgColorsMap.put("darkviolet", new Color JavaDoc(148, 0, 211));
91         svgColorsMap.put("deeppink", new Color JavaDoc(255, 20, 147));
92         svgColorsMap.put("deepskyblue", new Color JavaDoc( 0, 191, 255));
93         svgColorsMap.put("dimgray", new Color JavaDoc(105, 105, 105));
94         svgColorsMap.put("dimgrey", new Color JavaDoc(105, 105, 105));
95         svgColorsMap.put("dodgerblue", new Color JavaDoc( 30, 144, 255));
96         svgColorsMap.put("firebrick", new Color JavaDoc(178, 34, 34));
97         svgColorsMap.put("floralwhite", new Color JavaDoc(255, 250, 240));
98         svgColorsMap.put("forestgreen", new Color JavaDoc( 34, 139, 34));
99         svgColorsMap.put("fuchsia", new Color JavaDoc(255, 0, 255));
100         svgColorsMap.put("gainsboro", new Color JavaDoc(220, 220, 220));
101         svgColorsMap.put("ghostwhite", new Color JavaDoc(248, 248, 255));
102         svgColorsMap.put("gold", new Color JavaDoc(255, 215, 0));
103         svgColorsMap.put("goldenrod", new Color JavaDoc(218, 165, 32));
104         svgColorsMap.put("gray", new Color JavaDoc(128, 128, 128));
105         svgColorsMap.put("green", new Color JavaDoc( 0, 128, 0));
106         svgColorsMap.put("greenyellow", new Color JavaDoc(173, 255, 47));
107         svgColorsMap.put("grey", new Color JavaDoc(128, 128, 128));
108         svgColorsMap.put("honeydew", new Color JavaDoc(240, 255, 240));
109         svgColorsMap.put("hotpink", new Color JavaDoc(255, 105, 180));
110         svgColorsMap.put("indianred", new Color JavaDoc(205, 92, 92));
111         svgColorsMap.put("indigo", new Color JavaDoc( 75, 0, 130));
112         svgColorsMap.put("ivory", new Color JavaDoc(255, 255, 240));
113         svgColorsMap.put("khaki", new Color JavaDoc(240, 230, 140));
114         svgColorsMap.put("lavender", new Color JavaDoc(230, 230, 250));
115         svgColorsMap.put("lavenderblush", new Color JavaDoc(255, 240, 245));
116         svgColorsMap.put("lawngreen", new Color JavaDoc(124, 252, 0));
117         svgColorsMap.put("lemonchiffon", new Color JavaDoc(255, 250, 205));
118         svgColorsMap.put("lightblue", new Color JavaDoc(173, 216, 230));
119         svgColorsMap.put("lightcoral", new Color JavaDoc(240, 128, 128));
120         svgColorsMap.put("lightcyan", new Color JavaDoc(224, 255, 255));
121         svgColorsMap.put("lightgoldenrodyellow", new Color JavaDoc(250, 250, 210));
122         svgColorsMap.put("lightgray", new Color JavaDoc(211, 211, 211));
123         svgColorsMap.put("lightgreen", new Color JavaDoc(144, 238, 144));
124         svgColorsMap.put("lightgrey", new Color JavaDoc(211, 211, 211));
125         svgColorsMap.put("lightpink", new Color JavaDoc(255, 182, 193));
126         svgColorsMap.put("lightsalmon", new Color JavaDoc(255, 160, 122));
127         svgColorsMap.put("lightseagreen", new Color JavaDoc( 32, 178, 170));
128         svgColorsMap.put("lightskyblue", new Color JavaDoc(135, 206, 250));
129         svgColorsMap.put("lightslategray", new Color JavaDoc(119, 136, 153));
130         svgColorsMap.put("lightslategrey", new Color JavaDoc(119, 136, 153));
131         svgColorsMap.put("lightsteelblue", new Color JavaDoc(176, 196, 222));
132         svgColorsMap.put("lightyellow", new Color JavaDoc(255, 255, 224));
133         svgColorsMap.put("lime", new Color JavaDoc( 0, 255, 0));
134         svgColorsMap.put("limegreen", new Color JavaDoc( 50, 205, 50));
135         svgColorsMap.put("linen", new Color JavaDoc(250, 240, 230));
136         svgColorsMap.put("magenta", new Color JavaDoc(255, 0, 255));
137         svgColorsMap.put("maroon", new Color JavaDoc(128, 0, 0));
138         svgColorsMap.put("mediumaquamarine", new Color JavaDoc(102, 205, 170));
139         svgColorsMap.put("mediumblue", new Color JavaDoc( 0, 0, 205));
140         svgColorsMap.put("mediumorchid", new Color JavaDoc(186, 85, 211));
141         svgColorsMap.put("mediumpurple", new Color JavaDoc(147, 112, 219));
142         svgColorsMap.put("mediumseagreen", new Color JavaDoc( 60, 179, 113));
143         svgColorsMap.put("mediumslateblue", new Color JavaDoc(123, 104, 238));
144         svgColorsMap.put("mediumspringgreen", new Color JavaDoc( 0, 250, 154));
145         svgColorsMap.put("mediumturquoise", new Color JavaDoc( 72, 209, 204));
146         svgColorsMap.put("mediumvioletred", new Color JavaDoc(199, 21, 133));
147         svgColorsMap.put("midnightblue", new Color JavaDoc( 25, 25, 112));
148         svgColorsMap.put("mintcream", new Color JavaDoc(245, 255, 250));
149         svgColorsMap.put("mistyrose", new Color JavaDoc(255, 228, 225));
150         svgColorsMap.put("moccasin", new Color JavaDoc(255, 228, 181));
151         svgColorsMap.put("navajowhite", new Color JavaDoc(255, 222, 173));
152         svgColorsMap.put("navy", new Color JavaDoc( 0, 0, 128));
153         svgColorsMap.put("oldlace", new Color JavaDoc(253, 245, 230));
154         svgColorsMap.put("olive", new Color JavaDoc(128, 128, 0));
155         svgColorsMap.put("olivedrab", new Color JavaDoc(107, 142, 35));
156         svgColorsMap.put("orange", new Color JavaDoc(255, 165, 0));
157         svgColorsMap.put("orangered", new Color JavaDoc(255, 69, 0));
158         svgColorsMap.put("orchid", new Color JavaDoc(218, 112, 214));
159         svgColorsMap.put("palegoldenrod", new Color JavaDoc(238, 232, 170));
160         svgColorsMap.put("palegreen", new Color JavaDoc(152, 251, 152));
161         svgColorsMap.put("paleturquoise", new Color JavaDoc(175, 238, 238));
162         svgColorsMap.put("palevioletred", new Color JavaDoc(219, 112, 147));
163         svgColorsMap.put("papayawhip", new Color JavaDoc(255, 239, 213));
164         svgColorsMap.put("peachpuff", new Color JavaDoc(255, 218, 185));
165         svgColorsMap.put("peru", new Color JavaDoc(205, 133, 63));
166         svgColorsMap.put("pink", new Color JavaDoc(255, 192, 203));
167         svgColorsMap.put("plum", new Color JavaDoc(221, 160, 221));
168         svgColorsMap.put("powderblue", new Color JavaDoc(176, 224, 230));
169         svgColorsMap.put("purple", new Color JavaDoc(128, 0, 128));
170         svgColorsMap.put("red", new Color JavaDoc(255, 0, 0));
171         svgColorsMap.put("rosybrown", new Color JavaDoc(188, 143, 143));
172         svgColorsMap.put("royalblue", new Color JavaDoc( 65, 105, 225));
173         svgColorsMap.put("saddlebrown", new Color JavaDoc(139, 69, 19));
174         svgColorsMap.put("salmon", new Color JavaDoc(250, 128, 114));
175         svgColorsMap.put("sandybrown", new Color JavaDoc(244, 164, 96));
176         svgColorsMap.put("seagreen", new Color JavaDoc( 46, 139, 87));
177         svgColorsMap.put("seashell", new Color JavaDoc(255, 245, 238));
178         svgColorsMap.put("sienna", new Color JavaDoc(160, 82, 45));
179         svgColorsMap.put("silver", new Color JavaDoc(192, 192, 192));
180         svgColorsMap.put("skyblue", new Color JavaDoc(135, 206, 235));
181         svgColorsMap.put("slateblue", new Color JavaDoc(106, 90, 205));
182         svgColorsMap.put("slategray", new Color JavaDoc(112, 128, 144));
183         svgColorsMap.put("slategrey", new Color JavaDoc(112, 128, 144));
184         svgColorsMap.put("snow", new Color JavaDoc(255, 250, 250));
185         svgColorsMap.put("springgreen", new Color JavaDoc( 0, 255, 127));
186         svgColorsMap.put("steelblue", new Color JavaDoc( 70, 130, 180));
187         svgColorsMap.put("tan", new Color JavaDoc(210, 180, 140));
188         svgColorsMap.put("teal", new Color JavaDoc( 0, 128, 128));
189         svgColorsMap.put("thistle", new Color JavaDoc(216, 191, 216));
190         svgColorsMap.put("tomato", new Color JavaDoc(255, 99, 71));
191         svgColorsMap.put("turquoise", new Color JavaDoc( 64, 224, 208));
192         svgColorsMap.put("violet", new Color JavaDoc(238, 130, 238));
193         svgColorsMap.put("wheat", new Color JavaDoc(245, 222, 179));
194         svgColorsMap.put("white", new Color JavaDoc(255, 255, 255));
195         svgColorsMap.put("whitesmoke", new Color JavaDoc(245, 245, 245));
196         svgColorsMap.put("yellow", new Color JavaDoc(255, 255, 0));
197         svgColorsMap.put("yellowgreen", new Color JavaDoc(154, 205, 50));
198     }
199
200     public static Color JavaDoc decodeColor(String JavaDoc value) {
201         if (value.length() > 0) {
202             try {
203                 if (value.startsWith("#")) {
204                     if (value.length() == 9) {
205                         return new Color JavaDoc((
206                             Integer.parseInt(value.substring(1, 3), 16) << 24) +
207                             Integer.parseInt(value.substring(3), 16), true);
208                     }
209                     else {
210                         return new Color JavaDoc(
211                             Integer.parseInt(value.substring(1), 16));
212                     }
213                 }
214                 else {
215                     Color JavaDoc color = (Color JavaDoc) svgColorsMap.get(value);
216                     // We do not yet support system color names like
217
// activeBorder, activeCaption, ...
218
if (color == null) {
219                         color = new Color JavaDoc(127, 127, 127);
220                         // log the problem
221
}
222                     return color;
223                 }
224             }
225             catch (Exception JavaDoc ex) {
226                 System.out.println("Bad color: " + ex);
227             }
228         }
229         return null;
230     }
231
232     public static int decodeFontStyle(String JavaDoc value) {
233         if (value.equals("plain")) {
234             return Font.PLAIN;
235         }
236         if (value.equals("bold")) {
237             return Font.BOLD;
238         }
239         if (value.equals("italic")) {
240             return Font.ITALIC;
241         }
242         if (value.equals("bold-italic") || value.equals("italic-bold")) {
243             return Font.ITALIC | Font.BOLD;
244         }
245         return -1;
246     }
247
248     public static int decodeConstant(String JavaDoc value) {
249          Integer JavaDoc intObject = (Integer JavaDoc)constantsMap.get(value);
250          if (intObject == null) {
251              //remind(aim): should be replaced with suppressable warning
252
System.out.println("Illegal constant identifier: " + value);
253              return -1;
254          }
255          return intObject.intValue();
256     }
257
258     public static Class JavaDoc decodeType(String JavaDoc value) {
259         Class JavaDoc klass = (Class JavaDoc)typesMap.get(value);
260         if (klass == null) {
261             try {
262                 klass = Class.forName(value);
263             } catch (ClassNotFoundException JavaDoc e) {
264                 System.out.println("Could not convert \"" + value + "\" to a java type or class");
265             }
266         }
267         return klass;
268     }
269
270     public static int decodePatternMatchFlags(String JavaDoc value) {
271         int matchFlags = 0;
272         if (value.length() > 0) {
273             String JavaDoc[] flags = value.split("\\s");
274             for (int i = 0; i < flags.length; i++) {
275                 String JavaDoc flag = flags[i];
276                 if (flag.equals(CASE_INSENSITIVE)) {
277                     matchFlags |= Pattern.CASE_INSENSITIVE;
278                 }
279                 else if (flag.equals(MULTILINE)) {
280                     matchFlags |= Pattern.MULTILINE;
281                 }
282                 else if (flag.equals(DOT_ALL)) {
283                     matchFlags |= Pattern.DOTALL;
284                 }
285                 else if (flag.equals(UNICODE_CASE)) {
286                     matchFlags |= Pattern.UNICODE_CASE;
287                 }
288                 else if (flag.equals(CANON_EQ)) {
289                     matchFlags |= Pattern.CANON_EQ;
290                 }
291             }
292         }
293         return matchFlags;
294     }
295
296     private final static String JavaDoc CASE_INSENSITIVE ="caseInsensitive";
297     private final static String JavaDoc MULTILINE ="multiline";
298     private final static String JavaDoc DOT_ALL ="dotAll";
299     private final static String JavaDoc UNICODE_CASE ="unicodeCase";
300     private final static String JavaDoc CANON_EQ ="canonEq";
301 }
302
Popular Tags