Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007 Pascal Essiembre.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Pascal Essiembre - initial API and implementation
 ******************************************************************************/

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;

public class Main {
    /**
     * Parses a string into a locale. The string is expected to be of the same
     * format of the string obtained by calling Locale.toString().
     * 
     * @param localeString
     *            string representation of a locale
     * @return a locale or <code>null</code> if string is empty or null
     */
    public static Locale parseLocale(String localeString) {
        if (localeString == null || localeString.trim().length() == 0) {
            return null;
        }
        StringTokenizer tokens = new StringTokenizer(localeString, "_"); //$NON-NLS-1$
        List<String> localeSections = new ArrayList<String>();
        while (tokens.hasMoreTokens()) {
            localeSections.add(tokens.nextToken());
        }
        Locale locale = null;
        switch (localeSections.size()) {
        case 1:
            locale = new Locale(localeSections.get(0));
            break;
        case 2:
            locale = new Locale(localeSections.get(0), localeSections.get(1));
            break;
        case 3:
            locale = new Locale(localeSections.get(0), localeSections.get(1), localeSections.get(2));
            break;
        default:
            break;
        }
        return locale;
    }
}