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.schema.normalizers;
021    
022    
023    import org.apache.directory.shared.ldap.constants.SchemaConstants;
024    import org.apache.directory.shared.ldap.entry.StringValue;
025    import org.apache.directory.shared.ldap.entry.Value;
026    import org.apache.directory.shared.ldap.exception.LdapException;
027    import org.apache.directory.shared.ldap.name.DN;
028    import org.apache.directory.shared.ldap.schema.Normalizer;
029    import org.apache.directory.shared.ldap.schema.SchemaManager;
030    
031    
032    /**
033     * Normalizer a DN
034     * 
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $Rev$
037     */
038    public class DnNormalizer extends Normalizer
039    {
040        // The serial UID
041        private static final long serialVersionUID = 1L;
042        
043        /** A reference to the schema manager used to normalize the DN */
044        private SchemaManager schemaManager;
045        
046        /**
047         * Empty constructor
048         */
049        public DnNormalizer()
050        {
051            super( SchemaConstants.DISTINGUISHED_NAME_MATCH_MR_OID );
052        }
053    
054    
055        /**
056         * {@inheritDoc}
057         */
058        public Value<?> normalize( Value<?> value ) throws LdapException
059        {
060            DN dn = null;
061            
062            String dnStr = value.getString();
063            
064            dn = new DN( dnStr );
065            
066            dn.normalize( schemaManager.getNormalizerMapping() );
067            return new StringValue( dn.getNormName() );
068        }
069    
070    
071        /**
072         * {@inheritDoc}
073         */
074        public String normalize( String value ) throws LdapException
075        {
076            DN dn = null;
077            
078            dn = new DN( value );
079            
080            dn.normalize( schemaManager.getNormalizerMapping() );
081            return dn.getNormName();
082        }
083    
084    
085        /**
086         * Normalize a DN
087         * @param value The DN to normalize
088         * @return A normalized DN
089         * @throws LdapException
090         */
091        public String normalize( DN value ) throws LdapException
092        {
093            DN dn = null;
094            
095            dn = new DN( value );
096            
097            dn.normalize( schemaManager.getNormalizerMapping() );
098            return dn.getNormName();
099        }
100    
101    
102        /**
103         * {@inheritDoc}
104         */
105        public void setSchemaManager( SchemaManager schemaManager )
106        {
107            this.schemaManager = schemaManager;
108        }
109    }