directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
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 GMT
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;
+
+
+/**
+ * <p>
+ * 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...).
+ * <p>
+ * We will simply check that the assertion is present in the value at some place, 
+ * after having deep trimmed the word.
+ * <p>
+ * For instance, the word "  World  " will be found in the value "Hello world!".
+ * <p>
+ * 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 <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$
+ */
+public class WordComparator extends LdapComparator<String>
+{
+    /** 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 <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @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" ) );
+    }
+}



Mime
View raw message