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.ldap.model.schema.LdapComparator;
024import org.apache.directory.api.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    /**
057     * The StringComparator constructor. Its OID is the StringMatch matching
058     * rule OID.
059     * 
060     * @param oid The Comparator's OID
061     */
062    public WordComparator( String oid )
063    {
064        super( oid );
065    }
066
067
068    /**
069     * {@inheritDoc}
070     */
071    public int compare( String value, String assertion )
072    {
073        LOG.debug( "comparing String objects '{}' with '{}'", value, assertion );
074
075        if ( value == assertion )
076        {
077            return 0;
078        }
079
080        // -------------------------------------------------------------------
081        // Handle some basis cases
082        // -------------------------------------------------------------------
083        if ( ( value == null ) || ( assertion == null ) )
084        {
085            return ( assertion == null ) ? 1 : -1;
086        }
087
088        // Now, trim the assertion and find it in the value
089        String trimmedAssertion = Strings.trim( assertion );
090        int pos = value.indexOf( trimmedAssertion );
091
092        if ( pos != -1 )
093        {
094            int assertionLength = trimmedAssertion.length();
095
096            // Check that we are not in a middle of some text
097            if ( assertionLength == value.length() )
098            {
099                return 0;
100            }
101
102            if ( pos == 0 )
103            {
104                char after = value.charAt( assertionLength );
105
106                if ( !Character.isLetterOrDigit( after ) )
107                {
108                    return 0;
109                }
110                else
111                {
112                    return -1;
113                }
114            }
115
116            if ( pos + assertionLength == value.length() )
117            {
118                char before = value.charAt( value.length() - assertionLength - 1 );
119
120                if ( !Character.isLetterOrDigit( before ) )
121                {
122                    return 0;
123                }
124                else
125                {
126                    return -1;
127                }
128            }
129
130            char before = value.charAt( value.length() - assertionLength );
131            char after = value.charAt( assertionLength );
132
133            if ( Character.isLetterOrDigit( after ) )
134            {
135                return -1;
136            }
137
138            if ( !Character.isLetterOrDigit( before ) )
139            {
140                return -1;
141            }
142
143            return 0;
144        }
145
146        return -1;
147    }
148}