Author: vmassol
Date: 2008-02-01 12:42:29 +0100 (Fri, 01 Feb 2008)
New Revision: 7257

Added:
   xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/util/
   
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/util/UtilTest.java
Modified:
   
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/util/Util.java
Log:
XWIKI-2054: Add Utility API to remove all non alpha numeric chartacters from 
some text



Modified: 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/util/Util.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/util/Util.java 
    2008-02-01 11:09:04 UTC (rev 7256)
+++ 
xwiki-platform/core/trunk/xwiki-core/src/main/java/com/xpn/xwiki/util/Util.java 
    2008-02-01 11:42:29 UTC (rev 7257)
@@ -51,7 +51,6 @@
 import org.apache.oro.text.regex.Pattern;
 import org.apache.oro.text.regex.PatternMatcherInput;
 import org.apache.oro.text.regex.Perl5Matcher;
-import org.apache.tools.ant.filters.StringInputStream;
 import org.dom4j.Document;
 import org.dom4j.DocumentException;
 import org.dom4j.io.SAXReader;
@@ -697,4 +696,28 @@
         }
         return url;
     }
+
+    /**
+     * Removes all non alpha numerical characters from the passed text. First 
tries to convert
+     * accented chars to their alpha numeric representation.
+     *
+     * @param text the text to convert
+     * @return the alpha numeric equivalent
+     */
+    public static String convertToAlphaNumeric(String text)
+    {
+        // Start by removing accents
+        String textNoAccents = Util.noaccents(text);
+
+        // Now remove all non alphanumeric chars
+        StringBuffer result = new StringBuffer(textNoAccents.length());
+        char[] testChars = textNoAccents.toCharArray();
+        for (int i = 0; i < testChars.length; i++) {
+            if (Character.isLetterOrDigit(testChars[i])) {
+                result.append(testChars[i]);
+            }
+        }
+        return result.toString();
+    }
+
 }

Added: 
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/util/UtilTest.java
===================================================================
--- 
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/util/UtilTest.java
                         (rev 0)
+++ 
xwiki-platform/core/trunk/xwiki-core/src/test/java/com/xpn/xwiki/util/UtilTest.java
 2008-02-01 11:42:29 UTC (rev 7257)
@@ -0,0 +1,38 @@
+/*
+ * See the NOTICE file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ */
+package com.xpn.xwiki.util;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for [EMAIL PROTECTED] com.xpn.xwiki.util.Util}.
+ *
+ * @version $Id: $
+ */
+public class UtilTest extends TestCase
+{
+    public void testConvertToAlphaNumeric()
+    {
+        assertEquals("Search", Util.convertToAlphaNumeric("Search"));
+        assertEquals("Search", Util.convertToAlphaNumeric("S.earch"));
+        assertEquals("e", Util.convertToAlphaNumeric("�$%��#�^^^()"));
+    }
+}

_______________________________________________
notifications mailing list
[email protected]
http://lists.xwiki.org/mailman/listinfo/notifications

Reply via email to