Revision 2080 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToFile.java
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 |
} |
Also available in: Unified diff