You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2006/07/08 17:24:41 UTC

svn commit: r420117 - /directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java

Author: elecharny
Date: Sat Jul  8 08:24:41 2006
New Revision: 420117

URL: http://svn.apache.org/viewvc?rev=420117&view=rev
Log:
- Added methods which take a String parameter, to avoid array creations
- Improved some method speed 
- Formated the code
- Updated the javadoc

Modified:
    directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java

Modified: directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java
URL: http://svn.apache.org/viewvc/directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java?rev=420117&r1=420116&r2=420117&view=diff
==============================================================================
--- directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java (original)
+++ directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java Sat Jul  8 08:24:41 2006
@@ -16,6 +16,9 @@
  */
 package org.apache.directory.shared.ldap.util;
 
+import org.apache.directory.shared.ldap.util.Position;
+import org.apache.directory.shared.ldap.util.StringTools;
+
 
 /**
  * Utility class used by the LdapDN Parser.
@@ -26,69 +29,151 @@
 {
     // ~ Static fields/initializers
     // -----------------------------------------------------------------
+    /** A value if we got an error while parsing */
+    public static final int PARSING_ERROR = -1;
+
+    /** A value if we got a correct parsing */
+    public static final int PARSING_OK = 0;
+
+    /** If an hex pair contains only one char, this value is returned */
+    public static final int BAD_HEX_PAIR = -2;
+
+    /** A constant representing one char length */
+    public static final int ONE_CHAR = 1;
+
+    /** A constant representing two chars length */
+    public static final int TWO_CHARS = 2;
+
+    /** A constant representing one byte length */
+    public static final int ONE_BYTE = 1;
+
+    /** A constant representing two bytes length */
+    public static final int TWO_BYTES = 2;
 
     /**
      * <safe-init-char> ::= [0x01-0x09] | 0x0B | 0x0C | [0x0E-0x1F] |
      * [0x21-0x39] | 0x3B | [0x3D-0x7F]
      */
     private static final boolean[] SAFE_INIT_CHAR =
-        { false, true, true, true, true, true, true, true, true, true, false, true, true, false, true, true, true,
-            true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false, true,
-            true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
-            true, true, true, true, true, true, false, true, false, true, true, true, true, true, true, true, true,
-            true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
-            true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
-            true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
-            true, true, true, true, true };
+        { 
+            false, true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  false, true,  true,  false, true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            false, true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  false, true,  false, true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true 
+        };
 
     /** <safe-char> ::= [0x01-0x09] | 0x0B | 0x0C | [0x0E-0x7F] */
     private static final boolean[] SAFE_CHAR =
-        { false, true, true, true, true, true, true, true, true, true, false, true, true, false, true, true, true,
-            true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
-            true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
-            true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
-            true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
-            true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
-            true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
-            true, true, true };
+        { 
+            false, true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  false, true,  true,  false, true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+        };
 
     /**
      * <base64-char> ::= 0x2B | 0x2F | [0x30-0x39] | 0x3D | [0x41-0x5A] |
      * [0x61-0x7A]
      */
     private static final boolean[] BASE64_CHAR =
-        { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
-            false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
-            false, false, false, false, false, false, false, false, false, false, false, false, false, true, false,
-            false, false, true, true, true, true, true, true, true, true, true, true, true, false, false, false, true,
-            false, false, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
-            true, true, true, true, true, true, true, true, true, true, true, true, false, false, false, false, false,
-            false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
-            true, true, true, true, true, true, true, true, true, true, false, false, false, false, false };
+        { 
+            false, false, false, false, false, false, false, false, 
+            false, false, false, false, false, false, false, false, 
+            false, false, false, false, false, false, false, false, 
+            false, false, false, false, false, false, false, false, 
+            false, false, false, false, false, false, false, false, 
+            false, false, false, true,  false, false, false, true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  false, false, false, true,  false, false, 
+            false, true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  false, false, false, false, false,
+            false, true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  true,  false, false, false, false, false 
+        };
 
     /**
-     * '"' | '#' | '+' | ',' | [0-9] | ';' | '<' | '=' | '>' | [A-F] | '\' |
+     * ' ' | '"' | '#' | '+' | ',' | [0-9] | ';' | '<' | '=' | '>' | [A-F] | '\' |
      * [a-f] 0x22 | 0x23 | 0x2B | 0x2C | [0x30-0x39] | 0x3B | 0x3C | 0x3D | 0x3E |
      * [0x41-0x46] | 0x5C | [0x61-0x66]
      */
     private static final boolean[] PAIR_CHAR =
-        { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
-            false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
-            false, false, false, false, true, true, false, false, false, false, false, false, false, true, true, false,
-            false, false, true, true, true, true, true, true, true, true, true, true, false, true, true, true, true,
-            false, false, true, true, true, true, true, true, false, false, false, false, false, false, false, false,
-            false, false, false, false, false, false, false, false, false, false, false, false, false, true, false,
-            false, false, false, true, true, true, true, true, true, false, false, false, false, false, false, false,
-            false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
-            false, false, false };
+        { 
+            false, false, false, false, false, false, false, false, 
+            false, false, false, false, false, false, false, false, 
+            false, false, false, false, false, false, false, false, 
+            false, false, false, false, false, false, false, false, 
+            true,  false, true,  true,  false, false, false, false, 
+            false, false, false, true,  true,  false, false, false, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  false, true,  true,  true,  true,  false, 
+            false, true,  true,  true,  true,  true,  true,  false, 
+            false, false, false, false, false, false, false, false, 
+            false, false, false, false, false, false, false, false, 
+            false, false, false, false, true,  false, false, false, 
+            false, true,  true,  true,  true,  true,  true,  false, 
+            false, false, false, false, false, false, false, false, 
+            false, false, false, false, false, false, false, false, 
+            false, false, false, false, false, false, false, false 
+        };
+
+    /**
+     * '"' | '#' | '+' | ',' | [0-9] | ';' | '<' | '=' | '>' | [A-F] | '\' |
+     * [a-f] 0x22 | 0x23 | 0x2B | 0x2C | [0x30-0x39] | 0x3B | 0x3C | 0x3D | 0x3E |
+     * [0x41-0x46] | 0x5C | [0x61-0x66]
+     */
+    private static final int[] STRING_CHAR =
+        { 
+            ONE_CHAR,      ONE_CHAR,      ONE_CHAR,      ONE_CHAR,     // 00 -> 03
+            ONE_CHAR,      ONE_CHAR,      ONE_CHAR,      ONE_CHAR,     // 04 -> 07
+            ONE_CHAR,      ONE_CHAR,      PARSING_ERROR, ONE_CHAR,     // 08 -> 0B
+            ONE_CHAR,      PARSING_ERROR, ONE_CHAR,      ONE_CHAR,     // 0C -> 0F
+            ONE_CHAR,      ONE_CHAR,      ONE_CHAR,      ONE_CHAR,     // 10 -> 13
+            ONE_CHAR,      ONE_CHAR,      ONE_CHAR,      ONE_CHAR,     // 14 -> 17
+            ONE_CHAR,      ONE_CHAR,      ONE_CHAR,      ONE_CHAR,     // 18 -> 1B
+            ONE_CHAR,      ONE_CHAR,      ONE_CHAR,      ONE_CHAR,     // 1C -> 1F
+            ONE_CHAR,      ONE_CHAR,      PARSING_ERROR, PARSING_ERROR,// 20 -> 23
+            ONE_CHAR,      ONE_CHAR,      ONE_CHAR,      ONE_CHAR,     // 24 -> 27
+            ONE_CHAR,      ONE_CHAR,      ONE_CHAR,      PARSING_ERROR,// 28 -> 2B
+            PARSING_ERROR, ONE_CHAR,      ONE_CHAR,      ONE_CHAR,     // 2C -> 2F
+            ONE_CHAR,      ONE_CHAR,      ONE_CHAR,      ONE_CHAR,     // 30 -> 33
+            ONE_CHAR,      ONE_CHAR,      ONE_CHAR,      ONE_CHAR,     // 34 -> 37
+            ONE_CHAR,      ONE_CHAR,      ONE_CHAR,      PARSING_ERROR,// 38 -> 3B
+            PARSING_ERROR, PARSING_ERROR, PARSING_ERROR, ONE_CHAR      // 3C -> 3F
+        };
 
     /** "oid." static */
-    public static final char[] OID_LOWER = new char[]
-        { 'o', 'i', 'd', '.' };
+    public static final String OID_LOWER = "oid.";
 
     /** "OID." static */
-    public static final char[] OID_UPPER = new char[]
-        { 'O', 'I', 'D', '.' };
+    public static final String OID_UPPER = "OID.";
 
     /** "oid." static */
     public static final byte[] OID_LOWER_BYTES = new byte[]
@@ -98,23 +183,6 @@
     public static final byte[] OID_UPPER_BYTES = new byte[]
         { 'O', 'I', 'D', '.' };
 
-    /** A value if we got an error while parsing */
-    public static final int PARSING_ERROR = -1;
-
-    /** If an hex pair contains only one char, this value is returned */
-    public static final int BAD_HEX_PAIR = -2;
-
-    /** A constant representing one char length */
-    public static final int ONE_CHAR = 1;
-
-    /** A constant representing two chars length */
-    public static final int TWO_CHARS = 2;
-
-    /** A constant representing one byte length */
-    public static final int ONE_BYTE = 1;
-
-    /** A constant representing two bytes length */
-    public static final int TWO_BYTES = 2;
 
 
     // ~ Methods
@@ -308,6 +376,48 @@
         }
     }
 
+    /**
+     * Check if the current character is a Pair Char 
+     * 
+     * <pairchar> ::= ' ' | ',' | '=' | '+' | '<' | '>' | '#' | ';' | 
+     *                  '\' | '"' | [0-9a-fA-F] [0-9a-fA-F]
+     * 
+     * @param string
+     *            The string which contains the data
+     * @param index
+     *            Current position in the string
+     * @return <code>true</code> if the current character is a Pair Char
+     */
+    public static int isPairChar( String string, int index )
+    {
+        int length = string.length();
+        
+        if ( ( string == null ) || ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+        {
+            return PARSING_ERROR;
+        }
+        else
+        {
+            char c = string.charAt( index );
+
+            if ( ( c > 127 ) || ( PAIR_CHAR[c] == false ) )
+            {
+                return PARSING_ERROR;
+            }
+            else
+            {
+                if ( StringTools.isHex( string, index++ ) )
+                {
+                    return StringTools.isHex( string, index ) ? 2 : PARSING_ERROR;
+                }
+                else
+                {
+                    return 1;
+                }
+            }
+        }
+    }
+
 
     /**
      * Check if the current character is a String Char. Chars are Unicode, not
@@ -330,10 +440,9 @@
         {
             byte c = byteArray[index];
 
-            if ( ( c == 0x0A ) || ( c == 0x0D ) || ( c == '"' ) || ( c == '#' ) || ( c == '+' ) || ( c == ',' )
-                || ( c == ';' ) || ( c == '<' ) || ( c == '=' ) || ( c == '>' ) )
+            if ( c < 0x40 )
             {
-                return -1;
+                return STRING_CHAR[ c ];
             }
             else
             {
@@ -376,6 +485,40 @@
         }
     }
 
+    /**
+     * Check if the current character is a String Char. Chars are Unicode, not
+     * ASCII. <stringchar> ::= [0x00-0xFFFF] - [,=+<>#;\"\n\r]
+     * 
+     * @param string
+     *            The string which contains the data
+     * @param index
+     *            Current position in the string
+     * @return The current char if it is a String Char, or '#' (this is simpler
+     *         than throwing an exception :)
+     */
+    public static int isStringChar( String string, int index )
+    {
+        int length = string.length();
+        
+        if ( ( string == null ) || ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+        {
+            return PARSING_ERROR;
+        }
+        else
+        {
+            char c = string.charAt( index );
+
+            if ( c < 0x40 )
+            {
+                return STRING_CHAR[ c ];
+            }
+            else
+            {
+                return ONE_CHAR;
+            }
+        }
+    }
+
 
     /**
      * Check if the current character is a Quote Char We are testing Unicode
@@ -438,6 +581,38 @@
         }
     }
 
+    /**
+     * Check if the current character is a Quote Char We are testing Unicode
+     * chars <quotechar> ::= [0x00-0xFFFF] - [\"]
+     * 
+     * @param string The string which contains the data
+     * @param index Current position in the string
+     *
+     * @return <code>true</code> if the current character is a Quote Char
+     */
+    public static int isQuoteChar( String string, int index )
+    {
+        int length = string.length();
+
+        if ( ( string == null ) || ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+        {
+            return PARSING_ERROR;
+        }
+        else
+        {
+            char c = string.charAt( index );
+
+            if ( ( c == '\\' ) || ( c == '"' ) )
+            {
+                return PARSING_ERROR;
+            }
+            else
+            {
+                return ONE_CHAR;
+            }
+        }
+    }
+
 
     /**
      * Parse an hex pair <hexpair> ::= <hex> <hex>
@@ -498,6 +673,34 @@
         }
     }
 
+    /**
+     * Parse an hex pair <hexpair> ::= <hex> <hex>
+     * 
+     * @param string
+     *            The string which contains the data
+     * @param index
+     *            Current position in the string
+     * @return The new position, -1 if the string does not contain an HexPair,
+     *         -2 if the string contains an hex byte but not two.
+     */
+    public static int parseHexPair( String string, int index )
+    {
+        if ( StringTools.isHex( string, index ) )
+        {
+            if ( StringTools.isHex( string, index + 1 ) )
+            {
+                return index + TWO_CHARS;
+            }
+            else
+            {
+                return BAD_HEX_PAIR;
+            }
+        }
+        else
+        {
+            return PARSING_ERROR;
+        }
+    }
 
     /**
      * Parse an hex string, which is a list of hex pairs <hexstring> ::=
@@ -566,6 +769,39 @@
         return ( ( result == BAD_HEX_PAIR ) ? PARSING_ERROR : index );
     }
 
+    /**
+     * Parse an hex string, which is a list of hex pairs <hexstring> ::=
+     * <hexpair> <hexpairs> <hexpairs> ::= <hexpair> <hexpairs> | e
+     * 
+     * @param string
+     *            The string which contains the data
+     * @param index
+     *            Current position in the string
+     * @return Return the first position which is not an hex pair, or -1 if
+     *         there is no hexpair at the beginning or if an hexpair is invalid
+     *         (if we have only one hex instead of 2)
+     */
+    public static int parseHexString( String string, Position pos )
+    {
+        pos.end = pos.start;
+        int result = parseHexPair( string, pos.start );
+
+        if ( result < 0 )
+        {
+            return PARSING_ERROR;
+        }
+        else
+        {
+            pos.end += TWO_CHARS;
+        }
+
+        while ( ( result = parseHexPair( string, pos.end ) ) >= 0 )
+        {
+            pos.end += TWO_CHARS;
+        }
+
+        return ( ( result == BAD_HEX_PAIR ) ? PARSING_ERROR : PARSING_OK );
+    }
 
     /**
      * Walk the buffer while characters are Base64 characters : <base64-string>
@@ -611,5 +847,4 @@
             return index;
         }
     }
-
 }