1 package jfun.yan.xml.nuts; 2 3 4 import java.beans.PropertyEditor ; 5 6 import jfun.yan.Container; 7 import jfun.yan.Creator; 8 import jfun.yan.TypeMismatchException; 9 import jfun.yan.containers.DefaultContainer; 10 import jfun.yan.util.ReflectionUtil; 11 import jfun.yan.util.deserializer.Deserializer; 12 import jfun.yan.util.deserializer.PropertyEditorDeserializer; 13 import jfun.yan.xml.ConfigurationException; 14 15 import jfun.yan.xml.nut.Nut; 16 17 40 public class DeserializerNut extends Nut { 41 private Class target_type; 42 private Deserializer deserializer; 43 private boolean overriding = false; 44 private boolean mandatory = false; 45 private Class clazz; 46 public boolean isMandatory() { 47 return mandatory; 48 } 49 public void setMandatory(boolean mandatory) { 50 this.mandatory = mandatory; 51 } 52 public boolean isOverriding() { 53 return overriding; 54 } 55 public void setOverriding(boolean overriding) { 56 this.overriding = overriding; 57 } 58 59 public void setClass(Class clazz) 60 throws IllegalAccessException , InstantiationException { 61 checkDuplicate("deserializer", this.deserializer); 62 this.clazz = clazz; 63 this.deserializer = getStatelessDeserializer(clazz); 64 } 65 public Class getTargetType() { 66 return target_type; 67 } 68 public void setTarget_type(Class target_type) { 69 this.target_type = target_type; 70 } 71 public void eval() 72 throws IllegalAccessException , InstantiationException { 73 checkMandatory("target-type", target_type); 74 checkMandatory("class or deserializer", deserializer); 75 this.getNutEnvironment() 76 .registerDeserializer(target_type, deserializer, 77 overriding, mandatory); 78 } 79 80 public void setDeserializer(Object ds) { 81 if(ds==null) 82 raise("cannot set deserializer to null"); 83 checkDuplicate("class", this.clazz); 84 delegateTo(toDeserializer(ds)); 85 } 86 87 public void add(Object sub){ 88 checkDuplicate("class", this.clazz); 89 checkDuplicate("deserializer", this.deserializer); 90 delegateTo(toDeserializer(sub)); 91 } 92 private Deserializer getStatelessDeserializer(Class type) 93 throws IllegalAccessException , InstantiationException { 94 if(Deserializer.class.isAssignableFrom(type)){ 95 return (Deserializer)type.newInstance(); 96 } 97 else if(PropertyEditor .class.isAssignableFrom(type)){ 98 return new PropertyEditorDeserializer(type); 99 } 100 else{ 101 throw mismatchType(type); 102 } 103 } 104 protected void delegateTo(Deserializer deserializer){ 105 this.deserializer = deserializer; 106 } 107 private Deserializer toDeserializer(Object obj){ 108 if(obj instanceof Deserializer){ 109 return (Deserializer)obj; 110 } 111 else if(obj instanceof Creator){ 112 return instantiateDeserializer((Creator)obj); 113 } 114 else{ 115 return (Deserializer)this.convert(Deserializer.class, obj); 116 } 117 } 118 private ConfigurationException mismatchType(Class type){ 119 return raise(new TypeMismatchException(Deserializer.class, type)); 120 } 121 private ConfigurationException mismatch(Object obj){ 122 return mismatchType(obj==null?null:obj.getClass()); 123 } 124 private Deserializer instantiateDeserializer(Creator c){ 125 final Class type = c.getType(); 126 if(type!=null && !ReflectionUtil.isCompatible(Deserializer.class, type)){ 127 throw mismatchType(type); 128 } 129 final Object r = empty.instantiateComponent(c); 130 if(r instanceof Deserializer){ 131 return (Deserializer)r; 132 } 133 else{ 134 throw mismatch(r); 135 } 136 } 137 private static Container empty = new DefaultContainer(); 138 } 139 | Popular Tags |