You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/12/14 08:31:43 UTC

svn commit: r890205 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang3/StringUtils.java test/org/apache/commons/lang3/StringUtilsTrimEmptyTest.java

Author: bayard
Date: Mon Dec 14 07:31:42 2009
New Revision: 890205

URL: http://svn.apache.org/viewvc?rev=890205&view=rev
Log:
Adding stripAccents method to StringUtils. It requires JDK 1.6 (and accesses it via reflection until Lang becomes 1.6 dependent). LANG-285

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang3/StringUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang3/StringUtilsTrimEmptyTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang3/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang3/StringUtils.java?rev=890205&r1=890204&r2=890205&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang3/StringUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang3/StringUtils.java Mon Dec 14 07:31:42 2009
@@ -607,6 +607,70 @@
         return newArr;
     }
 
+    /**
+     * <p>Removes the accents from a string. </p>
+     * <p>NOTE: This is a JDK 1.6 method, it will fail on JDK 1.5. </p>
+     *
+     * <pre>
+     * StringUtils.stripAccents(null)                = null
+     * StringUtils.stripAccents("")                  = ""
+     * StringUtils.stripAccents("control")           = "control"
+     * StringUtils.stripAccents("&ecute;clair")      = "eclair"
+     * </pre>
+     * 
+     * @param input String to be stripped
+     * @return String without accents on the text
+     *
+     * @since 3.0
+     */
+    public static String stripAccents(String input) {
+        if(input == null) {
+            return null;
+        }
+        if(SystemUtils.isJavaVersionAtLeast(1.6f)) {
+
+            // String decomposed = Normalizer.normalize(input, Normalizer.Form.NFD);
+
+            // START of 1.5 reflection - in 1.6 use the line commented out above
+            try {
+                // get java.text.Normalizer.Form class
+                Class normalizerFormClass = ClassUtils.getClass("java.text.Normalizer$Form", false);
+
+                // get Normlizer class
+                Class normalizerClass = ClassUtils.getClass("java.text.Normalizer", false);
+
+                // get static method on Normalizer
+                java.lang.reflect.Method method = normalizerClass.getMethod("normalize", CharSequence.class, normalizerFormClass );
+
+                // get Normalizer.NFD field
+                java.lang.reflect.Field nfd = normalizerFormClass.getField("NFD");
+
+                // invoke method
+                String decomposed = (String) method.invoke( null, input, nfd.get(null) );
+                // END of 1.5 reflection
+
+                java.util.regex.Pattern accentPattern = java.util.regex.Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
+                return accentPattern.matcher(decomposed).replaceAll("");
+            } catch(ClassNotFoundException cnfe) {
+                throw new RuntimeException("ClassNotFoundException occurred during 1.6 backcompat code", cnfe);
+            } catch(NoSuchMethodException nsme) {
+                throw new RuntimeException("NoSuchMethodException occurred during 1.6 backcompat code", nsme);
+            } catch(NoSuchFieldException nsfe) {
+                throw new RuntimeException("NoSuchFieldException occurred during 1.6 backcompat code", nsfe);
+            } catch(IllegalAccessException iae) {
+                throw new RuntimeException("IllegalAccessException occurred during 1.6 backcompat code", iae);
+            } catch(IllegalArgumentException iae) {
+                throw new RuntimeException("IllegalArgumentException occurred during 1.6 backcompat code", iae);
+            } catch(java.lang.reflect.InvocationTargetException ite) {
+                throw new RuntimeException("InvocationTargetException occurred during 1.6 backcompat code", ite);
+            } catch(SecurityException se) {
+                throw new RuntimeException("SecurityException occurred during 1.6 backcompat code", se);
+            }
+        } else {
+            throw new UnsupportedOperationException("The stripAccents(String) method is not supported until Java 1.6");
+        }
+    }
+
     // Equals
     //-----------------------------------------------------------------------
     /**

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang3/StringUtilsTrimEmptyTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang3/StringUtilsTrimEmptyTest.java?rev=890205&r1=890204&r2=890205&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang3/StringUtilsTrimEmptyTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang3/StringUtilsTrimEmptyTest.java Mon Dec 14 07:31:42 2009
@@ -264,6 +264,34 @@
         assertArrayEquals(foo, StringUtils.stripAll(fooDots, "."));
     }
 
+    public void testStripAccents() {
+        if(SystemUtils.isJavaVersionAtLeast(1.6f)) {
+            String cue = "\u00C7\u00FA\u00EA";
+            assertEquals( "Failed to strip accents from " + cue, "Cue", StringUtils.stripAccents(cue));
+
+            String lots = "\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5\u00C7\u00C8\u00C9" + 
+                          "\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF\u00D1\u00D2\u00D3" + 
+                          "\u00D4\u00D5\u00D6\u00D9\u00DA\u00DB\u00DC\u00DD";
+            assertEquals( "Failed to strip accents from " + lots, 
+                          "AAAAAACEEEEIIIINOOOOOUUUUY", 
+                          StringUtils.stripAccents(lots));
+
+            assertNull( "Failed null safety", StringUtils.stripAccents(null) );
+            assertEquals( "Failed empty String", "", StringUtils.stripAccents("") );
+            assertEquals( "Failed to handle non-accented text", "control", StringUtils.stripAccents("control") );
+            assertEquals( "Failed to handle easy example", "eclair", StringUtils.stripAccents("\u00E9clair") );
+        } else {
+            try {
+                StringUtils.stripAccents("string");
+                fail("Before JDK 1.6, stripAccents is not expected to work");
+            } catch(UnsupportedOperationException uoe) {
+                assertEquals("The stripAccents(String) method is not supported until Java 1.6", uoe.getMessage());
+            }
+        }
+    }
+
+    //-----------------------------------------------------------------------
+
     private void assertArrayEquals(Object[] o1, Object[] o2) {
         if(o1 == null) {
             assertEquals(o1,o2);