directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1688444 - in /directory/shared/trunk/ldap/model/src: main/java/org/apache/directory/api/ldap/model/schema/SchemaUtils.java test/java/org/apache/directory/api/ldap/model/schema/SchemaUtilsTest.java
Date Tue, 30 Jun 2015 13:04:57 GMT
Author: elecharny
Date: Tue Jun 30 13:04:57 2015
New Revision: 1688444

URL: http://svn.apache.org/r1688444
Log:
Added a method to check that an attributeType name is valid

Modified:
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/SchemaUtils.java
    directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/SchemaUtilsTest.java

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/SchemaUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/SchemaUtils.java?rev=1688444&r1=1688443&r2=1688444&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/SchemaUtils.java
(original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/SchemaUtils.java
Tue Jun 30 13:04:57 2015
@@ -553,4 +553,113 @@ public final class SchemaUtils
 
         return bytes;
     }
+
+
+    /**
+     * Tells if an AttributeType name is valid or not. An Attribute name is valid if 
+     * it's a descr / numericoid, as described in rfc4512 :
+     * <pre>
+     * name = descr / numericOid
+     * descr = keystring
+     * keystring = leadkeychar *keychar
+     * leadkeychar = ALPHA
+     * keychar = ALPHA / DIGIT / HYPHEN / USCORE
+     * numericoid = number 1*( DOT number )
+     * number  = DIGIT / ( LDIGIT 1*DIGIT )
+     * ALPHA   = %x41-5A / %x61-7A   ; "A"-"Z" / "a"-"z"
+     * DIGIT   = %x30 / LDIGIT       ; "0"-"9"
+     * HYPHEN  = %x2D ; hyphen ("-")
+     * LDIGIT  = %x31-39             ; "1"-"9"
+     * DOT     = %x2E ; period (".")
+     * USCORE  = %x5F ; underscore ("_")
+     * </pre>
+     * 
+     * Note that we have extended this grammar to accept the '_' char, which is widely used
in teh LDAP world.
+     *
+     * @param attributeName The AttributeType name to check
+     * @return true if it's valid
+     */
+    public static boolean isAttributeNameValid( String attributeName )
+    {
+        if ( Strings.isEmpty( attributeName ) )
+        {
+            return false;
+        }
+        
+        // Check the first char which must be ALPHA or DIGIT
+        boolean descr = false;
+        boolean zero = false;
+        boolean dot = false;
+        
+        char c = attributeName.charAt( 0 );
+        
+        if ( ( ( c >= 'a') && ( c <= 'z' ) ) || ( ( c >= 'A' ) &&
( c <= 'Z' ) ) )
+        {
+            descr = true;
+        }
+        else if ( ( c >= '0' ) && ( c <= '9' ) )
+        {
+            descr = false;
+            
+            zero = c == '0'; 
+        }
+        else
+        {
+            return false;
+        }
+        
+        for ( int i = 1; i < attributeName.length(); i++ )
+        {
+            c = attributeName.charAt( i ); 
+            
+            if ( descr )
+            {
+                // This is a descr, iterate on KeyChars (ALPHA / DIGIT / HYPHEN / USCORE)
+                if ( ( ( c < 'a') || ( c > 'z' ) ) && 
+                     ( ( c < 'A' ) || ( c > 'Z' ) ) && 
+                     ( ( c < '0' ) || ( c > '9' ) ) &&
+                     ( c != '-' ) &&
+                     ( c != '_' ) )
+                {
+                    return false;
+                }
+            }
+            else
+            {
+                // This is a numericOid, check it
+                if ( c == '.' )
+                {
+                    // Not allowed if we already have had a dot
+                    if ( dot )
+                    {
+                        return false;
+                    }
+                    
+                    dot = true;
+                    zero = false;
+                }
+                else if ( ( c >= '0' ) && ( c <= '9' ) )
+                {
+                    dot = false;
+                    
+                    if ( zero )
+                    {
+                        // We can't have a leading '0' followed by another number
+                        return false;
+                    }
+                    else if ( c == '0' )
+                    {
+                        zero = true;
+                    }
+                }
+                else
+                {
+                    // Not valid
+                    return false;
+                }
+            }
+        }
+        
+        return true;
+    }
 }

Modified: directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/SchemaUtilsTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/SchemaUtilsTest.java?rev=1688444&r1=1688443&r2=1688444&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/SchemaUtilsTest.java
(original)
+++ directory/shared/trunk/ldap/model/src/test/java/org/apache/directory/api/ldap/model/schema/SchemaUtilsTest.java
Tue Jun 30 13:04:57 2015
@@ -21,6 +21,8 @@ package org.apache.directory.api.ldap.mo
 
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
 
 import java.util.Arrays;
 import java.util.List;
@@ -155,4 +157,30 @@ public class SchemaUtilsTest
             SchemaUtils.renderQDescrs( new StringBuffer(), Arrays.asList( new String[]
                 { "name1", "name2", "name3" } ) ).toString() );
     }
+    
+    
+    /**
+     * Test the isAttributeNameValid method
+     */
+    @Test
+    public void testIsAttributeNameValid()
+    {
+        assertFalse( SchemaUtils.isAttributeNameValid( null ) );
+        assertFalse( SchemaUtils.isAttributeNameValid( "" ) );
+        assertFalse( SchemaUtils.isAttributeNameValid( "    " ) );
+        
+        // Descr
+        assertTrue( SchemaUtils.isAttributeNameValid( "a" ) );
+        assertTrue( SchemaUtils.isAttributeNameValid( "ObjectClass-text_test123" ) );
+        assertFalse( SchemaUtils.isAttributeNameValid( "-text_test123" ) );
+        assertFalse( SchemaUtils.isAttributeNameValid( "text_te&st123" ) );
+        assertFalse( SchemaUtils.isAttributeNameValid( "text_te st123" ) );
+        
+        // Numericoid
+        assertTrue( SchemaUtils.isAttributeNameValid( "0" ) );
+        assertTrue( SchemaUtils.isAttributeNameValid( "0" ) );
+        assertFalse( SchemaUtils.isAttributeNameValid( "00.1.2.3" ) );
+        assertFalse( SchemaUtils.isAttributeNameValid( "0.1.2..3" ) );
+        assertFalse( SchemaUtils.isAttributeNameValid( "0.01.2.3" ) );
+    }
 }



Mime
View raw message