View Javadoc

1   /* Generated By:JavaCC: Do not edit this line. PropertyListParser.java */
2   package org.apache.commons.configuration.plist;
3   
4   import java.util.Date;
5   import java.util.List;
6   import java.util.ArrayList;
7   
8   import org.apache.commons.configuration.HierarchicalConfiguration;
9   import org.apache.commons.configuration.HierarchicalConfiguration.Node;
10  
11  import org.apache.commons.codec.binary.Hex;
12  
13  /**
14   * JavaCC based parser for the PropertyList format.
15   *
16   * @author Emmanuel Bourg
17   * @version $Revision$, $Date$
18   */
19  class PropertyListParser implements PropertyListParserConstants {
20  
21      /**
22       * Remove the quotes at the beginning and at the end of the specified String.
23       */
24      protected String removeQuotes(String s)
25      {
26          if (s == null)
27          {
28              return null;
29          }
30  
31          if (s.startsWith("\u005c"") && s.endsWith("\u005c"") && s.length() >= 2)
32          {
33              s = s.substring(1, s.length() - 1);
34          }
35  
36          return s;
37      }
38  
39      protected String unescapeQuotes(String s)
40      {
41          return s.replaceAll("\u005c\u005c\u005c\u005c\u005c"", "\u005c"");
42      }
43  
44      /**
45       * Remove the white spaces and the data delimiters from the specified
46       * string and parse it as a byte array.
47       */
48      protected byte[] filterData(String s) throws ParseException
49      {
50          if (s == null)
51          {
52              return null;
53          }
54  
55          // remove the delimiters
56          if (s.startsWith("<") && s.endsWith(">") && s.length() >= 2)
57          {
58              s = s.substring(1, s.length() - 1);
59          }
60  
61          // remove the white spaces
62          s = s.replaceAll("\u005c\u005cs", "");
63  
64          // add a leading 0 to ensure well formed bytes
65          if (s.length() % 2 != 0)
66          {
67              s = "0" + s;
68          }
69  
70          // parse and return the bytes
71          try
72          {
73              return Hex.decodeHex(s.toCharArray());
74          }
75          catch (Exception e)
76          {
77              throw (ParseException) new ParseException("Unable to parse the byte[] : " + e.getMessage());
78          }
79      }
80  
81      /**
82       * Parse a date formatted as <*D2002-03-22 11:30:00 +0100>
83       */
84      protected Date parseDate(String s) throws ParseException
85      {
86          return PropertyListConfiguration.parseDate(s);
87      }
88  
89    final public PropertyListConfiguration parse() throws ParseException {
90      PropertyListConfiguration configuration = null;
91      configuration = Dictionary();
92      jj_consume_token(0);
93        {if (true) return configuration;}
94      throw new Error("Missing return statement in function");
95    }
96  
97    final public PropertyListConfiguration Dictionary() throws ParseException {
98      PropertyListConfiguration configuration = new PropertyListConfiguration();
99      List<Node> children = new ArrayList<Node>();
100     Node child = null;
101     jj_consume_token(DICT_BEGIN);
102     label_1:
103     while (true) {
104       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
105       case STRING:
106       case QUOTED_STRING:
107         ;
108         break;
109       default:
110         jj_la1[0] = jj_gen;
111         break label_1;
112       }
113       child = Property();
114             if (child.getValue() instanceof HierarchicalConfiguration)
115             {
116                 // prune & graft the nested configuration to the parent configuration
117                 HierarchicalConfiguration conf = (HierarchicalConfiguration) child.getValue();
118                 Node root = conf.getRoot();
119                 root.setName(child.getName());
120                 children.add(root);
121             }
122             else
123             {
124                 children.add(child);
125             }
126     }
127     jj_consume_token(DICT_END);
128         for (int i = 0; i < children.size(); i++)
129         {
130             child = children.get(i);
131             configuration.getRoot().addChild(child);
132         }
133 
134         {if (true) return configuration;}
135     throw new Error("Missing return statement in function");
136   }
137 
138   final public Node Property() throws ParseException {
139     String key = null;
140     Object value = null;
141     Node node = new Node();
142     key = String();
143       node.setName(key);
144     jj_consume_token(EQUAL);
145     value = Element();
146       node.setValue(value);
147     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
148     case DICT_SEPARATOR:
149       jj_consume_token(DICT_SEPARATOR);
150       break;
151     default:
152       jj_la1[1] = jj_gen;
153       ;
154     }
155       {if (true) return node;}
156     throw new Error("Missing return statement in function");
157   }
158 
159   final public Object Element() throws ParseException {
160     Object value = null;
161     if (jj_2_1(2)) {
162       value = Array();
163       {if (true) return value;}
164     } else {
165       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
166       case DICT_BEGIN:
167         value = Dictionary();
168       {if (true) return value;}
169         break;
170       case STRING:
171       case QUOTED_STRING:
172         value = String();
173       {if (true) return value;}
174         break;
175       case DATA:
176         value = Data();
177       {if (true) return value;}
178         break;
179       case DATE:
180         value = Date();
181       {if (true) return value;}
182         break;
183       default:
184         jj_la1[2] = jj_gen;
185         jj_consume_token(-1);
186         throw new ParseException();
187       }
188     }
189     throw new Error("Missing return statement in function");
190   }
191 
192   final public List Array() throws ParseException {
193     List<Object> list = new ArrayList<Object>();
194     Object element = null;
195     jj_consume_token(ARRAY_BEGIN);
196     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
197     case ARRAY_BEGIN:
198     case DICT_BEGIN:
199     case DATA:
200     case DATE:
201     case STRING:
202     case QUOTED_STRING:
203       element = Element();
204           list.add(element);
205       label_2:
206       while (true) {
207         switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
208         case ARRAY_SEPARATOR:
209           ;
210           break;
211         default:
212           jj_la1[3] = jj_gen;
213           break label_2;
214         }
215         jj_consume_token(ARRAY_SEPARATOR);
216         element = Element();
217               list.add(element);
218       }
219       break;
220     default:
221       jj_la1[4] = jj_gen;
222       ;
223     }
224     jj_consume_token(ARRAY_END);
225       {if (true) return list;}
226     throw new Error("Missing return statement in function");
227   }
228 
229   final public String String() throws ParseException {
230     Token token = null;
231     String value = null;
232     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
233     case QUOTED_STRING:
234       token = jj_consume_token(QUOTED_STRING);
235       {if (true) return unescapeQuotes(removeQuotes(token.image));}
236       break;
237     case STRING:
238       token = jj_consume_token(STRING);
239       {if (true) return token.image;}
240       break;
241     default:
242       jj_la1[5] = jj_gen;
243       jj_consume_token(-1);
244       throw new ParseException();
245     }
246     throw new Error("Missing return statement in function");
247   }
248 
249   final public byte[] Data() throws ParseException {
250     Token token;
251     token = jj_consume_token(DATA);
252       {if (true) return filterData(token.image);}
253     throw new Error("Missing return statement in function");
254   }
255 
256   final public Date Date() throws ParseException {
257     Token token;
258     token = jj_consume_token(DATE);
259       {if (true) return parseDate(token.image);}
260     throw new Error("Missing return statement in function");
261   }
262 
263   private boolean jj_2_1(int xla) {
264     jj_la = xla; jj_lastpos = jj_scanpos = token;
265     try { return !jj_3_1(); }
266     catch(LookaheadSuccess ls) { return true; }
267     finally { jj_save(0, xla); }
268   }
269 
270   private boolean jj_3_1() {
271     if (jj_3R_3()) return true;
272     return false;
273   }
274 
275   private boolean jj_3R_5() {
276     Token xsp;
277     xsp = jj_scanpos;
278     if (jj_3_1()) {
279     jj_scanpos = xsp;
280     if (jj_3R_6()) {
281     jj_scanpos = xsp;
282     if (jj_3R_7()) {
283     jj_scanpos = xsp;
284     if (jj_3R_8()) {
285     jj_scanpos = xsp;
286     if (jj_3R_9()) return true;
287     }
288     }
289     }
290     }
291     return false;
292   }
293 
294   private boolean jj_3R_14() {
295     if (jj_scan_token(QUOTED_STRING)) return true;
296     return false;
297   }
298 
299   private boolean jj_3R_11() {
300     Token xsp;
301     xsp = jj_scanpos;
302     if (jj_3R_14()) {
303     jj_scanpos = xsp;
304     if (jj_3R_15()) return true;
305     }
306     return false;
307   }
308 
309   private boolean jj_3R_13() {
310     if (jj_scan_token(DATE)) return true;
311     return false;
312   }
313 
314   private boolean jj_3R_10() {
315     if (jj_scan_token(DICT_BEGIN)) return true;
316     return false;
317   }
318 
319   private boolean jj_3R_9() {
320     if (jj_3R_13()) return true;
321     return false;
322   }
323 
324   private boolean jj_3R_8() {
325     if (jj_3R_12()) return true;
326     return false;
327   }
328 
329   private boolean jj_3R_12() {
330     if (jj_scan_token(DATA)) return true;
331     return false;
332   }
333 
334   private boolean jj_3R_7() {
335     if (jj_3R_11()) return true;
336     return false;
337   }
338 
339   private boolean jj_3R_4() {
340     if (jj_3R_5()) return true;
341     return false;
342   }
343 
344   private boolean jj_3R_6() {
345     if (jj_3R_10()) return true;
346     return false;
347   }
348 
349   private boolean jj_3R_15() {
350     if (jj_scan_token(STRING)) return true;
351     return false;
352   }
353 
354   private boolean jj_3R_3() {
355     if (jj_scan_token(ARRAY_BEGIN)) return true;
356     Token xsp;
357     xsp = jj_scanpos;
358     if (jj_3R_4()) jj_scanpos = xsp;
359     if (jj_scan_token(ARRAY_END)) return true;
360     return false;
361   }
362 
363   /** Generated Token Manager. */
364   public PropertyListParserTokenManager token_source;
365   SimpleCharStream jj_input_stream;
366   /** Current token. */
367   public Token token;
368   /** Next token. */
369   public Token jj_nt;
370   private int jj_ntk;
371   private Token jj_scanpos, jj_lastpos;
372   private int jj_la;
373   private int jj_gen;
374   final private int[] jj_la1 = new int[6];
375   static private int[] jj_la1_0;
376   static {
377       jj_la1_init_0();
378    }
379    private static void jj_la1_init_0() {
380       jj_la1_0 = new int[] {0x18000000,0x10000,0x1e004000,0x2000,0x1e004800,0x18000000,};
381    }
382   final private JJCalls[] jj_2_rtns = new JJCalls[1];
383   private boolean jj_rescan = false;
384   private int jj_gc = 0;
385 
386   /** Constructor with InputStream. */
387   public PropertyListParser(java.io.InputStream stream) {
388      this(stream, null);
389   }
390   /** Constructor with InputStream and supplied encoding */
391   public PropertyListParser(java.io.InputStream stream, String encoding) {
392     try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
393     token_source = new PropertyListParserTokenManager(jj_input_stream);
394     token = new Token();
395     jj_ntk = -1;
396     jj_gen = 0;
397     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
398     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
399   }
400 
401   /** Reinitialise. */
402   public void ReInit(java.io.InputStream stream) {
403      ReInit(stream, null);
404   }
405   /** Reinitialise. */
406   public void ReInit(java.io.InputStream stream, String encoding) {
407     try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
408     token_source.ReInit(jj_input_stream);
409     token = new Token();
410     jj_ntk = -1;
411     jj_gen = 0;
412     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
413     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
414   }
415 
416   /** Constructor. */
417   public PropertyListParser(java.io.Reader stream) {
418     jj_input_stream = new SimpleCharStream(stream, 1, 1);
419     token_source = new PropertyListParserTokenManager(jj_input_stream);
420     token = new Token();
421     jj_ntk = -1;
422     jj_gen = 0;
423     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
424     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
425   }
426 
427   /** Reinitialise. */
428   public void ReInit(java.io.Reader stream) {
429     jj_input_stream.ReInit(stream, 1, 1);
430     token_source.ReInit(jj_input_stream);
431     token = new Token();
432     jj_ntk = -1;
433     jj_gen = 0;
434     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
435     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
436   }
437 
438   /** Constructor with generated Token Manager. */
439   public PropertyListParser(PropertyListParserTokenManager tm) {
440     token_source = tm;
441     token = new Token();
442     jj_ntk = -1;
443     jj_gen = 0;
444     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
445     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
446   }
447 
448   /** Reinitialise. */
449   public void ReInit(PropertyListParserTokenManager tm) {
450     token_source = tm;
451     token = new Token();
452     jj_ntk = -1;
453     jj_gen = 0;
454     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
455     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
456   }
457 
458   private Token jj_consume_token(int kind) throws ParseException {
459     Token oldToken;
460     if ((oldToken = token).next != null) token = token.next;
461     else token = token.next = token_source.getNextToken();
462     jj_ntk = -1;
463     if (token.kind == kind) {
464       jj_gen++;
465       if (++jj_gc > 100) {
466         jj_gc = 0;
467         for (int i = 0; i < jj_2_rtns.length; i++) {
468           JJCalls c = jj_2_rtns[i];
469           while (c != null) {
470             if (c.gen < jj_gen) c.first = null;
471             c = c.next;
472           }
473         }
474       }
475       return token;
476     }
477     token = oldToken;
478     jj_kind = kind;
479     throw generateParseException();
480   }
481 
482   static private final class LookaheadSuccess extends java.lang.Error { }
483   final private LookaheadSuccess jj_ls = new LookaheadSuccess();
484   private boolean jj_scan_token(int kind) {
485     if (jj_scanpos == jj_lastpos) {
486       jj_la--;
487       if (jj_scanpos.next == null) {
488         jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
489       } else {
490         jj_lastpos = jj_scanpos = jj_scanpos.next;
491       }
492     } else {
493       jj_scanpos = jj_scanpos.next;
494     }
495     if (jj_rescan) {
496       int i = 0; Token tok = token;
497       while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
498       if (tok != null) jj_add_error_token(kind, i);
499     }
500     if (jj_scanpos.kind != kind) return true;
501     if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
502     return false;
503   }
504 
505 
506 /** Get the next Token. */
507   final public Token getNextToken() {
508     if (token.next != null) token = token.next;
509     else token = token.next = token_source.getNextToken();
510     jj_ntk = -1;
511     jj_gen++;
512     return token;
513   }
514 
515 /** Get the specific Token. */
516   final public Token getToken(int index) {
517     Token t = token;
518     for (int i = 0; i < index; i++) {
519       if (t.next != null) t = t.next;
520       else t = t.next = token_source.getNextToken();
521     }
522     return t;
523   }
524 
525   private int jj_ntk() {
526     if ((jj_nt=token.next) == null)
527       return (jj_ntk = (token.next=token_source.getNextToken()).kind);
528     else
529       return (jj_ntk = jj_nt.kind);
530   }
531 
532   private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
533   private int[] jj_expentry;
534   private int jj_kind = -1;
535   private int[] jj_lasttokens = new int[100];
536   private int jj_endpos;
537 
538   private void jj_add_error_token(int kind, int pos) {
539     if (pos >= 100) return;
540     if (pos == jj_endpos + 1) {
541       jj_lasttokens[jj_endpos++] = kind;
542     } else if (jj_endpos != 0) {
543       jj_expentry = new int[jj_endpos];
544       for (int i = 0; i < jj_endpos; i++) {
545         jj_expentry[i] = jj_lasttokens[i];
546       }
547       jj_entries_loop: for (java.util.Iterator<?> it = jj_expentries.iterator(); it.hasNext();) {
548         int[] oldentry = (int[])(it.next());
549         if (oldentry.length == jj_expentry.length) {
550           for (int i = 0; i < jj_expentry.length; i++) {
551             if (oldentry[i] != jj_expentry[i]) {
552               continue jj_entries_loop;
553             }
554           }
555           jj_expentries.add(jj_expentry);
556           break jj_entries_loop;
557         }
558       }
559       if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
560     }
561   }
562 
563   /** Generate ParseException. */
564   public ParseException generateParseException() {
565     jj_expentries.clear();
566     boolean[] la1tokens = new boolean[30];
567     if (jj_kind >= 0) {
568       la1tokens[jj_kind] = true;
569       jj_kind = -1;
570     }
571     for (int i = 0; i < 6; i++) {
572       if (jj_la1[i] == jj_gen) {
573         for (int j = 0; j < 32; j++) {
574           if ((jj_la1_0[i] & (1<<j)) != 0) {
575             la1tokens[j] = true;
576           }
577         }
578       }
579     }
580     for (int i = 0; i < 30; i++) {
581       if (la1tokens[i]) {
582         jj_expentry = new int[1];
583         jj_expentry[0] = i;
584         jj_expentries.add(jj_expentry);
585       }
586     }
587     jj_endpos = 0;
588     jj_rescan_token();
589     jj_add_error_token(0, 0);
590     int[][] exptokseq = new int[jj_expentries.size()][];
591     for (int i = 0; i < jj_expentries.size(); i++) {
592       exptokseq[i] = jj_expentries.get(i);
593     }
594     return new ParseException(token, exptokseq, tokenImage);
595   }
596 
597   /** Enable tracing. */
598   final public void enable_tracing() {
599   }
600 
601   /** Disable tracing. */
602   final public void disable_tracing() {
603   }
604 
605   private void jj_rescan_token() {
606     jj_rescan = true;
607     for (int i = 0; i < 1; i++) {
608     try {
609       JJCalls p = jj_2_rtns[i];
610       do {
611         if (p.gen > jj_gen) {
612           jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
613           switch (i) {
614             case 0: jj_3_1(); break;
615           }
616         }
617         p = p.next;
618       } while (p != null);
619       } catch(LookaheadSuccess ls) { }
620     }
621     jj_rescan = false;
622   }
623 
624   private void jj_save(int index, int xla) {
625     JJCalls p = jj_2_rtns[index];
626     while (p.gen > jj_gen) {
627       if (p.next == null) { p = p.next = new JJCalls(); break; }
628       p = p.next;
629     }
630     p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
631   }
632 
633   static final class JJCalls {
634     int gen;
635     Token first;
636     int arg;
637     JJCalls next;
638   }
639 
640 }