| 1 package jfun.yan.xml.nuts; 2 3 import jfun.util.Misc; 4 import jfun.yan.Component; 5 import jfun.yan.Components; 6 import jfun.yan.Creator; 7 import jfun.yan.util.ReflectionUtil; 8 import jfun.yan.xml.NutsUtils; 9 import jfun.yan.xml.nut.Nut; 10 11 12 24 public class LiteralNut extends Nut { 25 private Object val; 26 private Object defaultval; 27 private Class type; 28 29 public Class getType() { 30 return type; 31 } 32 public void setType(Class type) { 33 this.type = type; 34 } 35 public void setDefault(Object val){ 36 this.defaultval = val; 37 } 38 public void setVal(Object val) { 39 this.val = val; 40 } 41 47 public void add(Object v){ 48 checkDuplicate("val", this.val); 49 this.val = v; 50 } 51 public Component getVal(Class target_type){ 52 return fromVal(target_type, val); 53 } 54 public Component getDefault(Class target_type){ 55 return fromVal(target_type, defaultval); 56 } 57 private void checkType(Class target_type){ 58 if(type!=null){ 59 if(!ReflectionUtil.isAssignableFrom(target_type, type)){ 60 raise("cannot convert " + Misc.getTypeName(type) + " to " 61 + Misc.getTypeName(target_type)); 62 } 63 } 64 } 65 private Component fromVal(Class target_type, Object val){ 66 checkType(target_type); 67 if(val == null) return null; 68 if(type != null){ 69 target_type = type; 70 } 71 if(val instanceof Creator){ 72 return Util.convert(this, target_type, 73 Components.adapt((Creator)val) 74 ); 75 } 76 else{ 77 return NutsUtils.asComponent(convert(target_type, val)); 78 } 79 95 } 96 private static String asText(Object v){ 97 return v==null?null:v.toString(); 98 } 99 public String getValueText(){ 100 return asText(val); 101 } 102 public String getDefaultText(){ 103 return asText(defaultval); 104 } 105 public Object getVal(){ 106 return val; 107 } 108 public Object getDefault(){ 109 return defaultval; 110 } 111 public void justify(){ 112 checkMandatory("val", val, "default", defaultval); 113 } 114 } 115 | Popular Tags |