KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Stack JavaDoc;
40 import java.util.Vector JavaDoc;
41
42 public class Evaluator {
43
44     Object JavaDoc[] program = null;
45     Stack JavaDoc operands = new Stack JavaDoc();
46     Mapper mapper = null;
47
48     public Evaluator()
49     {
50         reset();
51     }
52
53     public Evaluator(Object JavaDoc[] prog)
54     {
55         reset(prog);
56     }
57
58     public void reset()
59     {
60         program = null;
61         mapper = null;
62         operands.clear();
63     }
64
65     public void reset(Object JavaDoc[] prog)
66     {
67         reset();
68         setProgram(prog);
69     }
70
71     public void setProgram(Object JavaDoc[] prog)
72     {
73         program = prog;
74     }
75
76     public void setMapper(Mapper mapper)
77     {
78         this.mapper = mapper;
79     }
80
81     public Stack JavaDoc getOperands()
82     {
83         return operands;
84     }
85
86     public boolean evaluate(Mapper mapper) throws EvaluationException
87     {
88         try
89         {
90             // The following code is a little complicated because it
91
// is trying to deal with evaluating a given filter expression
92
// when it contains an attribute that does not exist in the
93
// supplied mapper. In such a situation the code below
94
// catches the "attribute not found" exception and inserts
95
// an instance of Unknown, which is used as a marker for
96
// non-existent attributes. The Unknown instance forces the
97
// operator to throw an "unsupported type" exception, which
98
// the code below converts in a FALSE and this has the effect
99
// of evaluating the entire subexpression that contained the
100
// non-existent attribute to FALSE. Any other exceptions are
101
// rethrown.
102
setMapper(mapper);
103             for (int i = 0; i < program.length; i++)
104             {
105                 try
106                 {
107                     Operator op = (Operator) program[i];
108                     op.execute(operands, mapper);
109 // printAction(op); // for debug output
110
}
111                 catch (AttributeNotFoundException ex)
112                 {
113                     operands.push(new Unknown());
114                 }
115                 catch (EvaluationException ex)
116                 {
117                     // If the exception is for an unsupported type of
118
// type Unknown, then just push FALSE onto the
119
// operand stack.
120
if (ex.isUnsupportedType() && (ex.getUnsupportedType() == Unknown.class))
121                     {
122                         operands.push(Boolean.FALSE);
123                     }
124                     // Otherwise, rethrow the exception.
125
else
126                     {
127                         throw ex;
128                     }
129                 }
130             }
131
132             if (operands.empty())
133             {
134                 throw new EvaluationException("Evaluation.evalute: final stack is empty");
135             }
136
137             Object JavaDoc result = operands.pop();
138
139             if (!operands.empty())
140             {
141                 throw new EvaluationException(
142                     "Evaluation.evalute: final stack has more than one result");
143             }
144
145             if (!(result instanceof Boolean JavaDoc))
146             {
147                 throw new EvaluationException(
148                     "Evaluation.evalute: final result is not Boolean");
149             }
150
151             return ((Boolean JavaDoc) result).booleanValue();
152         }
153         finally
154         {
155             // Clear the operands just in case an exception was thrown,
156
// otherwise stuff will be left in the stack.
157
operands.clear();
158         }
159     }
160
161     // For debugging; Dump the operator and stack
162
void printAction(Operator op)
163     {
164         System.err.println("Operator:"+op.toString());
165         System.err.print("Stack After:");
166         // recast operands as Vector to make interior access easier
167
Vector JavaDoc v = operands;
168         int len = v.size();
169         for (int i = 0; i < len; i++)
170             System.err.print(" " + v.elementAt(i));
171         System.err.println();
172     }
173
174     public String JavaDoc toString()
175     {
176         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
177         for (int i = 0; i < program.length; i++)
178         {
179             buf.append((i==0) ? "{" : ";");
180                 buf.append(((Operator) program[i]).toString());
181         }
182         buf.append("}");
183         return buf.toString();
184     }
185
186     public String JavaDoc toStringInfix()
187     {
188         // First, we "evaluate" the program
189
// but for the purpose of re-constructing
190
// a parsetree.
191
operands.clear();
192         for (int i = 0; i < program.length; i++)
193         {
194             ((Operator) program[i]).buildTree(operands);
195         }
196         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
197         Object JavaDoc result = operands.pop();
198         ((Operator)result).toStringInfix(b);
199         operands.clear();
200         return b.toString();
201     }
202 }
203
Popular Tags