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 *    https://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.exception.LdapException;
025import org.apache.directory.api.ldap.model.name.Dn;
026import org.apache.directory.api.ldap.model.schema.Normalizer;
027import org.apache.directory.api.ldap.model.schema.PrepareString;
028import org.apache.directory.api.ldap.model.schema.SchemaManager;
029
030
031/**
032 * Normalizer a Dn
033 * 
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036@SuppressWarnings("serial")
037public class DnNormalizer extends Normalizer
038{
039    /** A reference to the schema manager used to normalize the Dn */
040    private transient SchemaManager schemaManager;
041
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    @Override
056    public String normalize( String value ) throws LdapException
057    {
058        Dn dn = new Dn( schemaManager, value );
059
060        return dn.getNormName();
061    }
062
063
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    public String normalize( String value, PrepareString.AssertionType assertionType ) throws LdapException
069    {
070        Dn dn = new Dn( schemaManager, value );
071
072        return dn.getNormName();
073    }
074
075
076    /**
077     * Normalize a Dn
078     * @param value The Dn to normalize
079     * @return A normalized Dn
080     * @throws LdapException If the DN is invalid
081     */
082    public String normalize( Dn value ) throws LdapException
083    {
084        Dn dn = value;
085
086        if ( !value.isSchemaAware() )
087        {
088            dn = new Dn( schemaManager, value );
089        }
090
091        return dn.getNormName();
092    }
093
094
095    /**
096     * {@inheritDoc}
097     */
098    @Override
099    public void setSchemaManager( SchemaManager schemaManager )
100    {
101        this.schemaManager = schemaManager;
102    }
103}