~vcs-imports/xena/trunk

« back to all changes in this revision

Viewing changes to ext/src/xalan-j_2_7_1/src/org/apache/xml/utils/LocaleUtility.java

  • Committer: matthewoliver
  • Date: 2009-12-10 03:18:07 UTC
  • Revision ID: vcs-imports@canonical.com-20091210031807-l086qguzdlljtkl9
Merged Xena Testing into Xena Stable for the Xena 5 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
/*
 
4
 * Licensed to the Apache Software Foundation (ASF) under one
 
5
 * or more contributor license agreements. See the NOTICE file
 
6
 * distributed with this work for additional information
 
7
 * regarding copyright ownership. The ASF licenses this file
 
8
 * to you under the Apache License, Version 2.0 (the  "License");
 
9
 * you may not use this file except in compliance with the License.
 
10
 * You may obtain a copy of the License at
 
11
 *
 
12
 *     http://www.apache.org/licenses/LICENSE-2.0
 
13
 *
 
14
 * Unless required by applicable law or agreed to in writing, software
 
15
 * distributed under the License is distributed on an "AS IS" BASIS,
 
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
17
 * See the License for the specific language governing permissions and
 
18
 * limitations under the License.
 
19
 */
 
20
/*
 
21
 * $Id: LocaleUtility.java,v 1.2 2009/12/10 03:18:23 matthewoliver Exp $
 
22
 */
 
23
 
 
24
package org.apache.xml.utils;
 
25
 
 
26
import java.util.Locale;
 
27
 
 
28
/**
 
29
 * @author Igor Hersht, igorh@ca.ibm.com
 
30
 */
 
31
public class LocaleUtility {
 
32
    /**
 
33
     * IETF RFC 1766 tag separator
 
34
     */
 
35
    public final static char IETF_SEPARATOR = '-';  
 
36
    public final static String EMPTY_STRING = ""; 
 
37
    
 
38
   
 
39
 public static Locale langToLocale(String lang) {
 
40
       if((lang == null) || lang.equals(EMPTY_STRING)){ // not specified => getDefault
 
41
            return Locale.getDefault();
 
42
       }
 
43
        String language = EMPTY_STRING;
 
44
        String country =  EMPTY_STRING;
 
45
        String variant =  EMPTY_STRING;
 
46
 
 
47
        int i1 = lang.indexOf(IETF_SEPARATOR);
 
48
        if (i1 < 0) {
 
49
            language = lang;
 
50
        } else {
 
51
            language = lang.substring(0, i1);
 
52
            ++i1;
 
53
            int i2 = lang.indexOf(IETF_SEPARATOR, i1);
 
54
            if (i2 < 0) {
 
55
                country = lang.substring(i1);
 
56
            } else {
 
57
                country = lang.substring(i1, i2);
 
58
                variant = lang.substring(i2+1);
 
59
            }
 
60
        }
 
61
        
 
62
        if(language.length() == 2){
 
63
           language = language.toLowerCase();
 
64
        }else {
 
65
          language = EMPTY_STRING;
 
66
        }
 
67
        
 
68
        if(country.length() == 2){
 
69
           country = country.toUpperCase();
 
70
        }else {
 
71
          country = EMPTY_STRING;
 
72
        }
 
73
        
 
74
        if((variant.length() > 0) && 
 
75
        ((language.length() == 2) ||(country.length() == 2))){
 
76
           variant = variant.toUpperCase();
 
77
        }else{
 
78
            variant = EMPTY_STRING;
 
79
        }
 
80
             
 
81
        return new Locale(language, country, variant );
 
82
    }
 
83
    
 
84
  
 
85
   
 
86
 }
 
87
  
 
88