KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > wocompat > parser > Parser


1 /* Generated By:JavaCC: Do not edit this line. Parser.java */
2 /*****************************************************************
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements. See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership. The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied. See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  ****************************************************************/

20
21
22 package org.apache.cayenne.wocompat.parser;
23
24 import java.io.*;
25 import java.util.*;
26 import org.apache.cayenne.wocompat.PlistDataStructureFactory;
27
28 /**
29  * NeXT/Apple Property List format parser.
30  *
31  * <p>Contributed by Jeff Martin <reportmill@yahoo.com> and Josh from Report Mill -
32  * <a HREF="http://www.reportmill.com/">http://www.reportmill.com/</a></p>
33  */

34 public class Parser implements ParserConstants {
35
36     private PlistDataStructureFactory factory = new DefaultPlistDataStructureFactory();
37
38     public Parser() {
39         this(System.in, null);
40     }
41
42     public Parser(PlistDataStructureFactory factory) {
43         this(System.in, factory);
44     }
45
46     public Parser(String JavaDoc filename, PlistDataStructureFactory factory) throws FileNotFoundException {
47         this(new FileInputStream(filename), factory);
48     }
49
50     public Parser(File f, PlistDataStructureFactory factory) throws FileNotFoundException {
51         this(new FileInputStream(f), factory);
52     }
53
54     public Parser(InputStream in, PlistDataStructureFactory factory) {
55         this(in);
56
57         if(factory != null) {
58             this.factory = factory;
59         }
60     }
61
62     public Object JavaDoc propertyList() {
63         try {
64             return object("");
65         }
66         catch (ParseException pe) {
67            return null;
68         }
69     }
70
71     /**
72      * @deprecated since 3.0, use 'object(String)' instead.
73      */

74     public Object JavaDoc object() throws ParseException {
75         return object("");
76     }
77
78     /**
79      * @deprecated since 3.0, use 'map(String)' instead.
80      */

81     public final java.util.HashMap JavaDoc dictionary()
82                                    throws ParseException {
83             return (java.util.HashMap JavaDoc) map("");
84     }
85
86     /**
87      * @deprecated since 3.0, use 'collection(String)' instead.
88      */

89     public final java.util.ArrayList JavaDoc array()
90                                    throws ParseException {
91             return (java.util.ArrayList JavaDoc) collection("");
92     }
93
94   final public Object JavaDoc object(String JavaDoc parentKeyPath) throws ParseException {
95         Object JavaDoc node;
96     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
97     case 8:
98       node = collection(parentKeyPath);
99                                                  {if (true) return node;}
100       break;
101     case 10:
102       node = map(parentKeyPath);
103                                           {if (true) return node;}
104       break;
105     case INT:
106     case FLOAT:
107     case STRING:
108     case QUOTED_STRING:
109       node = leaf();
110                               {if (true) return node;}
111       break;
112     default:
113       jj_la1[0] = jj_gen;
114       jj_consume_token(-1);
115       throw new ParseException();
116     }
117     throw new Error JavaDoc("Missing return statement in function");
118   }
119
120   final public Object JavaDoc leaf() throws ParseException {
121     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
122     case INT:
123       jj_consume_token(INT);
124           {if (true) return Integer.valueOf(token.image);}
125       break;
126     case FLOAT:
127       jj_consume_token(FLOAT);
128             {if (true) return Double.valueOf(token.image);}
129       break;
130     case QUOTED_STRING:
131       jj_consume_token(QUOTED_STRING);
132                     {if (true) return token.image;}
133       break;
134     case STRING:
135       jj_consume_token(STRING);
136              {if (true) return token.image;}
137       break;
138     default:
139       jj_la1[1] = jj_gen;
140       jj_consume_token(-1);
141       throw new ParseException();
142     }
143     throw new Error JavaDoc("Missing return statement in function");
144   }
145
146   final public Map map(String JavaDoc parentKeyPath) throws ParseException {
147         Map h;
148     jj_consume_token(10);
149               h = factory.createMap(parentKeyPath);
150     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
151     case INT:
152     case FLOAT:
153     case 8:
154     case 10:
155     case STRING:
156     case QUOTED_STRING:
157       keyValuePair(parentKeyPath, h);
158       label_1:
159       while (true) {
160         if (jj_2_1(2)) {
161           ;
162         } else {
163           break label_1;
164         }
165         jj_consume_token(13);
166         keyValuePair(parentKeyPath, h);
167       }
168       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
169       case 13:
170         jj_consume_token(13);
171         break;
172       default:
173         jj_la1[2] = jj_gen;
174         ;
175       }
176       break;
177     default:
178       jj_la1[3] = jj_gen;
179       ;
180     }
181     jj_consume_token(11);
182               {if (true) return h;}
183     throw new Error JavaDoc("Missing return statement in function");
184   }
185
186   final public void keyValuePair(String JavaDoc parentKeyPath, Map map) throws ParseException {
187         Object JavaDoc key, val;
188     key = object(parentKeyPath);
189     jj_consume_token(7);
190     val = object(parentKeyPath + "." + key);
191                                                           map.put(key, val);
192   }
193
194   final public Collection collection(String JavaDoc parentKeyPath) throws ParseException {
195   Collection v;
196   Object JavaDoc o;
197     jj_consume_token(8);
198          v = factory.createCollection(parentKeyPath);
199     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
200     case INT:
201     case FLOAT:
202     case 8:
203     case 10:
204     case STRING:
205     case QUOTED_STRING:
206       o = object(parentKeyPath);
207                                 v.add(o);
208       label_2:
209       while (true) {
210         switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
211         case 12:
212           ;
213           break;
214         default:
215           jj_la1[4] = jj_gen;
216           break label_2;
217         }
218         jj_consume_token(12);
219         o = object(parentKeyPath);
220                                      v.add(o);
221       }
222       break;
223     default:
224       jj_la1[5] = jj_gen;
225       ;
226     }
227     jj_consume_token(9);
228         {if (true) return v;}
229     throw new Error JavaDoc("Missing return statement in function");
230   }
231
232   final private boolean jj_2_1(int xla) {
233     jj_la = xla; jj_lastpos = jj_scanpos = token;
234     try { return !jj_3_1(); }
235     catch(LookaheadSuccess ls) { return true; }
236     finally { jj_save(0, xla); }
237   }
238
239   final private boolean jj_3R_13() {
240     if (jj_scan_token(QUOTED_STRING)) return true;
241     return false;
242   }
243
244   final private boolean jj_3R_12() {
245     if (jj_scan_token(FLOAT)) return true;
246     return false;
247   }
248
249   final private boolean jj_3R_11() {
250     if (jj_scan_token(INT)) return true;
251     return false;
252   }
253
254   final private boolean jj_3R_10() {
255     Token xsp;
256     xsp = jj_scanpos;
257     if (jj_3R_11()) {
258     jj_scanpos = xsp;
259     if (jj_3R_12()) {
260     jj_scanpos = xsp;
261     if (jj_3R_13()) {
262     jj_scanpos = xsp;
263     if (jj_3R_14()) return true;
264     }
265     }
266     }
267     return false;
268   }
269
270   final private boolean jj_3R_3() {
271     if (jj_3R_4()) return true;
272     return false;
273   }
274
275   final private boolean jj_3R_8() {
276     if (jj_scan_token(8)) return true;
277     return false;
278   }
279
280   final private boolean jj_3R_9() {
281     if (jj_scan_token(10)) return true;
282     return false;
283   }
284
285   final private boolean jj_3_1() {
286     if (jj_scan_token(13)) return true;
287     if (jj_3R_3()) return true;
288     return false;
289   }
290
291   final private boolean jj_3R_7() {
292     if (jj_3R_10()) return true;
293     return false;
294   }
295
296   final private boolean jj_3R_6() {
297     if (jj_3R_9()) return true;
298     return false;
299   }
300
301   final private boolean jj_3R_4() {
302     Token xsp;
303     xsp = jj_scanpos;
304     if (jj_3R_5()) {
305     jj_scanpos = xsp;
306     if (jj_3R_6()) {
307     jj_scanpos = xsp;
308     if (jj_3R_7()) return true;
309     }
310     }
311     return false;
312   }
313
314   final private boolean jj_3R_5() {
315     if (jj_3R_8()) return true;
316     return false;
317   }
318
319   final private boolean jj_3R_14() {
320     if (jj_scan_token(STRING)) return true;
321     return false;
322   }
323
324   public ParserTokenManager token_source;
325   SimpleCharStream jj_input_stream;
326   public Token token, jj_nt;
327   private int jj_ntk;
328   private Token jj_scanpos, jj_lastpos;
329   private int jj_la;
330   public boolean lookingAhead = false;
331   private boolean jj_semLA;
332   private int jj_gen;
333   final private int[] jj_la1 = new int[6];
334   static private int[] jj_la1_0;
335   static {
336       jj_la1_0();
337    }
338    private static void jj_la1_0() {
339       jj_la1_0 = new int[] {0x40020560,0x40020060,0x2000,0x40020560,0x1000,0x40020560,};
340    }
341   final private JJCalls[] jj_2_rtns = new JJCalls[1];
342   private boolean jj_rescan = false;
343   private int jj_gc = 0;
344
345   public Parser(java.io.InputStream JavaDoc stream) {
346     jj_input_stream = new SimpleCharStream(stream, 1, 1);
347     token_source = new ParserTokenManager(jj_input_stream);
348     token = new Token();
349     jj_ntk = -1;
350     jj_gen = 0;
351     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
352     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
353   }
354
355   public void ReInit(java.io.InputStream JavaDoc stream) {
356     jj_input_stream.ReInit(stream, 1, 1);
357     token_source.ReInit(jj_input_stream);
358     token = new Token();
359     jj_ntk = -1;
360     jj_gen = 0;
361     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
362     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
363   }
364
365   public Parser(java.io.Reader JavaDoc stream) {
366     jj_input_stream = new SimpleCharStream(stream, 1, 1);
367     token_source = new ParserTokenManager(jj_input_stream);
368     token = new Token();
369     jj_ntk = -1;
370     jj_gen = 0;
371     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
372     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
373   }
374
375   public void ReInit(java.io.Reader JavaDoc stream) {
376     jj_input_stream.ReInit(stream, 1, 1);
377     token_source.ReInit(jj_input_stream);
378     token = new Token();
379     jj_ntk = -1;
380     jj_gen = 0;
381     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
382     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
383   }
384
385   public Parser(ParserTokenManager tm) {
386     token_source = tm;
387     token = new Token();
388     jj_ntk = -1;
389     jj_gen = 0;
390     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
391     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
392   }
393
394   public void ReInit(ParserTokenManager tm) {
395     token_source = tm;
396     token = new Token();
397     jj_ntk = -1;
398     jj_gen = 0;
399     for (int i = 0; i < 6; i++) jj_la1[i] = -1;
400     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
401   }
402
403   final private Token jj_consume_token(int kind) throws ParseException {
404     Token oldToken;
405     if ((oldToken = token).next != null) token = token.next;
406     else token = token.next = token_source.getNextToken();
407     jj_ntk = -1;
408     if (token.kind == kind) {
409       jj_gen++;
410       if (++jj_gc > 100) {
411         jj_gc = 0;
412         for (int i = 0; i < jj_2_rtns.length; i++) {
413           JJCalls c = jj_2_rtns[i];
414           while (c != null) {
415             if (c.gen < jj_gen) c.first = null;
416             c = c.next;
417           }
418         }
419       }
420       return token;
421     }
422     token = oldToken;
423     jj_kind = kind;
424     throw generateParseException();
425   }
426
427   static private final class LookaheadSuccess extends java.lang.Error JavaDoc { }
428   final private LookaheadSuccess jj_ls = new LookaheadSuccess();
429   final private boolean jj_scan_token(int kind) {
430     if (jj_scanpos == jj_lastpos) {
431       jj_la--;
432       if (jj_scanpos.next == null) {
433         jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
434       } else {
435         jj_lastpos = jj_scanpos = jj_scanpos.next;
436       }
437     } else {
438       jj_scanpos = jj_scanpos.next;
439     }
440     if (jj_rescan) {
441       int i = 0; Token tok = token;
442       while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
443       if (tok != null) jj_add_error_token(kind, i);
444     }
445     if (jj_scanpos.kind != kind) return true;
446     if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
447     return false;
448   }
449
450   final public Token getNextToken() {
451     if (token.next != null) token = token.next;
452     else token = token.next = token_source.getNextToken();
453     jj_ntk = -1;
454     jj_gen++;
455     return token;
456   }
457
458   final public Token getToken(int index) {
459     Token t = lookingAhead ? jj_scanpos : token;
460     for (int i = 0; i < index; i++) {
461       if (t.next != null) t = t.next;
462       else t = t.next = token_source.getNextToken();
463     }
464     return t;
465   }
466
467   final private int jj_ntk() {
468     if ((jj_nt=token.next) == null)
469       return (jj_ntk = (token.next=token_source.getNextToken()).kind);
470     else
471       return (jj_ntk = jj_nt.kind);
472   }
473
474   private java.util.Vector JavaDoc jj_expentries = new java.util.Vector JavaDoc();
475   private int[] jj_expentry;
476   private int jj_kind = -1;
477   private int[] jj_lasttokens = new int[100];
478   private int jj_endpos;
479
480   private void jj_add_error_token(int kind, int pos) {
481     if (pos >= 100) return;
482     if (pos == jj_endpos + 1) {
483       jj_lasttokens[jj_endpos++] = kind;
484     } else if (jj_endpos != 0) {
485       jj_expentry = new int[jj_endpos];
486       for (int i = 0; i < jj_endpos; i++) {
487         jj_expentry[i] = jj_lasttokens[i];
488       }
489       boolean exists = false;
490       for (java.util.Enumeration JavaDoc e = jj_expentries.elements(); e.hasMoreElements();) {
491         int[] oldentry = (int[])(e.nextElement());
492         if (oldentry.length == jj_expentry.length) {
493           exists = true;
494           for (int i = 0; i < jj_expentry.length; i++) {
495             if (oldentry[i] != jj_expentry[i]) {
496               exists = false;
497               break;
498             }
499           }
500           if (exists) break;
501         }
502       }
503       if (!exists) jj_expentries.addElement(jj_expentry);
504       if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
505     }
506   }
507
508   public ParseException generateParseException() {
509     jj_expentries.removeAllElements();
510     boolean[] la1tokens = new boolean[31];
511     for (int i = 0; i < 31; i++) {
512       la1tokens[i] = false;
513     }
514     if (jj_kind >= 0) {
515       la1tokens[jj_kind] = true;
516       jj_kind = -1;
517     }
518     for (int i = 0; i < 6; i++) {
519       if (jj_la1[i] == jj_gen) {
520         for (int j = 0; j < 32; j++) {
521           if ((jj_la1_0[i] & (1<<j)) != 0) {
522             la1tokens[j] = true;
523           }
524         }
525       }
526     }
527     for (int i = 0; i < 31; i++) {
528       if (la1tokens[i]) {
529         jj_expentry = new int[1];
530         jj_expentry[0] = i;
531         jj_expentries.addElement(jj_expentry);
532       }
533     }
534     jj_endpos = 0;
535     jj_rescan_token();
536     jj_add_error_token(0, 0);
537     int[][] exptokseq = new int[jj_expentries.size()][];
538     for (int i = 0; i < jj_expentries.size(); i++) {
539       exptokseq[i] = (int[])jj_expentries.elementAt(i);
540     }
541     return new ParseException(token, exptokseq, tokenImage);
542   }
543
544   final public void enable_tracing() {
545   }
546
547   final public void disable_tracing() {
548   }
549
550   final private void jj_rescan_token() {
551     jj_rescan = true;
552     for (int i = 0; i < 1; i++) {
553       JJCalls p = jj_2_rtns[i];
554       do {
555         if (p.gen > jj_gen) {
556           jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
557           switch (i) {
558             case 0: jj_3_1(); break;
559           }
560         }
561         p = p.next;
562       } while (p != null);
563     }
564     jj_rescan = false;
565   }
566
567   final private void jj_save(int index, int xla) {
568     JJCalls p = jj_2_rtns[index];
569     while (p.gen > jj_gen) {
570       if (p.next == null) { p = p.next = new JJCalls(); break; }
571       p = p.next;
572     }
573     p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
574   }
575
576   static final class JJCalls {
577     int gen;
578     Token first;
579     int arg;
580     JJCalls next;
581   }
582
583 }
584
Popular Tags