import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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.
*/
public class Main {
/**
* Parse the given localeString into a {@link java.util.Locale}.
*
* This is the inverse operation of
* {@link java.util.Locale#toString Locale's toString}.
*
* @param localeString
* the locale string
* @return a corresponding Locale instance
*/
public static Locale parseLocaleString(String localeString) {
String[] parts = tokenizeToStringArray(localeString, "_ ", false, false);
String language = (parts.length > 0 ? parts[0] : "");
String country = (parts.length > 1 ? parts[1] : "");
String variant = "";
if (parts.length >= 2) {
// There is definitely a variant, and it is everything after the country
// code sans the separator between the country code and the variant.
int endIndexOfCountryCode = localeString.indexOf(country) + country.length();
// Strip off any leading '_' and whitespace, what's left is the variant.
variant = trimLeadingWhitespace(localeString.substring(endIndexOfCountryCode));
if (variant.startsWith("_")) {
variant = trimLeadingCharacter(variant, '_');
}
}
return (language.length() > 0 ? new Locale(language, country, variant) : null);
}
/**
* Tokenize the given String into a String array via a StringTokenizer.
*
* The given delimiters string is supposed to consist of any number of
* delimiter characters. Each of those characters can be used to separate
* tokens. A delimiter is always a single character; for multi-character
* delimiters, consider using delimitedListToStringArray
*
* @param str
* the String to tokenize
* @param delimiters
* the delimiter characters, assembled as String (each of those
* characters is individually considered as delimiter)
* @param trimTokens
* trim the tokens via String's trim
* @param ignoreEmptyTokens
* omit empty tokens from the result array (only applies to tokens
* that are empty after trimming; StringTokenizer will not consider
* subsequent delimiters as token in the first place).
* @return an array of the tokens (null if the input String was null)
*/
public static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens,
boolean ignoreEmptyTokens) {
if (str == null) {
return null;
}
StringTokenizer st = new StringTokenizer(str, delimiters);
List tokens = new ArrayList();
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (trimTokens) {
token = token.trim();
}
if (!ignoreEmptyTokens || token.length() > 0) {
tokens.add(token);
}
}
return tokens.toArray(new String[tokens.size()]);
}
/**
* Trim leading whitespace from the given String.
*
* @param str
* the string to check
* @return the trimmed String
* @see java.lang.Character#isWhitespace(char)
*/
public static String trimLeadingWhitespace(String str) {
return trimLeadingCharacter(str, CharacterChecker.WHITESPACE);
}
/**
* Trim all occurences of the supplied leading character from the given
* String.
*
* @param str
* the string to check
* @param leadingCharacter
* the leading character to be trimmed
* @return the trimmed String
*/
public static String trimLeadingCharacter(String str, final char leadingCharacter) {
return trimLeadingCharacter(str, new CharacterChecker() {
public boolean isCharacterLegal(char character) {
return character == leadingCharacter;
}
});
}
/**
* Trim all occurences of the supplied leading character from the given
* String.
*
* @param str
* the string to check
* @param checker
* the character checker
* @return the trimmed String
*/
public static String trimLeadingCharacter(String str, CharacterChecker checker) {
if (hasLength(str) == false) {
return str;
}
if (checker == null)
throw new IllegalArgumentException("Null character checker");
StringBuffer buf = new StringBuffer(str);
while (buf.length() > 0 && checker.isCharacterLegal(buf.charAt(0))) {
buf.deleteCharAt(0);
}
return buf.toString();
}
/**
* Check that the given string param is neither null nor of length 0.
*
* @param string
* the string
* @return true if the String is not null and has length
*/
public static boolean hasLength(String string) {
return (string != null && string.length() > 0);
}
}