1 16 17 18 package org.apache.commons.beanutils.converters; 19 20 21 import org.apache.commons.beanutils.ConversionException; 22 import org.apache.commons.beanutils.Converter; 23 24 import java.net.URL ; 25 import java.net.MalformedURLException ; 26 27 28 29 39 40 public final class URLConverter implements Converter { 41 42 43 45 46 50 public URLConverter() { 51 52 this.defaultValue = null; 53 this.useDefault = false; 54 55 } 56 57 58 64 public URLConverter(Object defaultValue) { 65 66 this.defaultValue = defaultValue; 67 this.useDefault = true; 68 69 } 70 71 72 74 75 78 private Object defaultValue = null; 79 80 81 84 private boolean useDefault = true; 85 86 87 89 90 100 public Object convert(Class type, Object value) { 101 102 if (value == null) { 103 if (useDefault) { 104 return (defaultValue); 105 } else { 106 throw new ConversionException("No value specified"); 107 } 108 } 109 110 if (value instanceof URL ) { 111 return (value); 112 } 113 114 try { 115 return new URL (value.toString()); 116 } catch(MalformedURLException murle) { 117 if (useDefault) { 118 return (defaultValue); 119 } else { 120 throw new ConversionException(murle); 121 } 122 } 123 124 } 125 126 127 } 128 | Popular Tags |