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 *    http://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 org.apache.directory.api.ldap.model.constants.SchemaConstants;
024import org.apache.directory.api.ldap.model.entry.StringValue;
025import org.apache.directory.api.ldap.model.entry.Value;
026import org.apache.directory.api.ldap.model.exception.LdapException;
027import org.apache.directory.api.ldap.model.schema.Normalizer;
028import org.apache.directory.api.util.Strings;
029
030
031/**
032 * A normalizer which transforms an escape sequence into an internal 
033 * canonical form: into UTF-8 characters presuming the escape sequence
034 * fits that range.  This is used explicity for non-binary attribute
035 * types only.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039@SuppressWarnings("serial")
040public class DefaultStringNormalizer extends Normalizer
041{
042    /** A default String normalizer */
043    private static final DefaultStringNormalizer NORMALIZER = new DefaultStringNormalizer();
044
045
046    protected DefaultStringNormalizer()
047    {
048        // TODO : This is probably not the correct OID ... 
049        super( SchemaConstants.CASE_IGNORE_MATCH_MR_OID );
050    }
051
052
053    /**
054     * {@inheritDoc}
055     */
056    public Value<?> normalize( Value<?> value ) throws LdapException
057    {
058        String str = value.getString();
059
060        if ( Strings.isEmpty( str ) )
061        {
062            return new StringValue( str );
063        }
064
065        return new StringValue( str );
066    }
067
068
069    /**
070     * {@inheritDoc}
071     */
072    public String normalize( String value ) throws LdapException
073    {
074        if ( Strings.isEmpty( value ) )
075        {
076            return value;
077        }
078
079        return value;
080    }
081
082
083    /**
084     * Normalize the given String
085     *
086     * @param string The string to normalize
087     * @return The normalized object
088     * @throws LdapException If the normalization throws an error
089     */
090    public static String normalizeString( String string ) throws LdapException
091    {
092        return NORMALIZER.normalize( string );
093    }
094}