KickJava   Java API By Example, From Geeks To Geeks.

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


1 package jfun.yan.xml.nuts;
2
3 import jfun.util.Misc;
4 import jfun.yan.Component;
5 import jfun.yan.Components;
6 import jfun.yan.Creator;
7 import jfun.yan.util.ReflectionUtil;
8 import jfun.yan.xml.NutsUtils;
9 import jfun.yan.xml.nut.Nut;
10
11
12 /**
13  * Super class for any Nut that supports the following attributes: <br>
14  * type - the type of the value. <br>
15  * val - the value. <br>
16  * default - the default value if resolution of <code>val</code> failed.
17  * <p>
18  * It also optionally support a sub-element, which can also be used as the value.
19  * </p>
20  * <p>
21  * @author Ben Yu
22  * Nov 9, 2005 11:47:42 PM
23  */

24 public class LiteralNut extends Nut {
25   private Object JavaDoc val;
26   private Object JavaDoc defaultval;
27   private Class JavaDoc type;
28   
29   public Class JavaDoc getType() {
30     return type;
31   }
32   public void setType(Class JavaDoc type) {
33     this.type = type;
34   }
35   public void setDefault(Object JavaDoc val){
36     this.defaultval = val;
37   }
38   public void setVal(Object JavaDoc val) {
39     this.val = val;
40   }
41   /*
42   public void set(Object[] vals){
43     checkSingleChild(vals);
44     checkDuplicate("val", val);
45     val = vals[0];
46   }*/

47   public void add(Object JavaDoc v){
48     checkDuplicate("val", this.val);
49     this.val = v;
50   }
51   public Component getVal(Class JavaDoc target_type){
52     return fromVal(target_type, val);
53   }
54   public Component getDefault(Class JavaDoc target_type){
55     return fromVal(target_type, defaultval);
56   }
57   private void checkType(Class JavaDoc target_type){
58     if(type!=null){
59       if(!ReflectionUtil.isAssignableFrom(target_type, type)){
60         raise("cannot convert " + Misc.getTypeName(type) + " to "
61             + Misc.getTypeName(target_type));
62       }
63     }
64   }
65   private Component fromVal(Class JavaDoc target_type, Object JavaDoc val){
66     checkType(target_type);
67     if(val == null) return null;
68     if(type != null){
69       target_type = type;
70     }
71     if(val instanceof Creator){
72       return Util.convert(this, target_type,
73           Components.adapt((Creator)val)
74       );
75     }
76     else{
77       return NutsUtils.asComponent(convert(target_type, val));
78     }
79     /*
80     try{
81       if(val instanceof String){
82         return Components.value(
83             convert(target_type, (String)val));
84       }
85       else if(val instanceof Component){
86         return (Component)val;
87       }
88       else{
89         return Components.value(val);
90       }
91     }
92     catch(Exception e){
93       throw raise(e);
94     }*/

95   }
96   private static String JavaDoc asText(Object JavaDoc v){
97     return v==null?null:v.toString();
98   }
99   public String JavaDoc getValueText(){
100     return asText(val);
101   }
102   public String JavaDoc getDefaultText(){
103     return asText(defaultval);
104   }
105   public Object JavaDoc getVal(){
106     return val;
107   }
108   public Object JavaDoc getDefault(){
109     return defaultval;
110   }
111   public void justify(){
112     checkMandatory("val", val, "default", defaultval);
113   }
114 }
115
Popular Tags