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.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  @SuppressWarnings("serial")
42  public class DeepTrimNormalizer extends Normalizer implements PreparedNormalizer
43  {
44      /**
45       * Creates a new instance of DeepTrimNormalizer with OID known.
46       * 
47       * @param oid The MR OID to use with this Normalizer
48       */
49      public DeepTrimNormalizer( String oid )
50      {
51          super( oid );
52      }
53  
54  
55      /**
56       * Creates a new instance of DeepTrimNormalizer when the Normalizer must be
57       * instantiated before setting the OID.
58       */
59      public DeepTrimNormalizer()
60      {
61          super();
62      }
63  
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public String normalize( String value ) throws LdapException
70      {
71          return normalize( value, PrepareString.AssertionType.ATTRIBUTE_VALUE );
72      }
73  
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public String normalize( String value, PrepareString.AssertionType assertionType ) throws LdapException
80      {
81          if ( value == null )
82          {
83              return null;
84          }
85  
86          String normValue = null;
87  
88          try
89          {
90              // Transcoding is useless
91              // Map
92              String mapped = PrepareString.mapCaseSensitive( value );
93  
94              // Normalize
95              String normalized = PrepareString.normalize( mapped );
96              
97              char[] chars = normalized.toCharArray();
98              
99              // Prohibit
100             PrepareString.checkProhibited( chars );
101             
102             // Bidi is ignored
103             
104             // Insignificant Characters Handling
105             switch ( assertionType )
106             {
107                 case ATTRIBUTE_VALUE :
108                     normValue = PrepareString.insignificantSpacesStringValue( chars );
109                     break;
110                     
111                 case SUBSTRING_INITIAL :
112                     normValue = PrepareString.insignificantSpacesStringInitial( chars );
113                     break;
114                     
115                 case SUBSTRING_ANY :
116                     normValue = PrepareString.insignificantSpacesStringAny( chars );
117                     break;
118                     
119                 case SUBSTRING_FINAL :
120                     normValue = PrepareString.insignificantSpacesStringFinal( chars );
121                     break;
122                     
123                 default :
124                     // Do nothing
125                     break;
126             }
127 
128             return normValue;
129         }
130         catch ( IOException ioe )
131         {
132             throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, I18n.err(
133                 I18n.ERR_13724_INVALID_VALUE, value ), ioe );
134         }
135     }
136 }