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.server.schema;
021    
022    
023    import javax.naming.NamingException;
024    
025    import org.apache.directory.server.schema.registries.AttributeTypeRegistry;
026    import org.apache.directory.server.schema.registries.Registries;
027    import org.apache.directory.shared.ldap.entry.Value;
028    import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
029    import org.apache.directory.shared.ldap.name.LdapDN;
030    import org.apache.directory.shared.ldap.schema.Normalizer;
031    
032    
033    /**
034     * Normalizer a DN
035     * 
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Rev$
038     */
039    public class DnNormalizer implements Normalizer
040    {
041        // The serial UID
042        private static final long serialVersionUID = 1L;
043        
044        /** A static instance of this normalizer */
045        public static final DnNormalizer INSTANCE = new DnNormalizer();
046    
047        // @TODO use this later for setting up normalization
048        private AttributeTypeRegistry attrRegistry;
049        
050        
051        public DnNormalizer( AttributeTypeRegistry attrRegistry )
052        {
053            this.attrRegistry = attrRegistry;
054        }
055        
056    
057        /**
058         * Empty constructor
059         */
060        public DnNormalizer()
061        {
062            // Nothing to do
063        }
064    
065    
066        public void setRegistries( Registries registries )
067        {
068            this.attrRegistry = registries.getAttributeTypeRegistry();
069        }
070        
071    
072        /**
073         * {@inheritDoc}
074         */
075        public Value<?> normalize( Value<?> value ) throws NamingException
076        {
077            LdapDN dn = null;
078            
079            String dnStr = value.getString();
080            
081            dn = new LdapDN( dnStr );
082            
083            dn.normalize( attrRegistry.getNormalizerMapping() );
084            return new ClientStringValue( dn.getNormName() );
085        }
086    
087    
088        /**
089         * {@inheritDoc}
090         */
091        public String normalize( String value ) throws NamingException
092        {
093            LdapDN dn = null;
094            
095            dn = new LdapDN( value );
096            
097            dn.normalize( attrRegistry.getNormalizerMapping() );
098            return dn.getNormName();
099        }
100    
101    
102        /**
103         * Normalize a DN
104         * @param value The DN to normalize
105         * @return A normalized DN
106         * @throws NamingException
107         */
108        public String normalize( LdapDN value ) throws NamingException
109        {
110            LdapDN dn = null;
111            
112            dn = new LdapDN( value );
113            
114            dn.normalize( attrRegistry.getNormalizerMapping() );
115            return dn.getNormName();
116        }
117    }