Revision 112

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/ToolsLocator.java
1 1
package org.gvsig.tools;
2 2

  
3
import org.gvsig.tools.dataTypes.DataTypesManager;
3 4
import org.gvsig.tools.dispose.DisposableManager;
4 5
import org.gvsig.tools.dynobject.DynObjectManager;
5 6
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
......
48 49
			"org.gvsig.tools.dispose.manager";
49 50

  
50 51
	private static final String DISPOSABLE_MANAGER_DESCRIPTION =
51
			"Disposable Manager";
52
		"Disposable Manager";
52 53

  
54
	public static final String DATATYPES_MANAGER_NAME =
55
		"org.gvsig.tools.datatypes.manager";
56

  
57
	private static final String DATATYPES_MANAGER_DESCRIPTION =
58
		"Datatypes Manager";
59

  
53 60
	public static final String SERVICE_LOADER_NAME =
54 61
			"org.gvsig.tools.service.loader";
55 62

  
......
281 288
				clazz);
282 289
	}
283 290
	
291
	/**
292
	 * Registers the Class implementing the DataTypesManager interface.
293
	 * 
294
	 * @param clazz
295
	 *            implementing the DisposableManager interface
296
	 */
297
	public static void registerDataTypesManager(Class clazz) {
298
		getInstance().register(DATATYPES_MANAGER_NAME,
299
				DATATYPES_MANAGER_DESCRIPTION, clazz);
300
	}
301

  
302

  
303
	/**
304
	 * Registers the default Class implementing the DataTypesManager interface.
305
	 * 
306
	 * @param clazz
307
	 *            implementing the DisposableManager interface
308
	 */
309
	public static void registerDefaultDataTypesManager(Class clazz) {
310
		getInstance().registerDefault(DATATYPES_MANAGER_NAME,
311
				DATATYPES_MANAGER_DESCRIPTION, clazz);
312
	}
313

  
314

  
315
	/**
316
	 * Return a reference to DataTypesManager.
317
	 * 
318
	 * @return a reference to DataTypesManager
319
	 * @throws LocatorException
320
	 *             if there is no access to the class or the class cannot be
321
	 *             instantiated
322
	 * @see Locator#get(String)
323
	 */
324
	public static DataTypesManager getDataTypesManager()
325
			throws LocatorException {
326
		return (DataTypesManager) getInstance().get(DATATYPES_MANAGER_NAME);
327
	}
328

  
329

  
330
	
284 331
	private static Class xmlPullParser = null; 
285 332
	
286 333
	public static XmlPullParser getXmlPullParser() throws XmlPullParserException {
......
306 353
		}
307 354
		xmlPullParser = parser;
308 355
	}
356

  
309 357
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/ToolsLibrary.java
26 26
 */
27 27
package org.gvsig.tools;
28 28

  
29
import org.gvsig.tools.dataTypes.impl.DefaultDataTypesManager;
29 30
import org.gvsig.tools.dispose.impl.DefaultDisposableManager;
30 31
import org.gvsig.tools.dynobject.impl.DefaultDynObjectManager;
31 32
import org.gvsig.tools.library.AbstractLibrary;
......
36 37
public class ToolsLibrary extends AbstractLibrary {
37 38

  
38 39
	protected void doInitialize() throws LibraryException {
39
		ToolsLocator
40
				.registerDefaultOperationManager(DefaultOperationManager.class);
40
		ToolsLocator.registerDefaultOperationManager(DefaultOperationManager.class);
41 41
		ToolsLocator.registerDefaultTaskManager(DefaultTaskManager.class);
42 42
		ToolsLocator.registerDynObjectManager(DefaultDynObjectManager.class);
43 43
		ToolsLocator.registerDisposableManager(DefaultDisposableManager.class);
44
		ToolsLocator.registerDefaultDataTypesManager(DefaultDataTypesManager.class);
44 45
	}
45 46

  
46 47
	protected void doPostInitialize() throws LibraryException {
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dynobject/DynObjectManager.java
199 199
	void validate(DynObject object) throws DynObjectValidateException;
200 200

  
201 201
	/**
202
	 * Utility method to obtaint the default class associated to a type.
203
	 * 
204
	 * @param type
205
	 * @return the Class associated to the pased type.
202
	 * @deprecated use DataTypesManager.getDefaultClass
206 203
	 */
207 204
	Class getDefaultClassOfType(int type);
208 205
	
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dynobject/DynField.java
2 2

  
3 3
import java.util.List;
4 4

  
5
import org.gvsig.tools.dataTypes.CoercionException;
5 6
import org.gvsig.tools.dynobject.exception.DynFieldIsNotAContainerException;
6 7
import org.gvsig.tools.dynobject.exception.DynFieldValidateException;
7 8

  
......
130 131
	 * 
131 132
	 * @param value
132 133
	 * @return new value
134
	 * @throws CoercionException 
133 135
	 */
134
	Object coerce(Object value);
136
	Object coerce(Object value) throws CoercionException;
135 137
	
136 138
	/**
137 139
	 * When a field is of type container, this method return a DynClass
138
	 * that define the type of elements.
140
	 * that define the type of elements. 
141
 	 * When the type is DYNOBJECT return the DynClass associated
142
 	 * to this.
139 143
	 * 
140
	 * Return null if the type not is a container. A container is, ARRAY,
141
	 * LIST, MAP or SET.
144
	 * Return null if the type not is a container.
142 145
	 * 
143 146
	 * @return DynClass that define the type of elements.
144 147
	 */
......
146 149

  
147 150
	/**
148 151
 	 * Set the type of items when the field is a container.
152
 	 * When the type is DYNOBJECT is the DynClass associated
153
 	 * to this.
149 154
 	 * 
150 155
	 * @param definition
151 156
	 * @return this same {@link DynField} object
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dynobject/exception/DynFieldValidateException.java
1 1
package org.gvsig.tools.dynobject.exception;
2 2

  
3
import org.gvsig.tools.dataTypes.DataTypes;
3
import org.gvsig.tools.ToolsLocator;
4 4
import org.gvsig.tools.dynobject.DynField;
5 5

  
6 6
public class DynFieldValidateException extends DynMethodException {
......
25 25
			setValue("value", "(unknow)");
26 26
		}
27 27
		setValue("field", field.getName());
28
		switch( field.getTheTypeOfAvailableValues()) {
29
		case DynField.CHOICE:
28
		if( field.getAvailableValues() != null) {
30 29
			StringBuffer buffer = new StringBuffer();
31
			buffer.append(DataTypes.getTypeName(field.getType()));
30
			buffer.append(ToolsLocator.getDataTypesManager().getTypeName(field.getType()));
32 31
			buffer.append(" ( ");
33 32
			for( int i=0; i< field.getAvailableValues().length; i++ ) {
34 33
				buffer.append(  field.getAvailableValues()[i].getValue().toString());
......
36 35
			}
37 36
			buffer.append(")");
38 37
			setValue("fieldtype", buffer.toString());
39
			
40
			break;
41
		case DynField.RANGE:
38
		} else if( field.getMaxValue()!=null && field.getMinValue()!=null ) {
42 39
			setValue("fieldtype", 
43
					DataTypes.getTypeName(field.getType()) + 
40
					ToolsLocator.getDataTypesManager().getTypeName(field.getType()) + 
44 41
					"  (  "+ field.getMinValue().toString()+" <= x <=  "+field.getMaxValue().toString()+")");
45
			break;
46

  
47
		default:
48
			setValue("fieldtype", DataTypes.getTypeName(field.getType()));
42
			
43
		} else {
44
			setValue("fieldtype", ToolsLocator.getDataTypesManager().getTypeName(field.getType()));			
49 45
		}
50 46
		if( value == null ) {
51 47
			setValue("valuetype", "null");
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dynobject/impl/DefaultDynObjectManager.java
1 1
package org.gvsig.tools.dynobject.impl;
2 2

  
3
import java.io.File;
4 3
import java.io.IOException;
5 4
import java.io.InputStream;
6
import java.sql.Date;
7
import java.util.ArrayList;
8 5
import java.util.Arrays;
9 6
import java.util.Collections;
10 7
import java.util.HashMap;
......
14 11
import java.util.Map;
15 12
import java.util.Set;
16 13

  
17
import org.gvsig.tools.dataTypes.DataTypes;
14
import org.gvsig.tools.ToolsLocator;
18 15
import org.gvsig.tools.dynobject.DynClass;
19 16
import org.gvsig.tools.dynobject.DynMethod;
20 17
import org.gvsig.tools.dynobject.DynObject;
......
506 503
	}
507 504
	
508 505
	public Class getDefaultClassOfType(int type) {
509
		switch (type) {
510
		case DataTypes.BOOLEAN:
511
			return Boolean.class;
512
			
513
		case DataTypes.BYTE:
514
			return Byte.class;
515

  
516
		case DataTypes.DATE:
517
		case DataTypes.TIME:
518
		case DataTypes.TIMESTAMP:
519
			return Date.class;
520
			
521
		case DataTypes.DOUBLE:
522
			return Double.class;
523
			
524
		case DataTypes.FILE:
525
			return File.class;
526
			
527
		case DataTypes.FLOAT:
528
			return Float.class;
529
			
530
		case DataTypes.INT:
531
			return Integer.class;
532
			
533
		case DataTypes.LONG:
534
			return Long.class;
535
			
536
		case DataTypes.CHAR:
537
		case DataTypes.STRING:
538
			return String.class;
539

  
540
		case DataTypes.MAP:
541
			return HashMap.class;
542
			
543
		case DataTypes.SET:
544
			return HashSet.class;
545
			
546
		case DataTypes.LIST:
547
			return ArrayList.class;
548
			
549
		}
550
		return null;
506
		return ToolsLocator.getDataTypesManager().getDefaultClass(type);
551 507
	}
552 508

  
553 509
	public Map importDynClassDefinitions(InputStream resource,
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dynobject/impl/DefaultDynField.java
1 1
package org.gvsig.tools.dynobject.impl;
2 2

  
3
import java.io.File;
4
import java.net.URI;
5
import java.text.ParsePosition;
6
import java.text.SimpleDateFormat;
7 3
import java.util.Date;
8 4
import java.util.Iterator;
9 5
import java.util.List;
......
11 7
import java.util.Set;
12 8

  
13 9
import org.gvsig.tools.ToolsLocator;
10
import org.gvsig.tools.dataTypes.CoercionException;
14 11
import org.gvsig.tools.dataTypes.DataTypes;
12
import org.gvsig.tools.dataTypes.DataTypesManager;
15 13
import org.gvsig.tools.dynobject.DynClass;
16 14
import org.gvsig.tools.dynobject.DynField;
17 15
import org.gvsig.tools.dynobject.DynMethod;
......
33 31

  
34 32
	private Object defaultValue;
35 33

  
36
	/** @deprecated **/
37
	private int typeOfAvailableValues;
38
	
39 34
	private int order;
40 35
	private String groupName; 
41 36
	private DynObjectValueItem[] availableValues;
......
52 47
				DataTypes.UNKNOWN, // data type
53 48
				null, // default value
54 49
				true, // persistent
55
				false, // mandatory
56
				DynField.ANY // type of available values
50
				false // mandatory
57 51
		);
58 52
	}
59 53
	
60 54
	public DefaultDynField(String name, int dataType, Object defaultValue,
61
			boolean persistent, boolean mandatory, int typeOfAvailableValues) {
55
			boolean persistent, boolean mandatory) {
62 56
		this.name = name;
63 57
		this.dataType = dataType;
64 58
		this.defaultValue = defaultValue;
65 59
		this.persistent = persistent;
66 60
		this.mandatory = mandatory;
67
		this.typeOfAvailableValues = typeOfAvailableValues;
68 61
		this.theClass = null;
69 62
		this.validateElements = false;
70 63
		this.subtype = null;
......
104 97
	}
105 98

  
106 99
	public DynField setType(int dataType) {
100
		DataTypesManager manager = ToolsLocator.getDataTypesManager();
107 101
		this.dataType = dataType;
108
		this.theClass = ToolsLocator.getDynObjectManager().getDefaultClassOfType(dataType); 
102
		this.theClass = manager.getDefaultClass(dataType);
103
		this.subtype = manager.getSubtype(dataType);
109 104
		return this;
110 105
	}
111 106

  
......
131 126
		return defaultValue;
132 127
	}
133 128

  
134
	public int getTheTypeOfAvailableValues() {
135
		return typeOfAvailableValues;
136
	}
137

  
138 129
	public DynField setAvailableValues(DynObjectValueItem[] availableValues) {
139 130
		this.availableValues = availableValues;
140 131
		return this;
......
186 177
	}
187 178

  
188 179
	public DynField setTheTypeOfAvailableValues(int type) {
189
		if (type == DynField.ANY || type == DynField.RANGE
190
				|| type == DynField.CHOICE) {
191
		this.typeOfAvailableValues = type;
192
		} else {
193
			// FIXME
194
			throw new IllegalArgumentException();
195
		}
196
		return this;
180
		return this;  // FIXME: this method is @deprecated
197 181
	}
198 182

  
183
	public int getTheTypeOfAvailableValues() {
184
		return 1; // FIXME: this method is @deprecated
185
	}
199 186

  
187

  
200 188
    public boolean equals(Object obj) {
201 189
        if (this == obj) {
202 190
            return true;
......
272 260
		default:
273 261
			throw new DynFieldIsNotAContainerException(this.name);
274 262
		}
275
		DynField dynField = new DefaultDynField(this.name+"-"+DataTypes.getTypeName(this.dataType)+"-item");
263
		DynField dynField = new DefaultDynField(this.name+"-"+ToolsLocator.getDataTypesManager().getTypeName(this.dataType)+"-item");
276 264
		this.elementsType = new ElementTypeWrapper(this.name, this.description, dynField);
277 265
		return dynField;
278 266
	}
......
402 390
		if( this.mandatory && value==null ) {
403 391
			throw new DynFieldValidateException(value,this);
404 392
		}
405
		
406
		switch( this.typeOfAvailableValues) {
407
		case ANY:
408
			break;
409
			
410
		case CHOICE:
393

  
394
		if( this.getAvailableValues() != null ) {
411 395
			if( !(value instanceof Comparable) ) {
412 396
				throw new DynFieldValidateException(value,this);
413 397
			}
414 398
			v = (Comparable)value;
399
			boolean ok = false;
415 400
			for( int i=0; i<this.availableValues.length; i++ ) {
416
				if( v.compareTo(this.availableValues[i].getValue())!=0) {
417
					throw new DynFieldValidateException(value,this);
401
				if( v.compareTo(this.availableValues[i].getValue())==0) {
402
					ok = true;
403
					break;
418 404
				}
419 405
			}
420
			break;
421
			
422
		case RANGE: 
406
			if( !ok ) {
407
				throw new DynFieldValidateException(value,this);
408
			}
409
		} else if( this.getMaxValue()!=null && this.getMinValue()!=null ) {
423 410
			if( !(value instanceof Comparable) ) {
424 411
				throw new DynFieldValidateException(value,this);
425 412
			}
......
427 414
			if( v.compareTo(this.minValue)<0 || v.compareTo(this.maxValue)>0 ) {
428 415
				throw new DynFieldValidateException(value,this);
429 416
			}
430
			break;
431

  
432 417
		}
433 418
	}
434 419

  
435
	public Object coerce(Object value) {
420
	public Object coerce(Object value) throws CoercionException {
436 421
		if( value == null ) {
437 422
			return value; // O debe retorna this.defaultValue 
438 423
		} 
439
		switch( this.dataType ) {
440
		case DataTypes.FOLDER:
441
		case DataTypes.FILE:
442
			if( ! (value instanceof File) ) {
443
				if( value instanceof String ) {
444
					value = new File((String) value);
445
				} else if( value instanceof URI ) {
446
					value = new File((URI) value);
447
				} else {
448
					value = null;
449
				}
450
			}
451
			break;
452

  
453
		case DataTypes.STRING:
454
		case DataTypes.CHAR:
455
			if( !(value instanceof String )) {
456
				value = value.toString();
457
			}
458
			break;
459
			
460
		case DataTypes.INT:
461
			if( ! (value instanceof Integer) ) {
462
				if( value instanceof Number ) {
463
					value = new Integer(((Number)value).intValue());
464
				} else {
465
					value = Integer.valueOf(value.toString());
466
				}
467
			}
468
			break;
469

  
470
		case DataTypes.LONG:
471
			if( ! (value instanceof Long) ) {
472
				if( value instanceof Number ) {
473
					value = new Long(((Number)value).longValue());
474
				} else {
475
					value = Long.valueOf(value.toString());
476
				}
477
			}
478
			break;
479

  
480
		case DataTypes.DOUBLE:
481
			if( ! (value instanceof Double) ) {
482
				if( value instanceof Number ) {
483
					value = new Double(((Number)value).doubleValue());
484
				} else {
485
					value = Double.valueOf(value.toString());
486
				}
487
			}
488
			break;
489

  
490
		case DataTypes.FLOAT:
491
			if( ! (value instanceof Float) ) {
492
				if( value instanceof Number ) {
493
					value = new Float(((Number)value).floatValue());
494
				} else {
495
					value = Float.valueOf(value.toString());
496
				}
497
			}
498
			break;
499

  
500
		case DataTypes.DATE:
501
		case DataTypes.TIME:
502
		case DataTypes.TIMESTAMP:
503
			if( ! (value instanceof Date) ) {
504
				SimpleDateFormat formatter = new SimpleDateFormat();
505
				ParsePosition pp = new ParsePosition(0);
506
				Date d = formatter.parse(value.toString(), pp);
507
				value = d; // If not parse value is set to null
508
			}
509
			break;
510
			
511
		case DataTypes.BOOLEAN:
512
			if( ! (value instanceof Boolean) ) {
513
				if( value instanceof Number ) {
514
					if( ((Number)value).intValue()==1 ) {
515
						value = Boolean.TRUE;
516
					} else {
517
						value = Boolean.FALSE;
518
					}
519
				} else {
520
					value = Boolean.valueOf(value.toString());
521
				}
522
			}
523
			break;
524
			
525
		}
526
		return value;
424
		DataTypesManager manager = ToolsLocator.getDataTypesManager();
425
		return manager.coerce(this.dataType, value);
527 426
	}
528 427

  
529 428

  
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dynobject/impl/DefaultDynClass.java
447 447
		}
448 448
	}
449 449
	
450
	private DynField addDynFieldSingle(String name) {
451
		return addDynField(name).setTheTypeOfAvailableValues(DynField.ANY);
452
	}
450
//	private DynField addDynFieldSingle(String name) {
451
//		return addDynField(name).setTheTypeOfAvailableValues(DynField.ANY);
452
//	}
453 453

  
454 454
	public DynField addDynFieldString(String name) {
455
		return addDynFieldSingle(name).setType(DataTypes.STRING);
455
		return addDynField(name).setType(DataTypes.STRING);
456 456
	}
457 457

  
458 458
	public DynField addDynFieldDate(String name) {
459
		return addDynFieldSingle(name).setType(DataTypes.DATE);
459
		return addDynField(name).setType(DataTypes.DATE);
460 460
	}
461 461

  
462 462
	public DynField addDynFieldInt(String name) {
463
		return addDynFieldSingle(name).setType(DataTypes.INT);
463
		return addDynField(name).setType(DataTypes.INT);
464 464
	}
465 465

  
466 466
	public DynField addDynFieldLong(String name) {
467
		return addDynFieldSingle(name).setType(DataTypes.LONG);
467
		return addDynField(name).setType(DataTypes.LONG);
468 468
	}
469 469

  
470 470
	public DynField addDynFieldDouble(String name) {
471
		return addDynFieldSingle(name).setType(DataTypes.DOUBLE);
471
		return addDynField(name).setType(DataTypes.DOUBLE);
472 472
	}
473 473
	
474 474
	public DynField addDynFieldFloat(String name) {
475
		return addDynFieldSingle(name).setType(DataTypes.FLOAT);
475
		return addDynField(name).setType(DataTypes.FLOAT);
476 476
	}
477 477

  
478 478
	public DynField addDynFieldBoolean(String name) {
479
		return addDynFieldSingle(name).setType(DataTypes.BOOLEAN);
479
		return addDynField(name).setType(DataTypes.BOOLEAN);
480 480
	}
481 481

  
482 482
	public DynField addDynFieldObject(String name) {
483
		return addDynFieldSingle(name).setType(DataTypes.OBJECT);
483
		return addDynField(name).setType(DataTypes.OBJECT);
484 484
	}
485 485

  
486 486
	public DynField addDynFieldArray(String name) {
487
		return addDynFieldSingle(name).setType(DataTypes.ARRAY);
487
		return addDynField(name).setType(DataTypes.ARRAY);
488 488
	}
489 489

  
490 490
	public DynField addDynFieldList(String name) {
491
		return addDynFieldSingle(name).setType(DataTypes.LIST);
491
		return addDynField(name).setType(DataTypes.LIST);
492 492
	}
493 493

  
494 494
	public DynField addDynFieldMap(String name) {
495
		return addDynFieldSingle(name).setType(DataTypes.MAP);
495
		return addDynField(name).setType(DataTypes.MAP);
496 496
	}
497 497

  
498 498
	public DynField addDynFieldSet(String name) {
499
		return addDynFieldSingle(name).setType(DataTypes.SET);
499
		return addDynField(name).setType(DataTypes.SET);
500 500
	}
501 501

  
502 502
	public DynField addDynFieldChoice(String name, int type,
......
507 507
	public DynField addDynFieldChoice(String name, int type,
508 508
			Object defaultValue, DynObjectValueItem[] values,
509 509
			boolean mandatory, boolean persistent) {
510
		return addDynField(name).setTheTypeOfAvailableValues(DynField.CHOICE)
511
				.setType(type).setDefaultDynValue(defaultValue).setMandatory(
512
						mandatory).setPersistent(persistent)
513
				.setAvailableValues(values).setDescription(description);
510
		return addDynField(name)
511
				.setType(type)
512
				.setDefaultDynValue(defaultValue)
513
				.setMandatory(mandatory)
514
				.setPersistent(persistent)
515
				.setAvailableValues(values)
516
				.setDescription(description);
514 517
	}
515 518

  
516 519
	public DynField addDynFieldRange(String name, int type,
......
521 524
	public DynField addDynFieldRange(String name, int type,
522 525
			Object defaultValue, Object min, Object max, boolean mandatory,
523 526
			boolean persistent) {
524
		return addDynField(name).setTheTypeOfAvailableValues(DynField.RANGE)
525
				.setType(type).setDefaultDynValue(defaultValue).setMandatory(
526
						mandatory).setPersistent(persistent).setMinValue(min)
527
		return addDynField(name)
528
				.setType(type)
529
				.setDefaultDynValue(defaultValue)
530
				.setMandatory(mandatory)
531
				.setPersistent(persistent)
532
				.setMinValue(min)
527 533
				.setMaxValue(max);
528 534
	}
529 535

  
......
533 539

  
534 540
	public DynField addDynFieldSingle(String name, int type,
535 541
			Object defaultValue, boolean mandatory, boolean persistent) {
536
		return addDynField(name).setTheTypeOfAvailableValues(DynField.ANY)
537
				.setType(type).setDefaultDynValue(defaultValue).setMandatory(
538
						mandatory).setPersistent(persistent);
542
		return addDynField(name)
543
				.setType(type)
544
				.setDefaultDynValue(defaultValue)
545
				.setMandatory(mandatory)
546
				.setPersistent(persistent);
539 547
	}
540 548

  
541 549
	public void setDescription(String description) {
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dynobject/impl/DefaultDynObject.java
1 1
package org.gvsig.tools.dynobject.impl;
2 2

  
3
import org.gvsig.tools.dataTypes.CoercionException;
3 4
import org.gvsig.tools.dynobject.DelegatedDynObject;
4 5
import org.gvsig.tools.dynobject.DynClass;
5 6
import org.gvsig.tools.dynobject.DynField;
......
66 67
		if (x == null) {
67 68
			throw new DynFieldNotFoundException(name,this.getDynClass().getName());
68 69
		}
69
		values[x.getIndex()] = x.getDynField().coerce(value);
70
		try {
71
			values[x.getIndex()] = x.getDynField().coerce(value);
72
		} catch (CoercionException e) {
73
			// FIXME: Need throw specific exception
74
			throw new DynFieldNotFoundException(name, this.dynClass.getFullName());
75
		}
70 76
	}
71 77

  
72 78
	public boolean instanceOf(DynClass dynClass) {
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dynobject/impl/DynClassImportHelper.java
9 9
import java.util.Map;
10 10

  
11 11
import org.gvsig.tools.ToolsLocator;
12
import org.gvsig.tools.dataTypes.DataTypes;
12
import org.gvsig.tools.dataTypes.CoercionException;
13 13
import org.gvsig.tools.dynobject.DynClass;
14 14
import org.gvsig.tools.dynobject.DynField;
15 15
import org.gvsig.tools.dynobject.DynObjectManager;
......
303 303
				field.setDescription(value);
304 304

  
305 305
			} else if( name.equalsIgnoreCase(FIELD_TYPE_TAG)) {
306
				field.setType( DataTypes.getType(value) );
306
				field.setType( ToolsLocator.getDataTypesManager().getType(value) );
307 307
				
308 308
			} else if( name.equalsIgnoreCase(FIELD_SUBTYPE_TAG)) {
309 309
				field.setSubtype(value);
......
349 349
			}
350 350
		}
351 351

  
352
		//
353
		// Coerce the the min/max/default/available values to the type of the field
354
		//
355
		for( int i=0; i<availableValues.size(); i++ ) {
356
			PairValueLabel pair = (PairValueLabel) availableValues.get(i);
357
			if( pair.label == null ) {
358
				if( pair.value == null ) {
359
					pair.label = "null";
360
				} else {
361
					pair.label = pair.value.toString();
352
		try {
353
			//
354
			// Coerce the the min/max/default/available values to the type of
355
			// the field
356
			//
357
			for (int i = 0; i < availableValues.size(); i++) {
358
				PairValueLabel pair = (PairValueLabel) availableValues.get(i);
359
				if (pair.label == null) {
360
					if (pair.value == null) {
361
						pair.label = "null";
362
					} else {
363
						pair.label = pair.value.toString();
364
					}
362 365
				}
366
				availableValues.set(i, new DynObjectValueItem(field.coerce(pair.value), pair.label));
363 367
			}
364
			availableValues.set(i, new DynObjectValueItem( field.coerce(pair.value), pair.label));
368
			field.setAvailableValues(availableValues);
369
			field.setMaxValue(field.coerce(values.get(FIELD_MAXVALUE_TAG)));
370
			field.setMinValue(field.coerce(values.get(FIELD_MINVALUE_TAG)));
371
			field.setDefaultDynValue(field.coerce(values.get(FIELD_DEFAULTVALUE_TAG)));
372
		} catch (CoercionException e) {
373
			throw new ParseCoerceException(e, parser);
365 374
		}
366
		field.setAvailableValues(availableValues);
367
		field.setMaxValue( field.coerce(values.get(FIELD_MAXVALUE_TAG)));
368
		field.setMinValue( field.coerce(values.get(FIELD_MINVALUE_TAG)) );
369
		field.setDefaultDynValue( field.coerce(values.get(FIELD_DEFAULTVALUE_TAG)) );
370 375
	}
371 376

  
372 377
	private class PairValueLabel {
......
465 470
		public IncompatibleAttributeValueException(XmlPullParser parser, String tagname) { }
466 471
	}
467 472

  
473
	public static class ParseCoerceException extends RuntimeException { 
468 474

  
475
		/**
476
		 * 
477
		 */
478
		private static final long serialVersionUID = 1447718822981628834L;
479

  
480
		public ParseCoerceException(Throwable cause, XmlPullParser parser) { }
481
	}
482

  
483

  
469 484
}
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/persistence/xml/exception/PersistenceUnssuportedDefinitionTypeException.java
1 1
package org.gvsig.tools.persistence.xml.exception;
2 2

  
3
import org.gvsig.tools.dataTypes.DataTypes;
3
import org.gvsig.tools.ToolsLocator;
4 4
import org.gvsig.tools.dynobject.DynField;
5 5
import org.gvsig.tools.dynobject.DynStruct;
6 6
import org.gvsig.tools.persistence.exception.PersistenceRuntimeException;
......
17 17

  
18 18
	public PersistenceUnssuportedDefinitionTypeException(String classname, DynStruct definition, DynField field)  {
19 19
		super( MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
20
		this.setValue("type", DataTypes.getTypeName(field.getType()));
20
		this.setValue("type", ToolsLocator.getDataTypesManager().getTypeName(field.getType()));
21 21
		this.setValue("field", field.getName());
22 22
		this.setValue("definition", definition.getName());
23 23
		this.setValue("classname", classname);
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/CoercionException.java
1
package org.gvsig.tools.dataTypes;
2

  
3
public class CoercionException extends Exception {
4

  
5
	/**
6
	 * 
7
	 */
8
	private static final long serialVersionUID = 4367810513187193469L;
9
	
10
	public CoercionException() {
11
		super();
12
	}
13

  
14
	public CoercionException(Throwable cause) {
15
		this.initCause(cause);
16
	}
17

  
18
}
0 19

  
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/DefaultDataTypesManager.java
1
package org.gvsig.tools.dataTypes.impl;
2

  
3
import java.io.File;
4
import java.util.Date;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.Set;
8

  
9
import org.gvsig.tools.dataTypes.CoercionException;
10
import org.gvsig.tools.dataTypes.DataTypes;
11
import org.gvsig.tools.dataTypes.DataTypesManager;
12
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToBoolean;
13
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToByte;
14
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDate;
15
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToDouble;
16
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFile;
17
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToFloat;
18
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToInt;
19
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToLong;
20
import org.gvsig.tools.dataTypes.impl.coercion.CoerceToString;
21
import org.gvsig.tools.dynobject.DynObject;
22

  
23
public class DefaultDataTypesManager implements DataTypesManager {
24

  
25
	public void addtype(int type, String subtype, String name, Class defaultClass, Coercion coercion) {
26
		if( (type & DataTypes.OBJECT) != DataTypes.OBJECT ) {
27
			throw new IllegalArgumentException();
28
		}
29
		if( name == null || TYPE_NAMES[type] != null ) {
30
			throw new IllegalArgumentException();
31
		}
32
		SUBTYPES[type]=subtype;
33
		TYPE_NAMES[type]=name;
34
		DEFAULT_CLASSES[type]=defaultClass;
35
		COERCION[type]=coercion;
36
	}
37

  
38
	public boolean isValidType(int type) {
39
		return type>0 && type < 0x41 && TYPE_NAMES[type] != null;
40
	}
41

  
42
	public boolean isObject(int type) {
43
		return (type & DataTypes.OBJECT) == DataTypes.OBJECT;
44
	}
45
	
46
	public boolean isContainer(int type) {
47
		return (type & DataTypes.CONTAINER) == DataTypes.CONTAINER;
48
	}
49
	
50
	public  String getTypeName(int type) {
51
		if( type<0 || type>0x40 ) {
52
			return "unknow";
53
		}
54
		String name = TYPE_NAMES[type];
55
		if( name==null ) {
56
			return "unknow";
57
		}
58
		return name;
59
	}
60
	
61
	public  int getType(String name) {
62
		if( name != null ) {
63
			for( int i=0; i<TYPE_NAMES.length; i++ ) {
64
				if( name.equalsIgnoreCase(TYPE_NAMES[i]) ) {
65
					return i;
66
				}
67
			}
68
		}
69
		return DataTypes.INVALID;
70
	}
71

  
72
	public  Class getDefaultClass(int type) {
73
		if( type<0 || type>0x40 ) {
74
			return null;
75
		}
76
		return DEFAULT_CLASSES[type];
77
	}
78

  
79
	public String getSubtype(int type) {
80
		if( type<0 || type>0x40 ) {
81
			return null;
82
		}
83
		return SUBTYPES[type];
84
	}
85

  
86
	public Coercion getCoercion(int type) {
87
		// http://en.wikipedia.org/wiki/Type_conversion#Implicit_type_conversion
88

  
89
		return COERCION[type];
90
	}
91
	
92
	public Object coerce(int type, Object value) throws CoercionException {
93
		Coercion coercion = this.getCoercion(type);
94
		if( coercion != null ) {
95
			return coercion.coerce(value);
96
		}
97
		Class defaultClass = this.getDefaultClass(type);
98
		if( defaultClass == null ) {
99
			return value; // ??
100
		}
101
		if( defaultClass.isInstance(value) ) {
102
			return value;
103
		}
104
		throw new CoercionException();
105
	}
106
	
107
	private static final String[] SUBTYPES = new String[] {
108
		/* 0x00 */ null,
109
		/* 0x01 */ null,
110
		/* 0x02 */ null,
111
		/* 0x03 */ null,
112
		/* 0x04 */ null,
113
		/* 0x05 */ null,
114
		/* 0x06 */ null,
115
		/* 0x07 */ null,
116
		/* 0x08 */ null,
117
		/* 0x09 */ DataTypes.SUBTYPE_DATE,
118
		/* 0x0a */ DataTypes.SUBTYPE_DATE,
119
		/* 0x0b */ DataTypes.SUBTYPE_DATE,
120
		/* 0x0c */ null,
121
		/* 0x0d */ null,
122
		/* 0x0e */ null,
123
		/* 0x0f */ null,
124
	
125
		/* 0x14 */ null,
126
		/* 0x11 */ null,
127
		/* 0x12 */ null,
128
		/* 0x13 */ null,
129
		/* 0x14 */ null,
130
		/* 0x15 */ null,
131
		/* 0x16 */ null,
132
		/* 0x17 */ null,
133
		/* 0x18 */ null,
134
		/* 0x19 */ null,
135
		/* 0x1a */ null,
136
		/* 0x1b */ null,
137
		/* 0x1c */ null,
138
		/* 0x1d */ DataTypes.SUBTYPE_FILE,
139
		/* 0x1e */ DataTypes.SUBTYPE_FOLDER,
140
		/* 0x1f */ null,
141

  
142
		
143
		/* 0x20 */ null,
144
		/* 0x21 */ null,
145
		/* 0x22 */ null,
146
		/* 0x23 */ null,
147
		/* 0x24 */ null,
148
		/* 0x25 */ null,
149
		/* 0x26 */ null,
150
		/* 0x27 */ null,
151
		/* 0x28 */ null,
152
		/* 0x29 */ null,
153
		/* 0x30 */ null,
154
		/* 0x31 */ null,
155
		/* 0x32 */ null,
156
		/* 0x33 */ null,
157
		/* 0x34 */ null,
158
		/* 0x35 */ null,
159
		/* 0x36 */ null,
160
		/* 0x37 */ null,
161
		/* 0x38 */ null,
162
		/* 0x39 */ null,
163
		/* 0x40 */ null,
164

  
165
	};
166

  
167
		
168
	private static final String[] TYPE_NAMES = new String[] {
169
		/* 0x00 */ null,
170
		/* 0x01 */ "Boolean",
171
		/* 0x02 */ "Byte",
172
		/* 0x03 */ "Char",
173
		/* 0x04 */ "Integer",
174
		/* 0x05 */ "Long",
175
		/* 0x06 */ "Float",
176
		/* 0x07 */ "Double",
177
		/* 0x08 */ "String",
178
		/* 0x09 */ "Date",
179
		/* 0x0a */ "Time",
180
		/* 0x0b */ "Timestamp",
181
		/* 0x0c */ "ByteArray",
182
		/* 0x0d */ "File",
183
		/* 0x0e */ "Folder",
184
		/* 0x0f */ null,
185
	
186
		/* 0x14 */ null,
187
		/* 0x11 */ "Array",
188
		/* 0x12 */ "List",
189
		/* 0x13 */ "Set",
190
		/* 0x14 */ "Map",
191
		/* 0x15 */ "DynObject",
192
		/* 0x16 */ null,
193
		/* 0x17 */ null,
194
		/* 0x18 */ null,
195
		/* 0x19 */ null,
196
		/* 0x1a */ null,
197
		/* 0x1b */ null,
198
		/* 0x1c */ null,
199
		/* 0x1d */ null,
200
		/* 0x1e */ null,
201
		/* 0x1f */ null,
202

  
203
		
204
		/* 0x20 */ "Object",
205
		/* 0x21 */ null,
206
		/* 0x22 */ null,
207
		/* 0x23 */ null,
208
		/* 0x24 */ null,
209
		/* 0x25 */ null,
210
		/* 0x26 */ null,
211
		/* 0x27 */ null,
212
		/* 0x28 */ null,
213
		/* 0x29 */ null,
214
		/* 0x30 */ null,
215
		/* 0x31 */ null,
216
		/* 0x32 */ null,
217
		/* 0x33 */ null,
218
		/* 0x34 */ null,
219
		/* 0x35 */ null,
220
		/* 0x36 */ null,
221
		/* 0x37 */ null,
222
		/* 0x38 */ null,
223
		/* 0x39 */ null,
224
		/* 0x40 */ null,
225

  
226
	};
227

  
228
	private static final Class[] DEFAULT_CLASSES = new Class[] {
229
		/* 0x00 */ null,
230
		/* 0x01 */ Boolean.class,
231
		/* 0x02 */ Byte.class,
232
		/* 0x03 */ Character.class,
233
		/* 0x04 */ Integer.class,
234
		/* 0x05 */ Long.class,
235
		/* 0x06 */ Float.class,
236
		/* 0x07 */ Double.class,
237
		/* 0x08 */ String.class,
238
		/* 0x09 */ Date.class,
239
		/* 0x0a */ Date.class,
240
		/* 0x0b */ Date.class,
241
		/* 0x0c */ new byte[] {}.getClass(),
242
		/* 0x0d */ File.class,
243
		/* 0x0e */ File.class,
244
		/* 0x0f */ null,
245
	
246
		/* 0x10 */ null,
247
		/* 0x11 */ new Object[] {}.getClass(),
248
		/* 0x12 */ List.class,
249
		/* 0x13 */ Set.class,
250
		/* 0x14 */ Map.class,
251
		/* 0x15 */ DynObject.class,
252
		/* 0x16 */ null,
253
		/* 0x17 */ null,
254
		/* 0x18 */ null,
255
		/* 0x19 */ null,
256
		/* 0x1a */ null,
257
		/* 0x1b */ null,
258
		/* 0x1c */ null,
259
		/* 0x1d */ null,
260
		/* 0x1e */ null,
261
		/* 0x1f */ null,
262

  
263
		
264
		/* 0x20 */ Object.class,
265
		/* 0x21 */ null,
266
		/* 0x22 */ null,
267
		/* 0x23 */ null,
268
		/* 0x24 */ null,
269
		/* 0x25 */ null,
270
		/* 0x26 */ null,
271
		/* 0x27 */ null,
272
		/* 0x28 */ null,
273
		/* 0x29 */ null,
274
		/* 0x30 */ null,
275
		/* 0x31 */ null,
276
		/* 0x32 */ null,
277
		/* 0x33 */ null,
278
		/* 0x34 */ null,
279
		/* 0x35 */ null,
280
		/* 0x36 */ null,
281
		/* 0x37 */ null,
282
		/* 0x38 */ null,
283
		/* 0x39 */ null,
284
		/* 0x40 */ null,
285

  
286
	};
287

  
288
	private static final Coercion[] COERCION = new Coercion[] {
289
		/* 0x00 */ null,
290
		/* 0x01 */ new CoerceToBoolean(),
291
		/* 0x02 */ new CoerceToByte(),
292
		/* 0x03 */ new CoerceToString(),
293
		/* 0x04 */ new CoerceToInt(),
294
		/* 0x05 */ new CoerceToLong(),
295
		/* 0x06 */ new CoerceToFloat(),
296
		/* 0x07 */ new CoerceToDouble(),
297
		/* 0x08 */ new CoerceToString(),
298
		/* 0x09 */ new CoerceToDate(),
299
		/* 0x0a */ new CoerceToDate(),
300
		/* 0x0b */ new CoerceToDate(),
301
		/* 0x0c */ null,
302
		/* 0x0d */ new CoerceToFile(),
303
		/* 0x0e */ new CoerceToFile(),
304
		/* 0x0f */ null,
305
	
306
		/* 0x10 */ null,
307
		/* 0x11 */ null,
308
		/* 0x12 */ null,
309
		/* 0x13 */ null,
310
		/* 0x14 */ null,
311
		/* 0x15 */ null,
312
		/* 0x16 */ null,
313
		/* 0x17 */ null,
314
		/* 0x18 */ null,
315
		/* 0x19 */ null,
316
		/* 0x1a */ null,
317
		/* 0x1b */ null,
318
		/* 0x1c */ null,
319
		/* 0x1d */ null,
320
		/* 0x1e */ null,
321
		/* 0x1f */ null,
322

  
323
		
324
		/* 0x20 */ null,
325
		/* 0x21 */ null,
326
		/* 0x22 */ null,
327
		/* 0x23 */ null,
328
		/* 0x24 */ null,
329
		/* 0x25 */ null,
330
		/* 0x26 */ null,
331
		/* 0x27 */ null,
332
		/* 0x28 */ null,
333
		/* 0x29 */ null,
334
		/* 0x30 */ null,
335
		/* 0x31 */ null,
336
		/* 0x32 */ null,
337
		/* 0x33 */ null,
338
		/* 0x34 */ null,
339
		/* 0x35 */ null,
340
		/* 0x36 */ null,
341
		/* 0x37 */ null,
342
		/* 0x38 */ null,
343
		/* 0x39 */ null,
344
		/* 0x40 */ null,
345
	};
346

  
347

  
348
}
0 349

  
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDouble.java
1
package org.gvsig.tools.dataTypes.impl.coercion;
2

  
3
import org.gvsig.tools.dataTypes.CoercionException;
4
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
5

  
6
public class CoerceToDouble implements Coercion {
7

  
8
	public Object coerce(Object value) throws CoercionException {
9
		try {
10
			if (!(value instanceof Double)) {
11
				if (value instanceof Number) {
12
					value = new Double(((Number) value).doubleValue());
13
				} else {
14
					value = Double.valueOf(value.toString());
15
				}
16
			}
17
			return value;
18
		} catch (Exception e) {
19
			throw new CoercionException(e);
20
		}
21

  
22
	}
23

  
24
}
0 25

  
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToInt.java
1
package org.gvsig.tools.dataTypes.impl.coercion;
2

  
3
import org.gvsig.tools.dataTypes.CoercionException;
4
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
5

  
6
public class CoerceToInt implements Coercion {
7

  
8
	public Object coerce(Object value) throws CoercionException {
9
		try {
10
			if( ! (value instanceof Integer) ) {
11
				if( value instanceof Number ) {
12
					value = new Long(((Number)value).intValue());
13
				} else {
14
					value = Integer.valueOf(value.toString());
15
				}
16
			}
17
			return value;
18
		} catch(Exception e) {
19
			throw new CoercionException(e); 
20
		}
21
	}
22

  
23
}
0 24

  
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToDate.java
1
package org.gvsig.tools.dataTypes.impl.coercion;
2

  
3
import java.text.ParsePosition;
4
import java.text.SimpleDateFormat;
5
import java.util.Date;
6

  
7
import org.gvsig.tools.dataTypes.CoercionException;
8
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
9

  
10
public class CoerceToDate implements Coercion {
11

  
12
	public Object coerce(Object value) throws CoercionException {
13
		if( ! (value instanceof Date) ) {
14
			SimpleDateFormat formatter = new SimpleDateFormat();
15
			ParsePosition pp = new ParsePosition(0);
16
			Date d = formatter.parse(value.toString(), pp);
17
			if( d == null ) {
18
				throw new CoercionException();
19
			}
20
		}
21
		return value;
22
	}
23

  
24
}
0 25

  
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToBoolean.java
1
package org.gvsig.tools.dataTypes.impl.coercion;
2

  
3
import org.gvsig.tools.dataTypes.CoercionException;
4
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
5

  
6
public class CoerceToBoolean implements Coercion {
7

  
8
	public Object coerce(Object value) throws CoercionException {
9
		try {
10
			if (!(value instanceof Boolean)) {
11
				if (value instanceof Number) {
12
					if (((Number) value).intValue() == 1) {
13
						value = Boolean.TRUE;
14
					} else {
15
						value = Boolean.FALSE;
16
					}
17
				} else {
18
					value = Boolean.valueOf(value.toString());
19
				}
20
			}
21
			return value;
22
		} catch (Exception e) {
23
			throw new CoercionException(e);
24
		}
25

  
26
	}
27

  
28
}
0 29

  
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToFile.java
1
package org.gvsig.tools.dataTypes.impl.coercion;
2

  
3
import java.io.File;
4
import java.net.URI;
5

  
6
import org.gvsig.tools.dataTypes.CoercionException;
7
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
8

  
9

  
10
public class CoerceToFile implements Coercion {
11

  
12
	public Object coerce(Object value) throws CoercionException {
13
		if( ! (value instanceof File) ) {
14
			if( value instanceof String ) {
15
				value = new File((String) value);
16
			} else if( value instanceof URI ) {
17
				value = new File((URI) value);
18
			} else {
19
				throw new CoercionException();
20
			}
21
		}
22
		return value;
23
	}
24

  
25
}
0 26

  
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToLong.java
1
package org.gvsig.tools.dataTypes.impl.coercion;
2

  
3
import org.gvsig.tools.dataTypes.CoercionException;
4
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
5

  
6
public class CoerceToLong implements Coercion {
7

  
8
	public Object coerce(Object value) throws CoercionException {
9
		try {
10
			if( ! (value instanceof Long) ) {
11
				if( value instanceof Number ) {
12
					value = new Long(((Number)value).longValue());
13
				} else {
14
					value = Long.valueOf(value.toString());
15
				}
16
			}
17
			return value;
18
		} catch(Exception e) {
19
			throw new CoercionException(e); 
20
		}
21
	}
22

  
23
}
0 24

  
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToByte.java
1
package org.gvsig.tools.dataTypes.impl.coercion;
2

  
3
import org.gvsig.tools.dataTypes.CoercionException;
4
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
5

  
6
public class CoerceToByte implements Coercion {
7

  
8
	public Object coerce(Object value) throws CoercionException {
9
		try {
10
			if( ! (value instanceof Byte) ) {
11
				if( value instanceof Number ) {
12
					value = new Integer(((Number)value).byteValue());
13
				} else {
14
					value = Byte.valueOf(value.toString());
15
				}
16
			}
17
			return value;
18
		} catch (Exception e) {
19
			throw new CoercionException(e);
20
		}
21

  
22
	}
23

  
24
}
0 25

  
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToFloat.java
1
package org.gvsig.tools.dataTypes.impl.coercion;
2

  
3
import org.gvsig.tools.dataTypes.CoercionException;
4
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
5

  
6
public class CoerceToFloat implements Coercion {
7

  
8
	public Object coerce(Object value) throws CoercionException {
9
		try {
10
			if (!(value instanceof Float)) {
11
				if (value instanceof Number) {
12
					value = new Double(((Number) value).floatValue());
13
				} else {
14
					value = Float.valueOf(value.toString());
15
				}
16
			}
17
			return value;
18
		} catch (Exception e) {
19
			throw new CoercionException(e);
20
		}
21

  
22
	}
23

  
24
}
0 25

  
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToString.java
1
package org.gvsig.tools.dataTypes.impl.coercion;
2

  
3
import org.gvsig.tools.dataTypes.CoercionException;
4
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
5

  
6
public class CoerceToString implements Coercion {
7

  
8
	public Object coerce(Object value) throws CoercionException {
9
		try {
10
			if( !(value instanceof String )) {
11
				value = value.toString();
12
			}
13
			return value;
14
		} catch (Exception e) {
15
			throw new CoercionException(e);
16
		}
17

  
18
	}
19

  
20
}
0 21

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff