View Javadoc

1   /* Generated By:JavaCC: Do not edit this line. ServiceFileParser.java */
2   /*
3    *    Copyright 2010-2011 The 99 Software Foundation
4    *
5    *    Licensed under the Apache License, Version 2.0 (the "License");
6    *    you may not use this file except in compliance with the License.
7    *    You may obtain a copy of the License at
8    *
9    *       http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *    Unless required by applicable law or agreed to in writing, software
12   *    distributed under the License is distributed on an "AS IS" BASIS,
13   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *    See the License for the specific language governing permissions and
15   *    limitations under the License.
16   */
17  package org.nnsoft.commons.bspi;
18  
19  import java.util.Iterator;
20  import java.util.LinkedHashMap;
21  import java.util.LinkedHashSet;
22  import java.util.Set;
23  
24  final class ServiceFileParser<S>
25      implements Iterable<String>, ServiceFileParserConstants {
26  
27      private final Set<String> serviceClassNames = new LinkedHashSet<String>();
28  
29      private LinkedHashMap<String, Class<? extends S>> providerTypes;
30  
31      public void setProviderTypes( LinkedHashMap<String, Class<? extends S>> providerTypes )
32      {
33          this.providerTypes = providerTypes;
34      }
35  
36      public Iterator<String> iterator()
37      {
38          return serviceClassNames.iterator();
39      }
40  
41    final public void parse() throws ParseException {
42      label_1:
43      while (true) {
44        if (jj_2_1(2)) {
45          ;
46        } else {
47          break label_1;
48        }
49        serviceName();
50      }
51      jj_consume_token(0);
52    }
53  
54    final private void serviceName() throws ParseException {
55                                Token serviceClassToken;
56      serviceClassToken = jj_consume_token(QUALIFIED_NAME);
57          String serviceClassName = serviceClassToken.toString();
58          if ( !this.providerTypes.containsKey( serviceClassName ) )
59          {
60              serviceClassNames.add( serviceClassName );
61          }
62    }
63  
64    private boolean jj_2_1(int xla) {
65      jj_la = xla; jj_lastpos = jj_scanpos = token;
66      try { return !jj_3_1(); }
67      catch(LookaheadSuccess ls) { return true; }
68      finally { jj_save(0, xla); }
69    }
70  
71    private boolean jj_3_1() {
72      if (jj_3R_2()) return true;
73      return false;
74    }
75  
76    private boolean jj_3R_2() {
77      if (jj_scan_token(QUALIFIED_NAME)) return true;
78      return false;
79    }
80  
81    /** Generated Token Manager. */
82    public ServiceFileParserTokenManager token_source;
83    SimpleCharStream jj_input_stream;
84    /** Current token. */
85    public Token token;
86    /** Next token. */
87    public Token jj_nt;
88    private int jj_ntk;
89    private Token jj_scanpos, jj_lastpos;
90    private int jj_la;
91    private int jj_gen;
92    final private int[] jj_la1 = new int[0];
93    static private int[] jj_la1_0;
94    static {
95        jj_la1_init_0();
96     }
97     private static void jj_la1_init_0() {
98        jj_la1_0 = new int[] {};
99     }
100   final private JJCalls[] jj_2_rtns = new JJCalls[1];
101   private boolean jj_rescan = false;
102   private int jj_gc = 0;
103 
104   /** Constructor with InputStream. */
105   public ServiceFileParser(java.io.InputStream stream) {
106      this(stream, null);
107   }
108   /** Constructor with InputStream and supplied encoding */
109   public ServiceFileParser(java.io.InputStream stream, String encoding) {
110     try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
111     token_source = new ServiceFileParserTokenManager(jj_input_stream);
112     token = new Token();
113     jj_ntk = -1;
114     jj_gen = 0;
115     for (int i = 0; i < 0; i++) jj_la1[i] = -1;
116     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
117   }
118 
119   /** Reinitialise. */
120   public void ReInit(java.io.InputStream stream) {
121      ReInit(stream, null);
122   }
123   /** Reinitialise. */
124   public void ReInit(java.io.InputStream stream, String encoding) {
125     try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
126     token_source.ReInit(jj_input_stream);
127     token = new Token();
128     jj_ntk = -1;
129     jj_gen = 0;
130     for (int i = 0; i < 0; i++) jj_la1[i] = -1;
131     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
132   }
133 
134   /** Constructor. */
135   public ServiceFileParser(java.io.Reader stream) {
136     jj_input_stream = new SimpleCharStream(stream, 1, 1);
137     token_source = new ServiceFileParserTokenManager(jj_input_stream);
138     token = new Token();
139     jj_ntk = -1;
140     jj_gen = 0;
141     for (int i = 0; i < 0; i++) jj_la1[i] = -1;
142     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
143   }
144 
145   /** Reinitialise. */
146   public void ReInit(java.io.Reader stream) {
147     jj_input_stream.ReInit(stream, 1, 1);
148     token_source.ReInit(jj_input_stream);
149     token = new Token();
150     jj_ntk = -1;
151     jj_gen = 0;
152     for (int i = 0; i < 0; i++) jj_la1[i] = -1;
153     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
154   }
155 
156   /** Constructor with generated Token Manager. */
157   public ServiceFileParser(ServiceFileParserTokenManager tm) {
158     token_source = tm;
159     token = new Token();
160     jj_ntk = -1;
161     jj_gen = 0;
162     for (int i = 0; i < 0; i++) jj_la1[i] = -1;
163     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
164   }
165 
166   /** Reinitialise. */
167   public void ReInit(ServiceFileParserTokenManager tm) {
168     token_source = tm;
169     token = new Token();
170     jj_ntk = -1;
171     jj_gen = 0;
172     for (int i = 0; i < 0; i++) jj_la1[i] = -1;
173     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
174   }
175 
176   private Token jj_consume_token(int kind) throws ParseException {
177     Token oldToken;
178     if ((oldToken = token).next != null) token = token.next;
179     else token = token.next = token_source.getNextToken();
180     jj_ntk = -1;
181     if (token.kind == kind) {
182       jj_gen++;
183       if (++jj_gc > 100) {
184         jj_gc = 0;
185         for (int i = 0; i < jj_2_rtns.length; i++) {
186           JJCalls c = jj_2_rtns[i];
187           while (c != null) {
188             if (c.gen < jj_gen) c.first = null;
189             c = c.next;
190           }
191         }
192       }
193       return token;
194     }
195     token = oldToken;
196     jj_kind = kind;
197     throw generateParseException();
198   }
199 
200   static private final class LookaheadSuccess extends java.lang.Error { }
201   final private LookaheadSuccess jj_ls = new LookaheadSuccess();
202   private boolean jj_scan_token(int kind) {
203     if (jj_scanpos == jj_lastpos) {
204       jj_la--;
205       if (jj_scanpos.next == null) {
206         jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
207       } else {
208         jj_lastpos = jj_scanpos = jj_scanpos.next;
209       }
210     } else {
211       jj_scanpos = jj_scanpos.next;
212     }
213     if (jj_rescan) {
214       int i = 0; Token tok = token;
215       while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
216       if (tok != null) jj_add_error_token(kind, i);
217     }
218     if (jj_scanpos.kind != kind) return true;
219     if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
220     return false;
221   }
222 
223 
224 /** Get the next Token. */
225   final public Token getNextToken() {
226     if (token.next != null) token = token.next;
227     else token = token.next = token_source.getNextToken();
228     jj_ntk = -1;
229     jj_gen++;
230     return token;
231   }
232 
233 /** Get the specific Token. */
234   final public Token getToken(int index) {
235     Token t = token;
236     for (int i = 0; i < index; i++) {
237       if (t.next != null) t = t.next;
238       else t = t.next = token_source.getNextToken();
239     }
240     return t;
241   }
242 
243   private int jj_ntk() {
244     if ((jj_nt=token.next) == null)
245       return (jj_ntk = (token.next=token_source.getNextToken()).kind);
246     else
247       return (jj_ntk = jj_nt.kind);
248   }
249 
250   private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
251   private int[] jj_expentry;
252   private int jj_kind = -1;
253   private int[] jj_lasttokens = new int[100];
254   private int jj_endpos;
255 
256   private void jj_add_error_token(int kind, int pos) {
257     if (pos >= 100) return;
258     if (pos == jj_endpos + 1) {
259       jj_lasttokens[jj_endpos++] = kind;
260     } else if (jj_endpos != 0) {
261       jj_expentry = new int[jj_endpos];
262       for (int i = 0; i < jj_endpos; i++) {
263         jj_expentry[i] = jj_lasttokens[i];
264       }
265       jj_entries_loop: for (java.util.Iterator<?> it = jj_expentries.iterator(); it.hasNext();) {
266         int[] oldentry = (int[])(it.next());
267         if (oldentry.length == jj_expentry.length) {
268           for (int i = 0; i < jj_expentry.length; i++) {
269             if (oldentry[i] != jj_expentry[i]) {
270               continue jj_entries_loop;
271             }
272           }
273           jj_expentries.add(jj_expentry);
274           break jj_entries_loop;
275         }
276       }
277       if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
278     }
279   }
280 
281   /** Generate ParseException. */
282   public ParseException generateParseException() {
283     jj_expentries.clear();
284     boolean[] la1tokens = new boolean[13];
285     if (jj_kind >= 0) {
286       la1tokens[jj_kind] = true;
287       jj_kind = -1;
288     }
289     for (int i = 0; i < 0; i++) {
290       if (jj_la1[i] == jj_gen) {
291         for (int j = 0; j < 32; j++) {
292           if ((jj_la1_0[i] & (1<<j)) != 0) {
293             la1tokens[j] = true;
294           }
295         }
296       }
297     }
298     for (int i = 0; i < 13; i++) {
299       if (la1tokens[i]) {
300         jj_expentry = new int[1];
301         jj_expentry[0] = i;
302         jj_expentries.add(jj_expentry);
303       }
304     }
305     jj_endpos = 0;
306     jj_rescan_token();
307     jj_add_error_token(0, 0);
308     int[][] exptokseq = new int[jj_expentries.size()][];
309     for (int i = 0; i < jj_expentries.size(); i++) {
310       exptokseq[i] = jj_expentries.get(i);
311     }
312     return new ParseException(token, exptokseq, tokenImage);
313   }
314 
315   /** Enable tracing. */
316   final public void enable_tracing() {
317   }
318 
319   /** Disable tracing. */
320   final public void disable_tracing() {
321   }
322 
323   private void jj_rescan_token() {
324     jj_rescan = true;
325     for (int i = 0; i < 1; i++) {
326     try {
327       JJCalls p = jj_2_rtns[i];
328       do {
329         if (p.gen > jj_gen) {
330           jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
331           switch (i) {
332             case 0: jj_3_1(); break;
333           }
334         }
335         p = p.next;
336       } while (p != null);
337       } catch(LookaheadSuccess ls) { }
338     }
339     jj_rescan = false;
340   }
341 
342   private void jj_save(int index, int xla) {
343     JJCalls p = jj_2_rtns[index];
344     while (p.gen > jj_gen) {
345       if (p.next == null) { p = p.next = new JJCalls(); break; }
346       p = p.next;
347     }
348     p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
349   }
350 
351   static final class JJCalls {
352     int gen;
353     Token first;
354     int arg;
355     JJCalls next;
356   }
357 
358 }