1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.directory.server.core.event;
21
22
23 import javax.naming.NamingException;
24
25 import org.apache.directory.server.core.entry.ServerEntry;
26 import org.apache.directory.server.schema.registries.AttributeTypeRegistry;
27 import org.apache.directory.server.schema.registries.OidRegistry;
28 import org.apache.directory.shared.ldap.filter.AndNode;
29 import org.apache.directory.shared.ldap.filter.BranchNode;
30 import org.apache.directory.shared.ldap.filter.ExprNode;
31 import org.apache.directory.shared.ldap.filter.NotNode;
32 import org.apache.directory.shared.ldap.filter.OrNode;
33
34
35
36 /**
37 * Top level filter expression evaluator implemenation.
38 *
39 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40 * @version $Rev: 642496 $
41 */
42 public class ExpressionEvaluator implements Evaluator
43 {
44 /** Leaf Evaluator flyweight use for leaf filter assertions */
45 private LeafEvaluator leafEvaluator;
46
47
48 // ------------------------------------------------------------------------
49 // C O N S T R U C T O R S
50 // ------------------------------------------------------------------------
51
52 /**
53 * Creates a top level Evaluator where leaves are delegated to a leaf node
54 * evaluator which is already provided.
55 *
56 * @param leafEvaluator handles leaf node evaluation.
57 */
58 public ExpressionEvaluator(LeafEvaluator leafEvaluator)
59 {
60 this.leafEvaluator = leafEvaluator;
61 }
62
63
64 /**
65 * Creates a top level Evaluator where leaves are delegated to a leaf node
66 * evaluator which will be created.
67 *
68 * @param oidRegistry the oid reg used for attrID to oid resolution
69 * @param attributeTypeRegistry the attribtype reg used for value comparison
70 */
71 public ExpressionEvaluator(OidRegistry oidRegistry, AttributeTypeRegistry attributeTypeRegistry)
72 {
73 SubstringEvaluator substringEvaluator = null;
74 substringEvaluator = new SubstringEvaluator( oidRegistry, attributeTypeRegistry );
75 leafEvaluator = new LeafEvaluator( oidRegistry, attributeTypeRegistry, substringEvaluator );
76 }
77
78
79 /**
80 * Gets the leaf evaluator used by this top level expression evaluator.
81 *
82 * @return the leaf evaluator used by this top level expression evaluator
83 */
84 public LeafEvaluator getLeafEvaluator()
85 {
86 return leafEvaluator;
87 }
88
89
90 // ------------------------------------------------------------------------
91 // Evaluator.evaluate() implementation
92 // ------------------------------------------------------------------------
93
94 /**
95 * @see Evaluator#evaluate(ExprNode, String, ServerEntry)
96 */
97 public boolean evaluate( ExprNode node, String dn, ServerEntry entry ) throws NamingException
98 {
99 if ( node.isLeaf() )
100 {
101 return leafEvaluator.evaluate( node, dn, entry );
102 }
103
104 BranchNode bnode = ( BranchNode ) node;
105
106 if ( bnode instanceof OrNode )
107 {
108 for ( ExprNode child: bnode.getChildren() )
109 {
110 if ( evaluate( child, dn, entry ) )
111 {
112 return true;
113 }
114 }
115
116 return false;
117 }
118 else if ( bnode instanceof AndNode )
119 {
120 for ( ExprNode child: bnode.getChildren() )
121 {
122 if ( !evaluate( child, dn, entry ) )
123 {
124 return false;
125 }
126 }
127
128 return true;
129 }
130 else if ( bnode instanceof NotNode )
131 {
132 if ( null != bnode.getFirstChild() )
133 {
134 return !evaluate( bnode.getFirstChild(), dn, entry );
135 }
136
137 throw new NamingException( "Negation has no child: " + node );
138 }
139 else
140 {
141 throw new NamingException( "Unrecognized branch node operator: " + bnode );
142 }
143 }
144 }