1 |
|
|
2 |
|
package org.apache.commons.configuration.plist; |
3 |
|
|
4 |
|
import java.util.List; |
5 |
|
import java.util.ArrayList; |
6 |
|
|
7 |
|
import org.apache.commons.configuration.HierarchicalConfiguration; |
8 |
|
import org.apache.commons.configuration.HierarchicalConfiguration.Node; |
9 |
|
|
10 |
|
import org.apache.commons.lang.StringUtils; |
11 |
|
import org.apache.commons.codec.binary.Hex; |
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
class PropertyListParser implements PropertyListParserConstants |
20 |
|
{ |
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
protected String removeQuotes(String s) |
26 |
|
{ |
27 |
372 |
if (s == null) |
28 |
|
{ |
29 |
3 |
return null; |
30 |
|
} |
31 |
|
|
32 |
369 |
if (s.startsWith("\"") && s.endsWith("\"") && s.length() >= 2) |
33 |
|
{ |
34 |
363 |
s = s.substring(1, s.length() - 1); |
35 |
|
} |
36 |
|
|
37 |
369 |
return s; |
38 |
|
} |
39 |
|
|
40 |
|
protected String unescapeQuotes(String s) |
41 |
|
{ |
42 |
363 |
return StringUtils.replace(s, "\\\"", "\""); |
43 |
|
} |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
protected byte[] filterData(String s) throws ParseException |
50 |
|
{ |
51 |
99 |
if (s == null) |
52 |
|
{ |
53 |
3 |
return null; |
54 |
|
} |
55 |
|
|
56 |
|
|
57 |
96 |
if (s.startsWith("<") && s.endsWith(">") && s.length() >= 2) |
58 |
|
{ |
59 |
87 |
s = s.substring(1, s.length() - 1); |
60 |
|
} |
61 |
|
|
62 |
|
|
63 |
96 |
s = StringUtils.replaceChars(s, " \t\n\r", ""); |
64 |
|
|
65 |
|
|
66 |
96 |
if (s.length() % 2 != 0) |
67 |
|
{ |
68 |
3 |
s = "0" + s; |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
try |
73 |
|
{ |
74 |
96 |
return Hex.decodeHex(s.toCharArray()); |
75 |
|
} |
76 |
|
catch (Exception e) |
77 |
|
{ |
78 |
0 |
throw new ParseException(e.getMessage()); |
79 |
|
} |
80 |
|
} |
81 |
|
|
82 |
|
final public PropertyListConfiguration parse() throws ParseException |
83 |
|
{ |
84 |
45 |
PropertyListConfiguration configuration = null; |
85 |
45 |
configuration = Dictionary(); |
86 |
42 |
jj_consume_token(0); |
87 |
|
|
88 |
42 |
return configuration; |
89 |
|
} |
90 |
|
|
91 |
|
final public PropertyListConfiguration Dictionary() throws ParseException |
92 |
|
{ |
93 |
336 |
PropertyListConfiguration configuration = new PropertyListConfiguration(); |
94 |
336 |
List children = new ArrayList(); |
95 |
336 |
Node child = null; |
96 |
336 |
jj_consume_token(DICT_BEGIN); |
97 |
|
label_1: |
98 |
963 |
while (true) |
99 |
|
{ |
100 |
1170 |
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) |
101 |
|
{ |
102 |
|
case STRING: |
103 |
|
case QUOTED_STRING: |
104 |
|
; |
105 |
837 |
break; |
106 |
|
default: |
107 |
333 |
jj_la1[0] = jj_gen; |
108 |
333 |
break label_1; |
109 |
|
} |
110 |
837 |
child = Property(); |
111 |
837 |
if (child.getValue() instanceof HierarchicalConfiguration) |
112 |
|
{ |
113 |
|
|
114 |
207 |
HierarchicalConfiguration conf = (HierarchicalConfiguration) child.getValue(); |
115 |
207 |
Node root = conf.getRoot(); |
116 |
207 |
root.setName(child.getName()); |
117 |
207 |
children.add(root); |
118 |
|
} |
119 |
|
else |
120 |
|
{ |
121 |
630 |
children.add(child); |
122 |
|
} |
123 |
|
} |
124 |
333 |
jj_consume_token(DICT_END); |
125 |
1170 |
for (int i = 0; i < children.size(); i++) |
126 |
|
{ |
127 |
837 |
child = (Node) children.get(i); |
128 |
837 |
configuration.getRoot().addChild(child); |
129 |
|
} |
130 |
|
|
131 |
333 |
return configuration; |
132 |
|
} |
133 |
|
|
134 |
|
final public Node Property() throws ParseException |
135 |
|
{ |
136 |
837 |
Node node = new Node(); |
137 |
837 |
String key = String(); |
138 |
837 |
node.setName(key); |
139 |
837 |
jj_consume_token(EQUAL); |
140 |
837 |
Object value = Element(); |
141 |
837 |
node.setValue(value); |
142 |
837 |
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) |
143 |
|
{ |
144 |
|
case DICT_SEPARATOR: |
145 |
552 |
jj_consume_token(DICT_SEPARATOR); |
146 |
552 |
break; |
147 |
|
default: |
148 |
285 |
jj_la1[1] = jj_gen; |
149 |
|
; |
150 |
|
} |
151 |
|
|
152 |
837 |
return node; |
153 |
|
} |
154 |
|
|
155 |
|
final public Object Element() throws ParseException |
156 |
|
{ |
157 |
1299 |
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) |
158 |
|
{ |
159 |
|
case ARRAY_BEGIN: |
160 |
252 |
return Array(); |
161 |
|
|
162 |
|
case DICT_BEGIN: |
163 |
291 |
return Dictionary(); |
164 |
|
|
165 |
|
case STRING: |
166 |
|
case QUOTED_STRING: |
167 |
672 |
return String(); |
168 |
|
|
169 |
|
case DATA: |
170 |
84 |
return Data(); |
171 |
|
|
172 |
|
default: |
173 |
0 |
jj_la1[2] = jj_gen; |
174 |
0 |
jj_consume_token(-1); |
175 |
0 |
throw new ParseException(); |
176 |
|
} |
177 |
|
} |
178 |
|
|
179 |
|
final public List Array() throws ParseException |
180 |
|
{ |
181 |
252 |
List list = new ArrayList(); |
182 |
252 |
Object element = null; |
183 |
252 |
jj_consume_token(ARRAY_BEGIN); |
184 |
252 |
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) |
185 |
|
{ |
186 |
|
case ARRAY_BEGIN: |
187 |
|
case DICT_BEGIN: |
188 |
|
case DATA: |
189 |
|
case STRING: |
190 |
|
case QUOTED_STRING: |
191 |
210 |
element = Element(); |
192 |
210 |
list.add(element); |
193 |
|
label_2: |
194 |
462 |
while (true) |
195 |
|
{ |
196 |
462 |
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) |
197 |
|
{ |
198 |
|
case ARRAY_SEPARATOR: |
199 |
|
; |
200 |
252 |
break; |
201 |
|
default: |
202 |
210 |
jj_la1[3] = jj_gen; |
203 |
210 |
break label_2; |
204 |
|
} |
205 |
252 |
jj_consume_token(ARRAY_SEPARATOR); |
206 |
252 |
element = Element(); |
207 |
252 |
list.add(element); |
208 |
|
} |
209 |
|
break; |
210 |
|
default: |
211 |
42 |
jj_la1[4] = jj_gen; |
212 |
|
; |
213 |
|
} |
214 |
252 |
jj_consume_token(ARRAY_END); |
215 |
|
|
216 |
252 |
return list; |
217 |
|
} |
218 |
|
|
219 |
|
final public String String() throws ParseException |
220 |
|
{ |
221 |
1509 |
Token token = null; |
222 |
1509 |
switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) |
223 |
|
{ |
224 |
|
case QUOTED_STRING: |
225 |
357 |
token = jj_consume_token(QUOTED_STRING); |
226 |
357 |
return unescapeQuotes(removeQuotes(token.image)); |
227 |
|
|
228 |
|
case STRING: |
229 |
1152 |
token = jj_consume_token(STRING); |
230 |
1152 |
return token.image; |
231 |
|
|
232 |
|
default: |
233 |
0 |
jj_la1[5] = jj_gen; |
234 |
0 |
jj_consume_token(-1); |
235 |
0 |
throw new ParseException(); |
236 |
|
} |
237 |
|
} |
238 |
|
|
239 |
|
final public byte[] Data() throws ParseException |
240 |
|
{ |
241 |
|
Token token; |
242 |
84 |
token = jj_consume_token(DATA); |
243 |
|
|
244 |
84 |
return filterData(token.image); |
245 |
|
} |
246 |
|
|
247 |
|
public PropertyListParserTokenManager token_source; |
248 |
|
SimpleCharStream jj_input_stream; |
249 |
|
public Token token, jj_nt; |
250 |
|
private int jj_ntk; |
251 |
|
private int jj_gen; |
252 |
54 |
final private int[] jj_la1 = new class="keyword">int[6]; |
253 |
|
static private int[] jj_la1_0; |
254 |
|
|
255 |
|
static |
256 |
6 |
{ |
257 |
6 |
jj_la1_0(); |
258 |
|
} |
259 |
|
|
260 |
|
private static void jj_la1_0() |
261 |
|
{ |
262 |
6 |
jj_la1_0 = new int[]{0x180000, 0x400, 0x1c0120, 0x80, 0x1c0120, 0x180000, }; |
263 |
6 |
} |
264 |
|
|
265 |
|
public PropertyListParser(java.io.InputStream stream) |
266 |
0 |
{ |
267 |
0 |
jj_input_stream = new SimpleCharStream(stream, 1, 1); |
268 |
0 |
token_source = new PropertyListParserTokenManager(jj_input_stream); |
269 |
0 |
token = new Token(); |
270 |
0 |
jj_ntk = -1; |
271 |
0 |
jj_gen = 0; |
272 |
0 |
for (int i = 0; i < 6; i++) jj_la1[i] = -1; |
273 |
0 |
} |
274 |
|
|
275 |
|
public PropertyListParser(java.io.Reader stream) |
276 |
54 |
{ |
277 |
54 |
jj_input_stream = new SimpleCharStream(stream, 1, 1); |
278 |
54 |
token_source = new PropertyListParserTokenManager(jj_input_stream); |
279 |
54 |
token = new Token(); |
280 |
54 |
jj_ntk = -1; |
281 |
54 |
jj_gen = 0; |
282 |
54 |
for (int i = 0; i < 6; i++) jj_la1[i] = -1; |
283 |
54 |
} |
284 |
|
|
285 |
|
final private Token jj_consume_token(int kind) throws ParseException |
286 |
|
{ |
287 |
|
Token oldToken; |
288 |
4449 |
if ((oldToken = token).next != null) |
289 |
3525 |
token = token.next; |
290 |
|
else |
291 |
924 |
token = token.next = token_source.getNextToken(); |
292 |
4449 |
jj_ntk = -1; |
293 |
4449 |
if (token.kind == kind) |
294 |
|
{ |
295 |
4446 |
jj_gen++; |
296 |
4446 |
return token; |
297 |
|
} |
298 |
3 |
token = oldToken; |
299 |
3 |
jj_kind = kind; |
300 |
3 |
throw generateParseException(); |
301 |
|
} |
302 |
|
|
303 |
|
final private int jj_ntk() |
304 |
|
{ |
305 |
3525 |
if ((jj_nt = token.next) == null) |
306 |
3525 |
return (jj_ntk = (token.next = token_source.getNextToken()).kind); |
307 |
|
else |
308 |
0 |
return (jj_ntk = jj_nt.kind); |
309 |
|
} |
310 |
|
|
311 |
54 |
private java.util.Vector jj_expentries = new java.util.Vector(); |
312 |
|
private int[] jj_expentry; |
313 |
54 |
private int jj_kind = -1; |
314 |
|
|
315 |
|
public ParseException generateParseException() |
316 |
|
{ |
317 |
3 |
jj_expentries.removeAllElements(); |
318 |
3 |
boolean[] la1tokens = new class="keyword">boolean[22]; |
319 |
69 |
for (int i = 0; i < 22; i++) |
320 |
|
{ |
321 |
66 |
la1tokens[i] = false; |
322 |
|
} |
323 |
3 |
if (jj_kind >= 0) |
324 |
|
{ |
325 |
3 |
la1tokens[jj_kind] = true; |
326 |
3 |
jj_kind = -1; |
327 |
|
} |
328 |
21 |
for (int i = 0; i < 6; i++) |
329 |
|
{ |
330 |
18 |
if (jj_la1[i] == jj_gen) |
331 |
|
{ |
332 |
0 |
for (int j = 0; j < 32; j++) |
333 |
|
{ |
334 |
0 |
if ((jj_la1_0[i] & (1 << j)) != 0) |
335 |
|
{ |
336 |
0 |
la1tokens[j] = true; |
337 |
|
} |
338 |
|
} |
339 |
|
} |
340 |
|
} |
341 |
69 |
for (int i = 0; i < 22; i++) |
342 |
|
{ |
343 |
66 |
if (la1tokens[i]) |
344 |
|
{ |
345 |
3 |
jj_expentry = new int[1]; |
346 |
3 |
jj_expentry[0] = i; |
347 |
3 |
jj_expentries.addElement(jj_expentry); |
348 |
|
} |
349 |
|
} |
350 |
3 |
int[][] exptokseq = new class="keyword">int[jj_expentries.size()][]; |
351 |
6 |
for (int i = 0; i < jj_expentries.size(); i++) |
352 |
|
{ |
353 |
3 |
exptokseq[i] = (int[]) jj_expentries.elementAt(i); |
354 |
|
} |
355 |
3 |
return new ParseException(token, exptokseq, tokenImage); |
356 |
|
} |
357 |
|
|
358 |
|
} |