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.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
37
38
39
40 public class RefinementEvaluator
41 {
42
43 private RefinementLeafEvaluator leafEvaluator;
44
45
46
47
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 }