1 61 62 package org.apache.commons.cli; 63 64 import java.io.File ; 65 import java.net.URL ; 66 import java.net.MalformedURLException ; 67 import java.util.Date ; 68 69 import org.apache.commons.lang.NumberUtils; 70 71 80 public class TypeHandler { 81 82 91 public static Object createValue(String str, Object obj) { 92 return createValue(str, (Class )obj); 93 } 94 95 104 public static Object createValue(String str, Class clazz) { 105 if( PatternOptionBuilder.STRING_VALUE == clazz) { 106 return str; 107 } else 108 if( PatternOptionBuilder.OBJECT_VALUE == clazz) { 109 return createObject(str); 110 } else 111 if( PatternOptionBuilder.NUMBER_VALUE == clazz) { 112 return createNumber(str); 113 } else 114 if( PatternOptionBuilder.DATE_VALUE == clazz) { 115 return createDate(str); 116 } else 117 if( PatternOptionBuilder.CLASS_VALUE == clazz) { 118 return createClass(str); 119 } else 120 if( PatternOptionBuilder.FILE_VALUE == clazz) { 121 return createFile(str); 122 } else 123 if( PatternOptionBuilder.EXISTING_FILE_VALUE == clazz) { 124 return createFile(str); 125 } else 126 if( PatternOptionBuilder.FILES_VALUE == clazz) { 127 return createFiles(str); 128 } else 129 if( PatternOptionBuilder.URL_VALUE == clazz) { 130 return createURL(str); 131 } else { 132 return null; 133 } 134 } 135 136 142 public static Object createObject(String str) { 143 Class cl = null; 144 try { 145 cl = Class.forName(str); 146 } catch (ClassNotFoundException cnfe) { 147 System.err.println("Unable to find: "+str); 148 return null; 149 } 150 151 Object instance = null; 152 153 try { 154 instance = cl.newInstance(); 155 } catch (InstantiationException cnfe) { 156 System.err.println("InstantiationException; Unable to create: "+str); 157 return null; 158 } 159 catch (IllegalAccessException cnfe) { 160 System.err.println("IllegalAccessException; Unable to create: "+str); 161 return null; 162 } 163 164 return instance; 165 } 166 167 174 public static Number createNumber(String str) { 175 try { 177 return NumberUtils.createNumber(str); 179 } catch (NumberFormatException nfe) { 180 System.err.println(nfe.getMessage()); 181 return null; 182 } 183 } 184 185 191 public static Class createClass(String str) { 192 try { 193 return Class.forName(str); 194 } catch (ClassNotFoundException cnfe) { 195 System.err.println("Unable to find: "+str); 196 return null; 197 } 198 } 199 200 207 public static Date createDate(String str) { 208 Date date = null; 209 if(date == null) { 210 System.err.println("Unable to parse: "+str); 211 } 212 return date; 213 } 214 215 222 public static URL createURL(String str) { 223 try { 224 return new URL (str); 225 } catch (MalformedURLException mue) { 226 System.err.println("Unable to parse: "+str); 227 return null; 228 } 229 } 230 231 237 public static File createFile(String str) { 238 return new File (str); 239 } 240 241 247 public static File [] createFiles(String str) { 248 return null; 251 } 252 253 } 254 | Popular Tags |