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.ldap.model.schema.LdapComparator;
24 import org.apache.directory.api.util.Strings;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class WordComparator extends LdapComparator<String>
48 {
49
50 private static final long serialVersionUID = 2L;
51
52
53 private static final Logger LOG = LoggerFactory.getLogger( WordComparator.class );
54
55
56
57
58
59
60 public WordComparator( String oid )
61 {
62 super( oid );
63 }
64
65
66
67
68
69 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "ES_COMPARING_PARAMETER_STRING_WITH_EQ",
70 justification = "false positive")
71 public int compare( String value, String assertion )
72 {
73 LOG.debug( "comparing String objects '{}' with '{}'", value, assertion );
74
75 if ( value == assertion )
76 {
77 return 0;
78 }
79
80
81
82
83 if ( ( value == null ) || ( assertion == null ) )
84 {
85 return ( assertion == null ) ? 1 : -1;
86 }
87
88
89 String trimmedAssertion = Strings.trim( assertion );
90 int pos = value.indexOf( trimmedAssertion );
91
92 if ( pos != -1 )
93 {
94 int assertionLength = trimmedAssertion.length();
95
96
97 if ( assertionLength == value.length() )
98 {
99 return 0;
100 }
101
102 if ( pos == 0 )
103 {
104 char after = value.charAt( assertionLength );
105
106 if ( !Character.isLetterOrDigit( after ) )
107 {
108 return 0;
109 }
110 else
111 {
112 return -1;
113 }
114 }
115
116 if ( pos + assertionLength == value.length() )
117 {
118 char before = value.charAt( value.length() - assertionLength - 1 );
119
120 if ( !Character.isLetterOrDigit( before ) )
121 {
122 return 0;
123 }
124 else
125 {
126 return -1;
127 }
128 }
129
130 char before = value.charAt( value.length() - assertionLength );
131 char after = value.charAt( assertionLength );
132
133 if ( Character.isLetterOrDigit( after ) )
134 {
135 return -1;
136 }
137
138 if ( !Character.isLetterOrDigit( before ) )
139 {
140 return -1;
141 }
142
143 return 0;
144 }
145
146 return -1;
147 }
148 }