You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2010/12/20 20:50:10 UTC

svn commit: r1051261 - in /directory/shared/branches/alex_refactoring: ldap/src/main/java/org/apache/directory/shared/ldap/csn/ ldap/src/main/java/org/apache/directory/shared/ldap/filter/ ldap/src/main/java/org/apache/directory/shared/ldap/name/ ldap/s...

Author: akarasulu
Date: Mon Dec 20 19:50:09 2010
New Revision: 1051261

URL: http://svn.apache.org/viewvc?rev=1051261&view=rev
Log:
moved more general non LDAP methods in StringTools to shared-util Strings

Modified:
    directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/csn/Csn.java
    directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/filter/FilterParser.java
    directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/name/RDN.java
    directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/schema/normalizers/ConcreteNameComponentNormalizer.java
    directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntaxCheckers/UuidSyntaxChecker.java
    directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java
    directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java
    directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
    directory/shared/branches/alex_refactoring/util/src/main/java/org/apache/directory/shared/util/Strings.java

Modified: directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/csn/Csn.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/csn/Csn.java?rev=1051261&r1=1051260&r2=1051261&view=diff
==============================================================================
--- directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/csn/Csn.java (original)
+++ directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/csn/Csn.java Mon Dec 20 19:50:09 2010
@@ -371,7 +371,7 @@ public class Csn implements Serializable
         {
             for ( int i = 0; i < 6; i++ )
             {
-                if ( !StringTools.isHex( changeCountStr, i ) )
+                if ( !Strings.isHex(changeCountStr, i) )
                 {
                     return false;
                 }
@@ -406,7 +406,7 @@ public class Csn implements Serializable
         
         for ( int i = 0; i < 3; i++ )
         {
-            if ( !StringTools.isHex( replicaIdStr, i ) )
+            if ( !Strings.isHex(replicaIdStr, i) )
             {
                 return false;
             }
@@ -436,7 +436,7 @@ public class Csn implements Serializable
 
         for ( int i = 0; i < 6; i++ )
         {
-            if ( !StringTools.isHex( operationNumberStr, i ) )
+            if ( !Strings.isHex(operationNumberStr, i) )
             {
                 return false;
             }

Modified: directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/filter/FilterParser.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/filter/FilterParser.java?rev=1051261&r1=1051260&r2=1051261&view=diff
==============================================================================
--- directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/filter/FilterParser.java (original)
+++ directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/filter/FilterParser.java Mon Dec 20 19:50:09 2010
@@ -253,7 +253,7 @@ public class FilterParser
                 pos.start++;
 
                 // First hex
-                if ( StringTools.isHex( filter, pos.start ) )
+                if ( Strings.isHex(filter, pos.start) )
                 {
                     pos.start++;
                 }
@@ -263,9 +263,9 @@ public class FilterParser
                 }
 
                 // second hex
-                if ( StringTools.isHex( filter, pos.start ) )
+                if ( Strings.isHex(filter, pos.start) )
                 {
-                    value[current++] = StringTools.getHexValue( filter.charAt( pos.start - 1 ), filter.charAt( pos.start ) );
+                    value[current++] = Strings.getHexValue(filter.charAt(pos.start - 1), filter.charAt(pos.start));
                     pos.start++;
                 }
                 else

Modified: directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/name/RDN.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/name/RDN.java?rev=1051261&r1=1051260&r2=1051261&view=diff
==============================================================================
--- directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/name/RDN.java (original)
+++ directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/name/RDN.java Mon Dec 20 19:50:09 2010
@@ -1192,9 +1192,9 @@ public class RDN implements Cloneable, C
 
             for ( int i = 1; i < chars.length; i += 2 )
             {
-                if ( StringTools.isHex( chars, i ) && StringTools.isHex( chars, i + 1 ) )
+                if ( Strings.isHex(chars, i) && Strings.isHex(chars, i + 1) )
                 {
-                    hexValue[pos++] = StringTools.getHexValue( chars[i], chars[i + 1] );
+                    hexValue[pos++] = Strings.getHexValue(chars[i], chars[i + 1]);
                 }
                 else
                 {
@@ -1235,10 +1235,10 @@ public class RDN implements Cloneable, C
                             break;
 
                         default:
-                            if ( StringTools.isHex( chars, i ) )
+                            if ( Strings.isHex(chars, i) )
                             {
                                 isHex = true;
-                                pair = ( ( byte ) ( StringTools.getHexValue( chars[i] ) << 4 ) );
+                                pair = ( ( byte ) ( Strings.getHexValue(chars[i]) << 4 ) );
                             }
 
                             break;
@@ -1248,9 +1248,9 @@ public class RDN implements Cloneable, C
                 {
                     if ( isHex )
                     {
-                        if ( StringTools.isHex( chars, i ) )
+                        if ( Strings.isHex(chars, i) )
                         {
-                            pair += StringTools.getHexValue( chars[i] );
+                            pair += Strings.getHexValue(chars[i]);
                             bytes[pos++] = pair;
                         }
                     }

Modified: directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/schema/normalizers/ConcreteNameComponentNormalizer.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/schema/normalizers/ConcreteNameComponentNormalizer.java?rev=1051261&r1=1051260&r2=1051261&view=diff
==============================================================================
--- directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/schema/normalizers/ConcreteNameComponentNormalizer.java (original)
+++ directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/schema/normalizers/ConcreteNameComponentNormalizer.java Mon Dec 20 19:50:09 2010
@@ -30,7 +30,7 @@ import org.apache.directory.shared.ldap.
 import org.apache.directory.shared.ldap.schema.MatchingRule;
 import org.apache.directory.shared.ldap.schema.Normalizer;
 import org.apache.directory.shared.ldap.schema.SchemaManager;
-import org.apache.directory.shared.ldap.util.StringTools;
+import org.apache.directory.shared.util.Strings;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -96,7 +96,7 @@ public class ConcreteNameComponentNormal
                 case 2 :
                     escaped=0;
                     low = c;
-                    newVal[pos++] = (char)StringTools.getHexValue( high, low );
+                    newVal[pos++] = (char) Strings.getHexValue(high, low);
                     
             }
         }

Modified: directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntaxCheckers/UuidSyntaxChecker.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntaxCheckers/UuidSyntaxChecker.java?rev=1051261&r1=1051260&r2=1051261&view=diff
==============================================================================
--- directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntaxCheckers/UuidSyntaxChecker.java (original)
+++ directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/schema/syntaxCheckers/UuidSyntaxChecker.java Mon Dec 20 19:50:09 2010
@@ -21,7 +21,7 @@ package org.apache.directory.shared.ldap
 
 import org.apache.directory.shared.ldap.constants.SchemaConstants;
 import org.apache.directory.shared.ldap.schema.SyntaxChecker;
-import org.apache.directory.shared.ldap.util.StringTools;
+import org.apache.directory.shared.util.Strings;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -44,7 +44,7 @@ public class UuidSyntaxChecker extends S
     // Tells if the byte is alphanumeric
     private static boolean isHex( byte b )
     {
-        return StringTools.isHex( b );
+        return Strings.isHex(b);
     }
 
     

Modified: directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java?rev=1051261&r1=1051260&r2=1051261&view=diff
==============================================================================
--- directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java (original)
+++ directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java Mon Dec 20 19:50:09 2010
@@ -328,7 +328,7 @@ public final class AttributeUtils
                             break;
 
                         case 2:
-                            int high = StringTools.getHexValue( c );
+                            int high = Strings.getHexValue(c);
 
                             if ( high == -1 )
                             {
@@ -341,7 +341,7 @@ public final class AttributeUtils
                             break;
 
                         case 3:
-                            int low = StringTools.getHexValue( c );
+                            int low = Strings.getHexValue(c);
 
                             if ( low == -1 )
                             {

Modified: directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java?rev=1051261&r1=1051260&r2=1051261&view=diff
==============================================================================
--- directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java (original)
+++ directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/util/DNUtils.java Mon Dec 20 19:50:09 2010
@@ -419,9 +419,9 @@ public final class DNUtils
                 {
                     return true;
                 }
-                else if ( StringTools.isHex( bytes, index++ ) )
+                else if ( Strings.isHex(bytes, index++) )
                 {
-                    return StringTools.isHex( bytes, index );
+                    return Strings.isHex(bytes, index);
                 }
                 else
                 {
@@ -469,9 +469,9 @@ public final class DNUtils
                 {
                     return 1;
                 }
-                else if ( StringTools.isHex( bytes, index++ ) )
+                else if ( Strings.isHex(bytes, index++) )
                 {
-                    return StringTools.isHex( bytes, index ) ? 2 : PARSING_ERROR;
+                    return Strings.isHex(bytes, index) ? 2 : PARSING_ERROR;
                 }
                 else
                 {
@@ -507,7 +507,7 @@ public final class DNUtils
             }
             else
             {
-                return StringTools.countBytesPerChar( bytes, index );
+                return Strings.countBytesPerChar(bytes, index);
             }
         }
     }
@@ -540,7 +540,7 @@ public final class DNUtils
             }
             else
             {
-                return StringTools.countBytesPerChar( bytes, index );
+                return Strings.countBytesPerChar(bytes, index);
             }
         }
     }
@@ -571,7 +571,7 @@ public final class DNUtils
             }
             else
             {
-                return StringTools.countBytesPerChar( bytes, index );
+                return Strings.countBytesPerChar(bytes, index);
             }
         }
     }
@@ -587,9 +587,9 @@ public final class DNUtils
      */
     public static int parseHexPair( byte[] bytes, int index )
     {
-        if ( StringTools.isHex( bytes, index ) )
+        if ( Strings.isHex(bytes, index) )
         {
-            if ( StringTools.isHex( bytes, index + 1 ) )
+            if ( Strings.isHex(bytes, index + 1) )
             {
                 return index + 2;
             }
@@ -615,7 +615,7 @@ public final class DNUtils
      */
     private static byte getHexPair( byte[] bytes, int index )
     {
-        return StringTools.getHexValue( bytes[index], bytes[index + 1] );
+        return Strings.getHexValue(bytes[index], bytes[index + 1]);
     }
 
     

Modified: directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java?rev=1051261&r1=1051260&r2=1051261&view=diff
==============================================================================
--- directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java (original)
+++ directory/shared/branches/alex_refactoring/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java Mon Dec 20 19:50:09 2010
@@ -737,55 +737,6 @@ public final class StringTools
 
 
     /**
-     * Count the number of bytes needed to return an Unicode char. This can be
-     * from 1 to 6.
-     * 
-     * @param bytes The bytes to read
-     * @param pos Position to start counting. It must be a valid start of a
-     *            encoded char !
-     * @return The number of bytes to create a char, or -1 if the encoding is
-     *         wrong. TODO : Should stop after the third byte, as a char is only
-     *         2 bytes long.
-     */
-    public static int countBytesPerChar( byte[] bytes, int pos )
-    {
-        if ( bytes == null )
-        {
-            return -1;
-        }
-
-        if ( ( bytes[pos] & CharConstants.UTF8_MULTI_BYTES_MASK ) == 0 )
-        {
-            return 1;
-        }
-        else if ( ( bytes[pos] & CharConstants.UTF8_TWO_BYTES_MASK ) == CharConstants.UTF8_TWO_BYTES )
-        {
-            return 2;
-        }
-        else if ( ( bytes[pos] & CharConstants.UTF8_THREE_BYTES_MASK ) == CharConstants.UTF8_THREE_BYTES )
-        {
-            return 3;
-        }
-        else if ( ( bytes[pos] & CharConstants.UTF8_FOUR_BYTES_MASK ) == CharConstants.UTF8_FOUR_BYTES )
-        {
-            return 4;
-        }
-        else if ( ( bytes[pos] & CharConstants.UTF8_FIVE_BYTES_MASK ) == CharConstants.UTF8_FIVE_BYTES )
-        {
-            return 5;
-        }
-        else if ( ( bytes[pos] & CharConstants.UTF8_SIX_BYTES_MASK ) == CharConstants.UTF8_SIX_BYTES )
-        {
-            return 6;
-        }
-        else
-        {
-            return -1;
-        }
-    }
-
-
-    /**
      * Return the number of bytes that hold an Unicode char.
      * 
      * @param car The character to be decoded
@@ -1063,7 +1014,7 @@ public final class StringTools
 
         while ( currentPos < bytes.length )
         {
-            currentPos += countBytesPerChar( bytes, currentPos );
+            currentPos += Strings.countBytesPerChar(bytes, currentPos);
             nbChars++;
         }
 
@@ -1418,171 +1369,6 @@ public final class StringTools
         }
     }
 
-    
-    /**
-     * Translate two chars to an hex value. The chars must be 
-     * in [a-fA-F0-9]
-     *
-     * @param high The high value 
-     * @param low The low value
-     * @return A byte representation of the two chars
-     */
-    public static byte getHexValue( char high, char low )
-    {
-        if ( ( high > 127 ) || ( low > 127 ) || ( high < 0 ) | ( low < 0 ) )
-        {
-            return -1;
-        }
-        
-        return (byte)( ( CharConstants.HEX_VALUE[high] << 4 ) | CharConstants.HEX_VALUE[low] );
-    }
-
-
-    /**
-     * Translate two bytes to an hex value. The bytes must be 
-     * in [0-9a-fA-F]
-     *
-     * @param high The high value 
-     * @param low The low value
-     * @return A byte representation of the two bytes
-     */
-    public static byte getHexValue( byte high, byte low )
-    {
-        if ( ( high > 127 ) || ( low > 127 ) || ( high < 0 ) | ( low < 0 ) )
-        {
-            return -1;
-        }
-        
-        return (byte)( ( CharConstants.HEX_VALUE[high] << 4 ) | CharConstants.HEX_VALUE[low] );
-    }
-
-    
-    /**
-     * Return an hex value from a sinle char
-     * The char must be in [0-9a-fA-F]
-     *
-     * @param c The char we want to convert
-     * @return A byte between 0 and 15
-     */
-    public static byte getHexValue( char c )
-    {
-        if ( ( c > 127 ) || ( c < 0 ) )
-        {
-            return -1;
-        }
-        
-        return CharConstants.HEX_VALUE[c];
-    }
-
-
-    /**
-     * Check if the current byte is an Hex Char 
-     * &lt;hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]
-     * 
-     * @param b The byte we want to check
-     * @return <code>true</code> if the current byte is a Hex byte
-     */
-    public static boolean isHex( byte b )
-    {
-        return ( ( b | 0x7F ) == 0x7F ) || CharConstants.HEX[b];
-    }
-
-
-    /**
-     * Check if the current character is an Hex Char &lt;hex> ::= [0x30-0x39] |
-     * [0x41-0x46] | [0x61-0x66]
-     * 
-     * @param bytes The buffer which contains the data
-     * @param index Current position in the buffer
-     * @return <code>true</code> if the current character is a Hex Char
-     */
-    public static boolean isHex( byte[] bytes, int index )
-    {
-        if ( ( bytes == null ) || ( bytes.length == 0 ) || ( index < 0 ) || ( index >= bytes.length ) )
-        {
-            return false;
-        }
-        else
-        {
-            byte c = bytes[index];
-
-            if ( ( ( c | 0x7F ) != 0x7F ) || ( CharConstants.HEX[c] == false ) )
-            {
-                return false;
-            }
-            else
-            {
-                return true;
-            }
-        }
-    }
-
-
-    /**
-     * Check if the current character is an Hex Char &lt;hex> ::= [0x30-0x39] |
-     * [0x41-0x46] | [0x61-0x66]
-     * 
-     * @param chars The buffer which contains the data
-     * @param index Current position in the buffer
-     * @return <code>true</code> if the current character is a Hex Char
-     */
-    public static boolean isHex( char[] chars, int index )
-    {
-        if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) || ( index >= chars.length ) )
-        {
-            return false;
-        }
-        else
-        {
-            char c = chars[index];
-
-            if ( ( c > 127 ) || ( CharConstants.HEX[c] == false ) )
-            {
-                return false;
-            }
-            else
-            {
-                return true;
-            }
-        }
-    }
-
-    /**
-     * Check if the current character is an Hex Char &lt;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 )
-    {
-        if ( string == null )
-        {
-            return false;
-        }
-        
-        int length = string.length();
-        
-        if ( ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
-        {
-            return false;
-        }
-        else
-        {
-            char c = string.charAt( index );
-
-            if ( ( c > 127 ) || ( CharConstants.HEX[c] == false ) )
-            {
-                return false;
-            }
-            else
-            {
-                return true;
-            }
-        }
-    }
-    
 
     /**
      * Test if the current character is a digit &lt;digit> ::= '0' | '1' | '2' |
@@ -2872,7 +2658,7 @@ public final class StringTools
             if ( c == '\\' )
             {
                 // we have the start of a hex escape sequence
-                if ( isHex( str, i+1 ) && isHex ( str, i+2 ) )
+                if ( Strings.isHex(str, i + 1) && Strings.isHex(str, i + 2) )
                 {
                     byte value = ( byte ) ( ( CharConstants.HEX_VALUE[str.charAt( i + 1 )] << 4 )
                         + CharConstants.HEX_VALUE[str.charAt( i + 2 )] );
@@ -3065,26 +2851,26 @@ public final class StringTools
 
         char[] chars = string.toCharArray();
         byte[] bytes = new byte[16];
-        bytes[0] = getHexValue( chars[0], chars[1] );
-        bytes[1] = getHexValue( chars[2], chars[3] );
-        bytes[2] = getHexValue( chars[4], chars[5] );
-        bytes[3] = getHexValue( chars[6], chars[7] );
-
-        bytes[4] = getHexValue( chars[9], chars[10] );
-        bytes[5] = getHexValue( chars[11], chars[12] );
-
-        bytes[6] = getHexValue( chars[14], chars[15] );
-        bytes[7] = getHexValue( chars[16], chars[17] );
-
-        bytes[8] = getHexValue( chars[19], chars[20] );
-        bytes[9] = getHexValue( chars[21], chars[22] );
-
-        bytes[10] = getHexValue( chars[24], chars[25] );
-        bytes[11] = getHexValue( chars[26], chars[27] );
-        bytes[12] = getHexValue( chars[28], chars[29] );
-        bytes[13] = getHexValue( chars[30], chars[31] );
-        bytes[14] = getHexValue( chars[32], chars[33] );
-        bytes[15] = getHexValue( chars[34], chars[35] );
+        bytes[0] = Strings.getHexValue(chars[0], chars[1]);
+        bytes[1] = Strings.getHexValue(chars[2], chars[3]);
+        bytes[2] = Strings.getHexValue(chars[4], chars[5]);
+        bytes[3] = Strings.getHexValue(chars[6], chars[7]);
+
+        bytes[4] = Strings.getHexValue(chars[9], chars[10]);
+        bytes[5] = Strings.getHexValue(chars[11], chars[12]);
+
+        bytes[6] = Strings.getHexValue(chars[14], chars[15]);
+        bytes[7] = Strings.getHexValue(chars[16], chars[17]);
+
+        bytes[8] = Strings.getHexValue(chars[19], chars[20]);
+        bytes[9] = Strings.getHexValue(chars[21], chars[22]);
+
+        bytes[10] = Strings.getHexValue(chars[24], chars[25]);
+        bytes[11] = Strings.getHexValue(chars[26], chars[27]);
+        bytes[12] = Strings.getHexValue(chars[28], chars[29]);
+        bytes[13] = Strings.getHexValue(chars[30], chars[31]);
+        bytes[14] = Strings.getHexValue(chars[32], chars[33]);
+        bytes[15] = Strings.getHexValue(chars[34], chars[35]);
 
         return bytes;
     }

Modified: directory/shared/branches/alex_refactoring/util/src/main/java/org/apache/directory/shared/util/Strings.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/alex_refactoring/util/src/main/java/org/apache/directory/shared/util/Strings.java?rev=1051261&r1=1051260&r2=1051261&view=diff
==============================================================================
--- directory/shared/branches/alex_refactoring/util/src/main/java/org/apache/directory/shared/util/Strings.java (original)
+++ directory/shared/branches/alex_refactoring/util/src/main/java/org/apache/directory/shared/util/Strings.java Mon Dec 20 19:50:09 2010
@@ -301,4 +301,211 @@ public class Strings
 
         return sb.toString();
     }
+
+    /**
+     * Translate two chars to an hex value. The chars must be
+     * in [a-fA-F0-9]
+     *
+     * @param high The high value
+     * @param low The low value
+     * @return A byte representation of the two chars
+     */
+    public static byte getHexValue( char high, char low )
+    {
+        if ( ( high > 127 ) || ( low > 127 ) || ( high < 0 ) | ( low < 0 ) )
+        {
+            return -1;
+        }
+
+        return (byte)( ( CharConstants.HEX_VALUE[high] << 4 ) | CharConstants.HEX_VALUE[low] );
+    }
+
+    /**
+     * Translate two bytes to an hex value. The bytes must be
+     * in [0-9a-fA-F]
+     *
+     * @param high The high value
+     * @param low The low value
+     * @return A byte representation of the two bytes
+     */
+    public static byte getHexValue( byte high, byte low )
+    {
+        if ( ( high > 127 ) || ( low > 127 ) || ( high < 0 ) | ( low < 0 ) )
+        {
+            return -1;
+        }
+
+        return (byte)( ( CharConstants.HEX_VALUE[high] << 4 ) | CharConstants.HEX_VALUE[low] );
+    }
+
+    /**
+     * Return an hex value from a sinle char
+     * The char must be in [0-9a-fA-F]
+     *
+     * @param c The char we want to convert
+     * @return A byte between 0 and 15
+     */
+    public static byte getHexValue( char c )
+    {
+        if ( ( c > 127 ) || ( c < 0 ) )
+        {
+            return -1;
+        }
+
+        return CharConstants.HEX_VALUE[c];
+    }
+
+    /**
+     * Check if the current byte is an Hex Char
+     * &lt;hex> ::= [0x30-0x39] | [0x41-0x46] | [0x61-0x66]
+     *
+     * @param b The byte we want to check
+     * @return <code>true</code> if the current byte is a Hex byte
+     */
+    public static boolean isHex( byte b )
+    {
+        return ( ( b | 0x7F ) == 0x7F ) || CharConstants.HEX[b];
+    }
+
+    /**
+     * Check if the current character is an Hex Char &lt;hex> ::= [0x30-0x39] |
+     * [0x41-0x46] | [0x61-0x66]
+     *
+     * @param bytes The buffer which contains the data
+     * @param index Current position in the buffer
+     * @return <code>true</code> if the current character is a Hex Char
+     */
+    public static boolean isHex( byte[] bytes, int index )
+    {
+        if ( ( bytes == null ) || ( bytes.length == 0 ) || ( index < 0 ) || ( index >= bytes.length ) )
+        {
+            return false;
+        }
+        else
+        {
+            byte c = bytes[index];
+
+            if ( ( ( c | 0x7F ) != 0x7F ) || ( CharConstants.HEX[c] == false ) )
+            {
+                return false;
+            }
+            else
+            {
+                return true;
+            }
+        }
+    }
+
+    /**
+     * Check if the current character is an Hex Char &lt;hex> ::= [0x30-0x39] |
+     * [0x41-0x46] | [0x61-0x66]
+     *
+     * @param chars The buffer which contains the data
+     * @param index Current position in the buffer
+     * @return <code>true</code> if the current character is a Hex Char
+     */
+    public static boolean isHex( char[] chars, int index )
+    {
+        if ( ( chars == null ) || ( chars.length == 0 ) || ( index < 0 ) || ( index >= chars.length ) )
+        {
+            return false;
+        }
+        else
+        {
+            char c = chars[index];
+
+            if ( ( c > 127 ) || ( CharConstants.HEX[c] == false ) )
+            {
+                return false;
+            }
+            else
+            {
+                return true;
+            }
+        }
+    }
+
+    /**
+     * Check if the current character is an Hex Char &lt;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 )
+    {
+        if ( string == null )
+        {
+            return false;
+        }
+
+        int length = string.length();
+
+        if ( ( length == 0 ) || ( index < 0 ) || ( index >= length ) )
+        {
+            return false;
+        }
+        else
+        {
+            char c = string.charAt( index );
+
+            if ( ( c > 127 ) || ( CharConstants.HEX[c] == false ) )
+            {
+                return false;
+            }
+            else
+            {
+                return true;
+            }
+        }
+    }
+
+    /**
+     * Count the number of bytes needed to return an Unicode char. This can be
+     * from 1 to 6.
+     *
+     * @param bytes The bytes to read
+     * @param pos Position to start counting. It must be a valid start of a
+     *            encoded char !
+     * @return The number of bytes to create a char, or -1 if the encoding is
+     *         wrong. TODO : Should stop after the third byte, as a char is only
+     *         2 bytes long.
+     */
+    public static int countBytesPerChar( byte[] bytes, int pos )
+    {
+        if ( bytes == null )
+        {
+            return -1;
+        }
+
+        if ( ( bytes[pos] & CharConstants.UTF8_MULTI_BYTES_MASK ) == 0 )
+        {
+            return 1;
+        }
+        else if ( ( bytes[pos] & CharConstants.UTF8_TWO_BYTES_MASK ) == CharConstants.UTF8_TWO_BYTES )
+        {
+            return 2;
+        }
+        else if ( ( bytes[pos] & CharConstants.UTF8_THREE_BYTES_MASK ) == CharConstants.UTF8_THREE_BYTES )
+        {
+            return 3;
+        }
+        else if ( ( bytes[pos] & CharConstants.UTF8_FOUR_BYTES_MASK ) == CharConstants.UTF8_FOUR_BYTES )
+        {
+            return 4;
+        }
+        else if ( ( bytes[pos] & CharConstants.UTF8_FIVE_BYTES_MASK ) == CharConstants.UTF8_FIVE_BYTES )
+        {
+            return 5;
+        }
+        else if ( ( bytes[pos] & CharConstants.UTF8_SIX_BYTES_MASK ) == CharConstants.UTF8_SIX_BYTES )
+        {
+            return 6;
+        }
+        else
+        {
+            return -1;
+        }
+    }
 }