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.name.Dn;
028import org.apache.directory.api.ldap.model.schema.Normalizer;
029import org.apache.directory.api.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    /**
045     * Empty constructor
046     */
047    public DnNormalizer()
048    {
049        super( SchemaConstants.DISTINGUISHED_NAME_MATCH_MR_OID );
050    }
051
052
053    /**
054     * {@inheritDoc}
055     */
056    @Override
057    public Value<?> normalize( Value<?> value ) throws LdapException
058    {
059        Dn dn;
060
061        String dnStr = value.getString();
062
063        dn = new Dn( schemaManager, dnStr );
064
065        return new StringValue( dn.getNormName() );
066    }
067
068
069    /**
070     * {@inheritDoc}
071     */
072    @Override
073    public String normalize( String value ) throws LdapException
074    {
075        Dn dn;
076
077        dn = new Dn( schemaManager, value );
078
079        return dn.getNormName();
080    }
081
082
083    /**
084     * Normalize a Dn
085     * @param value The Dn to normalize
086     * @return A normalized Dn
087     * @throws LdapException If the DN is invalid
088     */
089    public String normalize( Dn value ) throws LdapException
090    {
091        Dn dn;
092
093        dn = value.apply( schemaManager );
094
095        return dn.getNormName();
096    }
097
098
099    /**
100     * {@inheritDoc}
101     */
102    @Override
103    public void setSchemaManager( SchemaManager schemaManager )
104    {
105        this.schemaManager = schemaManager;
106    }
107}