directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1604665 - /directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/PrepareString.java
Date Sun, 22 Jun 2014 23:14:48 GMT
Author: elecharny
Date: Sun Jun 22 23:14:48 2014
New Revision: 1604665

URL: http://svn.apache.org/r1604665
Log:
Created a new methods for insignifiantSpacesString which deal with ASCII only text (we default
to non ascii in case an exception occured)

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

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/PrepareString.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/PrepareString.java?rev=1604665&r1=1604664&r2=1604665&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/PrepareString.java
(original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/PrepareString.java
Sun Jun 22 23:14:48 2014
@@ -236,12 +236,26 @@ public final class PrepareString
             case CASE_EXACT:
             case CASE_EXACT_IA5:
             case DIRECTORY_STRING:
-                return insignifiantSpacesString( str, CASE_SENSITIVE );
+                try
+                {
+                    return insignifiantSpacesStringAscii( str, CASE_SENSITIVE );
+                }
+                catch ( Exception e )
+                {
+                    return insignifiantSpacesString( str, CASE_SENSITIVE );
+                }
 
             case CASE_IGNORE_IA5:
             case CASE_IGNORE_LIST:
             case CASE_IGNORE:
-                return insignifiantSpacesString( str, IGNORE_CASE );
+                try
+                {
+                    return insignifiantSpacesStringAscii( str, IGNORE_CASE );
+                }
+                catch ( Exception e )
+                {
+                    return insignifiantSpacesString( str, IGNORE_CASE );
+                }
 
             case WORD:
                 return str;
@@ -4941,4 +4955,138 @@ public final class PrepareString
 
         return new String( target, 0, pos );
     }
+
+
+    /**
+     * Remove all insignificant spaces in a Ascii string.
+     * 
+     * This method use a finite state machine to parse
+     * the text.
+     * 
+     * @param str The String to modify
+     * @param caseSensitive A flag telling if the chars must be lower cased
+     * @return The modified StringBuilder
+     * @throws InvalidCharacterException If an invalid character is found in the String
+     */
+    private static String insignifiantSpacesStringAscii( String str, boolean caseSensitive
)
+        throws InvalidCharacterException
+    {
+        if ( Strings.isEmpty( str ) )
+        {
+            // Special case : an empty strings is replaced by 2 spaces
+            return "";
+        }
+    
+        char[] array = str.toCharArray();
+    
+        int pos = 0;
+        char lowerCase = ( char ) ( caseSensitive ? 0x00 : 0x20 );
+    
+        // First pass to map the chars
+        for ( char c : array )
+        {
+            pos += map( c, array, pos, lowerCase );
+        }
+    
+        int limit = pos;
+        pos = 0;
+    
+        // Second pass to remove spaces. We work on the target
+        int i = 0;
+        char c = '\0';
+    
+        // First remove starting spaces
+        for ( i = 0; i < limit; i++ )
+        {
+            c = array[i];
+    
+            if ( c != ' ' )
+            {
+                checkProhibited( c );
+                break;
+            }
+        }
+    
+        // Now, 'i' will be the starting point. We will just handle the special
+        // case of a combining character
+        int start = i;
+    
+        if ( start == limit )
+        {
+            // we only have spaces, we keep only one
+            return " ";
+        }
+        else if ( isCombiningMark( c ) )
+        {
+            if ( start == 0 )
+            {
+                // The first char can't be a combining char
+                throw new InvalidCharacterException( c );
+            }
+            else
+            {
+                throw new InvalidCharacterException( c );
+            }
+        }
+        else
+        {
+            array[pos++] = c;
+            start++;
+        }
+    
+        // Now remove the spaces at the end
+        for ( i = limit - 1; i > start; i-- )
+        {
+            if ( array[i] != ' ' )
+            {
+                break;
+            }
+        }
+    
+        limit = i + 1;
+    
+        // Let's deal with the following chars. It will be
+        // a list of chars and spaces. We will consider that
+        // we have couples of chars and spaces :
+        // (char * space*)*. We have a special case :
+        // a space followed by a combining char.
+        boolean spaceSeen = false;
+        boolean space2Seen = false;
+    
+        for ( i = start; i < limit; i++ )
+        {
+            c = array[i];
+    
+            checkProhibited( c );
+    
+            if ( isCombiningMark( c ) )
+            {
+                throw new InvalidCharacterException( c );
+            }
+            else if ( c == ' ' )
+            {
+                if ( spaceSeen )
+                {
+                    space2Seen = true;
+                }
+                else
+                {
+                    spaceSeen = true;
+                }
+            }
+            else
+            {
+                if ( spaceSeen )
+                {
+                    array[pos++] = ' ';
+                    spaceSeen = false;
+                    space2Seen = false;
+                }
+    
+                array[pos++] = c;
+            }
+        }
+    
+        return new String( array, 0, pos );
+    }
 }



Mime
View raw message