001
014
015 package com.liferay.util;
016
017 import com.liferay.ibm.icu.text.Transliterator;
018 import com.liferay.portal.kernel.util.StringUtil;
019
020
024 public class Normalizer {
025
026 public static String normalizeToAscii(String s) {
027 if (!_hasNonASCIICode(s)) {
028 return s;
029 }
030
031 String normalizedText = _transliterator.transform(s);
032
033 return StringUtil.replace(
034 normalizedText, _UNICODE_TEXT, _NORMALIZED_TEXT);
035 }
036
037 private static boolean _hasNonASCIICode(String s) {
038 for (int i = 0; i < s.length(); i++) {
039 if (s.charAt(i) > 127) {
040 return true;
041 }
042 }
043
044 return false;
045 }
046
047 private static final String[] _NORMALIZED_TEXT = new String[] {
048 "l", "'", "\""
049 };
050
051 private static final String[] _UNICODE_TEXT = new String[] {
052 "\u0142", "\u02B9", "\u02BA"
053 };
054
055 private static Transliterator _transliterator = Transliterator.getInstance(
056 "Greek-Latin; Cyrillic-Latin; NFD; [:Nonspacing Mark:] Remove; NFC");
057
058 }