KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > gtk > PropertyParser


1 /*
2  * @(#)PropertyParser.java 1.12 05/12/22
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.java.swing.plaf.gtk;
9
10 import java.util.HashMap JavaDoc;
11 import java.awt.Color JavaDoc;
12 import java.awt.Insets JavaDoc;
13 import java.io.*;
14
15 /**
16  * @author Shannon Hickey
17  * @version 1.12 12/22/05
18  */

19 abstract class PropertyParser {
20
21     // Whether or not simple strings passed from GTKParser need to be escaped
22
// for rescanning/parsing by this parser. The default is false since most
23
// property parsers either get complex values to parse, or simple string
24
// values.
25
public boolean needSimpleStringsEscaped() {
26         return false;
27     }
28
29     private static final PropertyParser INSETS_PARSER = new PropertyParser() {
30         public Object JavaDoc parse(String JavaDoc source) {
31             GTKScanner scanner = new GTKScanner();
32             scanner.scanReader(new StringReader(source), source);
33
34             try {
35                 int left;
36                 int right;
37                 int top;
38                 int bottom;
39
40                 if (scanner.getToken() != GTKScanner.TOKEN_LEFT_CURLY) {
41                     return null;
42                 }
43                 
44                 if (scanner.getToken() != GTKScanner.TOKEN_INT) {
45                     return null;
46                 }
47                 
48                 left = (int)scanner.currValue.longVal;
49                 
50                 if (scanner.getToken() != GTKScanner.TOKEN_COMMA) {
51                     return null;
52                 }
53                 
54                 if (scanner.getToken() != GTKScanner.TOKEN_INT) {
55                     return null;
56                 }
57                 
58                 right = (int)scanner.currValue.longVal;
59                 
60                 if (scanner.getToken() != GTKScanner.TOKEN_COMMA) {
61                     return null;
62                 }
63                 
64                 if (scanner.getToken() != GTKScanner.TOKEN_INT) {
65                     return null;
66                 }
67                 
68                 top = (int)scanner.currValue.longVal;
69                 
70                 if (scanner.getToken() != GTKScanner.TOKEN_COMMA) {
71                     return null;
72                 }
73                 
74                 if (scanner.getToken() != GTKScanner.TOKEN_INT) {
75                     return null;
76                 }
77                 
78                 bottom = (int)scanner.currValue.longVal;
79                 
80                 if (scanner.getToken() != GTKScanner.TOKEN_RIGHT_CURLY) {
81                     return null;
82                 }
83                 
84                 return new Insets JavaDoc(top, left, bottom, right);
85             } catch (IOException ioe) {
86             } finally {
87                 scanner.clearScanner();
88             }
89
90             return null;
91         }
92     };
93
94     private static final PropertyParser BOOLEAN_PARSER = new PropertyParser() {
95         public Object JavaDoc parse(String JavaDoc source) {
96             source = source.trim();
97
98             if (source.equals("TRUE")) {
99                 return Boolean.TRUE;
100             } else if (source.equals("FALSE")) {
101                 return Boolean.FALSE;
102             }
103             
104             return null;
105         }
106     };
107
108     private static final PropertyParser SHADOW_PARSER = new PropertyParser() {
109         public Object JavaDoc parse(String JavaDoc source) {
110             source = source.trim();
111             
112             if (source.equals("SHADOW_IN")) {
113                 return new Integer JavaDoc(GTKConstants.SHADOW_IN);
114             } else if (source.equals("SHADOW_OUT")) {
115                 return new Integer JavaDoc(GTKConstants.SHADOW_OUT);
116             } else if (source.equals("SHADOW_ETCHED_IN")) {
117                 return new Integer JavaDoc(GTKConstants.SHADOW_ETCHED_IN);
118             } else if (source.equals("SHADOW_ETCHED_OUT")) {
119                 return new Integer JavaDoc(GTKConstants.SHADOW_ETCHED_OUT);
120             } else if (source.equals("SHADOW_NONE")) {
121                 return new Integer JavaDoc(GTKConstants.SHADOW_NONE);
122             }
123             
124             return null;
125         }
126     };
127
128     private static final PropertyParser FOCUS_LINE_PARSER = new PropertyParser() {
129         public Object JavaDoc parse(String JavaDoc source) {
130             int len = source.length();
131             int[] retVal = new int[len];
132
133             for (int i = 0; i < len; i++) {
134                 retVal[i] = (int)source.charAt(i);
135             }
136
137             if (retVal.length == 0 ||
138                 retVal.length == 1 && retVal[0] == 0) {
139                 // solid line
140
retVal = new int[] { Integer.MAX_VALUE };
141
142             } else if (retVal.length == 1) {
143                 // "\x" actually means "\x\x"
144
int value = retVal[0];
145                 retVal = new int[] { value, value };
146             }
147             return retVal;
148         }
149     };
150
151     private static final PropertyParser COLOR_PARSER = new PropertyParser() {
152         // We may receive simple strings such as "red" or "#CCCCCC". These need
153
// to be escaped by GTKParser first so we can rescan/parse them here.
154
public boolean needSimpleStringsEscaped() {
155             return true;
156         }
157
158         public Object JavaDoc parse(String JavaDoc source) {
159             GTKScanner scanner = new GTKScanner();
160             scanner.scanReader(new StringReader(source), source);
161
162             try {
163                 Color JavaDoc[] ret = new Color JavaDoc[1];
164                 int token = GTKParser.parseColor(scanner, ret, 0);
165                 if (token == GTKScanner.TOKEN_NONE) {
166                     return ret[0];
167                 }
168             } catch (IOException ioe) {
169             } finally {
170                 scanner.clearScanner();
171             }
172
173             return null;
174         }
175     };
176     private static final HashMap JavaDoc PARSERS = new HashMap JavaDoc();
177     
178     static {
179         PARSERS.put("default-border", INSETS_PARSER);
180         PARSERS.put("interior-focus", BOOLEAN_PARSER);
181         PARSERS.put("shadow-type", SHADOW_PARSER);
182         PARSERS.put("focus-line-pattern", FOCUS_LINE_PARSER);
183         PARSERS.put("cursor-color", COLOR_PARSER);
184     }
185
186     public static PropertyParser getParserFor(String JavaDoc type) {
187         return (PropertyParser)PARSERS.get(type);
188     }
189
190     public abstract Object JavaDoc parse(String JavaDoc source);
191
192 }
193
Popular Tags