1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ldap.server.event;
18
19
20 import java.util.Iterator;
21
22 import javax.naming.NamingException;
23 import javax.naming.directory.Attributes;
24
25 import org.apache.ldap.common.filter.BranchNode;
26 import org.apache.ldap.common.filter.ExprNode;
27 import org.apache.ldap.server.schema.AttributeTypeRegistry;
28 import org.apache.ldap.server.schema.OidRegistry;
29
30
31 /***
32 * Top level filter expression evaluator implemenation.
33 *
34 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35 * @version $Rev$
36 */
37 public class ExpressionEvaluator implements Evaluator
38 {
39 /*** Leaf Evaluator flyweight use for leaf filter assertions */
40 private LeafEvaluator leafEvaluator;
41
42
43
44
45
46
47
48 /***
49 * Creates a top level Evaluator where leaves are delegated to a leaf node
50 * evaluator which is already provided.
51 *
52 * @param leafEvaluator handles leaf node evaluation.
53 */
54 public ExpressionEvaluator( LeafEvaluator leafEvaluator )
55 {
56 this.leafEvaluator = leafEvaluator;
57 }
58
59
60 /***
61 * Creates a top level Evaluator where leaves are delegated to a leaf node
62 * evaluator which will be created.
63 *
64 * @param oidRegistry the oid reg used for attrID to oid resolution
65 * @param attributeTypeRegistry the attribtype reg used for value comparison
66 */
67 public ExpressionEvaluator( OidRegistry oidRegistry,
68 AttributeTypeRegistry attributeTypeRegistry ) throws NamingException
69 {
70 SubstringEvaluator substringEvaluator = null;
71 substringEvaluator = new SubstringEvaluator( oidRegistry, attributeTypeRegistry );
72 leafEvaluator = new LeafEvaluator( oidRegistry, attributeTypeRegistry, substringEvaluator );
73 }
74
75
76 /***
77 * Gets the leaf evaluator used by this top level expression evaluator.
78 *
79 * @return the leaf evaluator used by this top level expression evaluator
80 */
81 public LeafEvaluator getLeafEvaluator()
82 {
83 return leafEvaluator;
84 }
85
86
87
88
89
90
91
92 /***
93 * @see Evaluator#evaluate(ExprNode, String, Attributes)
94 */
95 public boolean evaluate( ExprNode node, String dn, Attributes entry )
96 throws NamingException
97 {
98 if ( node.isLeaf() )
99 {
100 return leafEvaluator.evaluate( node, dn, entry );
101 }
102
103 BranchNode bnode = ( BranchNode ) node;
104
105 switch( bnode.getOperator() )
106 {
107 case( BranchNode.OR ):
108 Iterator children = bnode.getChildren().iterator();
109
110 while ( children.hasNext() )
111 {
112 ExprNode child = ( ExprNode ) children.next();
113
114 if ( evaluate( child, dn, entry ) )
115 {
116 return true;
117 }
118 }
119
120 return false;
121 case( BranchNode.AND ):
122 children = bnode.getChildren().iterator();
123 while ( children.hasNext() )
124 {
125 ExprNode child = ( ExprNode ) children.next();
126
127 if ( ! evaluate( child, dn, entry ) )
128 {
129 return false;
130 }
131 }
132
133 return true;
134 case( BranchNode.NOT ):
135 if ( null != bnode.getChild() )
136 {
137 return ! evaluate( bnode.getChild(), dn, entry );
138 }
139
140 throw new NamingException( "Negation has no child: " + node );
141 default:
142 throw new NamingException( "Unrecognized branch node operator: "
143 + bnode.getOperator() );
144 }
145 }
146 }