Revision 2080
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/CoercionContextLocaleImpl.java | ||
---|---|---|
1 |
package org.gvsig.tools.dataTypes; |
|
2 |
|
|
3 |
import java.util.Locale; |
|
4 |
|
|
5 |
/** |
|
6 |
* |
|
7 |
* @author jjdelcerro |
|
8 |
*/ |
|
9 |
public class CoercionContextLocaleImpl implements CoercionContextLocale { |
|
10 |
|
|
11 |
final Locale locale; |
|
12 |
|
|
13 |
public CoercionContextLocaleImpl(Locale locale) { |
|
14 |
if( locale == null ) { |
|
15 |
this.locale = Locale.getDefault(); |
|
16 |
} else { |
|
17 |
this.locale = locale; |
|
18 |
} |
|
19 |
} |
|
20 |
|
|
21 |
@Override |
|
22 |
public Locale locale() { |
|
23 |
return this.locale; |
|
24 |
} |
|
25 |
|
|
26 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/DataTypesManager.java | ||
---|---|---|
1 | 1 |
/** |
2 | 2 |
* gvSIG. Desktop Geographic Information System. |
3 | 3 |
* |
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
|
5 | 5 |
* |
6 | 6 |
* This program is free software; you can redistribute it and/or |
7 | 7 |
* modify it under the terms of the GNU General Public License |
... | ... | |
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.tools.dataTypes; |
25 | 25 |
|
26 |
import java.math.MathContext; |
|
27 |
import java.math.RoundingMode; |
|
26 | 28 |
import java.util.Iterator; |
27 |
import java.util.Locale; |
|
28 | 29 |
|
29 | 30 |
/** |
30 | 31 |
* Manages data types. |
... | ... | |
34 | 35 |
*/ |
35 | 36 |
public interface DataTypesManager extends Iterable<DataType> { |
36 | 37 |
|
37 |
public interface Coercion { |
|
38 |
|
|
39 |
public Object coerce(Object value) throws CoercionException; |
|
40 |
} |
|
41 |
|
|
42 |
public interface CoercionWithLocale extends Coercion { |
|
43 |
|
|
44 |
public Object coerce(Object value, Locale locale) throws CoercionException; |
|
45 |
} |
|
46 |
|
|
47 | 38 |
public DataType get(int type); |
48 | 39 |
|
49 | 40 |
public boolean isValidType(int type); |
... | ... | |
76 | 67 |
|
77 | 68 |
public Coercion getCoercion(int type); |
78 | 69 |
|
79 |
public void setCoercion(int type, Coercion coercion); |
|
70 |
// public void setCoercion(int type, Coercion coercion);
|
|
80 | 71 |
|
81 | 72 |
public void addCoercion(int type, Coercion coercion); |
82 | 73 |
|
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.DataTypesManager.Coercion; |
|
27 | 26 |
import org.gvsig.tools.util.LabeledValue; |
28 | 27 |
|
29 | 28 |
public interface DataType extends LabeledValue<DataType>, org.gvsig.tools.lang.Cloneable { |
... | ... | |
44 | 43 |
|
45 | 44 |
public String getSubtype() ; |
46 | 45 |
|
47 |
public Coercion getCoercion() ;
|
|
46 |
public Coercion getCoercion(); |
|
48 | 47 |
|
49 |
public void setCoercion(Coercion coercion) ;
|
|
48 |
// public void setCoercion(Coercion coercion);
|
|
50 | 49 |
|
51 |
public void addCoercion(Coercion coercion) ;
|
|
50 |
public void addCoercion(Coercion coercion); |
|
52 | 51 |
|
53 | 52 |
public Object coerce(Object value) throws CoercionException; |
54 | 53 |
|
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/DataTypeUtils.java | ||
---|---|---|
1 | 1 |
package org.gvsig.tools.dataTypes; |
2 | 2 |
|
3 |
import java.util.Locale; |
|
3 | 4 |
import org.gvsig.tools.ToolsLocator; |
5 |
import org.gvsig.tools.dataTypes.CoercionContextLocale; |
|
6 |
import org.gvsig.tools.dataTypes.CoercionContextDecimal; |
|
4 | 7 |
|
5 | 8 |
/** |
6 | 9 |
* |
... | ... | |
8 | 11 |
*/ |
9 | 12 |
public class DataTypeUtils { |
10 | 13 |
|
14 |
private static CoercionContextLocale COERCE_CONTEXT_DEFAULT_LOCALE ; |
|
15 |
private static CoercionContextDecimal COERCE_CONTEXT_DEFAULT_DECIMAL ; |
|
16 |
|
|
11 | 17 |
protected DataTypeUtils() { |
12 | 18 |
|
13 | 19 |
} |
20 |
|
|
21 |
public static CoercionContextLocale coerceContextLocale(Locale locale) { |
|
22 |
return new CoercionContextLocaleImpl(locale); |
|
23 |
} |
|
14 | 24 |
|
25 |
public static CoercionContextLocale coerceContextDefaultLocale() { |
|
26 |
if( COERCE_CONTEXT_DEFAULT_LOCALE==null || |
|
27 |
Locale.getDefault()!=COERCE_CONTEXT_DEFAULT_LOCALE.locale() ) { |
|
28 |
COERCE_CONTEXT_DEFAULT_LOCALE = new CoercionContextLocaleImpl(Locale.getDefault()); |
|
29 |
} |
|
30 |
return COERCE_CONTEXT_DEFAULT_LOCALE; |
|
31 |
} |
|
32 |
|
|
33 |
public static CoercionContextDecimal coerceContextDefaultDecimal() { |
|
34 |
if( COERCE_CONTEXT_DEFAULT_DECIMAL==null || |
|
35 |
Locale.getDefault()!=COERCE_CONTEXT_DEFAULT_DECIMAL.locale() ) { |
|
36 |
COERCE_CONTEXT_DEFAULT_DECIMAL = new CoercionContextDecimalImpl( |
|
37 |
Locale.getDefault() |
|
38 |
); |
|
39 |
} |
|
40 |
return COERCE_CONTEXT_DEFAULT_DECIMAL; |
|
41 |
} |
|
42 |
|
|
43 |
public static CoercionContextDecimal coerceContextDecimal(Locale locale) { |
|
44 |
if( COERCE_CONTEXT_DEFAULT_DECIMAL!=null || |
|
45 |
locale==COERCE_CONTEXT_DEFAULT_DECIMAL.locale() ) { |
|
46 |
return COERCE_CONTEXT_DEFAULT_DECIMAL; |
|
47 |
} |
|
48 |
return new CoercionContextDecimalImpl(locale); |
|
49 |
} |
|
50 |
|
|
51 |
public static CoercionContextDecimal coerceContextDecimal(Locale locale, int precision, int scale, int roundMode) { |
|
52 |
return new CoercionContextDecimalImpl(locale,precision, scale, roundMode); |
|
53 |
} |
|
54 |
|
|
15 | 55 |
public static Object coerce(int type, Object value, Object defaultValue) { |
16 | 56 |
DataTypesManager manager = ToolsLocator.getDataTypesManager(); |
17 | 57 |
DataType dataType = manager.get(type); |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/DataTypes.java | ||
---|---|---|
51 | 51 |
public static final int URL = 0x10; |
52 | 52 |
public static final int URI = 0x11; |
53 | 53 |
public static final int VERSION = 0x12; |
54 |
public static final int BIGDECIMAL = 0x13; |
|
54 |
public static final int DECIMAL = 0x13; |
|
55 |
|
|
56 |
@Deprecated |
|
57 |
public static final int BIGDECIMAL = DECIMAL; |
|
55 | 58 |
|
56 |
|
|
57 | 59 |
public static final int CONTAINER = 0x20; |
58 | 60 |
public static final int ARRAY = 0x21; |
59 | 61 |
public static final int LIST = 0x22; |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/Coercion.java | ||
---|---|---|
1 |
/** |
|
2 |
* gvSIG. Desktop Geographic Information System. |
|
3 |
* |
|
4 |
* Copyright (C) 2007-2020 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; |
|
25 |
|
|
26 |
/** |
|
27 |
* |
|
28 |
* @author jjdelcerro |
|
29 |
*/ |
|
30 |
public interface Coercion { |
|
31 |
|
|
32 |
public Object coerce(Object value) throws CoercionException; |
|
33 |
|
|
34 |
public Object coerce(Object value, CoercionContext context) throws CoercionException; |
|
35 |
|
|
36 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/DefaultDataType.java | ||
---|---|---|
26 | 26 |
import java.text.MessageFormat; |
27 | 27 |
import java.util.ArrayList; |
28 | 28 |
import java.util.List; |
29 |
import java.util.Locale; |
|
30 | 29 |
|
31 | 30 |
import org.gvsig.tools.dataTypes.CoercionException; |
32 | 31 |
import org.gvsig.tools.dataTypes.DataType; |
33 | 32 |
import org.gvsig.tools.dataTypes.DataTypes; |
34 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
|
35 |
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale; |
|
33 |
import org.gvsig.tools.dataTypes.Coercion; |
|
36 | 34 |
import org.slf4j.Logger; |
37 | 35 |
import org.slf4j.LoggerFactory; |
36 |
import org.gvsig.tools.dataTypes.CoercionContext; |
|
38 | 37 |
|
39 | 38 |
public class DefaultDataType implements DataType { |
40 | 39 |
|
41 |
private static final Logger LOG = LoggerFactory.getLogger(DefaultDataTypesManager.class); |
|
42 |
|
|
43 |
private Coercion coercion; |
|
44 |
private Class defaultClass; |
|
45 |
private String subtype; |
|
46 |
private int type; |
|
47 |
private String name; |
|
48 |
private String iconName; |
|
49 |
|
|
50 |
DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion) { |
|
51 |
this(type, subtype, name, defaultClass, coercion, "datatype-any"); |
|
52 |
} |
|
53 |
|
|
54 |
DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion, String iconName) { |
|
40 |
private static final Logger LOG = LoggerFactory.getLogger(DefaultDataTypesManager.class); |
|
55 | 41 |
|
56 |
if( name == null ) { |
|
57 |
LOG.trace("Can't register null type name for type {}.", new Object[] { Integer.toHexString(type).toUpperCase()}); |
|
58 |
throw new IllegalArgumentException(); |
|
59 |
} |
|
60 |
this.type = type; |
|
61 |
this.subtype = subtype; |
|
62 |
this.name = name; |
|
63 |
this.defaultClass = defaultClass; |
|
64 |
this.coercion = coercion; |
|
65 |
this.iconName = iconName==null? "datatype-any":iconName; |
|
66 |
} |
|
42 |
private Coercion coercion; |
|
43 |
private Class defaultClass; |
|
44 |
private String subtype; |
|
45 |
private int type; |
|
46 |
private String name; |
|
47 |
private String iconName; |
|
67 | 48 |
|
68 |
@Override |
|
69 |
public DataType clone() throws CloneNotSupportedException { |
|
70 |
DefaultDataType other = (DefaultDataType) super.clone(); |
|
71 |
if( other.coercion instanceof Coercions ) { |
|
72 |
this.coercion = ((Coercions)this.coercion).clone(); |
|
73 |
} |
|
74 |
return other; |
|
75 |
} |
|
49 |
DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion) { |
|
50 |
this(type, subtype, name, defaultClass, coercion, "datatype-any"); |
|
51 |
} |
|
76 | 52 |
|
77 |
@Override |
|
78 |
public String getLabel() { |
|
79 |
return this.getName(); |
|
80 |
} |
|
53 |
DefaultDataType(int type, String subtype, String name, Class defaultClass, Coercion coercion, String iconName) { |
|
81 | 54 |
|
82 |
@Override |
|
83 |
public DataType getValue() { |
|
84 |
return this; |
|
85 |
} |
|
86 |
|
|
87 |
@Override |
|
88 |
public Object coerce(Object value) throws CoercionException { |
|
89 |
// http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion |
|
55 |
if (name == null) { |
|
56 |
LOG.trace("Can't register null type name for type {}.", new Object[]{Integer.toHexString(type).toUpperCase()}); |
|
57 |
throw new IllegalArgumentException(); |
|
58 |
} |
|
59 |
this.type = type; |
|
60 |
this.subtype = subtype; |
|
61 |
this.name = name; |
|
62 |
this.defaultClass = defaultClass; |
|
63 |
this.coercion = coercion; |
|
64 |
this.iconName = iconName == null ? "datatype-any" : iconName; |
|
65 |
} |
|
90 | 66 |
|
91 |
if( this.coercion != null ) { |
|
92 |
return this.coercion.coerce(value); |
|
93 |
} |
|
94 |
if( defaultClass == null ) { |
|
95 |
return value; // �? |
|
96 |
} |
|
97 |
if( defaultClass.isInstance(value) ) { |
|
98 |
return value; |
|
99 |
} |
|
100 |
throw new CoercionException(); |
|
101 |
} |
|
67 |
@Override |
|
68 |
public DataType clone() throws CloneNotSupportedException { |
|
69 |
DefaultDataType other = (DefaultDataType) super.clone(); |
|
70 |
if (other.coercion instanceof Coercions) { |
|
71 |
this.coercion = ((Coercions) this.coercion).clone(); |
|
72 |
} |
|
73 |
return other; |
|
74 |
} |
|
102 | 75 |
|
103 |
@Override
|
|
104 |
public Coercion getCoercion() {
|
|
105 |
return this.coercion;
|
|
106 |
}
|
|
76 |
@Override |
|
77 |
public String getLabel() {
|
|
78 |
return this.getName();
|
|
79 |
}
|
|
107 | 80 |
|
108 |
@Override
|
|
109 |
public Class getDefaultClass() {
|
|
110 |
return this.defaultClass;
|
|
111 |
}
|
|
81 |
@Override |
|
82 |
public DataType getValue() {
|
|
83 |
return this;
|
|
84 |
}
|
|
112 | 85 |
|
113 |
@Override |
|
114 |
public String getSubtype() { |
|
115 |
return this.subtype; |
|
116 |
} |
|
86 |
@Override |
|
87 |
public Object coerce(Object value) throws CoercionException { |
|
88 |
// http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion |
|
117 | 89 |
|
118 |
@Override |
|
119 |
public int getType() { |
|
120 |
return this.type; |
|
121 |
} |
|
90 |
if (this.coercion != null) { |
|
91 |
return this.coercion.coerce(value); |
|
92 |
} |
|
93 |
if (defaultClass == null) { |
|
94 |
return value; // ?? |
|
95 |
} |
|
96 |
if (defaultClass.isInstance(value)) { |
|
97 |
return value; |
|
98 |
} |
|
99 |
throw new CoercionException(); |
|
100 |
} |
|
122 | 101 |
|
123 |
@Override
|
|
124 |
public String getName() {
|
|
125 |
return this.name;
|
|
126 |
}
|
|
102 |
@Override |
|
103 |
public Coercion getCoercion() {
|
|
104 |
return this.coercion;
|
|
105 |
}
|
|
127 | 106 |
|
128 |
@Override
|
|
129 |
public String getIconName() {
|
|
130 |
return this.iconName;
|
|
131 |
}
|
|
107 |
@Override |
|
108 |
public Class getDefaultClass() {
|
|
109 |
return this.defaultClass;
|
|
110 |
} |
|
132 | 111 |
|
133 |
@Override
|
|
134 |
public boolean isContainer() {
|
|
135 |
return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
|
|
136 |
}
|
|
112 |
@Override |
|
113 |
public String getSubtype() {
|
|
114 |
return this.subtype;
|
|
115 |
}
|
|
137 | 116 |
|
138 |
@Override
|
|
139 |
public boolean isObject() {
|
|
140 |
return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
|
|
141 |
}
|
|
117 |
@Override |
|
118 |
public int getType() {
|
|
119 |
return this.type;
|
|
120 |
}
|
|
142 | 121 |
|
143 |
@Override
|
|
144 |
public boolean isDynObject() {
|
|
145 |
return type == DataTypes.DYNOBJECT;
|
|
146 |
}
|
|
122 |
@Override |
|
123 |
public String getName() {
|
|
124 |
return this.name;
|
|
125 |
}
|
|
147 | 126 |
|
148 |
@Override |
|
149 |
public void setCoercion(Coercion coercion) { |
|
150 |
this.coercion = coercion; |
|
151 |
LOG.trace("Add coercion operation for data type {}.", new Object[] { name }); |
|
152 |
} |
|
127 |
@Override |
|
128 |
public String getIconName() { |
|
129 |
return this.iconName; |
|
130 |
} |
|
153 | 131 |
|
154 |
@Override |
|
155 |
public void addCoercion(Coercion coercion) { |
|
156 |
if( this.coercion==null ) { |
|
157 |
this.setCoercion(coercion); |
|
158 |
return; |
|
159 |
} |
|
160 |
Coercions coercions; |
|
161 |
if (this.coercion instanceof Coercions) { |
|
162 |
coercions = (Coercions) this.coercion; |
|
163 |
} else { |
|
164 |
coercions = new Coercions(); |
|
165 |
coercions.add(this.coercion); |
|
166 |
this.coercion = coercions; |
|
167 |
} |
|
168 |
coercions.add(coercion); |
|
169 |
LOG.trace("Add coercion operation for data type {}.", new Object[] { name }); |
|
170 |
} |
|
132 |
@Override |
|
133 |
public boolean isContainer() { |
|
134 |
return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER; |
|
135 |
} |
|
171 | 136 |
|
172 |
@Override |
|
173 |
public String toString() { |
|
174 |
return MessageFormat.format( |
|
175 |
"type=0x{0};subtype={1};name={2};class={3};coercion={4}", |
|
176 |
new Object[] { |
|
177 |
Integer.toHexString(type).toUpperCase(), |
|
178 |
subtype, |
|
179 |
name, |
|
180 |
defaultClass==null? null:defaultClass.getName(), |
|
181 |
coercion==null? null:coercion.getClass().getName() |
|
182 |
} |
|
183 |
); |
|
184 |
} |
|
137 |
@Override |
|
138 |
public boolean isObject() { |
|
139 |
return (type & DataTypes.OBJECT) == DataTypes.OBJECT; |
|
140 |
} |
|
185 | 141 |
|
186 |
@Override |
|
187 |
public boolean isNumeric() { |
|
188 |
if( type == DataTypes.DOUBLE || |
|
189 |
type == DataTypes.FLOAT || |
|
190 |
type == DataTypes.INT || |
|
191 |
type == DataTypes.LONG){ |
|
192 |
return true; |
|
193 |
} |
|
194 |
return false; |
|
195 |
} |
|
142 |
@Override |
|
143 |
public boolean isDynObject() { |
|
144 |
return type == DataTypes.DYNOBJECT; |
|
145 |
} |
|
196 | 146 |
|
197 |
private static class Coercions |
|
198 |
implements CoercionWithLocale, org.gvsig.tools.lang.Cloneable |
|
199 |
{ |
|
147 |
// @Override |
|
148 |
public void setCoercion(Coercion coercion) { |
|
149 |
this.coercion = coercion; |
|
150 |
LOG.trace("Add coercion operation for data type {}.", new Object[]{name}); |
|
151 |
} |
|
200 | 152 |
|
201 |
private List<Coercion> coercions; |
|
202 |
|
|
203 |
public Coercions() { |
|
204 |
this.coercions = new ArrayList<>(); |
|
205 |
} |
|
206 |
|
|
207 |
public void add(Coercion coercion) { |
|
208 |
this.coercions.add(coercion); |
|
209 |
} |
|
153 |
@Override |
|
154 |
public void addCoercion(Coercion coercion) { |
|
155 |
if (this.coercion == null) { |
|
156 |
this.setCoercion(coercion); |
|
157 |
return; |
|
158 |
} |
|
159 |
Coercions coercions; |
|
160 |
if (this.coercion instanceof Coercions) { |
|
161 |
coercions = (Coercions) this.coercion; |
|
162 |
} else { |
|
163 |
coercions = new Coercions(); |
|
164 |
coercions.add(this.coercion); |
|
165 |
this.coercion = coercions; |
|
166 |
} |
|
167 |
coercions.add(coercion); |
|
168 |
LOG.trace("Add coercion operation for data type {}.", new Object[]{name}); |
|
169 |
} |
|
210 | 170 |
|
211 |
@Override |
|
212 |
public Coercions clone() throws CloneNotSupportedException { |
|
213 |
Coercions other = (Coercions) super.clone(); |
|
214 |
other.coercions = new ArrayList<>(); |
|
215 |
other.coercions.addAll(this.coercions); |
|
216 |
return other; |
|
171 |
@Override |
|
172 |
public String toString() { |
|
173 |
return MessageFormat.format( |
|
174 |
"type=0x{0};subtype={1};name={2};class={3};coercion={4}", |
|
175 |
new Object[]{ |
|
176 |
Integer.toHexString(type).toUpperCase(), |
|
177 |
subtype, |
|
178 |
name, |
|
179 |
defaultClass == null ? null : defaultClass.getName(), |
|
180 |
coercion == null ? null : coercion.getClass().getName() |
|
217 | 181 |
} |
218 |
|
|
219 |
@Override |
|
220 |
public Object coerce(Object value) throws CoercionException { |
|
221 |
return coerce(value, Locale.getDefault()); |
|
222 |
} |
|
223 |
|
|
224 |
@Override |
|
225 |
public Object coerce(Object value, Locale locale) throws CoercionException { |
|
226 |
for (int i = this.coercions.size(); --i >= 0;) { |
|
227 |
Coercion coercion = this.coercions.get(i); |
|
228 |
try { |
|
229 |
if (coercion instanceof CoercionWithLocale) { |
|
230 |
return ((CoercionWithLocale) coercion).coerce(value, locale); |
|
231 |
} |
|
232 |
return coercion.coerce(value); |
|
233 |
} catch (CoercionException e) { |
|
234 |
// Do nothing, go to next coercion |
|
235 |
coercion = null; // To allow set break point |
|
236 |
} |
|
237 |
} |
|
238 |
throw new CoercionException(); |
|
239 |
} |
|
182 |
); |
|
183 |
} |
|
240 | 184 |
|
185 |
@Override |
|
186 |
public boolean isNumeric() { |
|
187 |
if (type == DataTypes.DOUBLE |
|
188 |
|| type == DataTypes.FLOAT |
|
189 |
|| type == DataTypes.INT |
|
190 |
|| type == DataTypes.LONG |
|
191 |
|| type == DataTypes.DECIMAL) { |
|
192 |
return true; |
|
193 |
} |
|
194 |
return false; |
|
195 |
} |
|
196 |
|
|
197 |
private static class Coercions |
|
198 |
implements Coercion, org.gvsig.tools.lang.Cloneable { |
|
199 |
|
|
200 |
List<Coercion> coercions; |
|
201 |
|
|
202 |
public Coercions() { |
|
203 |
this.coercions = new ArrayList<>(); |
|
204 |
} |
|
205 |
|
|
206 |
public void add(Coercion coercion) { |
|
207 |
this.coercions.add(coercion); |
|
208 |
} |
|
209 |
|
|
210 |
@Override |
|
211 |
public Coercions clone() throws CloneNotSupportedException { |
|
212 |
Coercions other = (Coercions) super.clone(); |
|
213 |
other.coercions = new ArrayList<>(); |
|
214 |
other.coercions.addAll(this.coercions); |
|
215 |
return other; |
|
216 |
} |
|
217 |
|
|
218 |
@Override |
|
219 |
public Object coerce(Object value) throws CoercionException { |
|
220 |
return coerce(value, null); |
|
221 |
} |
|
222 |
|
|
223 |
@Override |
|
224 |
@SuppressWarnings("UnusedAssignment") |
|
225 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
226 |
for (int i = this.coercions.size(); --i >= 0;) { |
|
227 |
Coercion coercion = this.coercions.get(i); |
|
228 |
try { |
|
229 |
return coercion.coerce(value, context); |
|
230 |
} catch (CoercionException e) { |
|
231 |
// Do nothing, go to next coercion |
|
232 |
coercion = null; // To allow set break point |
|
241 | 233 |
} |
242 |
|
|
234 |
} |
|
235 |
throw new CoercionException(); |
|
236 |
} |
|
237 |
|
|
238 |
} |
|
239 |
|
|
243 | 240 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToBigDecimal.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 modify it under |
|
7 |
* the terms of the GNU General Public License as published by the Free Software |
|
8 |
* Foundation; either version 2 of the License, or (at your option) any later |
|
9 |
* version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
14 |
* details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License along with |
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51 |
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
19 |
* |
|
20 |
* For any additional information, do not hesitate to contact us at info AT |
|
21 |
* gvsig.com, or visit our website www.gvsig.com. |
|
22 |
*/ |
|
23 |
package org.gvsig.tools.dataTypes.impl.coercion; |
|
24 |
|
|
25 |
import java.math.BigDecimal; |
|
26 |
import java.text.NumberFormat; |
|
27 |
import java.text.ParsePosition; |
|
28 |
import java.util.Locale; |
|
29 |
import org.apache.commons.lang3.ArrayUtils; |
|
30 |
import org.apache.commons.lang3.StringUtils; |
|
31 |
|
|
32 |
import org.gvsig.tools.dataTypes.CoercionException; |
|
33 |
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale; |
|
34 |
|
|
35 |
public class CoerceToBigDecimal implements CoercionWithLocale { |
|
36 |
|
|
37 |
public Object coerce(Object value) throws CoercionException { |
|
38 |
if (value == null) { |
|
39 |
return null; |
|
40 |
} |
|
41 |
try { |
|
42 |
if (!(value instanceof BigDecimal)) { |
|
43 |
if (value instanceof Number) { |
|
44 |
value = new BigDecimal(((Number) value).doubleValue()); |
|
45 |
} else { |
|
46 |
String s = value.toString(); |
|
47 |
if (s == null) { |
|
48 |
return null; |
|
49 |
} |
|
50 |
s = s.trim().toLowerCase(); |
|
51 |
if (s.length() == 0) { |
|
52 |
return null; |
|
53 |
} |
|
54 |
if (s.startsWith("0x")) { |
|
55 |
value = new BigDecimal(Long.parseLong(s.substring(2), 16)); |
|
56 |
} else { |
|
57 |
value = new BigDecimal(Double.valueOf(s)); |
|
58 |
} |
|
59 |
} |
|
60 |
} |
|
61 |
return value; |
|
62 |
} catch (Exception e) { |
|
63 |
throw new CoercionException(e); |
|
64 |
} |
|
65 |
} |
|
66 |
|
|
67 |
public Object coerce(Object value, Locale locale) throws CoercionException { |
|
68 |
if (value == null) { |
|
69 |
return null; |
|
70 |
} |
|
71 |
try { |
|
72 |
if (!(value instanceof BigDecimal)) { |
|
73 |
if (value instanceof Number) { |
|
74 |
value = new BigDecimal(((Number) value).doubleValue()); |
|
75 |
} else { |
|
76 |
String s = value.toString().trim().toLowerCase(); |
|
77 |
if( StringUtils.isBlank(s) ) { |
|
78 |
return null; |
|
79 |
} |
|
80 |
if (s.startsWith("0x")) { |
|
81 |
value = new BigDecimal(Long.parseLong(s.substring(2), 16)); |
|
82 |
} else { |
|
83 |
if( locale == null ) { |
|
84 |
locale = Locale.getDefault(); |
|
85 |
} |
|
86 |
if( s.startsWith("+") ) { |
|
87 |
s = s.substring(1); |
|
88 |
} |
|
89 |
ParsePosition p = new ParsePosition(0); |
|
90 |
NumberFormat nf = NumberFormat.getInstance(locale); |
|
91 |
Number num = nf.parse(s, p); |
|
92 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
|
93 |
throw new CoercionException("Can't coerce '"+s+"' to BigDecimal with locale " + (locale==null?"null":locale.getLanguage()) + "(error index "+p.getErrorIndex()+", index "+p.getIndex()+")."); |
|
94 |
} |
|
95 |
value = new BigDecimal(num.doubleValue()); |
|
96 |
} |
|
97 |
} |
|
98 |
} |
|
99 |
return value; |
|
100 |
} catch (CoercionException e) { |
|
101 |
throw e; |
|
102 |
} catch (Exception e) { |
|
103 |
throw new CoercionException("Can't coerce '"+(value==null?"null":value.toString())+"' to BigDecimal with locale " + (locale==null?"null":locale.getLanguage())+"'.",e); |
|
104 |
} |
|
105 |
} |
|
106 |
|
|
107 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDouble.java | ||
---|---|---|
22 | 22 |
*/ |
23 | 23 |
package org.gvsig.tools.dataTypes.impl.coercion; |
24 | 24 |
|
25 |
import java.text.DecimalFormat; |
|
25 | 26 |
import java.text.NumberFormat; |
26 | 27 |
import java.text.ParsePosition; |
27 | 28 |
import java.util.Date; |
28 |
import java.util.Locale; |
|
29 |
import org.apache.commons.lang3.ArrayUtils; |
|
30 |
import org.apache.commons.lang3.StringUtils; |
|
29 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
|
31 | 30 |
|
32 | 31 |
import org.gvsig.tools.dataTypes.CoercionException; |
33 |
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale; |
|
32 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
|
33 |
import org.gvsig.tools.dataTypes.CoercionContext; |
|
34 |
import org.gvsig.tools.dataTypes.CoercionContextLocale; |
|
34 | 35 |
|
35 |
public class CoerceToDouble implements CoercionWithLocale { |
|
36 |
@SuppressWarnings("UseSpecificCatch") |
|
37 |
public class CoerceToDouble extends AbstractCoercion { |
|
36 | 38 |
|
37 |
public Object coerce(Object value) throws CoercionException { |
|
38 |
if (value == null) { |
|
39 |
return null; |
|
40 |
} |
|
41 |
try { |
|
42 |
if (!(value instanceof Double)) { |
|
43 |
if (value instanceof Number) { |
|
44 |
value = ((Number) value).doubleValue(); |
|
45 |
} else if( value instanceof Boolean ) { |
|
46 |
return (double)((boolean)value ? 1:0); |
|
47 |
} else if (value instanceof Date) { |
|
48 |
value = (double)(((Date)value).getTime()); |
|
49 |
} else { |
|
50 |
String s = value.toString(); |
|
51 |
if (s == null) { |
|
52 |
return null; |
|
53 |
} |
|
54 |
s = s.trim().toLowerCase(); |
|
55 |
if (s.length() == 0) { |
|
56 |
return null; |
|
57 |
} |
|
58 |
if (s.startsWith("0x")) { |
|
59 |
value = new Double(Long.parseLong(s.substring(2), 16)); |
|
60 |
} else { |
|
61 |
value = Double.valueOf(s); |
|
62 |
} |
|
63 |
} |
|
64 |
} |
|
65 |
return value; |
|
66 |
} catch (Exception e) { |
|
67 |
throw new CoercionException(e); |
|
68 |
} |
|
39 |
@Override |
|
40 |
public Object coerce(Object value) throws CoercionException { |
|
41 |
if (value == null || value instanceof Double) { |
|
42 |
return value; |
|
69 | 43 |
} |
44 |
return coerce(value, DataTypeUtils.coerceContextDefaultLocale()); |
|
45 |
} |
|
70 | 46 |
|
71 |
public Object coerce(Object value, Locale locale) throws CoercionException { |
|
72 |
if (value == null) { |
|
73 |
return null; |
|
47 |
@Override |
|
48 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
49 |
if (value == null || value instanceof Double) { |
|
50 |
return value; |
|
51 |
} |
|
52 |
CoercionContextLocale theContext; |
|
53 |
if (context instanceof CoercionContextLocale) { |
|
54 |
theContext = (CoercionContextLocale) context; |
|
55 |
} else { |
|
56 |
theContext = DataTypeUtils.coerceContextDefaultLocale(); |
|
57 |
} |
|
58 |
Double num; |
|
59 |
try { |
|
60 |
if (value instanceof Number) { |
|
61 |
num = ((Number) value).doubleValue(); |
|
62 |
|
|
63 |
} else if (value instanceof Boolean) { |
|
64 |
num = ((boolean) value ? 1d : 0d); |
|
65 |
|
|
66 |
} else if (value instanceof Date) { |
|
67 |
num = (double) (((Date) value).getTime()); |
|
68 |
|
|
69 |
} else { |
|
70 |
String s = value.toString(); |
|
71 |
if (s == null) { |
|
72 |
return null; |
|
74 | 73 |
} |
75 |
try { |
|
76 |
if (!(value instanceof Double)) { |
|
77 |
if (value instanceof Number) { |
|
78 |
value = ((Number) value).doubleValue(); |
|
79 |
} else if( value instanceof Boolean ) { |
|
80 |
return (double)((boolean)value ? 1:0); |
|
81 |
} else if (value instanceof Date) { |
|
82 |
value = (double)(((Date)value).getTime()); |
|
83 |
} else { |
|
84 |
String s = value.toString().trim().toLowerCase(); |
|
85 |
if( StringUtils.isBlank(s) ) { |
|
86 |
return null; |
|
87 |
} |
|
88 |
if (s.startsWith("0x")) { |
|
89 |
value = new Double(Long.parseLong(s.substring(2), 16)); |
|
90 |
} else { |
|
91 |
if( locale == null ) { |
|
92 |
locale = Locale.getDefault(); |
|
93 |
} |
|
94 |
if( s.startsWith("+") ) { |
|
95 |
s = s.substring(1); |
|
96 |
} |
|
97 |
ParsePosition p = new ParsePosition(0); |
|
98 |
NumberFormat nf = NumberFormat.getInstance(locale); |
|
99 |
Number num = nf.parse(s, p); |
|
100 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
|
101 |
throw new CoercionException("Can't coerce '"+s+"' to double with locale " + (locale==null?"null":locale.getLanguage()) + "(error index "+p.getErrorIndex()+", index "+p.getIndex()+")."); |
|
102 |
} |
|
103 |
value = new Double(num.doubleValue()); |
|
104 |
} |
|
105 |
} |
|
106 |
} |
|
107 |
return value; |
|
108 |
} catch (CoercionException e) { |
|
109 |
throw e; |
|
110 |
} catch (Exception e) { |
|
111 |
throw new CoercionException("Can't coerce '"+(value==null?"null":value.toString())+"' to double with locale " + (locale==null?"null":locale.getLanguage())+"'.",e); |
|
74 |
s = s.trim().toLowerCase(); |
|
75 |
if (s.length() == 0) { |
|
76 |
return null; |
|
112 | 77 |
} |
78 |
if (s.startsWith("0x")) { |
|
79 |
num = (double) Long.parseLong(s.substring(2), 16); |
|
80 |
|
|
81 |
} else { |
|
82 |
if (s.startsWith("+")) { |
|
83 |
s = s.substring(1); |
|
84 |
} |
|
85 |
ParsePosition p = new ParsePosition(0); |
|
86 |
DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale()); |
|
87 |
Number n = nf.parse(s, p); |
|
88 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
|
89 |
throw new CoercionException("Can't coerce '" + s + "' to double with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ")."); |
|
90 |
} |
|
91 |
num = n.doubleValue(); |
|
92 |
} |
|
93 |
} |
|
94 |
return num; |
|
95 |
} catch (CoercionException e) { |
|
96 |
throw e; |
|
97 |
} catch (Exception e) { |
|
98 |
throw new CoercionException("Can't coerce '" + value.toString() + "' to double with locale " + theContext.locale().getLanguage() + "'.", e); |
|
113 | 99 |
} |
100 |
} |
|
114 | 101 |
|
115 | 102 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDate.java | ||
---|---|---|
27 | 27 |
import java.text.SimpleDateFormat; |
28 | 28 |
import java.util.Date; |
29 | 29 |
import java.util.Locale; |
30 |
import org.gvsig.tools.dataTypes.CoercionException; |
|
31 |
import org.gvsig.tools.dataTypes.CoercionContext; |
|
30 | 32 |
|
31 | 33 |
/** |
32 | 34 |
* Coerces a value to a {@link Date}. If the value is not a {@link Date}, it |
... | ... | |
41 | 43 |
@SuppressWarnings("UseSpecificCatch") |
42 | 44 |
public class CoerceToDate extends AbstractCoerceToDate { |
43 | 45 |
|
44 |
@Override
|
|
45 |
protected DateFormat[] getFormatters(Locale locale) {
|
|
46 |
return new DateFormat[]{
|
|
47 |
DateFormat.getDateInstance(DateFormat.SHORT, locale),
|
|
48 |
DateFormat.getDateInstance(DateFormat.MEDIUM, locale),
|
|
49 |
DateFormat.getDateInstance(DateFormat.LONG, locale),
|
|
50 |
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale),
|
|
51 |
new SimpleDateFormat("yyyy-MM-dd"), // PostgreSQL format
|
|
52 |
new SimpleDateFormat("yyyyMMddHHmmss"), // DBF timestamp format
|
|
53 |
new SimpleDateFormat("yyyyMMdd"), // DBF date format
|
|
54 |
new SimpleDateFormat("HHmmss") // DBF time format
|
|
55 |
};
|
|
56 |
}
|
|
46 |
@Override |
|
47 |
protected DateFormat[] getFormatters(Locale locale) { |
|
48 |
return new DateFormat[]{ |
|
49 |
DateFormat.getDateInstance(DateFormat.SHORT, locale), |
|
50 |
DateFormat.getDateInstance(DateFormat.MEDIUM, locale), |
|
51 |
DateFormat.getDateInstance(DateFormat.LONG, locale), |
|
52 |
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale), |
|
53 |
new SimpleDateFormat("yyyy-MM-dd"), // PostgreSQL format |
|
54 |
new SimpleDateFormat("yyyyMMddHHmmss"), // DBF timestamp format |
|
55 |
new SimpleDateFormat("yyyyMMdd"), // DBF date format |
|
56 |
new SimpleDateFormat("HHmmss") // DBF time format |
|
57 |
}; |
|
58 |
} |
|
57 | 59 |
|
58 |
protected Date now() { |
|
59 |
Date d = new Date(); |
|
60 |
d.setHours(0); |
|
61 |
d.setMinutes(0); |
|
62 |
d.setSeconds(0); |
|
63 |
return d; |
|
60 |
@Override |
|
61 |
protected Date now() { |
|
62 |
Date d = new Date(); |
|
63 |
d.setHours(0); |
|
64 |
d.setMinutes(0); |
|
65 |
d.setSeconds(0); |
|
66 |
return d; |
|
67 |
} |
|
68 |
|
|
69 |
@Override |
|
70 |
protected String getDateType() { |
|
71 |
return "Date"; |
|
72 |
} |
|
73 |
|
|
74 |
@Override |
|
75 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
76 |
if( value == null ) { |
|
77 |
return null; |
|
64 | 78 |
} |
65 |
|
|
66 |
@Override
|
|
67 |
protected String getDateType() {
|
|
68 |
return "Date";
|
|
79 |
if (value instanceof Date) { |
|
80 |
Date d = (Date) value;
|
|
81 |
Date n = new Date(d.getYear(), d.getMonth(), d.getDate(), 0, 0, 0);
|
|
82 |
return n;
|
|
69 | 83 |
} |
84 |
return super.coerce(value, context); |
|
85 |
} |
|
70 | 86 |
|
71 | 87 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToFile.java | ||
---|---|---|
25 | 25 |
|
26 | 26 |
import java.io.File; |
27 | 27 |
import java.net.URI; |
28 |
import java.net.URL; |
|
29 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
|
28 | 30 |
|
29 | 31 |
import org.gvsig.tools.dataTypes.CoercionException; |
30 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
|
|
32 |
import org.gvsig.tools.dataTypes.CoercionContext;
|
|
31 | 33 |
|
34 |
public class CoerceToFile extends AbstractCoercion { |
|
32 | 35 |
|
33 |
public class CoerceToFile implements Coercion { |
|
36 |
@Override |
|
37 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
38 |
if (value == null || value instanceof File) { |
|
39 |
return value; |
|
40 |
} |
|
41 |
if (value instanceof CharSequence ) { |
|
42 |
String s = value.toString(); |
|
43 |
if (s == null || s.trim().isEmpty()) { |
|
44 |
return null; |
|
45 |
} |
|
46 |
return new File((String)s); |
|
47 |
} |
|
48 |
if (value instanceof URL) { |
|
49 |
return new File(((URL) value).getPath()); |
|
50 |
} |
|
51 |
if (value instanceof URI) { |
|
52 |
return new File((URI) value); |
|
53 |
} |
|
54 |
String s = value.toString(); |
|
55 |
if (s == null || s.trim().isEmpty()) { |
|
56 |
return null; |
|
57 |
} |
|
58 |
throw new CoercionException(); |
|
59 |
} |
|
34 | 60 |
|
35 |
public Object coerce(Object value) throws CoercionException { |
|
36 |
if( value==null ) { |
|
37 |
return null; |
|
38 |
} |
|
39 |
if( ! (value instanceof File) ) { |
|
40 |
if( value instanceof String ) { |
|
41 |
if(((String)value).isEmpty() ) { |
|
42 |
return null; |
|
43 |
} |
|
44 |
value = new File((String) value); |
|
45 |
} else if( value instanceof URI ) { |
|
46 |
value = new File((URI) value); |
|
47 |
} else { |
|
48 |
String s = value.toString(); |
|
49 |
if( s == null ) { |
|
50 |
return null; |
|
51 |
} |
|
52 |
s = s.trim().toLowerCase(); |
|
53 |
if( s.isEmpty() ) { |
|
54 |
return null; |
|
55 |
} |
|
56 |
throw new CoercionException(); |
|
57 |
} |
|
58 |
} |
|
59 |
return value; |
|
60 |
} |
|
61 |
|
|
62 | 61 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToBoolean.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.tools.dataTypes.impl.coercion; |
25 | 25 |
|
26 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
|
26 | 27 |
import org.gvsig.tools.dataTypes.CoercionException; |
27 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
|
|
28 |
import org.gvsig.tools.dataTypes.CoercionContext;
|
|
28 | 29 |
|
29 |
public class CoerceToBoolean implements Coercion {
|
|
30 |
public class CoerceToBoolean extends AbstractCoercion {
|
|
30 | 31 |
|
31 |
public Object coerce(Object value) throws CoercionException { |
|
32 |
if( value == null ) { |
|
33 |
return null; |
|
34 |
} |
|
32 |
@Override |
|
33 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
34 |
if (value == null || value instanceof Boolean) { |
|
35 |
return value; |
|
36 |
} |
|
35 | 37 |
|
36 |
try { |
|
37 |
if (!(value instanceof Boolean)) { |
|
38 |
if (value instanceof Number) { |
|
39 |
if (((Number) value).intValue() == 1) { |
|
40 |
value = Boolean.TRUE; |
|
41 |
} else { |
|
42 |
value = Boolean.FALSE; |
|
43 |
} |
|
44 |
} else { |
|
45 |
String s = value.toString(); |
|
46 |
if( s == null ) { |
|
47 |
return null; |
|
48 |
} |
|
49 |
s = s.trim().toLowerCase(); |
|
50 |
if( s.length()==0 ) { |
|
51 |
return null; |
|
52 |
} |
|
53 |
value = Boolean.valueOf(s); |
|
54 |
} |
|
55 |
} |
|
56 |
return value; |
|
57 |
} catch (Exception e) { |
|
58 |
throw new CoercionException(e); |
|
59 |
} |
|
38 |
try { |
|
39 |
if (value instanceof Number) { |
|
40 |
if (((Number) value).intValue() == 1) { |
|
41 |
value = Boolean.TRUE; |
|
42 |
} else { |
|
43 |
value = Boolean.FALSE; |
|
44 |
} |
|
45 |
} else { |
|
46 |
String s = value.toString(); |
|
47 |
if (s == null) { |
|
48 |
return null; |
|
49 |
} |
|
50 |
s = s.trim().toLowerCase(); |
|
51 |
if (s.length() == 0) { |
|
52 |
return null; |
|
53 |
} |
|
54 |
value = Boolean.valueOf(s); |
|
55 |
} |
|
56 |
return value; |
|
57 |
} catch (Exception e) { |
|
58 |
throw new CoercionException(e); |
|
59 |
} |
|
60 | 60 |
|
61 |
}
|
|
61 |
}
|
|
62 | 62 |
|
63 | 63 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToVersion.java | ||
---|---|---|
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.tools.dataTypes.impl.coercion; |
25 | 25 |
|
26 |
import java.net.URL; |
|
26 | 27 |
import org.gvsig.installer.lib.api.Version; |
27 | 28 |
import org.gvsig.tools.ToolsLocator; |
29 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
|
30 |
import org.gvsig.tools.dataTypes.CoercionContext; |
|
28 | 31 |
import org.gvsig.tools.dataTypes.CoercionException; |
29 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
|
|
32 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
|
30 | 33 |
import org.gvsig.tools.packageutils.PackageManager; |
31 | 34 |
|
32 |
public class CoerceToVersion implements Coercion { |
|
35 |
@SuppressWarnings("UseSpecificCatch") |
|
36 |
public class CoerceToVersion extends AbstractCoercion { |
|
33 | 37 |
|
34 |
public Object coerce(Object value) throws CoercionException { |
|
35 |
if( value == null ) { |
|
36 |
return null; |
|
37 |
} |
|
38 |
if (!(value instanceof Version)) { |
|
39 |
try { |
|
40 |
PackageManager manager = ToolsLocator.getPackageManager(); |
|
41 |
Version version = manager.createVersion(); |
|
42 |
String s = value.toString(); |
|
43 |
if( s == null ) { |
|
44 |
return null; |
|
45 |
} |
|
46 |
s = s.trim().toLowerCase(); |
|
47 |
if( s.length()==0 ) { |
|
48 |
return null; |
|
49 |
} |
|
50 |
version.parse(s); |
|
51 |
value = version; |
|
52 |
} catch(Exception ex) { |
|
53 |
throw new CoercionException(ex.getMessage(),ex); |
|
54 |
} |
|
55 |
} |
|
56 |
return value; |
|
38 |
@Override |
|
39 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
40 |
if (value == null || value instanceof Version) { |
|
41 |
return value; |
|
57 | 42 |
} |
43 |
try { |
|
44 |
PackageManager manager = ToolsLocator.getPackageManager(); |
|
45 |
Version version = manager.createVersion(); |
|
46 |
String s = value.toString(); |
|
47 |
if (s == null) { |
|
48 |
return null; |
|
49 |
} |
|
50 |
s = s.trim().toLowerCase(); |
|
51 |
if (s.length() == 0) { |
|
52 |
return null; |
|
53 |
} |
|
54 |
version.parse(s); |
|
55 |
value = version; |
|
56 |
} catch (Exception ex) { |
|
57 |
throw new CoercionException(ex.getMessage(), ex); |
|
58 |
} |
|
59 |
return value; |
|
60 |
} |
|
58 | 61 |
|
59 | 62 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToInt.java | ||
---|---|---|
22 | 22 |
*/ |
23 | 23 |
package org.gvsig.tools.dataTypes.impl.coercion; |
24 | 24 |
|
25 |
import java.text.NumberFormat; |
|
26 |
import java.text.ParsePosition; |
|
27 | 25 |
import java.util.Date; |
28 |
import java.util.Locale; |
|
29 |
import org.apache.commons.lang3.StringUtils; |
|
26 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
|
30 | 27 |
import org.gvsig.tools.dataTypes.CoercionException; |
31 |
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
|
|
28 |
import org.gvsig.tools.dataTypes.CoercionContext;
|
|
32 | 29 |
|
33 |
public class CoerceToInt implements CoercionWithLocale { |
|
30 |
@SuppressWarnings("UseSpecificCatch") |
|
31 |
public class CoerceToInt extends AbstractCoercion { |
|
34 | 32 |
|
35 |
@Override |
|
36 |
public Object coerce(Object value) throws CoercionException { |
|
37 |
if (value == null) { |
|
38 |
return null; |
|
39 |
} |
|
40 |
try { |
|
41 |
if (!(value instanceof Integer)) { |
|
42 |
if (value instanceof Number) { |
|
43 |
value = ((Number) value).intValue(); |
|
44 |
} else if( value instanceof Boolean ) { |
|
45 |
return (boolean)value ? 1:0; |
|
46 |
} else if (value instanceof Date) { |
|
47 |
value = (int)(((Date)value).getTime()); |
|
48 |
} else { |
|
49 |
String s = value.toString(); |
|
50 |
if (s == null) { |
|
51 |
return null; |
|
52 |
} |
|
53 |
s = s.trim().toLowerCase(); |
|
54 |
if (s.length() == 0) { |
|
55 |
return null; |
|
56 |
} |
|
57 |
if (s.startsWith("0x")) { |
|
58 |
value = Integer.valueOf(s.substring(2), 16); |
|
59 |
} else { |
|
60 |
try { |
|
61 |
value = Integer.valueOf(s); |
|
62 |
} catch (NumberFormatException ex) { |
|
63 |
value = Float.valueOf(s); |
|
64 |
value = ((Number) value).intValue(); |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
} |
|
69 |
return value; |
|
70 |
} catch (Exception e) { |
|
71 |
throw new CoercionException(e); |
|
72 |
} |
|
33 |
@Override |
|
34 |
public Object coerce(Object value) throws CoercionException { |
|
35 |
if (value == null || value instanceof Integer) { |
|
36 |
return value; |
|
73 | 37 |
} |
38 |
return coerce(value, null); |
|
39 |
} |
|
74 | 40 |
|
75 |
@Override |
|
76 |
public Object coerce(Object value, Locale locale) throws CoercionException { |
|
77 |
if (value == null) { |
|
78 |
return null; |
|
41 |
@Override |
|
42 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
43 |
if (value == null || value instanceof Integer) { |
|
44 |
return value; |
|
45 |
} |
|
46 |
Integer num; |
|
47 |
try { |
|
48 |
if (value instanceof Number) { |
|
49 |
num = ((Number) value).intValue(); |
|
50 |
|
|
51 |
} else if (value instanceof Boolean) { |
|
52 |
num = ((boolean) value ? 1 : 0); |
|
53 |
|
|
54 |
} else if (value instanceof Date) { |
|
55 |
num = (int) ((Date) value).getTime(); |
|
56 |
|
|
57 |
} else { |
|
58 |
String s = value.toString(); |
|
59 |
if (s == null) { |
|
60 |
return null; |
|
79 | 61 |
} |
80 |
try { |
|
81 |
if (!(value instanceof Integer)) { |
|
82 |
if (value instanceof Number) { |
|
83 |
value = ((Number) value).intValue(); |
|
84 |
} else if( value instanceof Boolean ) { |
|
85 |
return (boolean)value ? 1:0; |
|
86 |
} else if (value instanceof Date) { |
|
87 |
value = (int)(((Date)value).getTime()); |
|
88 |
} else { |
|
89 |
String s = value.toString().trim().toLowerCase(); |
|
90 |
if( StringUtils.isBlank(s) ) { |
|
91 |
return null; |
|
92 |
} |
|
93 |
s = s.trim().toLowerCase(); |
|
94 |
if (s.length() == 0) { |
|
95 |
return null; |
|
96 |
} |
|
97 |
if (s.startsWith("0x")) { |
|
98 |
value = Integer.valueOf(s.substring(2), 16); |
|
99 |
} else { |
|
100 |
try { |
|
101 |
value = Integer.valueOf(s); |
|
102 |
} catch (NumberFormatException ex) { |
|
103 |
ParsePosition p = new ParsePosition(0); |
|
104 |
NumberFormat nf = NumberFormat.getInstance(locale); |
|
105 |
Number num = nf.parse(s, p); |
|
106 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
|
107 |
throw new CoercionException("Can't coerce '"+s+"' to integer with locale " + (locale==null?"null":locale.getLanguage()) + "(error index "+p.getErrorIndex()+", index "+p.getIndex()+")."); |
|
108 |
} |
|
109 |
value = ((Number) num).intValue(); |
|
110 |
} |
|
111 |
} |
|
112 |
} |
|
113 |
} |
|
114 |
return value; |
|
115 |
} catch (Exception e) { |
|
116 |
throw new CoercionException("Can't coerce '"+(value==null?"null":value.toString())+"' to integer with locale " + (locale==null?"null":locale.getLanguage())+"'.",e); |
|
62 |
s = s.trim().toLowerCase(); |
|
63 |
if (s.length() == 0) { |
|
64 |
return null; |
|
117 | 65 |
} |
66 |
if (s.startsWith("0x")) { |
|
67 |
num = Integer.valueOf(s.substring(2), 16); |
|
68 |
|
|
69 |
} else { |
|
70 |
if (s.startsWith("+")) { |
|
71 |
s = s.substring(1); |
|
72 |
} |
|
73 |
num = Integer.valueOf(s); |
|
74 |
} |
|
75 |
} |
|
76 |
return num; |
|
77 |
} catch (Exception e) { |
|
78 |
throw new CoercionException("Can't coerce '" + value.toString() + "' to integer.", e); |
|
118 | 79 |
} |
80 |
} |
|
119 | 81 |
|
120 | 82 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDateTime.java | ||
---|---|---|
27 | 27 |
import java.text.SimpleDateFormat; |
28 | 28 |
import java.util.Date; |
29 | 29 |
import java.util.Locale; |
30 |
import org.gvsig.tools.dataTypes.CoercionException; |
|
31 |
import org.gvsig.tools.dataTypes.CoercionContext; |
|
30 | 32 |
|
31 | 33 |
/** |
32 | 34 |
* Coerces a value to a {@link Date}. If the value is not a {@link Date}, it |
... | ... | |
52 | 54 |
}; |
53 | 55 |
} |
54 | 56 |
|
57 |
@Override |
|
55 | 58 |
protected Date now() { |
56 | 59 |
Date d = new Date(); |
57 | 60 |
return d; |
... | ... | |
62 | 65 |
return "DateTime or Timestamp"; |
63 | 66 |
} |
64 | 67 |
|
68 |
@Override |
|
69 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
70 |
if( value == null || value instanceof Date ) { |
|
71 |
return value; |
|
72 |
} |
|
73 |
return super.coerce(value, context); |
|
74 |
} |
|
75 |
|
|
65 | 76 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDecimal.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 modify it under |
|
7 |
* the terms of the GNU General Public License as published by the Free Software |
|
8 |
* Foundation; either version 2 of the License, or (at your option) any later |
|
9 |
* version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
14 |
* details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License along with |
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51 |
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
19 |
* |
|
20 |
* For any additional information, do not hesitate to contact us at info AT |
|
21 |
* gvsig.com, or visit our website www.gvsig.com. |
|
22 |
*/ |
|
23 |
package org.gvsig.tools.dataTypes.impl.coercion; |
|
24 |
|
|
25 |
import java.math.BigDecimal; |
|
26 |
import java.text.DecimalFormat; |
|
27 |
import java.text.NumberFormat; |
|
28 |
import java.text.ParsePosition; |
|
29 |
import java.util.Date; |
|
30 |
import org.apache.commons.lang3.StringUtils; |
|
31 |
import org.gvsig.tools.dataTypes.AbstractCoercion; |
|
32 |
|
|
33 |
import org.gvsig.tools.dataTypes.CoercionException; |
|
34 |
import org.gvsig.tools.dataTypes.DataTypeUtils; |
|
35 |
import org.gvsig.tools.dataTypes.CoercionContext; |
|
36 |
import org.gvsig.tools.dataTypes.CoercionContextLocale; |
|
37 |
import org.gvsig.tools.dataTypes.CoercionContextDecimal; |
|
38 |
|
|
39 |
@SuppressWarnings("UseSpecificCatch") |
|
40 |
public class CoerceToDecimal extends AbstractCoercion { |
|
41 |
|
|
42 |
@Override |
|
43 |
public Object coerce(Object value) throws CoercionException { |
|
44 |
if (value == null || value instanceof BigDecimal) { |
|
45 |
return value; |
|
46 |
} |
|
47 |
return coerce(value, DataTypeUtils.coerceContextDefaultDecimal()); |
|
48 |
} |
|
49 |
|
|
50 |
@Override |
|
51 |
public Object coerce(Object value, CoercionContext context) throws CoercionException { |
|
52 |
if (value == null || value instanceof BigDecimal) { |
|
53 |
return value; |
|
54 |
} |
|
55 |
CoercionContextDecimal theContext; |
|
56 |
if (context instanceof CoercionContextDecimal) { |
|
57 |
theContext = (CoercionContextDecimal) context; |
|
58 |
} else { |
|
59 |
if (context instanceof CoercionContextLocale) { |
|
60 |
theContext = DataTypeUtils.coerceContextDecimal(((CoercionContextLocale) context).locale()); |
|
61 |
} else { |
|
62 |
theContext = DataTypeUtils.coerceContextDefaultDecimal(); |
|
63 |
} |
|
64 |
} |
|
65 |
BigDecimal num; |
|
66 |
try { |
|
67 |
if (value instanceof Number) { |
|
68 |
num = new BigDecimal(((Number) value).doubleValue(), theContext.getMathContext()); |
|
69 |
|
|
70 |
} else if (value instanceof Boolean) { |
|
71 |
num = new BigDecimal((boolean) value ? 1 : 0, theContext.getMathContext()); |
|
72 |
|
|
73 |
} else if (value instanceof Date) { |
|
74 |
num = new BigDecimal(((Date) value).getTime(), theContext.getMathContext()); |
|
75 |
|
|
76 |
} else { |
|
77 |
String s = value.toString().trim().toLowerCase(); |
|
78 |
if (StringUtils.isBlank(s)) { |
|
79 |
return null; |
|
80 |
} |
|
81 |
if (s.startsWith("0x")) { |
|
82 |
num = new BigDecimal(Long.parseLong(s.substring(2), 16), theContext.getMathContext()); |
|
83 |
|
|
84 |
} else { |
|
85 |
if (s.startsWith("+")) { |
|
86 |
s = s.substring(1); |
|
87 |
} |
|
88 |
ParsePosition p = new ParsePosition(0); |
|
89 |
DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(theContext.locale()); |
|
90 |
nf.setParseBigDecimal(true); |
|
91 |
num = (BigDecimal) nf.parse(s, p); |
|
92 |
if (p.getErrorIndex() > 0 || p.getIndex() < s.length()) { |
|
93 |
throw new CoercionException("Can't coerce '" + s + "' to BigDecimal with locale " + theContext.locale().getLanguage() + "(error index " + p.getErrorIndex() + ", index " + p.getIndex() + ")."); |
|
94 |
} |
|
95 |
} |
|
96 |
} |
|
97 |
num.setScale(theContext.scale(), theContext.roundMode()); |
|
98 |
return num; |
|
99 |
} catch (CoercionException e) { |
|
100 |
throw e; |
|
101 |
} catch (Exception e) { |
|
102 |
throw new CoercionException("Can't coerce '" + value.toString() + "' to BigDecimal with locale " + theContext.locale().getLanguage() + "'.", e); |
|
103 |
} |
|
104 |
} |
|
105 |
|
|
106 |
} |
Also available in: Unified diff