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   *    http://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.normalizers;
21  
22  
23  import java.io.IOException;
24  
25  import org.apache.directory.api.i18n.I18n;
26  import org.apache.directory.api.ldap.model.entry.StringValue;
27  import org.apache.directory.api.ldap.model.entry.Value;
28  import org.apache.directory.api.ldap.model.exception.LdapException;
29  import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
30  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
31  import org.apache.directory.api.ldap.model.schema.Normalizer;
32  import org.apache.directory.api.ldap.model.schema.PrepareString;
33  
34  
35  /**
36   * Normalizer which trims down whitespace replacing multiple whitespace
37   * characters on the edges and within the string with a single space character
38   * thereby preserving tokenization order - while doing all this in the same pass
39   * it lower cases all characters.
40   * 
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  @SuppressWarnings("serial")
44  public class DeepTrimToLowerNormalizer extends Normalizer
45  {
46      /**
47       * Creates a new instance of DeepTrimToLowerNormalizer.
48       * 
49       * @param oid The MR OID to use with this Normalizer
50       */
51      public DeepTrimToLowerNormalizer( String oid )
52      {
53          super( oid );
54      }
55  
56  
57      /**
58       * Creates a new instance of DeepTrimToLowerNormalizer where the OID is
59       * set after instantiation.
60       */
61      public DeepTrimToLowerNormalizer()
62      {
63      }
64  
65  
66      /**
67       * {@inheritDoc}
68       */
69      public Value<?> normalize( Value<?> value ) throws LdapException
70      {
71          if ( value == null )
72          {
73              return null;
74          }
75  
76          try
77          {
78              String normalized = PrepareString.normalize( value.getString(),
79                  PrepareString.StringType.CASE_IGNORE );
80  
81              return new StringValue( normalized );
82          }
83          catch ( IOException ioe )
84          {
85              throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(
86                  I18n.ERR_04224, value ), ioe );
87          }
88      }
89  
90  
91      /**
92       * {@inheritDoc}
93       */
94      public String normalize( String value ) throws LdapException
95      {
96          if ( value == null )
97          {
98              return null;
99          }
100 
101         try
102         {
103             String normalized = PrepareString.normalize( value,
104                 PrepareString.StringType.CASE_IGNORE );
105 
106             return normalized;
107         }
108         catch ( IOException ioe )
109         {
110             throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(
111                 I18n.ERR_04224, value ), ioe );
112         }
113     }
114 }