Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dataTypes / impl / coercion / CoerceToLocale.java @ 2088

History | View | Annotate | Download (4.31 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.dataTypes.impl.coercion;
25

    
26
import java.util.HashSet;
27
import java.util.Locale;
28
import java.util.Set;
29
import org.gvsig.tools.dataTypes.AbstractCoercion;
30
import org.gvsig.tools.dataTypes.Coercion;
31
import org.gvsig.tools.dataTypes.CoercionContext;
32
import org.gvsig.tools.dataTypes.CoercionException;
33

    
34
public class CoerceToLocale extends AbstractCoercion {
35

    
36
  public static final int LOCALE_DEFAULT_SIZE = 30;
37
  
38
  private static Set<String> availableLanguages = null;
39
  
40
  public static String toString(Locale locale) {
41
    return locale.toLanguageTag();
42
  }
43
  
44
  public static boolean isLanguageAvailable(String language) {
45
    if( language==null || language.isEmpty() ) {
46
      return false;
47
    }
48
    if( availableLanguages==null ) {
49
      availableLanguages = new HashSet<>();
50
      Locale[] locales = Locale.getAvailableLocales(); 
51
      for (Locale locale : locales) {
52
        availableLanguages.add(locale.getLanguage());
53
      }
54
    }
55
    return availableLanguages.contains(language);
56
  }
57
  
58
  public static boolean isValidCountry(final String country) {
59
    if( country == null ) {
60
      return false;
61
    }
62
    char ch0, ch1, ch2;
63
    switch(country.length()) {
64
      case 2:
65
        ch0 = country.charAt(0);
66
        ch1 = country.charAt(1);
67
        return Character.isUpperCase(ch0) && Character.isAlphabetic(ch0) && 
68
               Character.isUpperCase(ch1) && Character.isAlphabetic(ch1);
69
      case 3:
70
        ch0 = country.charAt(0);
71
        ch1 = country.charAt(1);
72
        ch2 = country.charAt(2);
73
        return Character.isDigit(ch0) && Character.isDigit(ch1) && Character.isDigit(ch2);
74
      default:
75
        return false;
76
    }
77
  }
78
  
79
  public static Locale parseLocale(final String locale_s) {
80
      String[] locale_p = locale_s.trim().split("_");
81
      String language = locale_p[0];
82
      String country;
83
      String variant;
84
      if( !isLanguageAvailable(language) ) {
85
        return null;
86
      }
87
      switch(locale_p.length) {
88
        default:
89
          return new Locale(language);
90
        case 2:
91
          country = locale_p[1];
92
          if( !isValidCountry(country) ) {
93
            return null;
94
          }
95
          return new Locale(language, country);
96

    
97
        case 3:
98
          country = locale_p[1];
99
          variant = locale_p[2];
100
          if( !isValidCountry(country) || variant.isEmpty() ) {
101
            return null;
102
          }
103
          return new Locale(language, country, variant);
104
      }
105
  }
106
  
107
        @Override
108
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
109
    if (value == null || value instanceof Locale) {
110
      return value;
111
    }
112
    Locale locale;
113
    String locale_s = "";
114
                try {
115
      locale_s = value.toString();
116
      if( locale_s.equalsIgnoreCase("Default") ) {
117
        return Locale.getDefault();
118
      }
119
      if( locale_s.equalsIgnoreCase("null") ) {
120
        return null;
121
      }
122
      locale = Locale.forLanguageTag(locale_s);
123
      if( isLanguageAvailable(locale.getLanguage()) ) {
124
        return locale;
125
      }
126
      locale = parseLocale(locale_s);
127
      if( locale != null && isLanguageAvailable(locale.getLanguage()) ) {
128
        return locale;
129
      }
130
                } catch (Exception e) {
131
                        throw new CoercionException("Can't coerce '" + locale_s + "' ("+value.getClass().getSimpleName()+") to Locale.", e);
132
                }
133
          throw new CoercionException("Can't coerce '" + locale_s + "' ("+value.getClass().getSimpleName()+") to Locale.");
134
        }
135
}