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.comparators;
021
022
023import org.apache.directory.shared.ldap.model.schema.LdapComparator;
024import org.apache.directory.shared.util.Strings;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028
029/**
030 * <p>
031 * A comparator for Words/KeyWords. RFC 4517 par. 4.2.21 (KeywordMatch) and par.
032 * 4.2.32 is pretty vague about the definition of what is a word or a keyword
033 * ("...The precise definition of a word is implementation specific...) 
034 * ("...The identification of keywords in the attribute value and the exactness
035 *  of the match are both implementation specific...).
036 * <p>
037 * We will simply check that the assertion is present in the value at some place, 
038 * after having deep trimmed the word.
039 * <p>
040 * For instance, the word "  World  " will be found in the value "Hello world!".
041 * <p>
042 * A word is defined by the following regexp : "(^|[^A-Za-z0-9])([A-Za-z0-9])*([^A-Za-z0-9]|$)". 
043 * Anything that is not matched by this regexp will not be considered as a word.
044 * 
045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046 */
047public class WordComparator extends LdapComparator<String>
048{
049    /** The serial version UID */
050    private static final long serialVersionUID = 2L;
051
052    /** A logger for this class */
053    private static final Logger LOG = LoggerFactory.getLogger( WordComparator.class );
054
055    /**
056     * The StringComparator constructor. Its OID is the StringMatch matching
057     * rule OID.
058     */
059    public WordComparator( String oid )
060    {
061        super( oid );
062    }
063
064
065    /**
066     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
067     */
068    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "ES_COMPARING_PARAMETER_STRING_WITH_EQ",
069                justification ="false positive")
070    public int compare( String value, String assertion )
071    {
072        LOG.debug( "comparing String objects '{}' with '{}'", value, assertion );
073
074        if ( value == assertion )
075        {
076            return 0;
077        }
078
079        // -------------------------------------------------------------------
080        // Handle some basis cases
081        // -------------------------------------------------------------------
082        if ( ( value == null ) || ( assertion == null ) )
083        {
084            return ( assertion == null ) ? 1 : -1;
085        }
086
087        // Now, trim the assertion and find it in the value
088        String trimmedAssertion = Strings.trim(assertion);
089        int pos = value.indexOf( trimmedAssertion );
090
091        if ( pos != -1 )
092        {
093            int assertionLength = trimmedAssertion.length();
094
095            // Check that we are not in a middle of some text
096            if ( assertionLength == value.length() )
097            {
098                return 0;
099            }
100
101            if ( pos == 0 )
102            {
103                char after = value.charAt( assertionLength );
104
105                if ( !Character.isLetterOrDigit( after ) )
106                {
107                    return 0;
108                }
109                else
110                {
111                    return -1;
112                }
113            }
114
115            if ( pos + assertionLength == value.length() )
116            {
117                char before = value.charAt( value.length() - assertionLength - 1 );
118
119                if ( !Character.isLetterOrDigit( before ) )
120                {
121                    return 0;
122                }
123                else
124                {
125                    return -1;
126                }
127            }
128
129            char before = value.charAt( value.length() - assertionLength );
130            char after = value.charAt( assertionLength );
131
132            if ( Character.isLetterOrDigit( after ) )
133            {
134                return -1;
135            }
136
137            if ( !Character.isLetterOrDigit( before ) )
138            {
139                return -1;
140            }
141
142            return 0;
143        }
144
145        return -1;
146    }
147}