View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
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.normalizers.DeepTrimNormalizer;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  
31  /**
32   * A comparator that uses the DeepTrimNormalizer before comparing two values
33   * 
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public class DeepTrimComparator extends LdapComparator<String>
37  {
38      /** The serial version UID */
39      private static final long serialVersionUID = 2L;
40  
41      /** A logger for this class */
42      private static final Logger LOG = LoggerFactory.getLogger( DeepTrimComparator.class );
43      
44      /**
45       * The NormalizingComparator constructor. Its OID is the  matching rule OID.
46       * 
47       * @param oid The Comparator's OID
48       */
49      public DeepTrimComparator( String oid )
50      {
51          super( oid );
52          normalizer = new DeepTrimNormalizer();
53      }
54  
55  
56      /**
57       * {@inheritDoc}
58       */
59      public int compare( String o1, String o2 )
60      {
61          String n1 = o1;
62          String n2;
63  
64          try
65          {
66              n1 = normalizer.normalize( o1 );
67          }
68          catch ( LdapException e )
69          {
70              if ( LOG.isWarnEnabled() )
71              {
72                  LOG.warn( I18n.msg( I18n.MSG_13700_FAILED_TO_NORMALIZE, o1 ), e );
73              }
74              
75              n1 = o1;
76          }
77  
78          try
79          {
80              n2 = normalizer.normalize( o2 );
81          }
82          catch ( LdapException e )
83          {
84              if ( LOG.isWarnEnabled() )
85              {
86                  LOG.warn( I18n.msg( I18n.MSG_13700_FAILED_TO_NORMALIZE, o2 ), e );
87              }
88              
89              n2 = o2;
90          }
91  
92          return n1.compareTo( n2 );
93      }
94  }