1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }