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