001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    https://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.directory.api.ldap.model.schema.normalizers;
021
022
023import java.io.IOException;
024
025import org.apache.directory.api.i18n.I18n;
026import org.apache.directory.api.ldap.model.exception.LdapException;
027import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
028import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
029import org.apache.directory.api.ldap.model.schema.Normalizer;
030import org.apache.directory.api.ldap.model.schema.PrepareString;
031import org.apache.directory.api.ldap.model.schema.PreparedNormalizer;
032
033
034/**
035 * Normalizer which trims down whitespace replacing multiple whitespace
036 * characters on the edges and within the string with a single space character
037 * thereby preserving tokenization order.
038 *
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 */
041@SuppressWarnings("serial")
042public class DeepTrimNormalizer extends Normalizer implements PreparedNormalizer
043{
044    /**
045     * Creates a new instance of DeepTrimNormalizer with OID known.
046     * 
047     * @param oid The MR OID to use with this Normalizer
048     */
049    public DeepTrimNormalizer( String oid )
050    {
051        super( oid );
052    }
053
054
055    /**
056     * Creates a new instance of DeepTrimNormalizer when the Normalizer must be
057     * instantiated before setting the OID.
058     */
059    public DeepTrimNormalizer()
060    {
061        super();
062    }
063
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    public String normalize( String value ) throws LdapException
070    {
071        return normalize( value, PrepareString.AssertionType.ATTRIBUTE_VALUE );
072    }
073
074
075    /**
076     * {@inheritDoc}
077     */
078    @Override
079    public String normalize( String value, PrepareString.AssertionType assertionType ) throws LdapException
080    {
081        if ( value == null )
082        {
083            return null;
084        }
085
086        String normValue = null;
087
088        try
089        {
090            // Transcoding is useless
091            // Map
092            String mapped = PrepareString.mapCaseSensitive( value );
093
094            // Normalize
095            String normalized = PrepareString.normalize( mapped );
096            
097            char[] chars = normalized.toCharArray();
098            
099            // 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}