package jfun.yan.xml.nuts;
import java.lang.reflect.Field;
import jfun.yan.Component;
import jfun.yan.Components;
import jfun.yan.Functions;
import jfun.yan.util.ReflectionUtil;
import jfun.yan.xml.NutsUtils;
/**
* Nut class for <field> tag.
* <p>
* @author Ben Yu
* Nov 9, 2005 11:42:15 PM
*/
public class FieldNut extends DelegatingNut {
private Class hostclass;
private String fldname;
private boolean private_access = false;
public boolean isPrivate_access() {
return private_access;
}
public void setPrivate_access(boolean private_access) {
this.private_access = private_access;
}
public void setClass(Class type){
//checkDuplicate("component", cc);
this.hostclass = type;
}
public void setName(String name){
fldname = name.trim();
}
private static Component getField1(Class hostclass, Component c, String name,
boolean private_access){
if(c!=null){
if(hostclass!=null){
final Field fld = ReflectionUtil.getField(hostclass, name,
private_access);
return c.field(fld);
}
else
return c.field(name, private_access);
}
else{
return Components.fun(
Functions.static_field(hostclass, name, private_access));
}
}
private static Component getFieldComponent(Class type, Component c,
String name, boolean private_access){
if(name.indexOf('.')<0)
return getField1(type, c, name.trim(), private_access);
final String[] parts = NutsUtils.split(name, ".");
c = getField1(type, c, parts[0], private_access);
for(int i=1; i<parts.length; i++){
c = c.field(parts[i].trim(), private_access);
}
return c;
}
public Component eval(){
final Component cc = getComponent();
checkMandatory("class", hostclass, "component", cc);
checkMandatory("name", fldname);
return getFieldComponent(hostclass, cc, fldname, private_access);
/*
if(hostclass != null){
if(cc!=null){
final Field fld = ReflectionUtil.getField(hostclass, fldname);
return cc.field(fld);
}
else{
return Components.static_field(hostclass, fldname);
}
}
else{
return cc.field(fldname);
}*/
}
}