KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ldap > server > db > ExpressionEvaluator


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.ldap.server.db;
18
19
20 import org.apache.ldap.common.filter.BranchNode;
21 import org.apache.ldap.common.filter.ExprNode;
22 import org.apache.ldap.server.schema.AttributeTypeRegistry;
23 import org.apache.ldap.server.schema.OidRegistry;
24
25 import javax.naming.NamingException JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28
29 /**
30  * Top level filter expression evaluator implemenation.
31  *
32  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
33  * @version $Rev: 169198 $
34  */

35 public class ExpressionEvaluator implements Evaluator
36 {
37     /** Leaf Evaluator flyweight use for leaf filter assertions */
38     private LeafEvaluator leafEvaluator;
39
40
41     // ------------------------------------------------------------------------
42
// C O N S T R U C T O R S
43
// ------------------------------------------------------------------------
44

45
46     /**
47      * Creates a top level Evaluator where leaves are delegated to a leaf node
48      * evaluator which is already provided.
49      *
50      * @param leafEvaluator handles leaf node evaluation.
51      */

52     public ExpressionEvaluator( LeafEvaluator leafEvaluator )
53     {
54         this.leafEvaluator = leafEvaluator;
55     }
56
57
58     /**
59      * Creates a top level Evaluator where leaves are delegated to a leaf node
60      * evaluator which will be created.
61      *
62      * @param db the database this evaluator operates upon
63      * @param oidRegistry the oid reg used for attrID to oid resolution
64      * @param attributeTypeRegistry the attribtype reg used for value comparison
65      */

66     public ExpressionEvaluator( Database db,
67                                 OidRegistry oidRegistry,
68                                 AttributeTypeRegistry attributeTypeRegistry )
69     {
70         ScopeEvaluator scopeEvaluator = null;
71         SubstringEvaluator substringEvaluator = null;
72
73         scopeEvaluator = new ScopeEvaluator( db );
74         substringEvaluator = new SubstringEvaluator( db, oidRegistry,
75             attributeTypeRegistry );
76         leafEvaluator = new LeafEvaluator( db, oidRegistry,
77             attributeTypeRegistry, scopeEvaluator, substringEvaluator );
78     }
79
80
81     /**
82      * Gets the leaf evaluator used by this top level expression evaluator.
83      *
84      * @return the leaf evaluator used by this top level expression evaluator
85      */

86     public LeafEvaluator getLeafEvaluator()
87     {
88         return leafEvaluator;
89     }
90
91
92     // ------------------------------------------------------------------------
93
// Evaluator.evaluate() implementation
94
// ------------------------------------------------------------------------
95

96
97     /**
98      * @see org.apache.ldap.server.db.Evaluator#evaluate(ExprNode, IndexRecord)
99      */

100     public boolean evaluate( ExprNode node, IndexRecord record )
101         throws NamingException JavaDoc
102     {
103         if ( node.isLeaf() )
104         {
105             return leafEvaluator.evaluate( node, record );
106         }
107
108         BranchNode bnode = ( BranchNode ) node;
109
110         switch( bnode.getOperator() )
111         {
112         case( BranchNode.OR ):
113             Iterator JavaDoc children = bnode.getChildren().iterator();
114             
115             while ( children.hasNext() )
116             {
117                 ExprNode child = ( ExprNode ) children.next();
118                 
119                 if ( evaluate( child, record ) )
120                 {
121                     return true;
122                 }
123             }
124
125             return false;
126         case( BranchNode.AND ):
127             children = bnode.getChildren().iterator();
128             while ( children.hasNext() )
129             {
130                 ExprNode child = ( ExprNode ) children.next();
131
132                 if ( ! evaluate( child, record ) )
133                 {
134                     return false;
135                 }
136             }
137
138             return true;
139         case( BranchNode.NOT ):
140             if ( null != bnode.getChild() )
141             {
142                 return ! evaluate( bnode.getChild(), record );
143             }
144
145             throw new NamingException JavaDoc( "Negation has no child: " + node );
146         default:
147             throw new NamingException JavaDoc( "Unrecognized branch node operator: "
148                 + bnode.getOperator() );
149         }
150     }
151 }
152
Popular Tags