KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > util > ConfConfigurationParser


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  */

22
23 package org.enhydra.util;
24
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import com.lutris.util.ConfigException;
30 import com.lutris.util.ConfigParserConstants;
31 import com.lutris.util.ConfigParserTokenManager;
32 import com.lutris.util.ParseException;
33 import com.lutris.util.SimpleCharStream;
34 import com.lutris.util.Token;
35 import com.lutris.util.TokenMgrError;
36
37 public class ConfConfigurationParser implements ConfigParserConstants {
38
39     private String JavaDoc comments="";
40     private HashMap JavaDoc elements = null;
41     
42     public static void main(String JavaDoc args[])
43     throws ParseException, ConfigException
44     {
45         ConfConfigurationParser parser = new ConfConfigurationParser(System.in);
46         
47         try {
48           parser.elements = new HashMap JavaDoc();
49         }
50         catch (Exception JavaDoc e){
51 System.err.println(" Exception in main");
52           e.printStackTrace();
53         }
54         parser.process(parser.elements);
55  // elements.write(System.out);
56
}
57
58     private static String JavaDoc makeConfigString (String JavaDoc oldString) {
59       if (oldString == null)
60         return null;
61
62       try {
63         String JavaDoc newString = null;
64         newString = oldString.replaceAll("/", ".");
65         return newString;
66       }
67       catch (Exception JavaDoc ex){
68         System.err.println("Error in makeContextString method");
69         return null;
70       }
71     }
72
73     /**
74      * Makes Context String (with "/" delimiters).
75      *
76      * @param oldString String to be transformed.
77      * @return String in Context form.
78      */

79     private static String JavaDoc makeContextString (String JavaDoc oldString) {
80       if (oldString == null)
81         return null;
82
83       try {
84         String JavaDoc newString = null;
85         int len = oldString.length();
86         if (len >0)
87           newString = new String JavaDoc("");
88         for (int i=0; i<len; i++){
89             char ch = oldString.charAt(i);
90           if (ch == '.') {
91             newString = newString + "/";
92           }
93           else {
94             newString = newString + ch;
95           }
96         }
97         return newString;
98       }
99       catch (Exception JavaDoc ex){
100         System.err.println("Error in makeContextString method");
101         return null;
102       }
103     }
104
105     public void process(HashMap JavaDoc results)
106     throws ParseException, ConfigException
107     {
108         this.elements = results;
109         try {
110            ParseConfigFile();
111            
112         }
113         catch (TokenMgrError e) {
114             throw generateParseException();
115         }
116         catch (ParseException e) {
117             System.err.println("Syntax error in config file at line " +
118                 e.currentToken.next.beginLine +
119                 ", column " + e.currentToken.next.beginColumn + ".");
120             throw e;
121         }
122         
123     }
124     
125     private static final boolean isodigit(char c) {
126         switch (c) {
127             case '0': case '1': case '2': case '3':
128             case '4': case '5': case '6': case '7':
129                 return true;
130         }
131         return false;
132     }
133
134     private static final boolean isxdigit(char c) {
135     switch (c) {
136         case '0': case '1': case '2': case '3':
137         case '4': case '5': case '6': case '7':
138         case '8': case '9':
139         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
140         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
141         return true;
142     }
143     return false;
144     }
145
146 // PARSER SPECIFICATIONS BEGIN HERE
147
final public void ParseConfigFile() throws ParseException, ConfigException {
148             boolean done;
149     label_1:
150     while (true) {
151       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
152       case 0:
153       case KEYWORD:
154       case WHITESPACE:
155       case COMMENT:
156       case EOL:
157         ;
158         break;
159       default:
160         jj_la1[0] = jj_gen;
161         break label_1;
162       }
163       done = ConfigEntry();
164                 if (done) {if (true) return;}
165     }
166   }
167
168   final public boolean ConfigEntry() throws ParseException, ConfigException {
169             Token comment, kw, val, ws;
170             String JavaDoc keyword=null;
171             Vector JavaDoc vlist=null;
172             boolean isArray=false;
173     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
174     case WHITESPACE:
175       comment = jj_consume_token(WHITESPACE);
176                 comments += comment.toString();
177       break;
178     default:
179       jj_la1[1] = jj_gen;
180       ;
181     }
182     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
183     case EOL:
184       comment = jj_consume_token(EOL);
185                 comments += comment.toString();
186       break;
187     case COMMENT:
188       comment = jj_consume_token(COMMENT);
189                 comments += comment.toString();
190       break;
191     case KEYWORD:
192       kw = jj_consume_token(KEYWORD);
193       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
194       case WHITESPACE:
195         jj_consume_token(WHITESPACE);
196         break;
197       default:
198         jj_la1[2] = jj_gen;
199         ;
200       }
201       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
202       case 1:
203         isArray = arraySpec();
204         switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
205         case WHITESPACE:
206           jj_consume_token(WHITESPACE);
207           break;
208         default:
209           jj_la1[3] = jj_gen;
210           ;
211         }
212         break;
213       default:
214         jj_la1[4] = jj_gen;
215         ;
216       }
217       jj_consume_token(ASSIGN);
218       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
219       case WHITESPACE:
220         jj_consume_token(WHITESPACE);
221         break;
222       default:
223         jj_la1[5] = jj_gen;
224         ;
225       }
226       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
227       case QSTRING:
228       case STRING:
229         vlist = ValueList();
230         break;
231       default:
232         jj_la1[6] = jj_gen;
233         ;
234       }
235       jj_consume_token(EOL);
236                 if (vlist == null) {
237                     try {
238                         if (isArray) {
239 // configFile.addEntry(kw.image,
240
// new String[0], comments);
241
// elements.put(makeContextString(kw.image)+"[]", new String[0]);
242
//System.err.println("rebind 1= " + makeContextString(kw.image)+"[]");
243
elements.put(makeContextString(kw.image)+"[]", new String JavaDoc[0]);
244                         } else {
245 // configFile.addEntry(kw.image, "", comments);
246
// elements.put(makeContextString(kw.image), "");
247
//System.err.println("rebind 2= " + makeContextString(kw.image));
248
elements.put(makeContextString(kw.image), "");
249                         }
250                     } catch (Exception JavaDoc e) {
251                         e.printStackTrace();
252                         {if (true) throw new ConfigException(
253                             "Key " + kw.image + " is not valid.");}
254                     }
255                 } else {
256                     Enumeration JavaDoc en = vlist.elements();
257                     String JavaDoc[] values = new String JavaDoc[vlist.size()];
258                     int pos = values.length - 1;
259                     while (en.hasMoreElements()) {
260                         values[pos--] = (String JavaDoc) en.nextElement();
261                     }
262                     try {
263                         if (isArray) {
264 // configFile.addEntry(kw.image, values, comments);
265
// elements.put(makeContextString(kw.image)+"[]", jndiValues);
266
//System.err.println("rebind 3= " + makeContextString(kw.image)+"[]");
267
elements.put(makeContextString(kw.image)+"[]", values);
268
269                         } else {
270                             if (values.length != 1) {
271                                 {if (true) throw new ConfigException(
272                                     "Key " + kw.image +
273                                     " is not a list. Use " +
274                                     kw.image +"[]");}
275                             }
276 // configFile.addEntry(kw.image, values[0], comments);
277
// elements.put(makeContextString(kw.image), values[0]);
278
//System.err.println("rebind 4= " + makeContextString(kw.image));
279
elements.put(makeContextString(kw.image), values[0]);
280                         }
281                     } catch (Exception JavaDoc e) {
282                       e.printStackTrace();
283                         {if (true) throw new ConfigException(
284                             "Key " + kw.image + " is not valid.");}
285                     }
286                 }
287                 comments = "";
288       break;
289     case 0:
290       jj_consume_token(0);
291                 try {
292                     // FIX - should really be addTrailingComment
293
// configFile.addEntry(ConfigFile.TRAILING_COMMENT,
294
// (String[])null, comments);
295
} catch (Exception JavaDoc e) {
296                     System.err.println("ConfigFile unable to add entry for TRAILING_COMMENT.");
297                 }
298                 comments = "";
299                 {if (true) return true;} // EOF reached - finished parsing.
300

301       break;
302     default:
303       jj_la1[7] = jj_gen;
304       jj_consume_token(-1);
305       throw new ParseException();
306     }
307         {if (true) return false;} // Not done until EOF.
308

309     throw new Error JavaDoc("Missing return statement in function");
310   }
311
312   final public Vector JavaDoc ValueList() throws ParseException {
313             String JavaDoc sval=null;
314             Vector JavaDoc vlist=null;
315     sval = Value();
316     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
317     case COMMA:
318       jj_consume_token(COMMA);
319       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
320       case WHITESPACE:
321         jj_consume_token(WHITESPACE);
322         break;
323       default:
324         jj_la1[8] = jj_gen;
325         ;
326       }
327       vlist = ValueList();
328       break;
329     default:
330       jj_la1[9] = jj_gen;
331       ;
332     }
333         if (vlist == null) {
334             vlist = new Vector JavaDoc();
335             vlist.addElement(sval);
336         } else {
337             vlist.addElement(sval);
338         }
339         {if (true) return vlist;}
340     throw new Error JavaDoc("Missing return statement in function");
341   }
342
343   final public String JavaDoc Value() throws ParseException {
344             String JavaDoc lhs="", rhs="";
345     lhs = ValueFragment();
346     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
347     case PLUS:
348       jj_consume_token(PLUS);
349       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
350       case WHITESPACE:
351         jj_consume_token(WHITESPACE);
352         break;
353       default:
354         jj_la1[10] = jj_gen;
355         ;
356       }
357       rhs = Value();
358       break;
359     default:
360       jj_la1[11] = jj_gen;
361       ;
362     }
363             {if (true) return lhs + rhs;}
364     throw new Error JavaDoc("Missing return statement in function");
365   }
366
367   final public String JavaDoc ValueFragment() throws ParseException {
368             Token val;
369             String JavaDoc sval;
370     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
371     case QSTRING:
372       sval = QuotedString();
373       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
374       case WHITESPACE:
375         jj_consume_token(WHITESPACE);
376         break;
377       default:
378         jj_la1[12] = jj_gen;
379         ;
380       }
381             {if (true) return sval;}
382       break;
383     case STRING:
384       val = jj_consume_token(STRING);
385       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
386       case WHITESPACE:
387         jj_consume_token(WHITESPACE);
388         break;
389       default:
390         jj_la1[13] = jj_gen;
391         ;
392       }
393             {if (true) return val.image;}
394       break;
395     default:
396       jj_la1[14] = jj_gen;
397       jj_consume_token(-1);
398       throw new ParseException();
399     }
400     throw new Error JavaDoc("Missing return statement in function");
401   }
402
403   final public boolean arraySpec() throws ParseException {
404             String JavaDoc lhs="", rhs="";
405     jj_consume_token(1);
406     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
407     case WHITESPACE:
408       jj_consume_token(WHITESPACE);
409       break;
410     default:
411       jj_la1[15] = jj_gen;
412       ;
413     }
414     jj_consume_token(2);
415             {if (true) return true;}
416     throw new Error JavaDoc("Missing return statement in function");
417   }
418
419   final public String JavaDoc QuotedString() throws ParseException {
420             Token qs;
421             StringBuffer JavaDoc sbuf;
422             int i;
423             char[] cbuf;
424     qs = jj_consume_token(QSTRING);
425         sbuf = new StringBuffer JavaDoc();
426         cbuf = qs.image.toCharArray();
427         for (i=1; i < (cbuf.length - 1); i++) {
428             switch (cbuf[i]) {
429                 case '\\':
430                     switch (cbuf[i+1]) {
431                         case 'n':
432                             sbuf.append("\n"); i += 1; break;
433                         case 't':
434                             sbuf.append("\t"); i += 1; break;
435                         case 'b':
436                             sbuf.append("\b"); i += 1; break;
437                         case 'r':
438                             sbuf.append("\r"); i += 1; break;
439                         case 'f':
440                             sbuf.append("\f"); i += 1; break;
441                         case 'u':
442                             if ((cbuf.length - i >= 6) &&
443                                 (isxdigit(cbuf[i+2])) &&
444                                 (isxdigit(cbuf[i+3])) &&
445                                 (isxdigit(cbuf[i+4])) &&
446                                 (isxdigit(cbuf[i+5]))) {
447                                 try {
448                                     String JavaDoc s = new String JavaDoc(cbuf, i+2, 4);
449                                     int val = Integer.parseInt(s, 16);
450                                     sbuf.append((char)val);
451                                     i += 5;
452                                 } catch (Throwable JavaDoc t) {
453                                     sbuf.append(cbuf[i+1]);
454                                     i += 1;
455                                 }
456                             } else {
457                                 sbuf.append(cbuf[i+1]);
458                                 i += 1;
459                             }
460                             break;
461                         case 'x':
462                             if ((cbuf.length - i >= 4) &&
463                                 (isxdigit(cbuf[i+2])) &&
464                                 (isxdigit(cbuf[i+3]))) {
465                                 try {
466                                     String JavaDoc s = new String JavaDoc(cbuf, i+2, 2);
467                                     int val = Integer.parseInt(s, 16);
468                                     sbuf.append((char)val);
469                                     i += 3;
470                                 } catch (Throwable JavaDoc t) {
471                                     sbuf.append(cbuf[i+1]);
472                                     i += 1;
473                                 }
474                             } else {
475                                 sbuf.append(cbuf[i+1]);
476                                 i += 1;
477                             }
478                             break;
479                         default:
480                             if ((cbuf.length - i >= 4) &&
481                                 (isodigit(cbuf[i+1])) &&
482                                 (isodigit(cbuf[i+2])) &&
483                                 (isodigit(cbuf[i+3]))) {
484                                 try {
485                                     String JavaDoc s = new String JavaDoc(cbuf, i+1, 3);
486                                     int val = Integer.parseInt(s, 8);
487                                     sbuf.append((char)val);
488                                     i += 3;
489                                 } catch (Throwable JavaDoc t) {
490                                     sbuf.append(cbuf[i+1]);
491                                     i += 1;
492                                 }
493                             } else {
494                                 sbuf.append(cbuf[i+1]);
495                                 i += 1;
496                             }
497                             break;
498                     } // inner switch
499
break;
500                 default:
501                     sbuf.append(cbuf[i]);
502                     break;
503             } // outer switch
504
} // outer for
505
{if (true) return sbuf.toString();}
506     throw new Error JavaDoc("Missing return statement in function");
507   }
508
509   public ConfigParserTokenManager token_source;
510   SimpleCharStream jj_input_stream;
511   public Token token, jj_nt;
512   private int jj_ntk;
513   private int jj_gen;
514   final private int[] jj_la1 = new int[16];
515   static private int[] jj_la1_0;
516   static {
517       jj_la1_0();
518    }
519    private static void jj_la1_0() {
520       jj_la1_0 = new int[] {0x171,0x20,0x20,0x20,0x2,0x20,0x600,0x151,0x20,0x2000,0x20,0x1000,0x20,0x20,0x600,0x20,};
521    }
522
523   public ConfConfigurationParser(java.io.InputStream JavaDoc stream) {
524     jj_input_stream = new SimpleCharStream(stream, 1, 1);
525     token_source = new ConfigParserTokenManager(jj_input_stream);
526     token = new Token();
527     jj_ntk = -1;
528     jj_gen = 0;
529     for (int i = 0; i < 16; i++) jj_la1[i] = -1;
530   }
531
532   public void ReInit(java.io.InputStream JavaDoc stream) {
533     jj_input_stream.ReInit(stream, 1, 1);
534     token_source.ReInit(jj_input_stream);
535     token = new Token();
536     jj_ntk = -1;
537     jj_gen = 0;
538     for (int i = 0; i < 16; i++) jj_la1[i] = -1;
539   }
540
541   public ConfConfigurationParser(java.io.Reader JavaDoc stream) {
542     jj_input_stream = new SimpleCharStream(stream, 1, 1);
543     token_source = new ConfigParserTokenManager(jj_input_stream);
544     token = new Token();
545     jj_ntk = -1;
546     jj_gen = 0;
547     for (int i = 0; i < 16; i++) jj_la1[i] = -1;
548   }
549
550   public void ReInit(java.io.Reader JavaDoc stream) {
551     jj_input_stream.ReInit(stream, 1, 1);
552     token_source.ReInit(jj_input_stream);
553     token = new Token();
554     jj_ntk = -1;
555     jj_gen = 0;
556     for (int i = 0; i < 16; i++) jj_la1[i] = -1;
557   }
558
559   public ConfConfigurationParser(ConfigParserTokenManager tm) {
560     token_source = tm;
561     token = new Token();
562     jj_ntk = -1;
563     jj_gen = 0;
564     for (int i = 0; i < 16; i++) jj_la1[i] = -1;
565   }
566
567   public void ReInit(ConfigParserTokenManager tm) {
568     token_source = tm;
569     token = new Token();
570     jj_ntk = -1;
571     jj_gen = 0;
572     for (int i = 0; i < 16; i++) jj_la1[i] = -1;
573   }
574
575   final private Token jj_consume_token(int kind) throws ParseException {
576     Token oldToken;
577     if ((oldToken = token).next != null) token = token.next;
578     else token = token.next = token_source.getNextToken();
579     jj_ntk = -1;
580     if (token.kind == kind) {
581       jj_gen++;
582       return token;
583     }
584     token = oldToken;
585     jj_kind = kind;
586     throw generateParseException();
587   }
588
589   final public Token getNextToken() {
590     if (token.next != null) token = token.next;
591     else token = token.next = token_source.getNextToken();
592     jj_ntk = -1;
593     jj_gen++;
594     return token;
595   }
596
597   final public Token getToken(int index) {
598     Token t = token;
599     for (int i = 0; i < index; i++) {
600       if (t.next != null) t = t.next;
601       else t = t.next = token_source.getNextToken();
602     }
603     return t;
604   }
605
606   final private int jj_ntk() {
607     if ((jj_nt=token.next) == null)
608       return (jj_ntk = (token.next=token_source.getNextToken()).kind);
609     else
610       return (jj_ntk = jj_nt.kind);
611   }
612
613   private java.util.Vector JavaDoc jj_expentries = new java.util.Vector JavaDoc();
614   private int[] jj_expentry;
615   private int jj_kind = -1;
616
617   public ParseException generateParseException() {
618     jj_expentries.removeAllElements();
619     boolean[] la1tokens = new boolean[16];
620     for (int i = 0; i < 16; i++) {
621       la1tokens[i] = false;
622     }
623     if (jj_kind >= 0) {
624       la1tokens[jj_kind] = true;
625       jj_kind = -1;
626     }
627     for (int i = 0; i < 16; i++) {
628       if (jj_la1[i] == jj_gen) {
629         for (int j = 0; j < 32; j++) {
630           if ((jj_la1_0[i] & (1<<j)) != 0) {
631             la1tokens[j] = true;
632           }
633         }
634       }
635     }
636     for (int i = 0; i < 16; i++) {
637       if (la1tokens[i]) {
638         jj_expentry = new int[1];
639         jj_expentry[0] = i;
640         jj_expentries.addElement(jj_expentry);
641       }
642     }
643     int[][] exptokseq = new int[jj_expentries.size()][];
644     for (int i = 0; i < jj_expentries.size(); i++) {
645       exptokseq[i] = (int[])jj_expentries.elementAt(i);
646     }
647     return new ParseException(token, exptokseq, tokenImage);
648   }
649
650   final public void enable_tracing() {
651   }
652
653   final public void disable_tracing() {
654   }
655
656 }
657
658
659 //class ConfigElement {
660
/**
661      * Comments for this element.
662      */

663     //public String comments;
664

665
666     /**
667      * Key name for this key.
668      */

669     //public String key;
670

671     /**
672      * Array of zero or more values assigned to key.
673      */

674     //public String[] values;
675
//}
676
Popular Tags