KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > xml > nut > NutDescriptor


1 package jfun.yan.xml.nut;
2
3 import java.lang.reflect.Method JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import jfun.util.Misc;
8 import jfun.yan.factory.Factory;
9
10
11 /**
12  * This class is the descriptor for a {@link Nut} class.
13  * <p>
14  * @author Ben Yu
15  * Nov 9, 2005 11:16:17 PM
16  */

17 public class NutDescriptor implements java.io.Serializable JavaDoc{
18   static final class CollectionDescriptor implements java.io.Serializable JavaDoc{
19     final Method JavaDoc setter;
20     final Class JavaDoc elem_type;
21     
22     CollectionDescriptor(Class JavaDoc elem_type, Method JavaDoc setter) {
23       this.elem_type = elem_type;
24       this.setter = setter;
25     }
26     public String JavaDoc toString(){
27       return Misc.getTypeName(elem_type)+"[]";
28     }
29   }
30   static final class RegularDescriptor implements java.io.Serializable JavaDoc{
31     final Map JavaDoc sub_descriptors;
32
33     RegularDescriptor(Map JavaDoc sub_descriptors) {
34       this.sub_descriptors = sub_descriptors;
35     }
36     public String JavaDoc toString(){
37       return sub_descriptors.toString();
38     }
39   }
40   private final Class JavaDoc type;
41   private Map JavaDoc props;
42   private boolean collection = false;
43   private CollectionDescriptor cdesc;
44   private RegularDescriptor rdesc;
45   private Evaluator evaluator;
46   private final Map JavaDoc adders = new HashMap JavaDoc();
47   private final Map JavaDoc addertypes = new HashMap JavaDoc();
48   private MethodSuite anonymous_adder_suite;
49   void setAdderSuite(MethodSuite suite){
50     this.anonymous_adder_suite = suite;
51   }
52   void putAdder(String JavaDoc name, Method JavaDoc mtd){
53     adders.put(name, mtd);
54     addertypes.put(name, mtd.getParameterTypes()[0]);
55   }
56   /**
57    * To get the adder method for a given name.
58    * @param name the name of the adder.
59    * @return the adder method. null is returned if not found.
60    */

61   public Method JavaDoc getAdder(String JavaDoc name){
62     return (Method JavaDoc)adders.get(name);
63   }
64   
65   /**
66    * Get the adder method for adding sub-elements anonymously.
67    * @param arg the argument to be added.
68    * @return the method.
69    * @throws IllegalArgumentException if a method cannot be found for the given argument
70    * or if ambiguity happens.
71    */

72   public Method JavaDoc getAnonymousAdder(Object JavaDoc arg){
73     return anonymous_adder_suite.getMethod(arg);
74   }
75   /**
76    * To get the parameter type for a given adder method.
77    * @param name the name of the adder.
78    * @return the parameter type. null is returned if not found.
79    */

80   public Class JavaDoc getAdderType(String JavaDoc name){
81     return (Class JavaDoc)addertypes.get(name);
82   }
83   public String JavaDoc toString(){
84     return Misc.getTypeName(type);
85   }
86   NutDescriptor(Class JavaDoc type) {
87     this.type = type;
88   }
89
90   CollectionDescriptor getCollectionDescriptor() {
91     return cdesc;
92   }
93   /**
94    * Is this a collection nut?
95    * <p>
96    * A collection nut is one that has a <code>set</code> with
97    * an array parameter.
98    * </p>
99    */

100   public boolean isCollectionNut() {
101     return collection;
102   }
103   /**
104    * Get the Evaluator object that evaluates a Nut object.
105    */

106   public Evaluator getEvaluator() {
107     return evaluator;
108   }
109
110   /**
111    * Get the Nut type.
112    */

113   public Class JavaDoc getType() {
114     return type;
115   }
116   /**
117    * Get a map of the property descriptors for this type.
118    */

119   public Map JavaDoc getPropertyDescriptors(){
120     return props;
121   }
122   /**
123    * Get the NutDescriptor object for a given sub-element.
124    * @param name the name of the sub-element.
125    * @return the NutDescriptor. null if not found.
126    */

127   public NutDescriptor getSubDescriptor(String JavaDoc name){
128     return (NutDescriptor)rdesc.sub_descriptors.get(name);
129   }
130   /**
131    * If the Nut is a collection nut, get the method corresponding
132    * to the <code>set</code> method.
133    * @return the setter method.
134    */

135   public Method JavaDoc getSetter(){
136     return cdesc.setter;
137   }
138   /**
139    * If the Nut is a collection nut, get the element type of the
140    * array parameter for the <code>set</code> method.
141    * For example, <code>getSetterElementType()</code> returns String
142    * if the type has a public <code>void set(String[])</code> method.
143    * @return the element type.
144    */

145   public Class JavaDoc getSetterElementType(){
146     return cdesc.elem_type;
147   }
148   void setCollectionDescriptor(CollectionDescriptor cdesc) {
149     this.cdesc = cdesc;
150     this.collection = true;
151   }
152
153   void setEvaluator(Evaluator evaluator) {
154     this.evaluator = evaluator;
155   }
156   void setPropertyDescriptors(Map JavaDoc props) {
157     this.props = props;
158   }
159   void setRegularDescriptor(RegularDescriptor rdesc) {
160     this.rdesc = rdesc;
161     this.collection = false;
162   }
163   public boolean equals(Object JavaDoc obj) {
164     if(obj instanceof NutDescriptor){
165       final NutDescriptor other = (NutDescriptor)obj;
166       return type.equals(other.type);
167     }
168     else return false;
169   }
170   public int hashCode() {
171     return type.hashCode();
172   }
173   private Factory factory = null;
174   /**
175    * To create a Nut object represented by this descriptor.
176    * @return the nut object.
177    * @throws IllegalAccessException if access is denied.
178    * @throws InstantiationException if instantiation failed.
179    */

180   public synchronized Object JavaDoc createNut()
181   throws IllegalAccessException JavaDoc, InstantiationException JavaDoc{
182     if(factory == null){
183       return type.newInstance();
184     }
185     else{
186       return factory.create();
187     }
188   }
189   /**
190    * To set the factory object that is responsible for
191    * creating the nut instance instead of invoking the default
192    * constructor.
193    * @param factory the factory object.
194    * Setting it to null means that the default constructor is used.
195    */

196   public synchronized void setFactory(Factory factory){
197     this.factory = factory;
198   }
199 }
200
Popular Tags