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.exception.LdapException;
24 import org.apache.directory.api.ldap.model.schema.LdapComparator;
25 import org.apache.directory.api.ldap.model.schema.Normalizer;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30
31
32
33
34
35
36 public class NormalizingComparator extends LdapComparator<String>
37 {
38
39 private static final long serialVersionUID = 2L;
40
41
42 private static final Logger LOG = LoggerFactory.getLogger( NormalizingComparator.class );
43
44
45 private Normalizer normalizer;
46
47
48 private LdapComparator<String> comparator;
49
50 private boolean onServer = false;
51
52
53
54
55
56
57
58
59
60 public NormalizingComparator( String oid, Normalizer normalizer, LdapComparator<String> comparator )
61 {
62 super( oid );
63 this.normalizer = normalizer;
64 this.comparator = comparator;
65 }
66
67
68
69
70
71 public int compare( String o1, String o2 )
72 {
73 if ( onServer )
74 {
75 return comparator.compare( o1, o2 );
76 }
77
78 String n1;
79 String n2;
80
81 try
82 {
83 n1 = normalizer.normalize( o1 );
84 }
85 catch ( LdapException e )
86 {
87 LOG.warn( "Failed to normalize: " + o1, e );
88 n1 = o1;
89 }
90
91 try
92 {
93 n2 = normalizer.normalize( o2 );
94 }
95 catch ( LdapException e )
96 {
97 LOG.warn( "Failed to normalize: " + o2, e );
98 n2 = o2;
99 }
100
101 return comparator.compare( n1, n2 );
102 }
103
104
105
106
107
108
109
110
111 @Override
112 public void setOid( String oid )
113 {
114 super.setOid( oid );
115 normalizer.setOid( oid );
116 comparator.setOid( oid );
117 }
118
119
120
121
122
123
124 public void setOnServer()
125 {
126 this.onServer = true;
127 }
128 }