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.comparators;
021
022
023import org.apache.directory.api.i18n.I18n;
024import org.apache.directory.api.ldap.model.exception.LdapException;
025import org.apache.directory.api.ldap.model.schema.LdapComparator;
026import org.apache.directory.api.ldap.model.schema.Normalizer;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030
031/**
032 * A comparator which normalizes a value first before using a subordinate
033 * comparator to compare them.
034 * 
035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 */
037public class NormalizingComparator extends LdapComparator<String>
038{
039    /** The serial version UID */
040    private static final long serialVersionUID = 2L;
041
042    /** A logger for this class */
043    private static final Logger LOG = LoggerFactory.getLogger( NormalizingComparator.class );
044
045    /** the underlying comparator to use for comparisons */
046    private LdapComparator<String> comparator;
047
048    private boolean onServer = false;
049
050
051    /**
052     * The NormalizingComparator constructor. Its OID is the  matching rule OID.
053     * 
054     * @param oid The Comparator's OID
055     */
056    public NormalizingComparator( String oid )
057    {
058        super( oid );
059    }
060
061
062    /**
063     * A comparator which normalizes a value first before comparing them.
064     * 
065     * @param oid The Comparator's OID
066     * @param normalizer the Normalizer to normalize values with before comparing
067     * @param comparator the underlying comparator to use for comparisons
068     */
069    public NormalizingComparator( String oid, Normalizer normalizer, LdapComparator<String> comparator )
070    {
071        super( oid );
072        this.normalizer = normalizer;
073        this.comparator = comparator;
074    }
075
076
077    /**
078     * {@inheritDoc}
079     */
080    public int compare( String o1, String o2 )
081    {
082        if ( onServer )
083        {
084            return comparator.compare( o1, o2 );
085        }
086
087        String n1;
088        String n2;
089
090        try
091        {
092            n1 = normalizer.normalize( o1 );
093        }
094        catch ( LdapException e )
095        {
096            if ( LOG.isWarnEnabled() )
097            {
098                LOG.warn( I18n.msg( I18n.MSG_13700_FAILED_TO_NORMALIZE, o1 ), e );
099            }
100            
101            n1 = o1;
102        }
103
104        try
105        {
106            n2 = normalizer.normalize( o2 );
107        }
108        catch ( LdapException e )
109        {
110            if ( LOG.isWarnEnabled() )
111            {
112                LOG.warn( I18n.msg( I18n.MSG_13700_FAILED_TO_NORMALIZE, o2 ), e );
113            }
114            
115            n2 = o2;
116        }
117
118        return comparator.compare( n1, n2 );
119    }
120
121
122    /**
123     * {@inheritDoc}
124     * 
125     * This implementation makes sure we update the oid property of the contained normalizer and 
126     * comparator.
127     */
128    @Override
129    public void setOid( String oid )
130    {
131        super.setOid( oid );
132        normalizer.setOid( oid );
133        comparator.setOid( oid );
134    }
135
136
137    /**
138     * tells that the normalizingComparator should not normalize values which are
139     * already normalized on the server 
140     */
141    public void setOnServer()
142    {
143        this.onServer = true;
144    }
145}