All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.util.Normalizer Maven / Gradle / Ivy

There is a newer version: 20.0.0
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library 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 library 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.
 */

package com.liferay.util;

import com.liferay.ibm.icu.text.Transliterator;
import com.liferay.portal.kernel.util.StringUtil;

/**
 * @author Brian Wing Shun Chan
 * @author Shuyang Zhou
 */
public class Normalizer {

	public static String normalizeToAscii(String s) {
		if (!_hasNonASCIICode(s)) {
			return s;
		}

		String normalizedText = _transliterator.transform(s);

		return StringUtil.replace(
			normalizedText, _UNICODE_TEXT, _NORMALIZED_TEXT);
	}

	private static boolean _hasNonASCIICode(String s) {
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) > 127) {
				return true;
			}
		}

		return false;
	}

	private static final char[] _NORMALIZED_TEXT = new char[] {'l', '\'', '\"'};

	private static final char[] _UNICODE_TEXT = new char[] {
		'\u0142', '\u02B9', '\u02BA'
	};

	private static final Transliterator _transliterator =
		Transliterator.getInstance(
			"Greek-Latin; Cyrillic-Latin; NFD; [:Nonspacing Mark:] " +
				"Remove; NFC");

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy