KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xml > nuts > FieldNut


1 package jfun.yan.xml.nuts;
2
3
4 import java.lang.reflect.Field JavaDoc;
5
6 import jfun.yan.Component;
7 import jfun.yan.Components;
8 import jfun.yan.Functions;
9 import jfun.yan.util.ReflectionUtil;
10 import jfun.yan.xml.NutsUtils;
11
12
13 /**
14  * Nut class for <field> tag.
15  * <p>
16  * @author Ben Yu
17  * Nov 9, 2005 11:42:15 PM
18  */

19 public class FieldNut extends DelegatingNut {
20   private Class JavaDoc hostclass;
21   private String JavaDoc fldname;
22   private boolean private_access = false;
23   
24
25   public boolean isPrivate_access() {
26     return private_access;
27   }
28   public void setPrivate_access(boolean private_access) {
29     this.private_access = private_access;
30   }
31   public void setClass(Class JavaDoc type){
32     //checkDuplicate("component", cc);
33
this.hostclass = type;
34   }
35   public void setName(String JavaDoc name){
36     fldname = name.trim();
37   }
38   private static Component getField1(Class JavaDoc hostclass, Component c, String JavaDoc name,
39       boolean private_access){
40     if(c!=null){
41       if(hostclass!=null){
42         final Field JavaDoc fld = ReflectionUtil.getField(hostclass, name,
43             private_access);
44         return c.field(fld);
45       }
46       else
47         return c.field(name, private_access);
48     }
49     else{
50       return Components.fun(
51           Functions.static_field(hostclass, name, private_access));
52     }
53     
54   }
55   private static Component getFieldComponent(Class JavaDoc type, Component c,
56       String JavaDoc name, boolean private_access){
57     if(name.indexOf('.')<0)
58       return getField1(type, c, name.trim(), private_access);
59     final String JavaDoc[] parts = NutsUtils.split(name, ".");
60     c = getField1(type, c, parts[0], private_access);
61     for(int i=1; i<parts.length; i++){
62       c = c.field(parts[i].trim(), private_access);
63     }
64     return c;
65   }
66   public Component eval(){
67     final Component cc = getComponent();
68     checkMandatory("class", hostclass, "component", cc);
69     checkMandatory("name", fldname);
70     return getFieldComponent(hostclass, cc, fldname, private_access);
71     /*
72     if(hostclass != null){
73       if(cc!=null){
74         final Field fld = ReflectionUtil.getField(hostclass, fldname);
75         return cc.field(fld);
76       }
77       else{
78         return Components.static_field(hostclass, fldname);
79       }
80     }
81     else{
82       return cc.field(fldname);
83     }*/

84   }
85 }
86
Popular Tags