Modified LDAP evaluation so approximate comparisons of Strings ignore
case and whitespace and characters ignore case. (FELIX-1259)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@786542 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java b/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java
index 7d88802..ec3de35 100644
--- a/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java
+++ b/framework/src/main/java/org/apache/felix/framework/util/ldap/Parser.java
@@ -1663,9 +1663,10 @@
         }
         else if (obj1 instanceof Character)
         {
-            char value1 = ((Character)obj1).charValue();
-            char value2 = ((Character)obj2).charValue();
-            return (value2 >= (value1-((Math.abs(value1)*(char)APPROX_CRITERIA)/(char)100)) 
+            // We should ignore case.
+            char value1 = Character.toLowerCase(((Character) obj1).charValue());
+            char value2 = Character.toLowerCase(((Character) obj2).charValue());
+            return (value2 >= (value1-((Math.abs(value1)*(char)APPROX_CRITERIA)/(char)100))
                 && value2 <= (value1+((Math.abs(value1)*(char)APPROX_CRITERIA)/(char)100)));
         }
         else if (obj1 instanceof Double)
@@ -1705,10 +1706,12 @@
         }
         else if (obj1 instanceof String)
         {
+            // Spec says to ignore case and whitespace, to let's convert
+            // to lower case and remove whitespace.
             int distance = getDistance(
-                obj1.toString().toLowerCase(), obj2.toString().toLowerCase());
-            int size = ((String)obj1).length();
-            return (distance <= ((size*APPROX_CRITERIA)/100));
+                removeWhitespace(obj1.toString()), removeWhitespace(obj2.toString()));
+             int size = ((String)obj1).length();
+             return (distance <= ((size*APPROX_CRITERIA)/100));
         }
         else if (obj1 instanceof BigInteger)
         {
@@ -1737,6 +1740,19 @@
             + obj1.getClass().getName());
     }
 
+    private static String removeWhitespace(String s)
+    {
+        StringBuffer sb = new StringBuffer();
+        for (int i = 0; i < s.length(); i++)
+        {
+            if (!Character.isWhitespace(s.charAt(i)))
+            {
+                sb.append(Character.toLowerCase(s.charAt(i)));
+            }
+        }
+        return sb.toString();
+    }
+
     /**
      * Calculate the Levenshtein distance (LD) between two strings.
      * The Levenshteing distance is a measure of the similarity between