Package jfun.yan.xml.nuts

Source Code of jfun.yan.xml.nuts.SetterNut

package jfun.yan.xml.nuts;

import java.beans.IntrospectionException;

import jfun.yan.Component;
import jfun.yan.Components;
import jfun.yan.Creator;
import jfun.yan.Monad;
import jfun.yan.ParameterBinder;
import jfun.yan.function.Signature;
/**
* Nut class for <setter> tag.
* <p>
* @author Ben Yu
* Nov 9, 2005 11:42:15 PM
*/
public class SetterNut extends LiteralNut {
  private Component component;
  private String name;
  private int ind = -1;
  public Component getComponent() {
    return component;
  }

  public void setComponent(Component component) {
    this.component = component;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name.trim();
  }

  public int getInd() {
    return ind;
  }

  public void setInd(int ind) {
    if(ind < 0)
      raise("invalid property index "+ind);
    this.ind = ind;
  }

  public Component eval()
  throws IntrospectionException{
    checkMandatory("component", component);
    checkMandatory("name", name);
    Component result = component;
    if(ind < 0){
      result = result.setter(name);
    }
    else{
      result = result.setter(name, ind);
    }
    final Object val = getVal();
    final Object defval = getDefault();
    if(val==null && defval==null){
      return result;
    }
   
    return result.bindArguments(new ParameterBinder(){
      public Creator bind(Signature src, int ind, Class type) {
        if(ind==0){
          Component arg = getVal(type);
          Component def = getDefault(type);
          Component result = arg;
          if(result==null){
            result = Components.useArgument(src, ind, type);
          }
          if(def!=null){
            result = Monad.mplus(result, def);
          }
          return result;
        }
        else
          return Components.useArgument(src, ind, type);
      }
    });
  }

}
TOP

Related Classes of jfun.yan.xml.nuts.SetterNut

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.