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.subtree;
18  
19  
20  import org.apache.ldap.common.filter.ExprNode;
21  import org.apache.ldap.common.filter.BranchNode;
22  import org.apache.ldap.common.filter.SimpleNode;
23  
24  import javax.naming.NamingException;
25  import javax.naming.directory.Attribute;
26  import java.util.Iterator;
27  
28  
29  /***
30   * The top level evaluation node for a refinement.
31   *
32   * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
33   * @version $Rev$
34   */
35  public class RefinementEvaluator
36  {
37      /*** Leaf Evaluator flyweight use for leaf filter assertions */
38      private RefinementLeafEvaluator leafEvaluator;
39  
40  
41      // ------------------------------------------------------------------------
42      // C O N S T R U C T O R S
43      // ------------------------------------------------------------------------
44  
45  
46      public RefinementEvaluator( RefinementLeafEvaluator leafEvaluator )
47      {
48          this.leafEvaluator = leafEvaluator;
49      }
50  
51  
52      public boolean evaluate( ExprNode node, Attribute objectClasses ) throws NamingException
53      {
54          if ( node == null )
55          {
56              throw new IllegalArgumentException( "node cannot be null" );
57          }
58          if ( objectClasses == null )
59          {
60              throw new IllegalArgumentException( "objectClasses cannot be null" );
61          }
62          if ( ! objectClasses.getID().equalsIgnoreCase( "objectClass" ) )
63          {
64              throw new IllegalArgumentException( "Attribute objectClasses should be of id 'objectClass'" );
65          }
66          if ( node.isLeaf() )
67          {
68              return leafEvaluator.evaluate( ( SimpleNode ) node, objectClasses );
69          }
70  
71          BranchNode bnode = ( BranchNode ) node;
72  
73          switch( bnode.getOperator() )
74          {
75          case( BranchNode.OR ):
76              Iterator children = bnode.getChildren().iterator();
77  
78              while ( children.hasNext() )
79              {
80                  ExprNode child = ( ExprNode ) children.next();
81  
82                  if ( evaluate( child, objectClasses ) )
83                  {
84                      return true;
85                  }
86              }
87  
88              return false;
89          case( BranchNode.AND ):
90              children = bnode.getChildren().iterator();
91              while ( children.hasNext() )
92              {
93                  ExprNode child = ( ExprNode ) children.next();
94  
95                  if ( ! evaluate( child, objectClasses ) )
96                  {
97                      return false;
98                  }
99              }
100 
101             return true;
102         case( BranchNode.NOT ):
103             if ( null != bnode.getChild() )
104             {
105                 return ! evaluate( bnode.getChild(), objectClasses );
106             }
107 
108             throw new IllegalArgumentException( "Negation has no child: " + node );
109         default:
110             throw new IllegalArgumentException( "Unrecognized branch node operator: "
111                 + bnode.getOperator() );
112         }
113     }
114 }