1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
26 import java.util.Iterator;
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: 159259 $
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
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
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
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 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( "Negation has no child: " + node );
146 default:
147 throw new NamingException( "Unrecognized branch node operator: "
148 + bnode.getOperator() );
149 }
150 }
151 }