View Javadoc

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.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      // C O N S T R U C T O R S
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      // Evaluator.evaluate() implementation
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 }