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