Revision 2080 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/dataTypes/impl/coercion/CoerceToURL.java

View differences:

CoerceToURL.java
25 25

  
26 26
import java.net.MalformedURLException;
27 27
import java.net.URL;
28
import org.gvsig.tools.dataTypes.AbstractCoercion;
29
import org.gvsig.tools.dataTypes.CoercionContext;
28 30

  
29 31
import org.gvsig.tools.dataTypes.CoercionException;
30
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
32
import org.gvsig.tools.dataTypes.DataTypesManager;
31 33

  
34
public class CoerceToURL extends AbstractCoercion {
32 35

  
33
public class CoerceToURL implements Coercion {
36
  @Override
37
  public Object coerce(Object value, CoercionContext context) throws CoercionException {
38
    if (value == null || value instanceof URL) {
39
      return value;
40
    }
41
    if (value instanceof String) {
42
      if (((String) value).isEmpty()) {
43
        return null;
44
      }
45
      try {
46
        value = new URL((String) value);
47
      } catch (MalformedURLException e) {
48
        throw new CoercionException(e);
49
      }
50
    } else {
51
      String s = value.toString();
52
      if (s == null) {
53
        return null;
54
      }
55
      s = s.trim().toLowerCase();
56
      if (s.length() == 0) {
57
        return null;
58
      }
59
      throw new CoercionException();
60
    }
61
    return value;
62
  }
34 63

  
35
	public Object coerce(Object value) throws CoercionException {
36
    	if( value == null ) {
37
    		return null;
38
    	}
39
		if( ! (value instanceof URL) ) {
40
			if( value instanceof String ) {
41
                                if(((String)value).isEmpty() ) {
42
                                    return null;
43
                                }
44
				try {
45
					value = new URL((String) value);
46
				} catch (MalformedURLException e) {
47
					throw new CoercionException(e);
48
				}
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
				throw new CoercionException();
59
			}
60
		}
61
		return value;
62
	}
63

  
64 64
}

Also available in: Unified diff