KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > properties > syntax > PropertiesSyntax


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.properties.syntax;
21
22 import org.netbeans.editor.Syntax;
23 import org.netbeans.editor.TokenID;
24
25 /**
26 * Syntax analyzes for properties files.
27 * Tokens and internal states are given below.
28 *
29 * @author Petr Jiricka, Miloslav Metelka
30 * @version 1.00
31 */

32
33 public class PropertiesSyntax extends Syntax {
34
35     // Internal states
36
private static final int ISI_LINE_COMMENT = 2; // inside line comment
37
private static final int ISI_KEY = 3; // inside a key
38
private static final int ISI_KEY_A_BSLASH = 4; // inside a key after backslash
39
private static final int ISI_EQUAL = 5; // inside an equal sign
40
private static final int ISI_EQUAL2 = 6; // after key but not yet value or equal Note: EQUAL2 was revised
41
private static final int ISI_VALUE = 7; // inside a value
42
private static final int ISI_VALUE_A_BSLASH = 8; // inside a value after backslash
43
private static final int ISI_VALUE_AT_NL = 9; // inside a value at new line
44
private static final int ISI_EQUAL_AT_NL = 10; // between key and not yet value at new line
45

46
47     public PropertiesSyntax() {
48         tokenContextPath = PropertiesTokenContext.contextPath;
49     }
50
51     protected TokenID parseToken() {
52         char actChar;
53
54         while(offset < stopOffset) {
55             actChar = buffer[offset];
56
57             switch (state) {
58             case INIT:
59                 switch (actChar) {
60                 case '\n':
61                     offset++;
62                     return PropertiesTokenContext.EOL;
63                 case '\t':
64                 case '\f':
65                 case ' ':
66                     offset++;
67                     return PropertiesTokenContext.TEXT;
68                 case '#':
69                 case '!':
70                     state = ISI_LINE_COMMENT;
71                     break;
72                 case '=': // in case the key is an empty string (first non-white is '=' or ':')
73
case ':':
74                     state = ISI_EQUAL;
75                     return PropertiesTokenContext.TEXT;
76                 case '\\': // when key begins with escape
77
state = ISI_KEY_A_BSLASH;
78                     break;
79                 default:
80                     state = ISI_KEY;
81                     break;
82                 }
83                 break; // end state INIT
84

85             case ISI_LINE_COMMENT:
86                 switch (actChar) {
87                 case '\n':
88                     state = INIT;
89                     return PropertiesTokenContext.LINE_COMMENT;
90                 }
91                 break; // end state ISI_LINE_COMMENT
92

93             case ISI_KEY:
94                 switch (actChar) {
95                 case '\n':
96                     state = INIT;
97                     return PropertiesTokenContext.KEY;
98                 case '\\':
99                     state = ISI_KEY_A_BSLASH;
100                     break;
101                 case '=':
102                 case ':':
103                 case ' ': // the whitspaces after key
104
case '\t':
105                     state = ISI_EQUAL;
106                     return PropertiesTokenContext.KEY;
107                 }
108                 break; // end state ISI_KEY
109

110             case ISI_KEY_A_BSLASH:
111                 switch (actChar) {
112                 case '\n':
113                     state = INIT;
114                     return PropertiesTokenContext.KEY;
115                 default:
116                     state = ISI_KEY;
117                 }
118                 break; // end state ISI_KEY_A_BSLASH
119

120             case ISI_EQUAL:
121                 switch (actChar) {
122                 case '=':
123                 case ':':
124                     offset++;
125                     state = ISI_VALUE;
126                     return PropertiesTokenContext.EQ;
127                 case ' ': // whitespaces also separates key from value: note which whitespaces can do that
128
case '\t':
129                     break;
130                 case '\\': // in case of alone '\\' line continuation character
131
state = ISI_EQUAL2;
132                     break;
133                 case '\n':
134                     state = INIT;
135                     return PropertiesTokenContext.EQ;
136                 default:
137                     state = ISI_VALUE;
138                 }
139                 break; // end state ISI_KEY
140

141             // only for case the last "\\" continuation char is but was not startes value yet (still can appear : or = char)
142
case ISI_EQUAL2:
143                 switch (actChar) {
144                 case '\n':
145                     state = ISI_EQUAL_AT_NL;
146                     return PropertiesTokenContext.EQ; // PENDING
147
default:
148                     state = ISI_VALUE;
149                 }
150                 break; // end state ISI_EQUAL_A_BSLASH
151

152             // in case of end of line
153
case ISI_EQUAL_AT_NL:
154                 switch (actChar) {
155                 case '\n':
156                     offset++;
157                     state = ISI_EQUAL;
158                     return PropertiesTokenContext.EOL;
159                 default:
160                     throw new Error JavaDoc("Something smells 4");
161                 }
162                 
163 // this previous version of ISI_EQUAL2 is needless because ':=' is not separator the second = char belongs to the value already
164
// case ISI_EQUAL2:
165
// switch (actChar) {
166
// case '\n':
167
// state = INIT;
168
// return EQ;
169
// case '=':
170
// case ':':
171
// offset++;
172
// state = ISI_VALUE;
173
// return EQ;
174
// default:
175
// state = ISI_VALUE;
176
// return EQ;
177
// }
178
//break; // end state ISI_KEY
179

180             case ISI_VALUE:
181                 switch (actChar) {
182                 case '\n':
183                     state = INIT;
184                     return PropertiesTokenContext.VALUE;
185                 case '\\':
186                     state = ISI_VALUE_A_BSLASH;
187                     break;
188                 }
189                 break; // end state ISI_KEY
190

191             case ISI_VALUE_A_BSLASH:
192                 switch (actChar) {
193                 case '\n':
194                     state = ISI_VALUE_AT_NL;
195                     return PropertiesTokenContext.VALUE;
196                 default:
197                     state = ISI_VALUE;
198                 }
199                 break; // end state ISI_KEY
200

201             case ISI_VALUE_AT_NL:
202                 switch (actChar) {
203                 case '\n':
204                     offset++;
205                     state = ISI_VALUE;
206                     return PropertiesTokenContext.EOL;
207                 default:
208                     throw new Error JavaDoc("Something smells 2");
209                 }
210                 //break; // end state ISI_KEY
211

212             default:
213                 throw new Error JavaDoc("Unhandled state " + state);
214
215             } // end of the outer switch statement
216

217             offset = ++offset;
218
219         } // end of while loop
220

221         /* At this stage there's no more text in the scanned buffer. */
222
223         if (lastBuffer || !lastBuffer) {
224             switch(state) {
225             case ISI_LINE_COMMENT:
226                 return PropertiesTokenContext.LINE_COMMENT;
227             case ISI_KEY:
228             case ISI_KEY_A_BSLASH:
229                 return PropertiesTokenContext.KEY;
230             case ISI_EQUAL:
231             case ISI_EQUAL2:
232                 return PropertiesTokenContext.EQ;
233             case ISI_VALUE:
234             case ISI_VALUE_A_BSLASH:
235                 return PropertiesTokenContext.VALUE;
236             case ISI_VALUE_AT_NL:
237             case ISI_EQUAL_AT_NL: // TEMP
238
throw new Error JavaDoc("Something smells 3");
239             }
240         }
241
242         return null;
243
244     } // parseToken
245

246     public String JavaDoc getStateName(int stateNumber) {
247         switch(stateNumber) {
248         case ISI_LINE_COMMENT:
249             return "ISI_LINE_COMMENT";
250         case ISI_KEY:
251             return "ISI_KEY";
252         case ISI_KEY_A_BSLASH:
253             return "ISI_KEY_A_BSLASH";
254         case ISI_EQUAL:
255             return "ISI_EQUAL";
256         case ISI_EQUAL2:
257             return "ISI_EQUAL2";
258         case ISI_EQUAL_AT_NL:
259             return "ISI_EQUAL_AT_NL";
260         case ISI_VALUE:
261             return "ISI_VALUE";
262         case ISI_VALUE_A_BSLASH:
263             return "ISI_VALUE_A_BSLASH";
264         case ISI_VALUE_AT_NL:
265             return "ISI_VALUE_AT_NL";
266         default:
267             return super.getStateName(stateNumber);
268         }
269     }
270
271
272 }
273
274 /*
275  * <<Log>>
276  * 5 Jaga 1.3.1.0 3/15/00 Miloslav Metelka Structural change
277  * 4 Gandalf 1.3 1/12/00 Petr Jiricka Syntax coloring API
278  * fixes
279  * 3 Gandalf 1.2 12/28/99 Miloslav Metelka Structural change and
280  * some renamings
281  * 2 Gandalf 1.1 10/23/99 Ian Formanek NO SEMANTIC CHANGE - Sun
282  * Microsystems Copyright in File Comment
283  * 1 Gandalf 1.0 9/13/99 Petr Jiricka
284  * $
285  */

286
287
Popular Tags