001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
021     * @author Brian Wing Shun Chan
022     * @author Shuyang Zhou
023     * @see com.liferay.rss.util.Normalizer
024     */
025    public class Normalizer {
026    
027            public static String normalizeToAscii(String s) {
028                    if (!_hasNonASCIICode(s)) {
029                            return s;
030                    }
031    
032                    String normalizedText = _transliterator.transform(s);
033    
034                    return StringUtil.replace(
035                            normalizedText, _UNICODE_TEXT, _NORMALIZED_TEXT);
036            }
037    
038            private static boolean _hasNonASCIICode(String s) {
039                    for (int i = 0; i < s.length(); i++) {
040                            if (s.charAt(i) > 127) {
041                                    return true;
042                            }
043                    }
044    
045                    return false;
046            }
047    
048            private static final char[] _NORMALIZED_TEXT = new char[] {'l', '\'', '\"'};
049    
050            private static final char[] _UNICODE_TEXT =
051                    new char[] {'\u0142', '\u02B9', '\u02BA'};
052    
053            private static final Transliterator _transliterator =
054                    Transliterator.getInstance(
055                            "Greek-Latin; Cyrillic-Latin; NFD; [:Nonspacing Mark:] " +
056                                    "Remove; NFC");
057    
058    }