1 7 8 package org.jdesktop.jdnc.markup.attr; 9 10 import java.awt.Color ; 11 import java.awt.Font ; 12 import java.util.HashMap ; 13 import java.util.regex.Pattern ; 14 15 import javax.swing.SwingConstants ; 16 17 20 21 public class Decoder { 22 private static HashMap constantsMap; 23 private static HashMap typesMap; 24 private static HashMap svgColorsMap; 25 26 static { 27 constantsMap = new HashMap (); 28 constantsMap.put("left", new Integer (SwingConstants.LEFT)); 29 constantsMap.put("right", new Integer (SwingConstants.RIGHT)); 30 constantsMap.put("center", new Integer (SwingConstants.CENTER)); 31 constantsMap.put("leading", new Integer (SwingConstants.LEADING)); 32 constantsMap.put("before", new Integer (SwingConstants.LEADING)); 33 constantsMap.put("trailing", new Integer (SwingConstants.TRAILING)); 34 constantsMap.put("after", new Integer (SwingConstants.TRAILING)); 35 constantsMap.put("top", new Integer (SwingConstants.TOP)); 36 constantsMap.put("bottom", new Integer (SwingConstants.BOTTOM)); 37 39 40 typesMap = new HashMap (); 41 typesMap.put("boolean", java.lang.Boolean .class); 42 typesMap.put("date", java.util.Date .class); 43 typesMap.put("double", java.lang.Double .class); 44 typesMap.put("float", java.lang.Float .class); 45 typesMap.put("integer", java.lang.Integer .class); 46 typesMap.put("string", java.lang.String .class); 47 typesMap.put("href", org.jdesktop.swing.data.Link.class); 48 50 svgColorsMap = new HashMap (255); 51 svgColorsMap.put("aliceblue", new Color (240, 248, 255)); 52 svgColorsMap.put("antiquewhite", new Color (250, 235, 215)); 53 svgColorsMap.put("aqua", new Color ( 0, 255, 255)); 54 svgColorsMap.put("aquamarine", new Color (127, 255, 212)); 55 svgColorsMap.put("azure", new Color (240, 255, 255)); 56 svgColorsMap.put("beige", new Color (245, 245, 220)); 57 svgColorsMap.put("bisque", new Color (255, 228, 196)); 58 svgColorsMap.put("black", new Color ( 0, 0, 0)); 59 svgColorsMap.put("blanchedalmond", new Color (255, 235, 205)); 60 svgColorsMap.put("blue", new Color ( 0, 0, 255)); 61 svgColorsMap.put("blueviolet", new Color (138, 43, 226)); 62 svgColorsMap.put("brown", new Color (165, 42, 42)); 63 svgColorsMap.put("burlywood", new Color (222, 184, 135)); 64 svgColorsMap.put("cadetblue", new Color ( 95, 158, 160)); 65 svgColorsMap.put("chartreuse", new Color (127, 255, 0)); 66 svgColorsMap.put("chocolate", new Color (210, 105, 30)); 67 svgColorsMap.put("coral", new Color (255, 127, 80)); 68 svgColorsMap.put("cornflowerblue", new Color (100, 149, 237)); 69 svgColorsMap.put("cornsilk", new Color (255, 248, 220)); 70 svgColorsMap.put("crimson", new Color (220, 20, 60)); 71 svgColorsMap.put("cyan", new Color ( 0, 255, 255)); 72 svgColorsMap.put("darkblue", new Color ( 0, 0, 139)); 73 svgColorsMap.put("darkcyan", new Color ( 0, 139, 139)); 74 svgColorsMap.put("darkgoldenrod", new Color (184, 134, 11)); 75 svgColorsMap.put("darkgray", new Color (169, 169, 169)); 76 svgColorsMap.put("darkgreen", new Color ( 0, 100, 0)); 77 svgColorsMap.put("darkgrey", new Color (169, 169, 169)); 78 svgColorsMap.put("darkkhaki", new Color (189, 183, 107)); 79 svgColorsMap.put("darkmagenta", new Color (139, 0, 139)); 80 svgColorsMap.put("darkolivegreen", new Color ( 85, 107, 47)); 81 svgColorsMap.put("darkorange", new Color (255, 140, 0)); 82 svgColorsMap.put("darkorchid", new Color (153, 50, 204)); 83 svgColorsMap.put("darkred", new Color (139, 0, 0)); 84 svgColorsMap.put("darksalmon", new Color (233, 150, 122)); 85 svgColorsMap.put("darkseagreen", new Color (143, 188, 143)); 86 svgColorsMap.put("darkslateblue", new Color ( 72, 61, 139)); 87 svgColorsMap.put("darkslategray", new Color ( 47, 79, 79)); 88 svgColorsMap.put("darkslategrey", new Color ( 47, 79, 79)); 89 svgColorsMap.put("darkturquoise", new Color ( 0, 206, 209)); 90 svgColorsMap.put("darkviolet", new Color (148, 0, 211)); 91 svgColorsMap.put("deeppink", new Color (255, 20, 147)); 92 svgColorsMap.put("deepskyblue", new Color ( 0, 191, 255)); 93 svgColorsMap.put("dimgray", new Color (105, 105, 105)); 94 svgColorsMap.put("dimgrey", new Color (105, 105, 105)); 95 svgColorsMap.put("dodgerblue", new Color ( 30, 144, 255)); 96 svgColorsMap.put("firebrick", new Color (178, 34, 34)); 97 svgColorsMap.put("floralwhite", new Color (255, 250, 240)); 98 svgColorsMap.put("forestgreen", new Color ( 34, 139, 34)); 99 svgColorsMap.put("fuchsia", new Color (255, 0, 255)); 100 svgColorsMap.put("gainsboro", new Color (220, 220, 220)); 101 svgColorsMap.put("ghostwhite", new Color (248, 248, 255)); 102 svgColorsMap.put("gold", new Color (255, 215, 0)); 103 svgColorsMap.put("goldenrod", new Color (218, 165, 32)); 104 svgColorsMap.put("gray", new Color (128, 128, 128)); 105 svgColorsMap.put("green", new Color ( 0, 128, 0)); 106 svgColorsMap.put("greenyellow", new Color (173, 255, 47)); 107 svgColorsMap.put("grey", new Color (128, 128, 128)); 108 svgColorsMap.put("honeydew", new Color (240, 255, 240)); 109 svgColorsMap.put("hotpink", new Color (255, 105, 180)); 110 svgColorsMap.put("indianred", new Color (205, 92, 92)); 111 svgColorsMap.put("indigo", new Color ( 75, 0, 130)); 112 svgColorsMap.put("ivory", new Color (255, 255, 240)); 113 svgColorsMap.put("khaki", new Color (240, 230, 140)); 114 svgColorsMap.put("lavender", new Color (230, 230, 250)); 115 svgColorsMap.put("lavenderblush", new Color (255, 240, 245)); 116 svgColorsMap.put("lawngreen", new Color (124, 252, 0)); 117 svgColorsMap.put("lemonchiffon", new Color (255, 250, 205)); 118 svgColorsMap.put("lightblue", new Color (173, 216, 230)); 119 svgColorsMap.put("lightcoral", new Color (240, 128, 128)); 120 svgColorsMap.put("lightcyan", new Color (224, 255, 255)); 121 svgColorsMap.put("lightgoldenrodyellow", new Color (250, 250, 210)); 122 svgColorsMap.put("lightgray", new Color (211, 211, 211)); 123 svgColorsMap.put("lightgreen", new Color (144, 238, 144)); 124 svgColorsMap.put("lightgrey", new Color (211, 211, 211)); 125 svgColorsMap.put("lightpink", new Color (255, 182, 193)); 126 svgColorsMap.put("lightsalmon", new Color (255, 160, 122)); 127 svgColorsMap.put("lightseagreen", new Color ( 32, 178, 170)); 128 svgColorsMap.put("lightskyblue", new Color (135, 206, 250)); 129 svgColorsMap.put("lightslategray", new Color (119, 136, 153)); 130 svgColorsMap.put("lightslategrey", new Color (119, 136, 153)); 131 svgColorsMap.put("lightsteelblue", new Color (176, 196, 222)); 132 svgColorsMap.put("lightyellow", new Color (255, 255, 224)); 133 svgColorsMap.put("lime", new Color ( 0, 255, 0)); 134 svgColorsMap.put("limegreen", new Color ( 50, 205, 50)); 135 svgColorsMap.put("linen", new Color (250, 240, 230)); 136 svgColorsMap.put("magenta", new Color (255, 0, 255)); 137 svgColorsMap.put("maroon", new Color (128, 0, 0)); 138 svgColorsMap.put("mediumaquamarine", new Color (102, 205, 170)); 139 svgColorsMap.put("mediumblue", new Color ( 0, 0, 205)); 140 svgColorsMap.put("mediumorchid", new Color (186, 85, 211)); 141 svgColorsMap.put("mediumpurple", new Color (147, 112, 219)); 142 svgColorsMap.put("mediumseagreen", new Color ( 60, 179, 113)); 143 svgColorsMap.put("mediumslateblue", new Color (123, 104, 238)); 144 svgColorsMap.put("mediumspringgreen", new Color ( 0, 250, 154)); 145 svgColorsMap.put("mediumturquoise", new Color ( 72, 209, 204)); 146 svgColorsMap.put("mediumvioletred", new Color (199, 21, 133)); 147 svgColorsMap.put("midnightblue", new Color ( 25, 25, 112)); 148 svgColorsMap.put("mintcream", new Color (245, 255, 250)); 149 svgColorsMap.put("mistyrose", new Color (255, 228, 225)); 150 svgColorsMap.put("moccasin", new Color (255, 228, 181)); 151 svgColorsMap.put("navajowhite", new Color (255, 222, 173)); 152 svgColorsMap.put("navy", new Color ( 0, 0, 128)); 153 svgColorsMap.put("oldlace", new Color (253, 245, 230)); 154 svgColorsMap.put("olive", new Color (128, 128, 0)); 155 svgColorsMap.put("olivedrab", new Color (107, 142, 35)); 156 svgColorsMap.put("orange", new Color (255, 165, 0)); 157 svgColorsMap.put("orangered", new Color (255, 69, 0)); 158 svgColorsMap.put("orchid", new Color (218, 112, 214)); 159 svgColorsMap.put("palegoldenrod", new Color (238, 232, 170)); 160 svgColorsMap.put("palegreen", new Color (152, 251, 152)); 161 svgColorsMap.put("paleturquoise", new Color (175, 238, 238)); 162 svgColorsMap.put("palevioletred", new Color (219, 112, 147)); 163 svgColorsMap.put("papayawhip", new Color (255, 239, 213)); 164 svgColorsMap.put("peachpuff", new Color (255, 218, 185)); 165 svgColorsMap.put("peru", new Color (205, 133, 63)); 166 svgColorsMap.put("pink", new Color (255, 192, 203)); 167 svgColorsMap.put("plum", new Color (221, 160, 221)); 168 svgColorsMap.put("powderblue", new Color (176, 224, 230)); 169 svgColorsMap.put("purple", new Color (128, 0, 128)); 170 svgColorsMap.put("red", new Color (255, 0, 0)); 171 svgColorsMap.put("rosybrown", new Color (188, 143, 143)); 172 svgColorsMap.put("royalblue", new Color ( 65, 105, 225)); 173 svgColorsMap.put("saddlebrown", new Color (139, 69, 19)); 174 svgColorsMap.put("salmon", new Color (250, 128, 114)); 175 svgColorsMap.put("sandybrown", new Color (244, 164, 96)); 176 svgColorsMap.put("seagreen", new Color ( 46, 139, 87)); 177 svgColorsMap.put("seashell", new Color (255, 245, 238)); 178 svgColorsMap.put("sienna", new Color (160, 82, 45)); 179 svgColorsMap.put("silver", new Color (192, 192, 192)); 180 svgColorsMap.put("skyblue", new Color (135, 206, 235)); 181 svgColorsMap.put("slateblue", new Color (106, 90, 205)); 182 svgColorsMap.put("slategray", new Color (112, 128, 144)); 183 svgColorsMap.put("slategrey", new Color (112, 128, 144)); 184 svgColorsMap.put("snow", new Color (255, 250, 250)); 185 svgColorsMap.put("springgreen", new Color ( 0, 255, 127)); 186 svgColorsMap.put("steelblue", new Color ( 70, 130, 180)); 187 svgColorsMap.put("tan", new Color (210, 180, 140)); 188 svgColorsMap.put("teal", new Color ( 0, 128, 128)); 189 svgColorsMap.put("thistle", new Color (216, 191, 216)); 190 svgColorsMap.put("tomato", new Color (255, 99, 71)); 191 svgColorsMap.put("turquoise", new Color ( 64, 224, 208)); 192 svgColorsMap.put("violet", new Color (238, 130, 238)); 193 svgColorsMap.put("wheat", new Color (245, 222, 179)); 194 svgColorsMap.put("white", new Color (255, 255, 255)); 195 svgColorsMap.put("whitesmoke", new Color (245, 245, 245)); 196 svgColorsMap.put("yellow", new Color (255, 255, 0)); 197 svgColorsMap.put("yellowgreen", new Color (154, 205, 50)); 198 } 199 200 public static Color decodeColor(String value) { 201 if (value.length() > 0) { 202 try { 203 if (value.startsWith("#")) { 204 if (value.length() == 9) { 205 return new Color (( 206 Integer.parseInt(value.substring(1, 3), 16) << 24) + 207 Integer.parseInt(value.substring(3), 16), true); 208 } 209 else { 210 return new Color ( 211 Integer.parseInt(value.substring(1), 16)); 212 } 213 } 214 else { 215 Color color = (Color ) svgColorsMap.get(value); 216 if (color == null) { 219 color = new Color (127, 127, 127); 220 } 222 return color; 223 } 224 } 225 catch (Exception ex) { 226 System.out.println("Bad color: " + ex); 227 } 228 } 229 return null; 230 } 231 232 public static int decodeFontStyle(String 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 value) { 249 Integer intObject = (Integer )constantsMap.get(value); 250 if (intObject == null) { 251 System.out.println("Illegal constant identifier: " + value); 253 return -1; 254 } 255 return intObject.intValue(); 256 } 257 258 public static Class decodeType(String value) { 259 Class klass = (Class )typesMap.get(value); 260 if (klass == null) { 261 try { 262 klass = Class.forName(value); 263 } catch (ClassNotFoundException 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 value) { 271 int matchFlags = 0; 272 if (value.length() > 0) { 273 String [] flags = value.split("\\s"); 274 for (int i = 0; i < flags.length; i++) { 275 String 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 CASE_INSENSITIVE ="caseInsensitive"; 297 private final static String MULTILINE ="multiline"; 298 private final static String DOT_ALL ="dotAll"; 299 private final static String UNICODE_CASE ="unicodeCase"; 300 private final static String CANON_EQ ="canonEq"; 301 } 302 | Popular Tags |