KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > ldap > Driver


1 /*
2  * Oscar - An implementation of the OSGi framework.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  * Dennis Heimbigner
35  *
36 **/

37 package org.ungoverned.oscar.ldap;
38
39 import java.io.*;
40 import java.util.Hashtable JavaDoc;
41 import java.util.Stack JavaDoc;
42 import java.util.Vector JavaDoc;
43
44 public class Driver {
45
46     public static void main(String JavaDoc[] argv)
47     {
48     Mapper mapper = new DriverMapper();
49
50     if(argv== null || argv.length == 0) {
51         System.err.println("usage: Driver <ldap spec file>");
52         return;
53     }
54     LdapLexer lexer = new LdapLexer();
55     FileReader fr = null;
56     char[] line = null;
57     Evaluator engine = new Evaluator();
58
59     Parser parser = new Parser();
60 // parser.setDebug(System.out);
61

62     try {
63         File spec = new File(argv[0]);
64         fr = new FileReader(spec);
65
66         // The basic operation of the driver is:
67
// 1. read a line from the file
68
// 2. parse that line
69
// 3. print the resulting program
70
// 4. repeat 1 until eof
71

72         for(;;) {
73         line = getLine(fr);
74         if(line == null) break;
75         System.out.println("Driver: filter: "+new String JavaDoc(line));
76         CharArrayReader car = new CharArrayReader(line);
77         lexer.setReader(car);
78         parser.reset(lexer);
79         boolean status = false;
80         try {
81             status = parser.start();
82             if(!status) {
83             System.err.println("parse failed");
84             printErrorLocation(line,lexer.charno());
85             }
86         } catch (ParseException pe) {
87             System.err.println(pe.toString());
88             printErrorLocation(line,lexer.charno());
89         }
90         if(status) {
91             try {
92             engine.reset(parser.getProgram());
93 // System.out.println("Driver: program: "+engine.toString());
94
System.out.println("Driver: program: "+engine.toStringInfix());
95             System.out.println("Eval = " + engine.evaluate(mapper));
96             } catch (EvaluationException ee) {
97             System.err.print("Driver: ");
98             printEvaluationStack(engine.getOperands());
99             System.err.println(ee.toString());
100             }
101         }
102         }
103     } catch (Exception JavaDoc e) {
104         System.err.println(e.toString());
105         printErrorLocation(line,lexer.charno());
106         e.printStackTrace();
107     }
108     }
109
110     // Get a line of input at a time and return a char[] buffer
111
// containing the line
112

113     static char[] getLine(Reader reader) throws IOException
114     {
115     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
116     for(;;) {
117         int c = reader.read();
118         if(c == '\r') continue;
119         if(c < 0) {
120         if(buf.length() == 0) return null; // no more lines
121
break;
122         }
123         if(c == '\n') break;
124         buf.append((char)c);
125     }
126
127     char[] cbuf = new char[buf.length()];
128     buf.getChars(0,buf.length(),cbuf,0);
129     return cbuf;
130     }
131
132
133     static void printErrorLocation(char[] line, int charno)
134     {
135     System.err.print("|");
136     if(line != null) System.err.print(new String JavaDoc(line));
137     System.err.println("|");
138     for(int i=0;i<charno;i++) System.err.print(" ");
139     System.err.println("^");
140     }
141
142     // Report the final contents of the evaluation stack
143
static void printEvaluationStack(Stack JavaDoc stack)
144     {
145     System.err.print("Stack:");
146     // recast operands as Vector to make interior access easier
147
Vector JavaDoc operands = stack;
148     int len = operands.size();
149     for(int i=0;i<len;i++) System.err.print(" "+operands.elementAt(i));
150     System.err.println();
151     }
152
153 }
154
155 class DriverMapper implements Mapper {
156
157     Hashtable JavaDoc hash = new Hashtable JavaDoc();
158
159     public DriverMapper()
160     {
161         hash.put("cn","Babs Jensen");
162         hash.put("objectClass","Person");
163         hash.put("sn","Jensen");
164         hash.put("o","university of michigan");
165         hash.put("foo","bar");
166     }
167
168     public Object JavaDoc lookup(String JavaDoc key)
169     {
170         return hash.get(key);
171     }
172 }
173
Popular Tags