| 1 37 package org.ungoverned.oscar.ldap; 38 39 import java.io.*; 40 import java.util.Hashtable ; 41 import java.util.Stack ; 42 import java.util.Vector ; 43 44 public class Driver { 45 46 public static void main(String [] 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 62 try { 63 File spec = new File(argv[0]); 64 fr = new FileReader(spec); 65 66 72 for(;;) { 73 line = getLine(fr); 74 if(line == null) break; 75 System.out.println("Driver: filter: "+new String (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.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 e) { 104 System.err.println(e.toString()); 105 printErrorLocation(line,lexer.charno()); 106 e.printStackTrace(); 107 } 108 } 109 110 113 static char[] getLine(Reader reader) throws IOException 114 { 115 StringBuffer buf = new StringBuffer (); 116 for(;;) { 117 int c = reader.read(); 118 if(c == '\r') continue; 119 if(c < 0) { 120 if(buf.length() == 0) return null; 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 (line)); 137 System.err.println("|"); 138 for(int i=0;i<charno;i++) System.err.print(" "); 139 System.err.println("^"); 140 } 141 142 static void printEvaluationStack(Stack stack) 144 { 145 System.err.print("Stack:"); 146 Vector 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 hash = new Hashtable (); 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 lookup(String key) 169 { 170 return hash.get(key); 171 } 172 } 173 | Popular Tags |