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.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.exception.LdapException;
27  import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
28  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
29  import org.apache.directory.api.ldap.model.schema.Normalizer;
30  import org.apache.directory.api.ldap.model.schema.PrepareString;
31  import org.apache.directory.api.ldap.model.schema.PreparedNormalizer;
32  
33  
34  /**
35   * Normalizer which trims down whitespace replacing multiple whitespace
36   * characters on the edges and within the string with a single space character
37   * thereby preserving tokenization order - while doing all this in the same pass
38   * it lower cases all characters.
39   * 
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  @SuppressWarnings("serial")
43  public class DeepTrimToLowerNormalizer extends Normalizer implements PreparedNormalizer
44  {
45      /**
46       * Creates a new instance of DeepTrimToLowerNormalizer.
47       * 
48       * @param oid The MR OID to use with this Normalizer
49       */
50      public DeepTrimToLowerNormalizer( String oid )
51      {
52          super( oid );
53      }
54  
55  
56      /**
57       * Creates a new instance of DeepTrimToLowerNormalizer where the OID is
58       * set after instantiation.
59       */
60      public DeepTrimToLowerNormalizer()
61      {
62          super();
63      }
64  
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public String normalize( String value ) throws LdapException
71      {
72          return normalize( value, PrepareString.AssertionType.ATTRIBUTE_VALUE );
73      }
74      
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public String normalize( String value, PrepareString.AssertionType assertionType ) throws LdapException
81      {
82          if ( value == null )
83          {
84              return null;
85          }
86  
87          String normValue = null;
88  
89          try
90          {
91              // Transcoding is useless
92              // Map
93              String mapped = PrepareString.mapIgnoreCase( value );
94  
95              // Normalize
96              String normalized = PrepareString.normalize( mapped );
97              
98              char[] chars = normalized.toCharArray();
99              
100             // Prohibit
101             PrepareString.checkProhibited( chars );
102             
103             // Bidi is ignored
104             
105             // Insignificant Characters Handling
106             switch ( assertionType )
107             {
108                 case ATTRIBUTE_VALUE :
109                     normValue = PrepareString.insignificantSpacesStringValue( chars );
110                     break;
111                     
112                 case SUBSTRING_INITIAL :
113                     normValue = PrepareString.insignificantSpacesStringInitial( chars );
114                     break;
115                     
116                 case SUBSTRING_ANY :
117                     normValue = PrepareString.insignificantSpacesStringAny( chars );
118                     break;
119                     
120                 case SUBSTRING_FINAL :
121                     normValue = PrepareString.insignificantSpacesStringFinal( chars );
122                     break;
123                     
124                 default :
125                     // Do nothing
126                     break;
127             }
128 
129             return normValue;
130         }
131         catch ( IOException ioe )
132         {
133             throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(
134                 I18n.ERR_13724_INVALID_VALUE, value ), ioe );
135         }
136     }
137 }