1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.model.schema.comparators;
21
22
23 import org.apache.directory.api.i18n.I18n;
24 import org.apache.directory.api.ldap.model.schema.LdapComparator;
25 import org.apache.directory.api.util.Strings;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class WordComparator extends LdapComparator<String>
49 {
50
51 private static final long serialVersionUID = 2L;
52
53
54 private static final Logger LOG = LoggerFactory.getLogger( WordComparator.class );
55
56
57
58
59
60
61
62
63 public WordComparator( String oid )
64 {
65 super( oid );
66 }
67
68
69
70
71
72 public int compare( String value, String assertion )
73 {
74 if ( LOG.isDebugEnabled() )
75 {
76 LOG.debug( I18n.msg( I18n.MSG_13749_COMPARING_STRING, value, assertion ) );
77 }
78
79 if ( value == assertion )
80 {
81 return 0;
82 }
83
84
85
86
87 if ( ( value == null ) || ( assertion == null ) )
88 {
89 return ( assertion == null ) ? 1 : -1;
90 }
91
92
93 String trimmedAssertion = Strings.trim( assertion );
94 int pos = value.indexOf( trimmedAssertion );
95
96 if ( pos != -1 )
97 {
98 int assertionLength = trimmedAssertion.length();
99
100
101 if ( assertionLength == value.length() )
102 {
103 return 0;
104 }
105
106 if ( pos == 0 )
107 {
108 char after = value.charAt( assertionLength );
109
110 if ( !Character.isLetterOrDigit( after ) )
111 {
112 return 0;
113 }
114 else
115 {
116 return -1;
117 }
118 }
119
120 if ( pos + assertionLength == value.length() )
121 {
122 char before = value.charAt( value.length() - assertionLength - 1 );
123
124 if ( !Character.isLetterOrDigit( before ) )
125 {
126 return 0;
127 }
128 else
129 {
130 return -1;
131 }
132 }
133
134 char before = value.charAt( value.length() - assertionLength );
135 char after = value.charAt( assertionLength );
136
137 if ( Character.isLetterOrDigit( after ) )
138 {
139 return -1;
140 }
141
142 if ( !Character.isLetterOrDigit( before ) )
143 {
144 return -1;
145 }
146
147 return 0;
148 }
149
150 return -1;
151 }
152 }