1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
38
39
40
41
42 public class RefinementEvaluator
43 {
44
45 private RefinementLeafEvaluator leafEvaluator;
46
47
48
49
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 }