1 18 19 package de.gulden.framework.amoda.generic.data; 20 21 import de.gulden.framework.amoda.model.metadata.NamedValue; 22 import java.awt.*; 23 import java.io.*; 24 import java.lang.*; 25 import java.util.*; 26 27 33 public class GenericValue implements NamedValue { 34 35 39 public static String [][] BOOLEAN_LITERALS = {{"true","yes","on"},{"false","no","off"}}; 40 41 protected Class type; 42 43 protected Object object; 44 45 protected String name; 46 47 48 52 public GenericValue() { 53 } 55 56 public GenericValue(Object value) { 57 this(); 58 setType(value.getClass()); 59 set(value); 60 } 61 62 63 67 public String getString() { 68 return toString(get()); 69 } 70 71 public int getInt() { 72 if (object instanceof Number ) { 73 return ((Number )object).intValue(); 74 } else if (object instanceof String ) { 75 try { 76 return Integer.valueOf((String )object).intValue(); 77 } catch (NumberFormatException nfe) { 78 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert "+((String )object)+" to int"); 79 } 80 } else if (object==null) { 81 return 0; 82 } else { 83 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to int"); 84 } 85 } 86 87 public float getFloat() { 88 if (object instanceof Number ) { 89 return ((Number )object).floatValue(); 90 } else if (object instanceof String ) { 91 try { 92 return Float.valueOf((String )object).floatValue(); 93 } catch (NumberFormatException nfe) { 94 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert "+((String )object)+" to float"); 95 } 96 } else if (object==null) { 97 return 0.0f; 98 } else { 99 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to float"); 100 } 101 } 102 103 public double getDouble() { 104 if (object instanceof Number ) { 105 return ((Number )object).doubleValue(); 106 } else if (object instanceof String ) { 107 try { 108 return Double.valueOf((String )object).doubleValue(); 109 } catch (NumberFormatException nfe) { 110 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert "+((String )object)+" to double"); 111 } 112 } else if (object==null) { 113 return 0.0; 114 } else { 115 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to double"); 116 } 117 } 118 119 public boolean getBoolean() { 120 if (object instanceof Boolean ) { 121 return ((Boolean )object).booleanValue(); 122 } else if (object instanceof String ) { 123 String boolString=(String )object; 124 return isBooleanLiteral(true,boolString); 125 } else if (object instanceof Number ) { 126 return (((Number )object).doubleValue()!=0.0); 127 } else if (object==null) { 128 return false; 129 } else { 130 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to boolean"); 131 } 132 } 133 134 public boolean isTypeAvailable(Class type) { 135 if ((object!=null)&&(type.isAssignableFrom(object.getClass()))) { 137 return true; 138 } else if (type.isAssignableFrom(this.type)) { 139 return true; 140 } else if (String .class.isAssignableFrom(type)) { 141 return true; } else if (Integer .class.isAssignableFrom(type)||Double .class.isAssignableFrom(type)) { 143 return Number .class.isAssignableFrom(this.type); 144 } else if (Boolean .class.isAssignableFrom(type)) { 145 return Boolean .class.isAssignableFrom(this.type) 146 || Number .class.isAssignableFrom(this.type) 147 || String .class.isAssignableFrom(this.type); 148 } else { 149 return false; 150 } 151 } 152 153 public void parseString(String s) { 154 if (type!=null) { try { 156 if (String .class.isAssignableFrom(type)) { 157 object=s; 158 } else if (Boolean .class.isAssignableFrom(type)) { 159 object=isBooleanLiteral(true,s.trim()) ? Boolean.TRUE : Boolean.FALSE; } else if (Integer .class.isAssignableFrom(type)) { 161 object=Integer.valueOf(s.trim()); 162 } else if (Float .class.isAssignableFrom(type)) { 163 object=Float.valueOf(s.trim()); 164 } else if (Double .class.isAssignableFrom(type)) { 165 object=Double.valueOf(s.trim()); 166 } else if (Class .class.isAssignableFrom(type)) { 167 try { 168 object=Class.forName(s.trim()); 169 } catch (ClassNotFoundException cnfe) { 170 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("string '"+s.trim()+"' is not a valid class name, class not found"); 171 } 172 } else if (File.class.isAssignableFrom(type)) { 173 object=(new File(s.trim())); } else if (Color.class.isAssignableFrom(type)) { 175 object=de.gulden.util.Toolbox.parseColor(s.trim()); 176 if (object==null) { 177 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("string '"+s+"' is not a valid color"); 178 } 179 } else { 180 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot handle type '"+type.getName()+"' for parsing '"+s+"'"); 181 } 182 } catch (NumberFormatException nfe) { 183 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot parse string '"+s+"' as value for type "+type.getName()); 184 } 185 } else { if (s.equalsIgnoreCase("yes")||s.equalsIgnoreCase("true")||s.equalsIgnoreCase("on")) { 188 set(Boolean.TRUE); 189 return; 190 } else if (s.equalsIgnoreCase("no")||s.equalsIgnoreCase("false")||s.equalsIgnoreCase("off")) { 191 set(Boolean.FALSE); 192 return; 193 } 194 try { 196 double d=Double.parseDouble(s); 197 double dRound=Math.round(d); 198 if (d==dRound) { set(new Integer ((int)dRound)); 200 } else { set(new Double (d)); 202 } 203 return; 204 } catch (NumberFormatException nfe) { 205 213 set(s); } 215 } 216 } 217 218 public String toString(Object o) { 219 if (object instanceof Color) { 220 return de.gulden.util.Toolbox.toString((Color)object); 221 } else if (object instanceof File) { 222 return ((File)object).getAbsolutePath(); 223 } else if (o!=null) { 224 return o.toString(); 225 } else { 226 return null; 227 } 228 } 229 230 public File getFile() { 231 if (object instanceof File) { 232 return ((File)object); 233 } else if (object instanceof String ) { 234 File f=new File((String )object); 235 try { 236 f=f.getCanonicalFile(); 237 return f; 238 } catch (java.io.IOException ioe) { 239 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to File"); 240 } 241 } else if (object==null) { 242 return null; 243 } else { 244 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to File"); 245 } 246 } 247 248 public Color getColor() { 249 if (object instanceof java.awt.Color ) { 250 return ((Color)object); 251 } else if (object instanceof String ) { 252 Color c=de.gulden.util.Toolbox.parseColor((String )object); 253 if (c!=null) { 254 return c; 255 } else { 256 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert '"+((String )object)+" to Color"); 257 } 258 } else if (object==null) { 259 return null; 260 } else { 261 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to Color"); 262 } 263 } 264 265 public Class getType() { 266 return type; 267 } 268 269 public void setType(Class _type) { 270 type = _type; 271 } 272 273 public Object get() { 274 return object; 275 } 276 277 public Object set(Object _object) { 278 Object old = object; 279 if ((type==null)&&(_object!=null)) { 280 type=_object.getClass(); 281 } 282 object = _object; 283 return old; 284 } 285 286 public String getName() { 287 return name; 288 } 289 290 public void setName(String _name) { 291 name = _name; 292 } 293 294 public Class getClassType() { 295 if (object instanceof Class ) { 296 return ((Class )object); 297 } else if (object instanceof String ) { 298 try { 299 Class c = Class.forName((String )object); 300 return c; 301 } catch (ClassNotFoundException cnfe) { 302 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert '"+((String )object)+" to Class"); 303 } 304 } else if (object==null) { 305 return null; 306 } else { 307 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot convert value of type "+object.getClass().getName()+" ["+object.toString()+"] to Class"); 308 } 309 } 310 311 public Object getClassInstance() { 312 Class cl = getClassType(); 313 try { 314 return cl.newInstance(); 315 } catch (InstantiationException ie) { 316 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot create instance of "+cl.getName()+": "+ie.getMessage()); 317 } catch (IllegalAccessException iae) { 318 throw new de.gulden.framework.amoda.model.data.IllegalTypeError("cannot create instance of "+cl.getName()+": "+iae.getMessage()); 319 } 320 } 321 322 323 327 public static boolean isBooleanLiteral(boolean booleanValue, String s) { 328 int b=booleanValue?0:1; 329 for (int i=0;i<BOOLEAN_LITERALS[b].length;i++) { 330 if (s.equalsIgnoreCase(BOOLEAN_LITERALS[b][i])) { 331 return true; 332 } 333 } 334 return false; 335 } 336 337 } | Popular Tags |