/** * Copyright 1999-2009 The Pegadi Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.pegadi.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Locale; /** * This class provides helper methods for common parsing tasks. * * @author HÃ¥vard Wigtil <havardw at pvv.org> * @version $Revision$, $Date$ */ public class Parser { private static Logger log = LoggerFactory.getLogger(Parser.class); /** * Creates a new locale from a string on the form * <code>[lang]_[country]_[variant], e.g. <code>no_NO_NY</code>. * The <code>country</code> and <code>variant</code> parts of * the string are optional. * * @param locale The string to be parsed. * @return The new locale. * @see java.util.Locale */ public static Locale parseLocale(String locale) { String lang, country, variant; log.debug("org.pegadi.util.Parser.parseLocale: Locale is " + locale); int i = locale.indexOf('_'); log.debug("org.pegadi.util.Parser.parseLocale: i is " + i); if (i != -1) { lang = locale.substring(0, i); log.debug("org.pegadi.util.Parser.parseLocale: Lang is " + lang); int j = locale.indexOf('_', i+1); log.debug("org.pegadi.util.Parser.parseLocale: j is " + j); if (j != -1) { country = locale.substring(i+1, j); log.debug("org.pegadi.util.Parser.parseLocale: Country is " + country); variant = locale.substring(j+1); log.debug("org.pegadi.util.Parser.parseLocale: Variant is " + variant); } else { country = locale.substring(i+1); variant = ""; } } else { lang = locale; country = ""; variant = ""; } return new java.util.Locale(lang, country, variant); } }