1 package jfun.yan.xml.nuts; 2 3 import java.beans.IntrospectionException ; 4 5 import jfun.yan.Component; 6 import jfun.yan.Components; 7 import jfun.yan.Creator; 8 import jfun.yan.Monad; 9 import jfun.yan.ParameterBinder; 10 import jfun.yan.function.Signature; 11 17 public class SetterNut extends LiteralNut { 18 private Component component; 19 private String name; 20 private int ind = -1; 21 public Component getComponent() { 22 return component; 23 } 24 25 public void setComponent(Component component) { 26 this.component = component; 27 } 28 29 public String getName() { 30 return name; 31 } 32 33 public void setName(String name) { 34 this.name = name.trim(); 35 } 36 37 public int getInd() { 38 return ind; 39 } 40 41 public void setInd(int ind) { 42 if(ind < 0) 43 raise("invalid property index "+ind); 44 this.ind = ind; 45 } 46 47 public Component eval() 48 throws IntrospectionException { 49 checkMandatory("component", component); 50 checkMandatory("name", name); 51 Component result = component; 52 if(ind < 0){ 53 result = result.setter(name); 54 } 55 else{ 56 result = result.setter(name, ind); 57 } 58 final Object val = getVal(); 59 final Object defval = getDefault(); 60 if(val==null && defval==null){ 61 return result; 62 } 63 64 return result.bindArguments(new ParameterBinder(){ 65 public Creator bind(Signature src, int ind, Class type) { 66 if(ind==0){ 67 Component arg = getVal(type); 68 Component def = getDefault(type); 69 Component result = arg; 70 if(result==null){ 71 result = Components.useArgument(src, ind, type); 72 } 73 if(def!=null){ 74 result = Monad.mplus(result, def); 75 } 76 return result; 77 } 78 else 79 return Components.useArgument(src, ind, type); 80 } 81 }); 82 } 83 84 } 85 | Popular Tags |