View Javadoc

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