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.Normalizer;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  
31  /**
32   * A comparator which normalizes a value first before using a subordinate
33   * comparator to compare them.
34   * 
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  public class NormalizingComparator extends LdapComparator<String>
38  {
39      /** The serial version UID */
40      private static final long serialVersionUID = 2L;
41  
42      /** A logger for this class */
43      private static final Logger LOG = LoggerFactory.getLogger( NormalizingComparator.class );
44  
45      /** the underlying comparator to use for comparisons */
46      private LdapComparator<String> comparator;
47  
48      private boolean onServer = false;
49  
50  
51      /**
52       * The NormalizingComparator constructor. Its OID is the  matching rule OID.
53       * 
54       * @param oid The Comparator's OID
55       */
56      public NormalizingComparator( String oid )
57      {
58          super( oid );
59      }
60  
61  
62      /**
63       * A comparator which normalizes a value first before comparing them.
64       * 
65       * @param oid The Comparator's OID
66       * @param normalizer the Normalizer to normalize values with before comparing
67       * @param comparator the underlying comparator to use for comparisons
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       * {@inheritDoc}
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      * {@inheritDoc}
124      * 
125      * This implementation makes sure we update the oid property of the contained normalizer and 
126      * comparator.
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      * tells that the normalizingComparator should not normalize values which are
139      * already normalized on the server 
140      */
141     public void setOnServer()
142     {
143         this.onServer = true;
144     }
145 }