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 */
020 package org.apache.directory.shared.ldap.name;
021
022 import javax.naming.NamingException;
023
024 import org.apache.directory.shared.ldap.entry.Value;
025 import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
026 import org.apache.directory.shared.ldap.schema.Normalizer;
027 import org.apache.directory.shared.ldap.util.StringTools;
028
029
030 /**
031 * A normalizer which transforms an escape sequence into an internal
032 * canonical form: into UTF-8 characters presuming the escape sequence
033 * fits that range. This is used explicity for non-binary attribute
034 * types only.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 * @version $Rev$, $Date$
038 */
039 public class DefaultStringNormalizer implements Normalizer
040 {
041 // The serial UID
042 private static final long serialVersionUID = 1L;
043
044 /** A default String normalizer */
045 private static final DefaultStringNormalizer NORMALIZER = new DefaultStringNormalizer();
046
047 /**
048 * {@inheritDoc}
049 */
050 public Value<?> normalize( Value<?> value ) throws NamingException
051 {
052 String str = value.getString();
053
054 if ( StringTools.isEmpty( str ) )
055 {
056 return new ClientStringValue( str );
057 }
058
059 if ( str.charAt( 0 ) == '#' )
060 {
061 return new ClientStringValue( StringTools.decodeHexString( str ) );
062 }
063
064 if ( str.indexOf( '\\' ) != -1 )
065 {
066 return new ClientStringValue( StringTools.decodeEscapedHex( str ) );
067 }
068
069 return new ClientStringValue( str );
070 }
071
072
073 /**
074 * {@inheritDoc}
075 */
076 public String normalize( String value ) throws NamingException
077 {
078 if ( StringTools.isEmpty( value ) )
079 {
080 return value;
081 }
082
083 if ( value.charAt( 0 ) == '#' )
084 {
085 return StringTools.decodeHexString( value );
086 }
087
088 if ( value.indexOf( '\\' ) != -1 )
089 {
090 return StringTools.decodeEscapedHex( value );
091 }
092
093 return value;
094 }
095
096
097 /**
098 * Normalize the given String
099 *
100 * @param string The string to normalize
101 * @return The normalized object
102 * @throws NamingException If the normalization throws an error
103 */
104 public static String normalizeString( String string ) throws NamingException
105 {
106 return NORMALIZER.normalize( string );
107 }
108 }