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