KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xml > nuts > optional > BinaryNut


1 package jfun.yan.xml.nuts.optional;
2
3 import jfun.yan.Component;
4 import jfun.yan.xml.NutsUtils;
5 import jfun.yan.xml.nut.ComponentNut;
6
7 /**
8  * Super class for any tag that supports val1, val2, type1, type2 attributes.
9  * It also supports up to 2 sub-elements for these two values.
10  * <p>
11  * @author Ben Yu
12  * Nov 11, 2005 3:41:02 PM
13  */

14 public abstract class BinaryNut extends ComponentNut {
15   private Object JavaDoc val1;
16   private Object JavaDoc val2;
17   private Class JavaDoc type1;
18   private Class JavaDoc type2;
19   private boolean set1 = false;
20   private boolean set2 = false;
21
22   /**
23    * Is the val1 attribute set?
24    */

25   public boolean isVal1Set() {
26     return set1;
27   }
28
29   /**
30    * Is the val2 attribute set?
31    */

32   public boolean isVal2Set() {
33     return set2;
34   }
35
36   /**
37    * Get val1 as a component.
38    */

39   public Component getComponent1(){
40     return NutsUtils.asComponent(getVal1());
41   }
42   /**
43    * Get val2 as a component.
44    */

45   public Component getComponent2(){
46     return NutsUtils.asComponent(getVal2());
47   }
48   public Object JavaDoc getVal1() {
49     return convert(type1, val1);
50   }
51
52   public void setVal1(Object JavaDoc val1) {
53     this.val1 = val1;
54     this.set1 = true;
55   }
56   private void checkVal(String JavaDoc name, Object JavaDoc v, boolean flag){
57     if(flag){
58       raise("attribute " + name+" already set");
59     }
60   }
61   public void set(Object JavaDoc[] vals){
62     if(vals.length>2){
63       raise("at most 2 sub-elements are allowed.");
64     }
65     if(set1){
66       if(vals.length>1){
67         raise("attribute val1 already set");
68       }
69       else{
70         checkVal("val2", val2, set2);
71         val2 = vals[0];
72         set2 = true;
73       }
74     }
75     else if(set2){
76       if(vals.length>1){
77         raise("attribute val2 already set");
78       }
79       else{
80         val1 = vals[0];
81         set1 = true;
82       }
83     }
84     else{
85       if(vals.length>0){
86         val1 = vals[0];
87         set1 = true;
88       }
89       if(vals.length>1){
90         val2 = vals[1];
91         set2 = true;
92       }
93     }
94   }
95   public Object JavaDoc getVal2() {
96     return convert(type2, val2);
97   }
98
99   public void setVal2(Object JavaDoc val2) {
100     this.val2 = val2;
101     this.set2 = true;
102   }
103
104
105   public Class JavaDoc getType1() {
106     return type1;
107   }
108
109   public void setType1(Class JavaDoc type1) {
110     this.type1 = type1;
111   }
112
113   public Class JavaDoc getType2() {
114     return type2;
115   }
116
117   public void setType2(Class JavaDoc type2) {
118     this.type2 = type2;
119   }
120 /*
121   private Object convertValue(Class type, Object v){
122     if(ReflectionUtil.isInstance(type, v)) return v;
123     else if(String.class.equals(type)){
124       return ""+v;
125     }
126     else return convert(type, v);
127     if(ReflectionUtil.isInstance(type, v)) return v;
128     if(v instanceof String){
129       return convertLiteral(type, (String)v);
130     }
131     else if(String.class.equals(type)){
132       return ""+v;
133     }
134     else return v;
135   }*/

136
137 }
138
Popular Tags