Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 91189 invoked from network); 28 May 2010 12:32:37 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 28 May 2010 12:32:37 -0000 Received: (qmail 64864 invoked by uid 500); 28 May 2010 12:32:37 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 64804 invoked by uid 500); 28 May 2010 12:32:37 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 64797 invoked by uid 99); 28 May 2010 12:32:36 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 28 May 2010 12:32:36 +0000 X-ASF-Spam-Status: No, hits=-1484.2 required=10.0 tests=ALL_TRUSTED,AWL X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 28 May 2010 12:32:35 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id ACB7423889E5; Fri, 28 May 2010 12:32:15 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r949162 - in /directory/shared/trunk: ldap-schema/src/main/resources/schema/ou=schema/cn=system/ou=comparators/ ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/ ldap/src/test/java/org/apache/directory/shared/ldap/sche... Date: Fri, 28 May 2010 12:32:15 -0000 To: commits@directory.apache.org From: elecharny@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100528123215.ACB7423889E5@eris.apache.org> Author: elecharny Date: Fri May 28 12:32:15 2010 New Revision: 949162 URL: http://svn.apache.org/viewvc?rev=949162&view=rev Log: Added comparators for Word and Keyword Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/WordComparator.java directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/comparators/WordComparatorTest.java Modified: directory/shared/trunk/ldap-schema/src/main/resources/schema/ou=schema/cn=system/ou=comparators/m-oid=2.5.13.32.ldif directory/shared/trunk/ldap-schema/src/main/resources/schema/ou=schema/cn=system/ou=comparators/m-oid=2.5.13.33.ldif Modified: directory/shared/trunk/ldap-schema/src/main/resources/schema/ou=schema/cn=system/ou=comparators/m-oid=2.5.13.32.ldif URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap-schema/src/main/resources/schema/ou%3Dschema/cn%3Dsystem/ou%3Dcomparators/m-oid%3D2.5.13.32.ldif?rev=949162&r1=949161&r2=949162&view=diff ============================================================================== --- directory/shared/trunk/ldap-schema/src/main/resources/schema/ou=schema/cn=system/ou=comparators/m-oid=2.5.13.32.ldif (original) +++ directory/shared/trunk/ldap-schema/src/main/resources/schema/ou=schema/cn=system/ou=comparators/m-oid=2.5.13.32.ldif Fri May 28 12:32:15 2010 @@ -3,8 +3,7 @@ dn: m-oid=2.5.13.32,ou=comparators,cn=sy createtimestamp: 20090818022728Z m-oid: 2.5.13.32 entrycsn: 20090818052728.356000Z#000000#000#000000 -m-fqcn: org.apache.directory.shared.ldap.schema.comparators.ComparableComparator - +m-fqcn: org.apache.directory.shared.ldap.schema.comparators.WordComparator objectclass: metaComparator objectclass: metaTop objectclass: top Modified: directory/shared/trunk/ldap-schema/src/main/resources/schema/ou=schema/cn=system/ou=comparators/m-oid=2.5.13.33.ldif URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap-schema/src/main/resources/schema/ou%3Dschema/cn%3Dsystem/ou%3Dcomparators/m-oid%3D2.5.13.33.ldif?rev=949162&r1=949161&r2=949162&view=diff ============================================================================== --- directory/shared/trunk/ldap-schema/src/main/resources/schema/ou=schema/cn=system/ou=comparators/m-oid=2.5.13.33.ldif (original) +++ directory/shared/trunk/ldap-schema/src/main/resources/schema/ou=schema/cn=system/ou=comparators/m-oid=2.5.13.33.ldif Fri May 28 12:32:15 2010 @@ -3,7 +3,7 @@ dn: m-oid=2.5.13.33,ou=comparators,cn=sy createtimestamp: 20090818022727Z m-oid: 2.5.13.33 entrycsn: 20090818052727.972000Z#000000#000#000000 -m-fqcn: org.apache.directory.shared.ldap.schema.comparators.ComparableComparator +m-fqcn: org.apache.directory.shared.ldap.schema.comparators.WordComparator objectclass: metaComparator objectclass: metaTop objectclass: top Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/WordComparator.java URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/WordComparator.java?rev=949162&view=auto ============================================================================== --- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/WordComparator.java (added) +++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/schema/comparators/WordComparator.java Fri May 28 12:32:15 2010 @@ -0,0 +1,147 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.directory.shared.ldap.schema.comparators; + + +import org.apache.directory.shared.ldap.schema.LdapComparator; +import org.apache.directory.shared.ldap.util.StringTools; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + *

+ * A comparator for Words/KeyWords. RFC 4517 par. 4.2.21 (KeywordMatch) and par. + * 4.2.32 is pretty vague about the definition of what is a word or a keyword + * ("...The precise definition of a word is implementation specific...) + * ("...The identification of keywords in the attribute value and the exactness + * of the match are both implementation specific...). + *

+ * We will simply check that the assertion is present in the value at some place, + * after having deep trimmed the word. + *

+ * For instance, the word " World " will be found in the value "Hello world!". + *

+ * A word is defined by the following regexp : "(^|[^A-Za-z0-9])([A-Za-z0-9])*([^A-Za-z0-9]|$)". + * Anything that is not matched by this regexp will not be considered as a word. + * + * @author Apache Directory Project + * @version $Rev$ + */ +public class WordComparator extends LdapComparator +{ + /** A logger for this class */ + private static final Logger LOG = LoggerFactory.getLogger( WordComparator.class ); + + /** The serialVersionUID */ + private static final long serialVersionUID = 1L; + + + /** + * The StringComparator constructor. Its OID is the StringMatch matching + * rule OID. + */ + public WordComparator( String oid ) + { + super( oid ); + } + + + /** + * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) + */ + public int compare( String value, String assertion ) + { + LOG.debug( "comparing String objects '{}' with '{}'", value, assertion ); + + if ( value == assertion ) + { + return 0; + } + + // ------------------------------------------------------------------- + // Handle some basis cases + // ------------------------------------------------------------------- + if ( ( value == null ) || ( assertion == null ) ) + { + return ( assertion == null ) ? 1 : -1; + } + + // Now, trim the assertion and find it in the value + String trimmedAssertion = StringTools.trim( assertion ); + int pos = value.indexOf( trimmedAssertion ); + + if ( pos != -1 ) + { + int assertionLength = trimmedAssertion.length(); + + // Check that we are not in a middle of some text + if ( assertionLength == value.length() ) + { + return 0; + } + + if ( pos == 0 ) + { + char after = value.charAt( assertionLength ); + + if ( !Character.isLetterOrDigit( after ) ) + { + return 0; + } + else + { + return -1; + } + } + + if ( pos + assertionLength == value.length() ) + { + char before = value.charAt( value.length() - assertionLength - 1 ); + + if ( !Character.isLetterOrDigit( before ) ) + { + return 0; + } + else + { + return -1; + } + } + + char before = value.charAt( value.length() - assertionLength ); + char after = value.charAt( assertionLength ); + + if ( Character.isLetterOrDigit( after ) ) + { + return -1; + } + + if ( !Character.isLetterOrDigit( before ) ) + { + return -1; + } + + return 0; + } + + return -1; + } +} Added: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/comparators/WordComparatorTest.java URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/comparators/WordComparatorTest.java?rev=949162&view=auto ============================================================================== --- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/comparators/WordComparatorTest.java (added) +++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/schema/comparators/WordComparatorTest.java Fri May 28 12:32:15 2010 @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.apache.directory.shared.ldap.schema.comparators; + + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + + +/** + * Test the Word/Keyword comparator + * + * @author Apache Directory Project + * @version $Rev$ + */ +public class WordComparatorTest +{ + private WordComparator comparator; + + + @Before + public void init() + { + comparator = new WordComparator( null ); + } + + + @Test + public void testNullWordss() + { + assertEquals( 0, comparator.compare( null, null ) ); + assertEquals( -1, comparator.compare( null, " abc " ) ); + assertEquals( 1, comparator.compare( " abc def ghi ", null ) ); + } + + + @Test + public void testEqualsWords() + { + assertEquals( 0, comparator.compare( "abc", "abc" ) ); + assertEquals( 0, comparator.compare( "abc", " abc " ) ); + assertEquals( 0, comparator.compare( "abc def ghi", "def" ) ); + assertEquals( 0, comparator.compare( "abc def ghi", " def " ) ); + assertEquals( 0, comparator.compare( "abc def ghi", "abc" ) ); + assertEquals( 0, comparator.compare( "abc def ghi", "ghi" ) ); + } + + + @Test + public void testNotEqualsWords() + { + assertEquals( -1, comparator.compare( "abc", "" ) ); + assertEquals( -1, comparator.compare( "abc", "ab" ) ); + assertEquals( -1, comparator.compare( "abc", " ab " ) ); + assertEquals( -1, comparator.compare( "abc def ghi", "dkf" ) ); + assertEquals( -1, comparator.compare( "abc def ghi", " dkf " ) ); + assertEquals( -1, comparator.compare( "abc def ghi", "hi" ) ); + assertEquals( -1, comparator.compare( "abc def ghi", "e" ) ); + } +}