Revision 2088

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/DataTypesManager.java
65 65

  
66 66
    public int addtype(int type, String subtype, String name,
67 67
        Class defaultClass, Coercion coercion, String iconName, 
68
        boolean predefinedPrecision, int max_precision, 
68
        int flags, int max_precision, 
69 69
        int default_precision, int default_scale);
70 70

  
71
    public int addtype(int type, String subtype, String name,
72
        Class defaultClass, Coercion coercion, String iconName, int flags);
73
    
71 74
    public Coercion getCoercion(int type);
72 75

  
73 76
//    public void setCoercion(int type, Coercion coercion);
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/DataType.java
23 23
 */
24 24
package org.gvsig.tools.dataTypes;
25 25

  
26
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToLocale;
26 27
import org.gvsig.tools.util.LabeledValue;
27 28

  
28 29
public interface DataType extends LabeledValue<DataType>, org.gvsig.tools.lang.Cloneable {
29 30

  
31
  
32
  public static final int FLAG_NONE = 0;
33
  public static final int FLAG_NUMBER = 1; // byte, int, long, float, double, decimal
34
  public static final int FLAG_SUPPORT_PRECISION = 2; // byte, int, long, float, double, decimal
35
  public static final int FLAG_SUPPORT_SCALE = 4;     // float, double, decimal
36
  public static final int FLAG_PREDEFINED_PRECISION = 8;   // byte, int, long, float, double
37
  public static final int FLAG_SUPPORT_SIZE = 16;   // String, byte[]
38
  public static final int FLAG_FLOATING_POINT = 32;     // float, double
39
  
40
  /**
41
   * Without scale asigned
42
   */
43
  public static final int SCALE_NONE = -1; 
44

  
45
  public static final int PRECISION_NONE = -1; 
46

  
30 47
  public static final int BYTE_MAX_PRECISION = 3; // max byte 255
31 48
  public static final int BYTE_DEFAULT_PRECISION = BYTE_MAX_PRECISION;
32 49

  
......
55 72
  public static final int DECIMAL_DEFAULT_PRECISION = 20;
56 73
  public static final int DECIMAL_DEFAULT_SCALE = 3;
57 74

  
75
  public static final int LOCALE_DEFAULT_SIZE = CoerceToLocale.LOCALE_DEFAULT_SIZE;
58 76
  
59 77
  public interface NumberPrecisionAndScale {
60 78
    public int getPrecision();
......
106 124
  
107 125
  public boolean isPredefinedPrecision();
108 126
  
127
  public boolean isFloatingPoint();
128
  
109 129
  public NumberPrecisionAndScale fixPrecisionAndScale(int precision, int scale);
130
  
131
  public int getFlags();
110 132
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/DataTypes.java
52 52
	public static final int URI = 0x11;
53 53
	public static final int VERSION = 0x12;
54 54
	public static final int DECIMAL = 0x13;
55
	public static final int LOCALE = 0x14;
55 56
        
56 57
        @Deprecated 
57 58
	public static final int BIGDECIMAL = DECIMAL;
59
	public static final int INTEGER = INT;
58 60

  
59 61
	public static final int CONTAINER = 0x20;
60 62
	public static final int ARRAY = 0x21;
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/DefaultDataTypesManager.java
31 31
import java.util.Date;
32 32
import java.util.Iterator;
33 33
import java.util.List;
34
import java.util.Locale;
34 35
import java.util.Map;
35 36
import java.util.Set;
36 37

  
......
46 47
import static org.gvsig.tools.dataTypes.DataType.DOUBLE_DEFAULT_PRECISION;
47 48
import static org.gvsig.tools.dataTypes.DataType.DOUBLE_DEFAULT_SCALE;
48 49
import static org.gvsig.tools.dataTypes.DataType.DOUBLE_MAX_PRECISION;
50
import static org.gvsig.tools.dataTypes.DataType.FLAG_FLOATING_POINT;
51
import static org.gvsig.tools.dataTypes.DataType.FLAG_NUMBER;
52
import static org.gvsig.tools.dataTypes.DataType.FLAG_PREDEFINED_PRECISION;
53
import static org.gvsig.tools.dataTypes.DataType.FLAG_SUPPORT_PRECISION;
54
import static org.gvsig.tools.dataTypes.DataType.FLAG_SUPPORT_SCALE;
55
import static org.gvsig.tools.dataTypes.DataType.FLAG_SUPPORT_SIZE;
49 56
import static org.gvsig.tools.dataTypes.DataType.FLOAT_DEFAULT_PRECISION;
50 57
import static org.gvsig.tools.dataTypes.DataType.FLOAT_DEFAULT_SCALE;
51 58
import static org.gvsig.tools.dataTypes.DataType.FLOAT_MAX_PRECISION;
......
53 60
import static org.gvsig.tools.dataTypes.DataType.INT_MAX_PRECISION;
54 61
import static org.gvsig.tools.dataTypes.DataType.LONG_DEFAULT_PRECISION;
55 62
import static org.gvsig.tools.dataTypes.DataType.LONG_MAX_PRECISION;
63
import static org.gvsig.tools.dataTypes.DataType.PRECISION_NONE;
64
import static org.gvsig.tools.dataTypes.DataType.SCALE_NONE;
56 65
import org.gvsig.tools.dataTypes.DataTypes;
57 66
import org.gvsig.tools.dataTypes.DataTypesManager;
58
import static org.gvsig.tools.dataTypes.impl.DefaultDataType.NO_SUPPORT_SCALE;
59 67
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDecimal;
60 68
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBoolean;
61 69
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToByte;
......
66 74
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFile;
67 75
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFloat;
68 76
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToInt;
77
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToLocale;
69 78
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToLong;
70 79
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToObject;
71 80
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToString;
......
93 102
    );
94 103
    this.addtype(BYTE, null, "Byte", Byte.class, new CoerceToByte(),
95 104
            "datatype-byte",
96
            true, BYTE_MAX_PRECISION, BYTE_DEFAULT_PRECISION
105
            FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION, 
106
            BYTE_MAX_PRECISION, BYTE_DEFAULT_PRECISION
97 107
    );
98 108
    this.addtype(CHAR, null, "Char", Character.class, new CoerceToString());
99 109
    this.addtype(INT, null, "Integer", Integer.class, new CoerceToInt(), 
100 110
            "datatype-integer", 
101
            true, INT_MAX_PRECISION, INT_DEFAULT_PRECISION
111
            FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION, 
112
            INT_MAX_PRECISION, INT_DEFAULT_PRECISION
102 113
    );
103 114
    this.addtype(LONG, null, "Long", Long.class, new CoerceToLong(), 
104 115
            "datatype-long", 
105
            true, LONG_MAX_PRECISION, LONG_DEFAULT_PRECISION
116
            FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION, 
117
            LONG_MAX_PRECISION, LONG_DEFAULT_PRECISION
106 118
    );
107 119
    this.addtype(FLOAT, null, "Float", Float.class, new CoerceToFloat(), 
108 120
            "datatype-float", 
109
            false, FLOAT_MAX_PRECISION, FLOAT_DEFAULT_PRECISION, FLOAT_DEFAULT_SCALE
121
            FLAG_FLOATING_POINT|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION|FLAG_SUPPORT_SCALE, 
122
            FLOAT_MAX_PRECISION, FLOAT_DEFAULT_PRECISION, FLOAT_DEFAULT_SCALE
110 123
    );
111 124
    this.addtype(DOUBLE, null, "Double", Double.class, new CoerceToDouble(), 
112 125
            "datatype-double", 
113
            false, DOUBLE_MAX_PRECISION, DOUBLE_DEFAULT_PRECISION, DOUBLE_DEFAULT_SCALE
126
            FLAG_FLOATING_POINT|FLAG_SUPPORT_PRECISION|FLAG_PREDEFINED_PRECISION|FLAG_SUPPORT_SCALE, 
127
            DOUBLE_MAX_PRECISION, DOUBLE_DEFAULT_PRECISION, DOUBLE_DEFAULT_SCALE
114 128
    );
115 129
    this.addtype(DECIMAL, null, "Decimal", BigDecimal.class, new CoerceToDecimal(), 
116 130
            "datatype-decimal", 
117
            false, DECIMAL_MAX_PRECISION, DECIMAL_DEFAULT_PRECISION, DECIMAL_DEFAULT_SCALE
131
            FLAG_NUMBER|FLAG_SUPPORT_PRECISION|FLAG_SUPPORT_SCALE, 
132
            DECIMAL_MAX_PRECISION, DECIMAL_DEFAULT_PRECISION, DECIMAL_DEFAULT_SCALE
118 133
    );
119
    this.addtype(STRING, null, "String", String.class, new CoerceToString(), "datatype-string");
134
    this.addtype(STRING, null, "String", String.class, new CoerceToString(), 
135
            "datatype-string",
136
            FLAG_SUPPORT_SIZE
137
    );
120 138
    this.addtype(DATE, SUBTYPE_DATE, "Date", Date.class, new CoerceToDate(), "datatype-date");
121 139
    this.addtype(TIME, SUBTYPE_DATE, "Time", Date.class, new CoerceToTime(), "datatype-time");
122 140
//        this.addtype(TIMESTAMP, null, "Timestamp", Timestamp.class, new CoerceToTimestamp(),"datatype-timestamp");
123 141
    this.addtype(TIMESTAMP, null, "Timestamp", Date.class, new CoerceToDateTime(), "datatype-timestamp");
142

  
143
    this.addtype(LOCALE, null, "Locale", Locale.class, new CoerceToLocale(), 
144
            "datatype-locale"
145
    );
146

  
147

  
124 148
    this.addtype(VERSION, null, "Version", Version.class, new CoerceToVersion());
125 149

  
126 150
    this.addtype(BYTEARRAY, null, "ByteArray",
127 151
            (new byte[1]).getClass(),
128 152
            new CoerceToBytearray(),
129
            "datatype-bytearray"
153
            "datatype-bytearray",
154
            FLAG_SUPPORT_SIZE
130 155
    );
131 156
    this.addtype(FILE, SUBTYPE_FILE, "File", File.class, new CoerceToFile());
132 157
    this.addtype(FOLDER, SUBTYPE_FOLDER, "Folder", File.class, new CoerceToFile());
......
145 170
  @Override
146 171
  public final synchronized int addtype(int type, String subtype, String name,
147 172
          Class defaultClass, Coercion coercion) {
148
    return addtype(type, subtype, name, defaultClass, coercion, null, false, 0, 0, 0);
173
    return addtype(type, subtype, name, defaultClass, coercion, null, 0, PRECISION_NONE, PRECISION_NONE, SCALE_NONE);
149 174
  }
150 175

  
151 176
  public final synchronized int addtype(int type, String subtype, String name,
152 177
          Class defaultClass, Coercion coercion, String iconName) {
153
    return addtype(type, subtype, name, defaultClass, coercion, iconName, false, 0, 0, 0);
178
    return addtype(type, subtype, name, defaultClass, coercion, iconName, 0, PRECISION_NONE, PRECISION_NONE, SCALE_NONE);
154 179
  }
155 180

  
181
  @Override
156 182
  public final synchronized int addtype(int type, String subtype, String name,
183
          Class defaultClass, Coercion coercion, String iconName, int flags) {
184
    return addtype(type, subtype, name, defaultClass, coercion, iconName,
185
            flags, PRECISION_NONE, PRECISION_NONE, SCALE_NONE
186
    );
187
  }
188

  
189
  public final synchronized int addtype(int type, String subtype, String name,
157 190
          Class defaultClass, Coercion coercion, String iconName,
158
          boolean predefinedPrecision, int max_precision, int default_precision) {
191
          int flags, int max_precision, int default_precision) {
159 192
    return addtype(type, subtype, name, defaultClass, coercion, iconName,
160
            predefinedPrecision, max_precision, default_precision, NO_SUPPORT_SCALE
193
            flags, max_precision, default_precision, SCALE_NONE
161 194
    );
162 195
  }
163 196

  
164 197
  @Override
165 198
  public final synchronized int addtype(int type, String subtype, String name,
166 199
          Class defaultClass, Coercion coercion, String iconName,
167
          boolean predefinedPrecision, int max_precision, int default_precision, int default_scale) {
200
          int flags, int max_precision, int default_precision, int default_scale) {
168 201
    if (type == INVALID) {
169 202
      type = getNewObjectIndexType();
170 203
    }
......
185 218

  
186 219
    DataType dataType = new DefaultDataType(
187 220
            type, subtype, name, defaultClass, coercion, iconName,
188
            predefinedPrecision, max_precision, default_precision, default_scale
221
            flags, max_precision, default_precision, default_scale
189 222
    );
190 223
    types[type] = dataType;
191 224
    LOG.info("Registered data type {}.", dataType.toString());
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/DefaultDataType.java
66 66
    }
67 67
  }
68 68

  
69
  public static final int NO_SUPPORT_SCALE = -1;
70
  public static final int NO_SUPPORT_PRECISION = -1;
71
  
72
  private static final NumberPrecisionAndScale EMPTY_PRECISION_AND_SCALE = new DefaultNumberPrecisionAndScale(0, 0);
69
  private static final NumberPrecisionAndScale EMPTY_PRECISION_AND_SCALE = new DefaultNumberPrecisionAndScale(PRECISION_NONE, SCALE_NONE);
73 70

  
74 71
  private Coercion coercion;
75 72
  private Class defaultClass;
......
80 77
  private int max_precision;
81 78
  private int default_precision;
82 79
  private int default_scale;
83
  private boolean predefinedPrecision;
80
  private int flags;
84 81

  
85 82
  DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion) {
86 83
    this(type, subtype, name, defaultClass, coercion, "datatype-any");
87 84
  }
88 85

  
89 86
  DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion, String iconName) {
90
    this(type, subtype, name, defaultClass, coercion, "datatype-any", false, NO_SUPPORT_PRECISION, NO_SUPPORT_PRECISION, NO_SUPPORT_SCALE);
87
    this(type, subtype, name, defaultClass, coercion, "datatype-any", 0, PRECISION_NONE, PRECISION_NONE, SCALE_NONE);
91 88
  }
92 89

  
93
  DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion, String iconName, boolean isFixedPrecision, int max_precision, int default_precision) {
94
    this(type, subtype, name, defaultClass, coercion, "datatype-any", isFixedPrecision, max_precision, default_precision, NO_SUPPORT_SCALE);
90
  DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion, String iconName, int psflag, int max_precision, int default_precision) {
91
    this(type, subtype, name, defaultClass, coercion, "datatype-any", psflag, max_precision, default_precision, SCALE_NONE);
95 92
  }
96 93

  
97
  DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion, String iconName, boolean isFixedPrecision, int max_precision, int default_precision, int default_scale) {
94
  DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion, String iconName, int flags, int max_precision, int default_precision, int default_scale) {
98 95

  
99 96
    if (name == null) {
100 97
      LOG.trace("Can't register null type name for type {}.", new Object[]{Integer.toHexString(type).toUpperCase()});
......
106 103
    this.defaultClass = defaultClass;
107 104
    this.coercion = coercion;
108 105
    this.iconName = iconName == null ? "datatype-any" : iconName;
109
    this.max_precision = max_precision;
110
    this.default_precision = default_precision;
111
    this.default_scale = default_scale;
112
    this.predefinedPrecision = isFixedPrecision;
106
    
107
    this.flags = flags;
108
    if( (this.flags&FLAG_FLOATING_POINT)==FLAG_FLOATING_POINT ) {
109
      this.flags |= FLAG_NUMBER;
110
    }
111
    if( (this.flags&FLAG_SUPPORT_PRECISION)==FLAG_SUPPORT_PRECISION ) {
112
      this.max_precision = max_precision;
113
      if( this.default_precision>max_precision ) {
114
        this.default_precision = max_precision;
115
      } else {
116
        this.default_precision = default_precision;
117
      }
118
      if( (this.flags&FLAG_SUPPORT_SCALE)==FLAG_SUPPORT_SCALE ) {
119
        if( this.default_scale>this.max_precision ) {
120
          this.default_scale = this.max_precision -1;
121
        } else {
122
          this.default_scale = default_scale;
123
        }
124
      } else {
125
        this.default_scale = SCALE_NONE;
126
      }
127
    } else {
128
      this.max_precision = PRECISION_NONE;
129
      this.default_precision = PRECISION_NONE;
130
      this.default_scale = SCALE_NONE;
131
    }
113 132
  }
114 133

  
115 134
  @Override
......
247 266

  
248 267
  @Override
249 268
  public boolean isNumeric() {
250
    if (type == DataTypes.DOUBLE
251
            || type == DataTypes.FLOAT
252
            || type == DataTypes.BYTE
253
            || type == DataTypes.INT
254
            || type == DataTypes.LONG
255
            || type == DataTypes.DECIMAL) {
256
      return true;
257
    }
258
    return false;
269
    return (this.flags&FLAG_NUMBER) == FLAG_NUMBER;
259 270
  }
260 271

  
261 272
  @Override
262 273
  public boolean supportSize() {
263
    switch (this.type) {
264
      case DataTypes.STRING:
265
      case DataTypes.BYTEARRAY:
266
        return true;
267
      default:
268
        return false;
269
    }
274
    return (this.flags&FLAG_SUPPORT_SIZE) == FLAG_SUPPORT_SIZE;
270 275
  }
271 276

  
272 277
  @Override
273 278
  public boolean supportPrecision() {
274
    return this.max_precision!=NO_SUPPORT_PRECISION;
279
    return (this.flags&FLAG_SUPPORT_PRECISION) == FLAG_SUPPORT_PRECISION;
275 280
  }
276 281

  
277 282
  @Override
278 283
  public boolean supportScale() {
279
    return this.default_scale!=NO_SUPPORT_SCALE;
284
    return (this.flags&FLAG_SUPPORT_SCALE) == FLAG_SUPPORT_SCALE;
280 285
  }
281 286

  
282 287
  @Override
288
  public boolean isPredefinedPrecision() {
289
    return (this.flags&FLAG_PREDEFINED_PRECISION) == FLAG_PREDEFINED_PRECISION ;
290
  }
291

  
292
  @Override
293
  public boolean isFloatingPoint() {
294
    return (this.flags&FLAG_FLOATING_POINT) == FLAG_FLOATING_POINT ;
295
  }
296
  
297
  @Override
298
  public int getFlags() {
299
    return this.flags;
300
  }
301

  
302
  @Override
283 303
  public NumberPrecisionAndScale fixPrecisionAndScale(int precision, int scale) {
284
    if( this.max_precision==NO_SUPPORT_PRECISION ) {
304
    if( (this.flags&FLAG_SUPPORT_PRECISION) != FLAG_SUPPORT_PRECISION ) {
285 305
       return EMPTY_PRECISION_AND_SCALE;
286 306
    }
287 307
    DefaultNumberPrecisionAndScale r = new DefaultNumberPrecisionAndScale(
288 308
            precision, 
289 309
            scale
290 310
    );
291
    if( this.predefinedPrecision ) {
311
    if( (this.flags&FLAG_PREDEFINED_PRECISION) == FLAG_PREDEFINED_PRECISION ) {
292 312
      r.precision = max_precision;
293 313
    }
294
    if( this.default_scale==NO_SUPPORT_SCALE ) {
314
    if( (this.flags&FLAG_SUPPORT_SCALE) != FLAG_SUPPORT_SCALE ) {
295 315
      // Soporta precision y no escala (byte, int, long... enteros)
296 316
      r.scale = 0;
297 317
      if( r.precision > max_precision ) {
298 318
        r.precision = max_precision;
319
      } else if( r.precision < 1 ) {
320
        r.precision = default_precision;
299 321
      }
300 322
    } else {
323
      if( r.scale == SCALE_NONE ) {
324
        r.scale = default_scale;
325
      }
301 326
      // Soporta precision y escala.
302 327
      if (r.precision < 1) {
303 328
        // Sin precision
......
385 410
  public int getDefaultScale() {
386 411
    return this.default_scale;
387 412
  }
388

  
389
  @Override
390
  public boolean isPredefinedPrecision() {
391
    return this.predefinedPrecision;
392
  }
393 413
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToLocale.java
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
}
0 136

  
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToString.java
33 33
import org.gvsig.tools.dataTypes.CoercionContext;
34 34
import org.gvsig.tools.dataTypes.CoercionContextLocale;
35 35
import org.gvsig.tools.dataTypes.CoercionException;
36
import org.gvsig.tools.dataTypes.DataTypesManager;
37 36

  
38 37
public class CoerceToString extends AbstractCoercion {
39 38

  
......
68 67
			} else if (value instanceof BigDecimal ) {
69 68
        s = ((BigDecimal) value).toPlainString();
70 69

  
70
			} else if (value instanceof Locale ) {
71
        s = CoerceToLocale.toString((Locale)value);
72
        
71 73
  		} else {
72 74
				s = value.toString();
73 75
			}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/ToolsSwingDefaultImplLibrary.java
106 106
            new String[] { "DataTypes", "datatype-float" },
107 107
            new String[] { "DataTypes", "datatype-image" },
108 108
            new String[] { "DataTypes", "datatype-integer" },
109
            new String[] { "DataTypes", "datatype-locale" },
109 110
            new String[] { "DataTypes", "datatype-long" },
110 111
            new String[] { "DataTypes", "datatype-object" },
111 112
            new String[] { "DataTypes", "datatype-string" },

Also available in: Unified diff