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.DeepTrimToLowerNormalizer;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  
31  /**
32   * A comparator that uses the DeepTrimToLowerNormalizer before comparing two values
33   * 
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public class DeepTrimToLowerComparator 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( DeepTrimToLowerComparator.class );
43  
44  
45      /**
46       * The NormalizingComparator constructor. Its OID is the  matching rule OID.
47       * 
48       * @param oid The Comparator's OID
49       */
50      public DeepTrimToLowerComparator( String oid )
51      {
52          super( oid );
53          normalizer = new DeepTrimToLowerNormalizer();
54      }
55  
56  
57      /**
58       * {@inheritDoc}
59       */
60      public int compare( String key, String value )
61      {
62          String normalizedValue;
63  
64          try
65          {
66              normalizedValue = normalizer.normalize( value );
67          }
68          catch ( LdapException e )
69          {
70              if ( LOG.isWarnEnabled() )
71              {
72                  LOG.warn( I18n.msg( I18n.MSG_13700_FAILED_TO_NORMALIZE, value ), e );
73              }
74              
75              normalizedValue = value;
76          }
77  
78          return key.compareTo( normalizedValue );
79      }
80  }