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