KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > spi > conf > util > ConfigToContextParser


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.spi.conf.util;
24
25 import java.io.*;
26 import java.util.*;
27 import javax.naming.*;
28
29 public class ConfigToContextParser implements ConfigParserConstants {
30
31     private String JavaDoc comments="";
32 // private Hashtable elements = null;
33
private Context elements = null;
34
35     public static void main(String JavaDoc args[])
36     throws ParseException, ConfigException
37     {
38         ConfigToContextParser parser = new ConfigToContextParser(System.in);
39         Context elements = null;
40         try {
41           elements = new InitialContext();
42         }
43         catch (Exception JavaDoc e){}
44         parser.process(elements);
45  // elements.write(System.out);
46
}
47
48     private static String JavaDoc makeConfigtString (String JavaDoc oldString) {
49       if (oldString == null)
50         return null;
51
52       try {
53         String JavaDoc newString = null;
54         newString = oldString.replaceAll("/", ".");
55         return newString;
56       }
57       catch (Exception JavaDoc ex){
58         System.err.println("Error in makeContextString method");
59         return null;
60       }
61     }
62
63     /**
64      * Makes Context String (with "/" delimiters).
65      *
66      * @param oldString String to be transformed.
67      * @return String in Context form.
68      */

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

289       break;
290     default:
291       jj_la1[7] = jj_gen;
292       jj_consume_token(-1);
293       throw new ParseException();
294     }
295         {if (true) return false;} // Not done until EOF.
296

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

650     public String JavaDoc comments;
651
652
653     /**
654      * Key name for this key.
655      */

656     public String JavaDoc key;
657
658     /**
659      * Array of zero or more values assigned to key.
660      */

661     public String JavaDoc[] values;
662 }
663
Popular Tags