1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ldap.server.event;
18
19
20 import javax.naming.NamingEnumeration;
21 import javax.naming.NamingException;
22 import javax.naming.directory.Attribute;
23 import javax.naming.directory.Attributes;
24
25 import org.apache.ldap.common.filter.ExprNode;
26 import org.apache.ldap.common.filter.SubstringNode;
27 import org.apache.ldap.common.schema.AttributeType;
28 import org.apache.ldap.common.schema.Normalizer;
29 import org.apache.ldap.server.schema.AttributeTypeRegistry;
30 import org.apache.ldap.server.schema.OidRegistry;
31 import org.apache.regexp.RE;
32 import org.apache.regexp.RESyntaxException;
33
34
35 /***
36 * Evaluates substring filter assertions on an entry.
37 *
38 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39 * @version $Rev$
40 */
41 public class SubstringEvaluator implements Evaluator
42 {
43 /*** Oid Registry used to translate attributeIds to OIDs */
44 private OidRegistry oidRegistry;
45 /*** AttributeType registry needed for normalizing and comparing values */
46 private AttributeTypeRegistry attributeTypeRegistry;
47
48
49 /***
50 * Creates a new SubstringEvaluator for substring expressions.
51 *
52 * @param oidRegistry the OID registry for name to OID mapping
53 * @param attributeTypeRegistry the attributeType registry
54 */
55 public SubstringEvaluator( OidRegistry oidRegistry,
56 AttributeTypeRegistry attributeTypeRegistry )
57 {
58 this.oidRegistry = oidRegistry;
59 this.attributeTypeRegistry = attributeTypeRegistry;
60 }
61
62
63 /***
64 * @see Evaluator#evaluate(ExprNode, String, Attributes)
65 */
66 public boolean evaluate( ExprNode node, String dn, Attributes entry )
67 throws NamingException
68 {
69 RE regex = null;
70 SubstringNode snode = ( SubstringNode ) node;
71 String oid = oidRegistry.getOid( snode.getAttribute() );
72 AttributeType type = attributeTypeRegistry.lookup( oid );
73 Normalizer normalizer = type.getSubstr().getNormalizer();
74
75
76 Attribute attr = entry.get( snode.getAttribute() );
77
78
79 if ( null == attr )
80 {
81 return false;
82 }
83
84
85 try
86 {
87 regex = snode.getRegex( normalizer );
88 }
89 catch ( RESyntaxException e )
90 {
91 NamingException ne = new NamingException( "SubstringNode '"
92 + node + "' had " + "incorrect syntax" );
93 ne.setRootCause( e );
94 throw ne;
95 }
96
97
98
99
100
101
102
103 NamingEnumeration list = attr.getAll();
104 while ( list.hasMore() )
105 {
106 String value = ( String )
107 normalizer.normalize( list.next() );
108
109
110 if ( regex.match( value ) )
111 {
112 list.close();
113 return true;
114 }
115 }
116
117
118 return false;
119 }
120 }