KickJava   Java API By Example, From Geeks To Geeks.

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


1 package jfun.yan.xml.nuts.optional;
2
3 import jfun.yan.xml.NutsFunction;
4
5 /**
6  * This class dynamically register components returned from the function body
7  * in each loop.
8  * <p>
9  * @author Ben Yu
10  * Dec 22, 2005 12:31:02 AM
11  */

12 public abstract class AbstractLoopRegisterNut extends AbstractRegisterNut {
13   private String JavaDoc prefix;
14   private String JavaDoc suffix;
15   private NutsFunction body;
16   protected NutsFunction getMandatoryBody(){
17     checkMandatory("body", body);
18     return body;
19   }
20   public NutsFunction getBody() {
21     return body;
22   }
23   public void setBody(NutsFunction body) {
24     this.body = body;
25   }
26   public String JavaDoc getPrefix() {
27     return prefix;
28   }
29   public void setPrefix(String JavaDoc prefix) {
30     this.prefix = prefix;
31   }
32   public String JavaDoc getSuffix() {
33     return suffix;
34   }
35   public void setSuffix(String JavaDoc postfix) {
36     this.suffix = postfix;
37   }
38   /*
39   public void set(NutsFunction[] funs){
40     checkSingleChild(funs);
41     checkDuplicate("body", body);
42     this.body = funs[0];
43   }*/

44   public void add(NutsFunction fun){
45     checkDuplicate("body", this.body);
46     this.body = fun;
47   }
48   /**
49    * Calculate the fully qualified name using the prefix and suffix.
50    * @param id the id.
51    * @return the fully qualified id.
52    */

53   protected Object JavaDoc calcFQN(Object JavaDoc id){
54     if(prefix==null){
55       if(suffix==null)
56         return id;
57       else
58         return ""+id+suffix;
59     }
60     else if(suffix==null){
61       return prefix+id;
62     }
63     else
64       return prefix+id+suffix;
65   }
66   /**
67    * Create an array containing prefix, suffix and another value.
68    * If prefix or suffix is not provided, it is omitted from the result array.
69    * @param val the value.
70    * @return the array.
71    */

72   protected Object JavaDoc[] createArgs(Object JavaDoc val){
73     if(prefix==null){
74       if(suffix==null){
75         return new Object JavaDoc[]{val};
76       }
77       else{
78         return new Object JavaDoc[]{suffix, val};
79       }
80     }
81     else{
82       if(suffix==null){
83         return new Object JavaDoc[]{prefix, val};
84       }
85       else{
86         return new Object JavaDoc[]{prefix, suffix, val};
87       }
88     }
89   }
90   /**
91    * Create an array containing prefix, suffix and two other values.
92    * If prefix or suffix is not specified, it is omitted from the array.
93    * @param val1 the first value.
94    * @param val2 the second value.
95    * @return the array.
96    */

97   protected Object JavaDoc[] createArgs(Object JavaDoc val1, Object JavaDoc val2){
98     if(prefix==null){
99       if(suffix==null){
100         return new Object JavaDoc[]{val1, val2};
101       }
102       else{
103         return new Object JavaDoc[]{suffix, val1, val2};
104       }
105     }
106     else{
107       if(suffix==null){
108         return new Object JavaDoc[]{prefix, val1, val2};
109       }
110       else{
111         return new Object JavaDoc[]{prefix, suffix, val1, val2};
112       }
113     }
114   }
115
116   /**
117    * Loop once by calling the body function and register the result. prefix, suffix and the value is passed
118    * to the funciton. If prefix or suffix is not specified, it is omitted from the argument list.
119    * @param id the id for the registration.
120    * @param val the value for the loop.
121    */

122   protected void loop(Object JavaDoc id, Object JavaDoc val){
123     registerValue(calcFQN(id), getMandatoryBody().call(createArgs(val)));
124   }
125   /**
126    * Loop once by calling the body function and register the result. prefix, suffix and the
127    * 2 values are passed to the funciton.
128    * If prefix or suffix is not specified, it is omitted from the argument list.
129    * @param id the id for the registration.
130    * @param val1 the first value.
131    * @param val2 the second value.
132    */

133   protected void loop(Object JavaDoc id, Object JavaDoc val1, Object JavaDoc val2){
134     registerValue(calcFQN(id), getMandatoryBody().call(createArgs(val1, val2)));
135   }
136 }
137
Popular Tags