001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: */
022:
023: package org.enhydra.spi.conf.util;
024:
025: import java.io.*;
026: import java.util.*;
027: import javax.naming.*;
028:
029: public class ConfigToContextParser implements ConfigParserConstants {
030:
031: private String comments = "";
032: // private Hashtable elements = null;
033: private Context elements = null;
034:
035: public static void main(String args[]) throws ParseException,
036: ConfigException {
037: ConfigToContextParser parser = new ConfigToContextParser(
038: System.in);
039: Context elements = null;
040: try {
041: elements = new InitialContext();
042: } catch (Exception e) {
043: }
044: parser.process(elements);
045: // elements.write(System.out);
046: }
047:
048: private static String makeConfigtString(String oldString) {
049: if (oldString == null)
050: return null;
051:
052: try {
053: String newString = null;
054: newString = oldString.replaceAll("/", ".");
055: return newString;
056: } catch (Exception ex) {
057: System.err.println("Error in makeContextString method");
058: return null;
059: }
060: }
061:
062: /**
063: * Makes Context String (with "/" delimiters).
064: *
065: * @param oldString String to be transformed.
066: * @return String in Context form.
067: */
068: private static String makeContextString(String oldString) {
069: if (oldString == null)
070: return null;
071:
072: try {
073: String newString = null;
074: int len = oldString.length();
075: if (len > 0)
076: newString = new String("");
077: for (int i = 0; i < len; i++) {
078: char ch = oldString.charAt(i);
079: if (ch == '.') {
080: newString = newString + "/";
081: } else {
082: newString = newString + ch;
083: }
084: }
085: return newString;
086: } catch (Exception ex) {
087: System.err.println("Error in makeContextString method");
088: return null;
089: }
090: }
091:
092: public void process(Context con) throws ParseException,
093: ConfigException {
094: this .elements = con;
095: try {
096: ParseConfigFile();
097: } catch (TokenMgrError e) {
098: throw generateParseException();
099: } catch (ParseException e) {
100: System.err.println("Syntax error in config file at line "
101: + e.currentToken.next.beginLine + ", column "
102: + e.currentToken.next.beginColumn + ".");
103: throw e;
104: }
105: }
106:
107: private static final boolean isodigit(char c) {
108: switch (c) {
109: case '0':
110: case '1':
111: case '2':
112: case '3':
113: case '4':
114: case '5':
115: case '6':
116: 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':
125: case '1':
126: case '2':
127: case '3':
128: case '4':
129: case '5':
130: case '6':
131: case '7':
132: case '8':
133: case '9':
134: case 'a':
135: case 'b':
136: case 'c':
137: case 'd':
138: case 'e':
139: case 'f':
140: case 'A':
141: case 'B':
142: case 'C':
143: case 'D':
144: case 'E':
145: case 'F':
146: return true;
147: }
148: return false;
149: }
150:
151: // PARSER SPECIFICATIONS BEGIN HERE
152: final public void ParseConfigFile() throws ParseException,
153: ConfigException {
154: boolean done;
155: label_1: while (true) {
156: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
157: case 0:
158: case KEYWORD:
159: case WHITESPACE:
160: case COMMENT:
161: case EOL:
162: ;
163: break;
164: default:
165: jj_la1[0] = jj_gen;
166: break label_1;
167: }
168: done = ConfigEntry();
169: if (done) {
170: if (true)
171: return;
172: }
173: }
174: }
175:
176: final public boolean ConfigEntry() throws ParseException,
177: ConfigException {
178: Token comment, kw, val, ws;
179: String keyword = null;
180: Vector vlist = null;
181: boolean isArray = false;
182: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
183: case WHITESPACE:
184: comment = jj_consume_token(WHITESPACE);
185: comments += comment.toString();
186: break;
187: default:
188: jj_la1[1] = jj_gen;
189: ;
190: }
191: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
192: case EOL:
193: comment = jj_consume_token(EOL);
194: comments += comment.toString();
195: break;
196: case COMMENT:
197: comment = jj_consume_token(COMMENT);
198: comments += comment.toString();
199: break;
200: case KEYWORD:
201: kw = jj_consume_token(KEYWORD);
202: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
203: case WHITESPACE:
204: jj_consume_token(WHITESPACE);
205: break;
206: default:
207: jj_la1[2] = jj_gen;
208: ;
209: }
210: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
211: case 1:
212: isArray = arraySpec();
213: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
214: case WHITESPACE:
215: jj_consume_token(WHITESPACE);
216: break;
217: default:
218: jj_la1[3] = jj_gen;
219: ;
220: }
221: break;
222: default:
223: jj_la1[4] = jj_gen;
224: ;
225: }
226: jj_consume_token(ASSIGN);
227: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
228: case WHITESPACE:
229: jj_consume_token(WHITESPACE);
230: break;
231: default:
232: jj_la1[5] = jj_gen;
233: ;
234: }
235: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
236: case QSTRING:
237: case STRING:
238: vlist = ValueList();
239: break;
240: default:
241: jj_la1[6] = jj_gen;
242: ;
243: }
244: jj_consume_token(EOL);
245: if (vlist == null) {
246: try {
247: if (isArray) {
248: // configFile.addEntry(kw.image,
249: // new String[0], comments);
250: // elements.put(makeContextString(kw.image)+"[]", new String[0]);
251: elements.rebind(makeContextString(kw.image)
252: + "[]", new String[0]);
253: } else {
254: // configFile.addEntry(kw.image, "", comments);
255: // elements.put(makeContextString(kw.image), "");
256: elements
257: .rebind(makeContextString(kw.image), "");
258: }
259: } catch (Exception e) {
260: {
261: if (true)
262: throw new ConfigException("Key " + kw.image
263: + " is not valid.");
264: }
265: }
266: } else {
267: Enumeration en = vlist.elements();
268: String[] values = new String[vlist.size()];
269: int pos = values.length - 1;
270: while (en.hasMoreElements()) {
271: values[pos--] = (String) en.nextElement();
272: }
273: try {
274: if (isArray) {
275: // configFile.addEntry(kw.image, values, comments);
276: String jndiValues = values[0];
277: if (values.length > 1)
278: for (int i = 1; i < values.length; i++) {
279: jndiValues = jndiValues + ","
280: + values[i];
281: }
282: // elements.put(makeContextString(kw.image)+"[]", jndiValues);
283: elements.rebind(makeContextString(kw.image)
284: + "[]", jndiValues);
285:
286: } else {
287: if (values.length != 1) {
288: {
289: if (true)
290: throw new ConfigException("Key "
291: + kw.image
292: + " is not a list. Use "
293: + kw.image + "[]");
294: }
295: }
296: // configFile.addEntry(kw.image, values[0], comments);
297: // elements.put(makeContextString(kw.image), values[0]);
298: elements.rebind(makeContextString(kw.image),
299: values[0]);
300: }
301: } catch (Exception e) {
302: e.printStackTrace();
303: {
304: if (true)
305: throw new ConfigException("Key " + kw.image
306: + " is not valid.");
307: }
308: }
309: }
310: comments = "";
311: break;
312: case 0:
313: jj_consume_token(0);
314: try {
315: // FIX - should really be addTrailingComment
316: // configFile.addEntry(ConfigFile.TRAILING_COMMENT,
317: // (String[])null, comments);
318: } catch (Exception e) {
319: System.err
320: .println("ConfigFile unable to add entry for TRAILING_COMMENT.");
321: }
322: comments = "";
323: {
324: if (true)
325: return true;
326: } // EOF reached - finished parsing.
327:
328: break;
329: default:
330: jj_la1[7] = jj_gen;
331: jj_consume_token(-1);
332: throw new ParseException();
333: }
334: {
335: if (true)
336: return false;
337: } // Not done until EOF.
338:
339: throw new Error("Missing return statement in function");
340: }
341:
342: final public Vector ValueList() throws ParseException {
343: String sval = null;
344: Vector vlist = null;
345: sval = Value();
346: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
347: case COMMA:
348: jj_consume_token(COMMA);
349: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
350: case WHITESPACE:
351: jj_consume_token(WHITESPACE);
352: break;
353: default:
354: jj_la1[8] = jj_gen;
355: ;
356: }
357: vlist = ValueList();
358: break;
359: default:
360: jj_la1[9] = jj_gen;
361: ;
362: }
363: if (vlist == null) {
364: vlist = new Vector();
365: vlist.addElement(sval);
366: } else {
367: vlist.addElement(sval);
368: }
369: {
370: if (true)
371: return vlist;
372: }
373: throw new Error("Missing return statement in function");
374: }
375:
376: final public String Value() throws ParseException {
377: String lhs = "", rhs = "";
378: lhs = ValueFragment();
379: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
380: case PLUS:
381: jj_consume_token(PLUS);
382: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
383: case WHITESPACE:
384: jj_consume_token(WHITESPACE);
385: break;
386: default:
387: jj_la1[10] = jj_gen;
388: ;
389: }
390: rhs = Value();
391: break;
392: default:
393: jj_la1[11] = jj_gen;
394: ;
395: }
396: {
397: if (true)
398: return lhs + rhs;
399: }
400: throw new Error("Missing return statement in function");
401: }
402:
403: final public String ValueFragment() throws ParseException {
404: Token val;
405: String sval;
406: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
407: case QSTRING:
408: sval = QuotedString();
409: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
410: case WHITESPACE:
411: jj_consume_token(WHITESPACE);
412: break;
413: default:
414: jj_la1[12] = jj_gen;
415: ;
416: }
417: {
418: if (true)
419: return sval;
420: }
421: break;
422: case STRING:
423: val = jj_consume_token(STRING);
424: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
425: case WHITESPACE:
426: jj_consume_token(WHITESPACE);
427: break;
428: default:
429: jj_la1[13] = jj_gen;
430: ;
431: }
432: {
433: if (true)
434: return val.image;
435: }
436: break;
437: default:
438: jj_la1[14] = jj_gen;
439: jj_consume_token(-1);
440: throw new ParseException();
441: }
442: throw new Error("Missing return statement in function");
443: }
444:
445: final public boolean arraySpec() throws ParseException {
446: String lhs = "", rhs = "";
447: jj_consume_token(1);
448: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
449: case WHITESPACE:
450: jj_consume_token(WHITESPACE);
451: break;
452: default:
453: jj_la1[15] = jj_gen;
454: ;
455: }
456: jj_consume_token(2);
457: {
458: if (true)
459: return true;
460: }
461: throw new Error("Missing return statement in function");
462: }
463:
464: final public String QuotedString() throws ParseException {
465: Token qs;
466: StringBuffer sbuf;
467: int i;
468: char[] cbuf;
469: qs = jj_consume_token(QSTRING);
470: sbuf = new StringBuffer();
471: cbuf = qs.image.toCharArray();
472: for (i = 1; i < (cbuf.length - 1); i++) {
473: switch (cbuf[i]) {
474: case '\\':
475: switch (cbuf[i + 1]) {
476: case 'n':
477: sbuf.append("\n");
478: i += 1;
479: break;
480: case 't':
481: sbuf.append("\t");
482: i += 1;
483: break;
484: case 'b':
485: sbuf.append("\b");
486: i += 1;
487: break;
488: case 'r':
489: sbuf.append("\r");
490: i += 1;
491: break;
492: case 'f':
493: sbuf.append("\f");
494: i += 1;
495: break;
496: case 'u':
497: if ((cbuf.length - i >= 6)
498: && (isxdigit(cbuf[i + 2]))
499: && (isxdigit(cbuf[i + 3]))
500: && (isxdigit(cbuf[i + 4]))
501: && (isxdigit(cbuf[i + 5]))) {
502: try {
503: String s = new String(cbuf, i + 2, 4);
504: int val = Integer.parseInt(s, 16);
505: sbuf.append((char) val);
506: i += 5;
507: } catch (Throwable t) {
508: sbuf.append(cbuf[i + 1]);
509: i += 1;
510: }
511: } else {
512: sbuf.append(cbuf[i + 1]);
513: i += 1;
514: }
515: break;
516: case 'x':
517: if ((cbuf.length - i >= 4)
518: && (isxdigit(cbuf[i + 2]))
519: && (isxdigit(cbuf[i + 3]))) {
520: try {
521: String s = new String(cbuf, i + 2, 2);
522: int val = Integer.parseInt(s, 16);
523: sbuf.append((char) val);
524: i += 3;
525: } catch (Throwable t) {
526: sbuf.append(cbuf[i + 1]);
527: i += 1;
528: }
529: } else {
530: sbuf.append(cbuf[i + 1]);
531: i += 1;
532: }
533: break;
534: default:
535: if ((cbuf.length - i >= 4)
536: && (isodigit(cbuf[i + 1]))
537: && (isodigit(cbuf[i + 2]))
538: && (isodigit(cbuf[i + 3]))) {
539: try {
540: String s = new String(cbuf, i + 1, 3);
541: int val = Integer.parseInt(s, 8);
542: sbuf.append((char) val);
543: i += 3;
544: } catch (Throwable t) {
545: sbuf.append(cbuf[i + 1]);
546: i += 1;
547: }
548: } else {
549: sbuf.append(cbuf[i + 1]);
550: i += 1;
551: }
552: break;
553: } // inner switch
554: break;
555: default:
556: sbuf.append(cbuf[i]);
557: break;
558: } // outer switch
559: } // outer for
560: {
561: if (true)
562: return sbuf.toString();
563: }
564: throw new Error("Missing return statement in function");
565: }
566:
567: public ConfigParserTokenManager token_source;
568: SimpleCharStream jj_input_stream;
569: public Token token, jj_nt;
570: private int jj_ntk;
571: private int jj_gen;
572: final private int[] jj_la1 = new int[16];
573: static private int[] jj_la1_0;
574: static {
575: jj_la1_0();
576: }
577:
578: private static void jj_la1_0() {
579: jj_la1_0 = new int[] { 0x171, 0x20, 0x20, 0x20, 0x2, 0x20,
580: 0x600, 0x151, 0x20, 0x2000, 0x20, 0x1000, 0x20, 0x20,
581: 0x600, 0x20, };
582: }
583:
584: public ConfigToContextParser(java.io.InputStream stream) {
585: jj_input_stream = new SimpleCharStream(stream, 1, 1);
586: token_source = new ConfigParserTokenManager(jj_input_stream);
587: token = new Token();
588: jj_ntk = -1;
589: jj_gen = 0;
590: for (int i = 0; i < 16; i++)
591: jj_la1[i] = -1;
592: }
593:
594: public void ReInit(java.io.InputStream stream) {
595: jj_input_stream.ReInit(stream, 1, 1);
596: token_source.ReInit(jj_input_stream);
597: token = new Token();
598: jj_ntk = -1;
599: jj_gen = 0;
600: for (int i = 0; i < 16; i++)
601: jj_la1[i] = -1;
602: }
603:
604: public ConfigToContextParser(java.io.Reader stream) {
605: jj_input_stream = new SimpleCharStream(stream, 1, 1);
606: token_source = new ConfigParserTokenManager(jj_input_stream);
607: token = new Token();
608: jj_ntk = -1;
609: jj_gen = 0;
610: for (int i = 0; i < 16; i++)
611: jj_la1[i] = -1;
612: }
613:
614: public void ReInit(java.io.Reader stream) {
615: jj_input_stream.ReInit(stream, 1, 1);
616: token_source.ReInit(jj_input_stream);
617: token = new Token();
618: jj_ntk = -1;
619: jj_gen = 0;
620: for (int i = 0; i < 16; i++)
621: jj_la1[i] = -1;
622: }
623:
624: public ConfigToContextParser(ConfigParserTokenManager tm) {
625: token_source = tm;
626: token = new Token();
627: jj_ntk = -1;
628: jj_gen = 0;
629: for (int i = 0; i < 16; i++)
630: jj_la1[i] = -1;
631: }
632:
633: public void ReInit(ConfigParserTokenManager tm) {
634: token_source = tm;
635: token = new Token();
636: jj_ntk = -1;
637: jj_gen = 0;
638: for (int i = 0; i < 16; i++)
639: jj_la1[i] = -1;
640: }
641:
642: final private Token jj_consume_token(int kind)
643: throws ParseException {
644: Token oldToken;
645: if ((oldToken = token).next != null)
646: token = token.next;
647: else
648: token = token.next = token_source.getNextToken();
649: jj_ntk = -1;
650: if (token.kind == kind) {
651: jj_gen++;
652: return token;
653: }
654: token = oldToken;
655: jj_kind = kind;
656: throw generateParseException();
657: }
658:
659: final public Token getNextToken() {
660: if (token.next != null)
661: token = token.next;
662: else
663: token = token.next = token_source.getNextToken();
664: jj_ntk = -1;
665: jj_gen++;
666: return token;
667: }
668:
669: final public Token getToken(int index) {
670: Token t = token;
671: for (int i = 0; i < index; i++) {
672: if (t.next != null)
673: t = t.next;
674: else
675: t = t.next = token_source.getNextToken();
676: }
677: return t;
678: }
679:
680: final private int jj_ntk() {
681: if ((jj_nt = token.next) == null)
682: return (jj_ntk = (token.next = token_source.getNextToken()).kind);
683: else
684: return (jj_ntk = jj_nt.kind);
685: }
686:
687: private java.util.Vector jj_expentries = new java.util.Vector();
688: private int[] jj_expentry;
689: private int jj_kind = -1;
690:
691: public ParseException generateParseException() {
692: jj_expentries.removeAllElements();
693: boolean[] la1tokens = new boolean[16];
694: for (int i = 0; i < 16; i++) {
695: la1tokens[i] = false;
696: }
697: if (jj_kind >= 0) {
698: la1tokens[jj_kind] = true;
699: jj_kind = -1;
700: }
701: for (int i = 0; i < 16; i++) {
702: if (jj_la1[i] == jj_gen) {
703: for (int j = 0; j < 32; j++) {
704: if ((jj_la1_0[i] & (1 << j)) != 0) {
705: la1tokens[j] = true;
706: }
707: }
708: }
709: }
710: for (int i = 0; i < 16; i++) {
711: if (la1tokens[i]) {
712: jj_expentry = new int[1];
713: jj_expentry[0] = i;
714: jj_expentries.addElement(jj_expentry);
715: }
716: }
717: int[][] exptokseq = new int[jj_expentries.size()][];
718: for (int i = 0; i < jj_expentries.size(); i++) {
719: exptokseq[i] = (int[]) jj_expentries.elementAt(i);
720: }
721: return new ParseException(token, exptokseq, tokenImage);
722: }
723:
724: final public void enable_tracing() {
725: }
726:
727: final public void disable_tracing() {
728: }
729:
730: }
731:
732: class ConfigElement {
733: /**
734: * Comments for this element.
735: */
736: public String comments;
737:
738: /**
739: * Key name for this key.
740: */
741: public String key;
742:
743: /**
744: * Array of zero or more values assigned to key.
745: */
746: public String[] values;
747: }
|