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.comparators;
021    
022    
023    import org.apache.directory.shared.ldap.exception.LdapException;
024    import org.apache.directory.shared.ldap.exception.LdapInvalidDnException;
025    
026    import org.apache.directory.shared.i18n.I18n;
027    import org.apache.directory.shared.ldap.name.DN;
028    import org.apache.directory.shared.ldap.schema.LdapComparator;
029    import org.apache.directory.shared.ldap.schema.SchemaManager;
030    
031    
032    /**
033     * 
034     * 
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $Rev$
037     */
038    public class DnComparator extends LdapComparator<Object>
039    {
040        /** A reference to the schema manager */ 
041        private transient SchemaManager schemaManager;
042        
043        public DnComparator( String oid )
044        {
045            super( oid );
046        }
047        
048        
049        /**
050         * {@inheritDoc}
051         */
052        public int compare( Object obj0, Object obj1 ) 
053        {
054            DN dn0 = null;
055            DN dn1 = null;
056            
057            try 
058            {
059                dn0 = getDn( obj0 );
060                dn1 = getDn( obj1 );
061            }
062            catch ( LdapException e )
063            {
064                // -- what do we do here ?
065                return -1;
066            }
067            
068            return dn0.compareTo( dn1 );
069        }
070    
071    
072        public DN getDn( Object obj ) throws LdapInvalidDnException
073        {
074            DN dn = null;
075            
076            if ( obj instanceof DN )
077            {
078                dn = (DN)obj;
079                
080                dn = ( dn.isNormalized() ? dn : DN.normalize( dn, schemaManager.getNormalizerMapping() ) );
081            }
082            else if ( obj instanceof String )
083            {
084                dn = new DN( ( String ) obj );
085                dn.normalize( schemaManager.getNormalizerMapping() );
086            }
087            else
088            {
089                throw new IllegalStateException( I18n.err( I18n.ERR_04218, (obj == null ? null : obj.getClass() ) ) );
090            }
091            
092            return dn;
093        }
094    
095    
096        /**
097         * {@inheritDoc}
098         */
099        public void setSchemaManager( SchemaManager schemaManager )
100        {
101            this.schemaManager = schemaManager;
102        }
103    }