directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r427199 - in /directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util: AttributeUtils.java ComponentsMonitor.java DNUtils.java Position.java StringTools.java
Date Mon, 31 Jul 2006 18:45:23 GMT
Author: elecharny
Date: Mon Jul 31 11:45:22 2006
New Revision: 427199

URL: http://svn.apache.org/viewvc?rev=427199&view=rev
Log:
Merging changes form optimization branch of shared/ldap/src/main/java/org/apache/directory/shared/ldap/util back into this 1.1 trunks
branch.  Here's the merge command used: svn merge -r 414084:427187 https://svn.apache.org/repos/asf/directory/branches/shared/optimization .

Added:
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/Position.java
      - copied unchanged from r427187, directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/util/Position.java
Modified:
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/ComponentsMonitor.java
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java

Modified: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java?rev=427199&r1=427198&r2=427199&view=diff
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java (original)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java Mon Jul 31 11:45:22 2006
@@ -23,6 +23,7 @@
 
 import javax.naming.directory.Attribute;
 import javax.naming.directory.Attributes;
+import javax.naming.directory.ModificationItem;
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 
@@ -35,7 +36,88 @@
  */
 public class AttributeUtils
 {
-    public static boolean containsValue( Attribute attr, Object compared, AttributeType type ) throws NamingException
+    /**
+     * Utility method to extract an attribute from Attributes object using
+     * all combinationos of the name including aliases.
+     * 
+     * @param attrs the Attributes to get the Attribute object from
+     * @param type the attribute type specification
+     * @return an Attribute with matching the attributeType spec or null
+     */
+    public final static Attribute getAttribute( Attributes attrs, AttributeType type )
+    {
+        // optimization bypass to avoid cost of the loop below
+        if ( type.getNames().length == 1 )
+        {
+            return attrs.get( type.getNames()[0] );
+        }
+        
+        // check if the attribute's OID is used
+        if ( attrs.get( type.getOid() ) != null )
+        {
+            return attrs.get( type.getOid() );
+        }
+        
+        // iterate through aliases
+        for ( int ii = 0; ii < type.getNames().length; ii++ )
+        {
+            if ( attrs.get( type.getNames()[ii] ) != null )
+            {
+                return attrs.get( type.getNames()[ii] );
+            }
+        }
+        
+        return null;
+    }
+    
+    
+    /**
+     * Utility method to extract an attribute from an array of modifications.
+     * 
+     * @param mods the array of ModificationItems to extract the Attribute from.
+     * @param type the attributeType spec of the Attribute to extract
+     * @return the extract Attribute or null if no such attribute exists
+     */
+    public final static Attribute getAttribute( ModificationItem[] mods, AttributeType type )
+    {
+        // optimization bypass to avoid cost of the loop below
+        if ( type.getNames().length == 1 )
+        {
+            for ( int jj = 0; jj < mods.length; jj++ )
+            {
+                if ( mods[jj].getAttribute().getID().equalsIgnoreCase( type.getNames()[0] ) )
+                {
+                    return mods[jj].getAttribute();
+                }
+            }
+        }
+        
+        // check if the attribute's OID is used
+        for ( int jj = 0; jj < mods.length; jj++ )
+        {
+            if ( mods[jj].getAttribute().getID().equals( type.getOid() ) )
+            {
+                return mods[jj].getAttribute();
+            }
+        }
+        
+        // iterate through aliases
+        for ( int ii = 0; ii < type.getNames().length; ii++ )
+        {
+            for ( int jj = 0; jj < mods.length; jj++ )
+            {
+                if ( mods[jj].getAttribute().getID().equalsIgnoreCase( type.getNames()[ii] ) )
+                {
+                    return mods[jj].getAttribute();
+                }
+            }
+        }
+        
+        return null;
+    }
+    
+    
+    public final static boolean containsValue( Attribute attr, Object compared, AttributeType type ) throws NamingException
     {
         // quick bypass test
         if ( attr.contains( compared ) )

Modified: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/ComponentsMonitor.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/ComponentsMonitor.java?rev=427199&r1=427198&r2=427199&view=diff
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/ComponentsMonitor.java (original)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/ComponentsMonitor.java Mon Jul 31 11:45:22 2006
@@ -1,3 +1,19 @@
+/*
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed 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.util;
 
 

Modified: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java?rev=427199&r1=427198&r2=427199&view=diff
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java (original)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java Mon Jul 31 11:45:22 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;
         }
     }
-
 }

Modified: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java?rev=427199&r1=427198&r2=427199&view=diff
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java (original)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java Mon Jul 31 11:45:22 2006
@@ -46,6 +46,188 @@
     private static final String DEFAULT_CHARSET_JDK_1_5 = Charset.defaultCharset().name();
     private static final String JAVA_VERSION = System.getProperty( "java.version" );
     
+    // ~ Static fields/initializers
+    // -----------------------------------------------------------------
+
+    /** Hex chars */
+    private static final byte[] HEX_CHAR = new byte[]
+        { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
+
+    private static int UTF8_MULTI_BYTES_MASK = 0x0080;
+
+    private static int UTF8_TWO_BYTES_MASK = 0x00E0;
+
+    private static int UTF8_TWO_BYTES = 0x00C0;
+
+    private static int UTF8_THREE_BYTES_MASK = 0x00F0;
+
+    private static int UTF8_THREE_BYTES = 0x00E0;
+
+    private static int UTF8_FOUR_BYTES_MASK = 0x00F8;
+
+    private static int UTF8_FOUR_BYTES = 0x00F0;
+
+    private static int UTF8_FIVE_BYTES_MASK = 0x00FC;
+
+    private static int UTF8_FIVE_BYTES = 0x00F8;
+
+    private static int UTF8_SIX_BYTES_MASK = 0x00FE;
+
+    private static int UTF8_SIX_BYTES = 0x00FC;
+
+    /** <alpha> ::= [0x41-0x5A] | [0x61-0x7A] */
+    public static final boolean[] ALPHA =
+        { 
+            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, false, false, 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, true,  true,  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 
+        };
+
+    /** <alpha> | <digit> | '-' */
+    public static final boolean[] 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, false, false, true,  false, false, 
+            true,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  false, 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, true,  true,  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' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' */
+    public static final boolean[] DIGIT =
+        { 
+            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,  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, false, false, false, false, false, false, 
+            false, false, false, false, false, false, false, false, 
+            false, false, false, false, false, false, false, false
+        };
+
+    /** <hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66] */
+    private static final boolean[] HEX =
+        { 
+            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,  true,  true,  true,  true,  true,  true,  true, 
+            true,  true,  false, false, false, 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, 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 };
+
+    /** <hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66] */
+    public static final byte[] HEX_VALUE =
+        { 
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00 -> 0F
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10 -> 1F
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 20 -> 2F
+             0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1, // 30 -> 3F ( 0, 1,2, 3, 4,5, 6, 7, 8, 9 )
+            -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 40 -> 4F ( A, B, C, D, E, F )
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 50 -> 5F
+            -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1  // 60 -> 6F ( a, b, c, d, e, f )
+        };
+
+    /** lowerCase = 'a' .. 'z', '0'..'9', '-' */
+    public static final char[] LOWER_CASE =
+        { 
+              0,   0,   0,   0,   0,   0,   0,   0, 
+              0,   0,   0,   0,   0,   0,   0,   0, 
+              0,   0,   0,   0,   0,   0,   0,   0, 
+              0,   0,   0,   0,   0,   0,   0,   0, 
+              0,   0,   0,   0,   0,   0,   0,   0, 
+              0,   0,   0,   0,   0, '-',   0,   0, 
+            '0', '1', '2', '3', '4', '5', '6', '7', 
+            '8', '9',   0,   0,   0,   0,   0,   0, 
+              0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 
+            'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
+            'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 
+            'x', 'y', 'z',   0,   0,   0,   0,   0, 
+              0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 
+            'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 
+            'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 
+            'x', 'y', 'z',   0,   0,   0,   0,   0, 
+              0,   0,   0,   0,   0,   0,   0,   0, 
+              0,   0,   0,   0,   0,   0,   0,   0, 
+              0,   0,   0,   0,   0,   0,   0,   0, 
+              0,   0,   0,   0,   0,   0,   0,   0, 
+              0,   0,   0,   0,   0,   0,   0,   0, 
+              0,   0,   0,   0,   0,   0,   0,   0, 
+              0,   0,   0,   0,   0,   0,   0,   0, 
+              0,   0,   0,   0,   0,   0,   0,   0 
+        };
+
+    private static int CHAR_ONE_BYTE_MASK = 0xFFFFFF80;
+
+    private static int CHAR_TWO_BYTES_MASK = 0xFFFFF800;
+
+    private static int CHAR_THREE_BYTES_MASK = 0xFFFF0000;
+
+    private static int CHAR_FOUR_BYTES_MASK = 0xFFE00000;
+
+    private static int CHAR_FIVE_BYTES_MASK = 0xFC000000;
+
+    private static int CHAR_SIX_BYTES_MASK = 0x80000000;
+
+    public static final int NOT_EQUAL = -1;
+
+    // The following methods are taken from org.apache.commons.lang.StringUtils
+
+    /**
+     * The empty String <code>""</code>.
+     * 
+     * @since 2.0
+     */
+    public static final String EMPTY = "";
+
+    /**
+     * The empty byte[]
+     */
+    public static final byte[] EMPTY_BYTES = new byte[]
+        {};
+
     /**
      * Trims several consecutive characters into one.
      * 
@@ -134,6 +316,8 @@
      *            how to normalize for case: upper or lower
      * @return the deep trimmed string
      * @see StringTools#deepTrim( String )
+     * 
+     * @TODO Replace the toCharArray() by substrig manipulations
      */
     public static String deepTrim( String str, boolean toLowerCase )
     {
@@ -198,159 +382,184 @@
         return ( pos == 0 ? "" : new String( newbuf, 0, ( wsSeen ? pos - 1 : pos ) ) );
     }
 
-
     /**
      * Truncates large Strings showing a portion of the String's head and tail
      * with the center cut out and replaced with '...'. Also displays the total
      * length of the truncated string so size of '...' can be interpreted.
      * Useful for large strings in UIs or hex dumps to log files.
      * 
-     * @param a_str
+     * @param str
      *            the string to truncate
-     * @param a_head
+     * @param head
      *            the amount of the head to display
-     * @param a_tail
+     * @param tail
      *            the amount of the tail to display
      * @return the center truncated string
      */
-    public static String centerTrunc( String a_str, int a_head, int a_tail )
+    public static String centerTrunc( String str, int head, int tail )
     {
-        StringBuffer l_buf = null;
+        StringBuffer buf = null;
 
         // Return as-is if String is smaller than or equal to the head plus the
         // tail plus the number of characters added to the trunc representation
         // plus the number of digits in the string length.
-        if ( a_str.length() <= ( a_head + a_tail + 7 + a_str.length() / 10 ) )
+        if ( str.length() <= ( head + tail + 7 + str.length() / 10 ) )
         {
-            return a_str;
+            return str;
         }
 
-        l_buf = new StringBuffer();
-        l_buf.append( '[' ).append( a_str.length() ).append( "][" );
-        l_buf.append( a_str.substring( 0, a_head ) ).append( "..." );
-        l_buf.append( a_str.substring( a_str.length() - a_tail ) );
-        l_buf.append( ']' );
-        return l_buf.toString();
+        buf = new StringBuffer();
+        buf.append( '[' ).append( str.length() ).append( "][" );
+        buf.append( str.substring( 0, head ) ).append( "..." );
+        buf.append( str.substring( str.length() - tail ) );
+        buf.append( ']' );
+        return buf.toString();
     }
 
 
     /**
      * Gets a hex string from byte array.
      * 
-     * @param a_res
+     * @param res
      *            the byte array
      * @return the hex string representing the binary values in the array
      */
-    public static String toHexString( byte[] a_res )
+    public static String toHexString( byte[] res )
     {
-        StringBuffer l_buf = new StringBuffer( a_res.length << 1 );
-        for ( int ii = 0; ii < a_res.length; ii++ )
+        StringBuffer buf = new StringBuffer( res.length << 1 );
+        
+        for ( int ii = 0; ii < res.length; ii++ )
         {
-            String l_digit = Integer.toHexString( 0xFF & a_res[ii] );
-            if ( l_digit.length() == 1 )
+            String digit = Integer.toHexString( 0xFF & res[ii] );
+            
+            if ( digit.length() == 1 )
             {
-                l_digit = '0' + l_digit;
+                digit = '0' + digit;
             }
-            l_buf.append( l_digit );
+            
+            buf.append( digit );
         }
-        return l_buf.toString().toUpperCase();
+        return buf.toString().toUpperCase();
     }
 
-
+    /**
+     * Rewrote the toLowercase method to improve performances.
+     * In Ldap, attributesType are supposed to use ASCII chars :
+     * 'a'-'z', 'A'-'Z', '0'-'9', '.' and '-' only.
+     * 
+     * @param value The String to lowercase
+     * @return The lowercase string
+     */
+    public static String toLowerCase( String value )
+    {
+        char[] chars = value.toCharArray();
+        
+        for ( int i = 0; i < chars.length; i++ )
+        {
+            chars[i] = LOWER_CASE[ chars[i]];
+        }
+        
+        return new String( chars );
+    }
+    
     /**
      * Get byte array from hex string
      * 
-     * @param a_hexString
+     * @param hexString
      *            the hex string to convert to a byte array
      * @return the byte form of the hex string.
      */
-    public static byte[] toByteArray( String a_hexString )
+    public static byte[] toByteArray( String hexString )
     {
-        int l_arrLength = a_hexString.length() >> 1;
-        byte l_buf[] = new byte[l_arrLength];
-        for ( int ii = 0; ii < l_arrLength; ii++ )
-        {
-            int l_index = ii << 1;
-            String l_digit = a_hexString.substring( l_index, l_index + 2 );
-            l_buf[ii] = ( byte ) Integer.parseInt( l_digit, 16 );
+        int arrLength = hexString.length() >> 1;
+        byte buf[] = new byte[arrLength];
+        
+        for ( int ii = 0; ii < arrLength; ii++ )
+        {
+            int index = ii << 1;
+            
+            String l_digit = hexString.substring( index, index + 2 );
+            buf[ii] = ( byte ) Integer.parseInt( l_digit, 16 );
         }
-        return l_buf;
+        
+        return buf;
     }
 
 
     /**
      * This method is used to insert HTML block dynamically
      * 
-     * @param a_source
+     * @param source
      *            the HTML code to be processes
-     * @param a_bReplaceNl
+     * @param replaceNl
      *            if true '\n' will be replaced by <br>
-     * @param a_bReplaceTag
+     * @param replaceTag
      *            if true '<' will be replaced by &lt; and '>' will be replaced
      *            by &gt;
-     * @param a_bReplaceQuote
+     * @param replaceQuote
      *            if true '\"' will be replaced by &quot;
      * @return the formated html block
      */
-    public static String formatHtml( String a_source, boolean a_bReplaceNl, boolean a_bReplaceTag,
-        boolean a_bReplaceQuote )
+    public static String formatHtml( String source, boolean replaceNl, boolean replaceTag,
+        boolean replaceQuote )
     {
-        StringBuffer l_buf = new StringBuffer();
-        int l_len = a_source.length();
+        StringBuffer buf = new StringBuffer();
+        int len = source.length();
 
-        for ( int ii = 0; ii < l_len; ii++ )
+        for ( int ii = 0; ii < len; ii++ )
         {
-            char ch = a_source.charAt( ii );
+            char ch = source.charAt( ii );
+            
             switch ( ch )
             {
                 case '\"':
-                    if ( a_bReplaceQuote )
+                    if ( replaceQuote )
                     {
-                        l_buf.append( "&quot;" );
+                        buf.append( "&quot;" );
                     }
                     else
                     {
-                        l_buf.append( ch );
+                        buf.append( ch );
                     }
                     break;
 
                 case '<':
-                    if ( a_bReplaceTag )
+                    if ( replaceTag )
                     {
-                        l_buf.append( "&lt;" );
+                        buf.append( "&lt;" );
                     }
                     else
                     {
-                        l_buf.append( ch );
+                        buf.append( ch );
                     }
                     break;
 
                 case '>':
-                    if ( a_bReplaceTag )
+                    if ( replaceTag )
                     {
-                        l_buf.append( "&gt;" );
+                        buf.append( "&gt;" );
                     }
                     else
                     {
-                        l_buf.append( ch );
+                        buf.append( ch );
                     }
                     break;
 
                 case '\n':
-                    if ( a_bReplaceNl )
+                    if ( replaceNl )
                     {
-                        if ( a_bReplaceTag )
+                        if ( replaceTag )
                         {
-                            l_buf.append( "&lt;br&gt;" );
+                            buf.append( "&lt;br&gt;" );
                         }
                         else
                         {
-                            l_buf.append( "<br>" );
+                            buf.append( "<br>" );
                         }
                     }
                     else
                     {
-                        l_buf.append( ch );
+                        buf.append( ch );
                     }
                     break;
 
@@ -358,16 +567,16 @@
                     break;
 
                 case '&':
-                    l_buf.append( "&amp;" );
+                    buf.append( "&amp;" );
                     break;
 
                 default:
-                    l_buf.append( ch );
+                    buf.append( ch );
                     break;
             }
         }
 
-        return l_buf.toString();
+        return buf.toString();
     }
 
 
@@ -375,11 +584,11 @@
      * Creates a regular expression from an LDAP substring assertion filter
      * specification.
      * 
-     * @param a_initial
+     * @param initialPattern
      *            the initial fragment before wildcards
-     * @param a_any
+     * @param anyPattern
      *            fragments surrounded by wildcards if any
-     * @param a_final
+     * @param finalPattern
      *            the final fragment after last wildcard if any
      * @return the regular expression for the substring match filter
      * @throws RESyntaxException
@@ -421,7 +630,7 @@
      * Generates a regular expression from an LDAP substring match expression by
      * parsing out the supplied string argument.
      * 
-     * @param a_ldapRegex
+     * @param ldapRegex
      *            the substring match expression
      * @return the regular expression for the substring match filter
      * @throws RESyntaxException
@@ -489,50 +698,50 @@
      * components returned. If the filter is null all path components are
      * returned.
      * 
-     * @param a_paths
+     * @param paths
      *            a set of paths delimited using the OS path separator
-     * @param a_filter
+     * @param filter
      *            a FileFilter used to filter the return set
      * @return the filter accepted path component Strings in the order
      *         encountered
      */
-    public static List getPaths( String a_paths, FileFilter a_filter )
+    public static List getPaths( String paths, FileFilter filter )
     {
-        final int l_max = a_paths.length() - 1;
-        int l_start = 0;
-        int l_stop = -1;
-        String l_path = null;
-        ArrayList l_list = new ArrayList();
+        final int max = paths.length() - 1;
+        int start = 0;
+        int stop = -1;
+        String path = null;
+        ArrayList list = new ArrayList();
 
         // Abandon with no values if paths string is null
-        if ( a_paths == null || a_paths.trim().equals( "" ) )
+        if ( paths == null || paths.trim().equals( "" ) )
         {
-            return l_list;
+            return list;
         }
 
         // Loop spliting string using OS path separator: terminate
         // when the start index is at the end of the paths string.
-        while ( l_start < l_max )
+        while ( start < max )
         {
-            l_stop = a_paths.indexOf( File.pathSeparatorChar, l_start );
+            stop = paths.indexOf( File.pathSeparatorChar, start );
 
             // The is no file sep between the start and the end of the string
-            if ( l_stop == -1 )
+            if ( stop == -1 )
             {
                 // If we have a trailing path remaining without ending separator
-                if ( l_start < l_max )
+                if ( start < max )
                 {
                     // Last path is everything from start to the string's end
-                    l_path = a_paths.substring( l_start );
+                    path = paths.substring( start );
 
                     // Protect against consecutive separators side by side
-                    if ( !l_path.trim().equals( "" ) )
+                    if ( !path.trim().equals( "" ) )
                     {
                         // If filter is null add path, if it is not null add the
                         // path only if the filter accepts the path component.
-                        if ( a_filter == null || a_filter.accept( new File( l_path ) ) )
+                        if ( filter == null || filter.accept( new File( path ) ) )
                         {
-                            l_list.add( l_path );
+                            list.add( path );
                         }
                     }
                 }
@@ -543,154 +752,26 @@
             // There is a separator between start and the end if we got here!
             // start index is now at 0 or the index of last separator + 1
             // stop index is now at next separator in front of start index
-            l_path = a_paths.substring( l_start, l_stop );
+            path = paths.substring( start, stop );
 
             // Protect against consecutive separators side by side
-            if ( !l_path.trim().equals( "" ) )
+            if ( !path.trim().equals( "" ) )
             {
                 // If filter is null add path, if it is not null add the path
                 // only if the filter accepts the path component.
-                if ( a_filter == null || a_filter.accept( new File( l_path ) ) )
+                if ( filter == null || filter.accept( new File( path ) ) )
                 {
-                    l_list.add( l_path );
+                    list.add( path );
                 }
             }
 
             // Advance start index past separator to start of next path comp
-            l_start = l_stop + 1;
+            start = stop + 1;
         }
 
-        return l_list;
+        return list;
     }
 
-    // ~ Static fields/initializers
-    // -----------------------------------------------------------------
-
-    /** Hex chars */
-    private static final byte[] HEX_CHAR = new byte[]
-        { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
-
-    private static int UTF8_MULTI_BYTES_MASK = 0x0080;
-
-    private static int UTF8_TWO_BYTES_MASK = 0x00E0;
-
-    private static int UTF8_TWO_BYTES = 0x00C0;
-
-    private static int UTF8_THREE_BYTES_MASK = 0x00F0;
-
-    private static int UTF8_THREE_BYTES = 0x00E0;
-
-    private static int UTF8_FOUR_BYTES_MASK = 0x00F8;
-
-    private static int UTF8_FOUR_BYTES = 0x00F0;
-
-    private static int UTF8_FIVE_BYTES_MASK = 0x00FC;
-
-    private static int UTF8_FIVE_BYTES = 0x00F8;
-
-    private static int UTF8_SIX_BYTES_MASK = 0x00FE;
-
-    private static int UTF8_SIX_BYTES = 0x00FC;
-
-    /** <alpha> ::= [0x41-0x5A] | [0x61-0x7A] */
-    public static final boolean[] ALPHA =
-        { 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, false, false, 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, true, true, 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 };
-
-    /** <alpha> | <digit> | '-' */
-    public static final boolean[] 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, false, false,
-            true, false, false, true, true, true, true, true, true, true, true, true, true, false, 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, true, true, 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' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' */
-    public static final boolean[] DIGIT =
-        { 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, 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,
-            false, false, false, false, false, false, false, false, false, false, false, false, false, false, false,
-            false, false, false, false, false, false, false, };
-
-    /** <hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66] */
-    private static final boolean[] HEX =
-        { 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, true, true, true, true, true, true, true, true, true, false, false, false,
-            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, 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 };
-
-    /** <hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66] */
-    public static final byte[] HEX_VALUE =
-        { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00
-                                                                            // ->
-                                                                            // 0F
-            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10
-                                                                            // ->
-                                                                            // 1F
-            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 20
-                                                                            // ->
-                                                                            // 2F
-            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 30 -> 3F
-                                                                    // ( 0, 1,
-                                                                    // 2, 3, 4,
-                                                                    // 5, 6, 7,
-                                                                    // 8, 9 )
-            -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 40
-                                                                            // ->
-                                                                            // 4F (
-                                                                            // A,
-                                                                            // B,
-                                                                            // C,
-                                                                            // D,
-                                                                            // E, F
-                                                                            // )
-            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 50
-                                                                            // ->
-                                                                            // 5F
-            -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1 // 60
-                                                                            // ->
-                                                                            // 6F (
-                                                                            // a,
-                                                                            // b,
-                                                                            // c,
-                                                                            // d,
-                                                                            // e, f
-                                                                            // )
-        };
-
-    private static int CHAR_ONE_BYTE_MASK = 0xFFFFFF80;
-
-    private static int CHAR_TWO_BYTES_MASK = 0xFFFFF800;
-
-    private static int CHAR_THREE_BYTES_MASK = 0xFFFF0000;
-
-    private static int CHAR_FOUR_BYTES_MASK = 0xFFE00000;
-
-    private static int CHAR_FIVE_BYTES_MASK = 0xFC000000;
-
-    private static int CHAR_SIX_BYTES_MASK = 0x80000000;
-
-    public static final int NOT_EQUAL = -1;
-
 
     // ~ Methods
     // ------------------------------------------------------------------------------------
@@ -1199,6 +1280,34 @@
         }
     }
 
+    /**
+     * Check if a text is present at the current position in another string.
+     * 
+     * @param string1
+     *            The string which contains the data
+     * @param index
+     *            Current position in the string
+     * @param test
+     *            The text we want to check
+     * @return <code>true</code> if the string contains the text.
+     */
+    public static boolean areEquals( String string1, int index, String text )
+    {
+        int length1 = string1.length();
+        int length2 = text.length();
+
+        if ( ( string1 == null ) || ( length1 == 0 ) || ( length1 <= index ) || ( index < 0 )
+            || ( text == null ) || ( length2 == 0 )
+            || ( length2 > ( length1 + index ) ) )
+        {
+            return false;
+        }
+        else
+        {
+            return string1.substring( index ).startsWith( text );
+        }
+    }
+    
 
     /**
      * Check if a text is present at the current position in a buffer.
@@ -1288,6 +1397,56 @@
         }
     }
 
+    /**
+     * Test if the current character is equal to a specific character.
+     * 
+     * @param string
+     *            The String which contains the data
+     * @param index
+     *            Current position in the string
+     * @param car
+     *            The character we want to compare with the current string
+     *            position
+     * @return <code>true</code> if the current character equals the given
+     *         character.
+     */
+    public static boolean isCharASCII( String string, int index, char car )
+    {
+        int length = string.length();
+        
+        if ( ( string == null ) || ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+        {
+            return false;
+        }
+        else
+        {
+            return string.charAt( index ) == car;
+        }
+    }
+
+    /**
+     * Get the character at a given position in a string, checking fo limits
+     * 
+     * @param string
+     *            The string which contains the data
+     * @param index
+     *            Current position in the string
+     * @return The character ar the given position, or '\0' if something went wrong 
+     */
+    public static char charAt( String string, int index )
+    {
+        int length = string.length();
+        
+        if ( ( string == null ) || ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+        {
+            return '\0';
+        }
+        else
+        {
+            return string.charAt( index ) ;
+        }
+    }
+
 
     /**
      * Check if the current character is an Hex Char <hex> ::= [0x30-0x39] |
@@ -1352,6 +1511,39 @@
         }
     }
 
+    /**
+     * Check if the current character is an Hex Char <hex> ::= [0x30-0x39] |
+     * [0x41-0x46] | [0x61-0x66]
+     * 
+     * @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 Hex Char
+     */
+    public static boolean isHex( String string, int index )
+    {
+        int length = string.length();
+        
+        if ( ( string == null ) || ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+        {
+            return false;
+        }
+        else
+        {
+            char c = string.charAt( index );
+
+            if ( ( c > 127 ) || ( HEX[c] == false ) )
+            {
+                return false;
+            }
+            else
+            {
+                return true;
+            }
+        }
+    }
+    
 
     /**
      * Test if the current character is a digit <digit> ::= '0' | '1' | '2' |
@@ -1373,6 +1565,19 @@
         }
     }
 
+    /**
+     * Test if the current character is a digit <digit> ::= '0' | '1' | '2' |
+     * '3' | '4' | '5' | '6' | '7' | '8' | '9'
+     * 
+     * @param car the character to test
+     *            
+     * @return <code>true</code> if the character is a Digit
+     */
+    public static boolean isDigit( char car )
+    {
+        return ( car >= '0' ) && ( car <= '9' );
+    }
+
 
     /**
      * Test if the current character is an Alpha character : <alpha> ::=
@@ -1439,6 +1644,40 @@
         }
     }
 
+    /**
+     * Test if the current character is an Alpha character : <alpha> ::=
+     * [0x41-0x5A] | [0x61-0x7A]
+     * 
+     * @param string
+     *            The string which contains the data
+     * @param index
+     *            Current position in the string
+     * @return <code>true</code> if the current character is an Alpha
+     *         character
+     */
+    public static boolean isAlphaASCII( String string, int index )
+    {
+        int length = string.length();
+        
+        if ( ( string == null ) || ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+        {
+            return false;
+        }
+        else
+        {
+            char c = string.charAt( index++ );
+
+            if ( ( c > 127 ) || ( ALPHA[c] == false ) )
+            {
+                return false;
+            }
+            else
+            {
+                return true;
+            }
+        }
+    }
+
 
     /**
      * Test if the current character is a digit <digit> ::= '0' | '1' | '2' |
@@ -1485,6 +1724,30 @@
         }
     }
 
+    /**
+     * Test if the current character is a digit <digit> ::= '0' | '1' | '2' |
+     * '3' | '4' | '5' | '6' | '7' | '8' | '9'
+     * 
+     * @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 Digit
+     */
+    public static boolean isDigit( String string, int index )
+    {
+        int length = string.length();
+        
+        if ( ( string == null ) || ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+        {
+            return false;
+        }
+        else
+        {
+            char c = string.charAt(  index  );
+            return ( ( ( c > 127 ) || !DIGIT[c] ) ? false : true );
+        }
+    }
 
     /**
      * Test if the current character is a digit <digit> ::= '0' | '1' | '2' |
@@ -1570,21 +1833,38 @@
         }
     }
 
-    // The following methods are taken from org.apache.commons.lang.StringUtils
-
     /**
-     * The empty String <code>""</code>.
+     * Check if the current character is an 7 bits ASCII CHAR (between 0 and
+     * 127). <char> ::= <alpha> | <digit> | '-'
      * 
-     * @since 2.0
-     */
-    public static final String EMPTY = "";
-
-    /**
-     * The empty byte[]
+     * @param string
+     *            The string which contains the data
+     * @param index
+     *            Current position in the string
+     * @return The position of the next character, if the current one is a CHAR.
      */
-    public static final byte[] EMPTY_BYTES = new byte[]
-        {};
+    public static boolean isAlphaDigitMinus( String string, int index )
+    {
+        int length = string.length();
+        
+        if ( ( string == null ) || ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+        {
+            return false;
+        }
+        else
+        {
+            char c = string.charAt( index++ );
 
+            if ( ( c > 127 ) || ( CHAR[c] == false ) )
+            {
+                return false;
+            }
+            else
+            {
+                return true;
+            }
+        }
+    }
 
     // Empty checks
     // -----------------------------------------------------------------------
@@ -1673,26 +1953,7 @@
      */
     public static String trim( String str )
     {
-        if ( isEmpty( str ) )
-        {
-            return "";
-        }
-
-        char[] array = str.toCharArray();
-        int start = 0;
-        int end = array.length;
-
-        while ( ( start < end ) && ( array[start] == ' ' ) )
-        {
-            start++;
-        }
-
-        while ( ( end > start ) && ( array[end - 1] == ' ' ) )
-        {
-            end--;
-        }
-
-        return new String( array, start, ( end - start ) );
+        return ( isEmpty( str ) ? "" : str.trim() );
     }
 
 
@@ -1768,15 +2029,15 @@
             return "";
         }
 
-        char[] array = str.toCharArray();
         int start = 0;
-
-        while ( ( start < array.length ) && ( array[start] == ' ' ) )
+        int end = str.length();
+        
+        while ( ( start < end ) && ( str.charAt( start ) == ' ' ) )
         {
             start++;
         }
 
-        return new String( array, start, array.length - start );
+        return ( start == 0 ? str : str.substring( start ) );
     }
 
 
@@ -1815,6 +2076,45 @@
         return pos;
     }
 
+    /**
+     * <p>
+     * Removes spaces (char &lt;= 32) from start of this array, handling
+     * <code>null</code> by returning <code>null</code>.
+     * </p>
+     * Trim removes start characters &lt;= 32.
+     * 
+     * <pre>
+     *  StringUtils.trimLeft(null)          = null
+     *  StringUtils.trimLeft(&quot;&quot;)            = &quot;&quot;
+     *  StringUtils.trimLeft(&quot;     &quot;)       = &quot;&quot;
+     *  StringUtils.trimLeft(&quot;abc&quot;)         = &quot;abc&quot;
+     *  StringUtils.trimLeft(&quot;    abc    &quot;) = &quot;abc    &quot;
+     * </pre>
+     * 
+     * @param string
+     *            the string to be trimmed, may be null
+     * @return the position of the first char which is not a space, or the last
+     *         position of the array.
+     */
+    public static void trimLeft( String string, Position pos )
+    {
+        if ( string == null )
+        {
+            return;
+        }
+
+        int length = string.length();
+        
+        while ( ( pos.start < length ) && ( string.charAt( pos.start ) == ' ' ) )
+        {
+            pos.start++;
+        }
+        
+        pos.end = pos.start;
+
+        return;
+    }
+
 
     /**
      * <p>
@@ -1878,16 +2178,20 @@
             return "";
         }
 
-        char[] array = str.toCharArray();
-        int start = 0;
-        int end = array.length;
-
-        while ( ( start < end ) && ( array[start] == ' ' ) )
+        int length = str.length();
+        int end = length;
+        
+        while ( ( end > 0 ) && ( str.charAt( end - 1 ) == ' ' ) )
         {
-            start++;
+            if ( ( end > 1 ) && ( str.charAt(  end - 2 ) == '\\' ) )
+            {
+                break;
+            }
+            
+            end--;
         }
 
-        return new String( array, start, ( end - start ) );
+        return ( end == length ? str : str.substring( 0, end ) );
     }
 
 
@@ -1918,7 +2222,7 @@
             return pos;
         }
 
-        while ( ( pos > 0 ) && ( chars[pos - 1] == ' ' ) )
+        while ( ( pos >= 0 ) && ( chars[pos - 1] == ' ' ) )
         {
             pos--;
         }
@@ -1926,6 +2230,45 @@
         return pos;
     }
 
+    /**
+     * <p>
+     * Removes spaces (char &lt;= 32) from end of this string, handling
+     * <code>null</code> by returning <code>null</code>.
+     * </p>
+     * Trim removes start characters &lt;= 32.
+     * 
+     * <pre>
+     *  StringUtils.trimRight(null)          = null
+     *  StringUtils.trimRight(&quot;&quot;)            = &quot;&quot;
+     *  StringUtils.trimRight(&quot;     &quot;)       = &quot;&quot;
+     *  StringUtils.trimRight(&quot;abc&quot;)         = &quot;abc&quot;
+     *  StringUtils.trimRight(&quot;    abc    &quot;) = &quot;    abc&quot;
+     * </pre>
+     * 
+     * @param string
+     *            the string to be trimmed, may be null
+     * @return the position of the first char which is not a space, or the last
+     *         position of the string.
+     */
+    public static String trimRight( String string, Position pos )
+    {
+        if ( string == null )
+        {
+            return "";
+        }
+
+        while ( ( pos.end >= 0 ) && ( string.charAt( pos.end - 1 ) == ' ' ) )
+        {
+            if ( ( pos.end > 1 ) && ( string.charAt(  pos.end - 2 ) == '\\' ) )
+            {
+                break;
+            }
+            
+            pos.end--;
+        }
+
+        return ( pos.end == string.length() ? string : string.substring( 0, pos.end ) );
+    }
 
     /**
      * <p>



Mime
View raw message