KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > common > beans > GenericProbe


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.common.beans;
17
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21
22 /**
23  * StaticBeanProbe provides methods that allow simple, reflective access to
24  * JavaBeans style properties. Methods are provided for all simple types as
25  * well as object types.
26  * <p/>
27  * Examples:
28  * <p/>
29  * StaticBeanProbe.setObject(object, propertyName, value);
30  * <P>
31  * Object value = StaticBeanProbe.getObject(object, propertyName);
32  */

33 public class GenericProbe extends BaseProbe {
34
35   private static final BaseProbe BEAN_PROBE = new ComplexBeanProbe();
36   private static final BaseProbe DOM_PROBE = new DomProbe();
37   private static final String JavaDoc START_INDEX = "[";
38   private static final String JavaDoc END_INDEX = "]";
39   
40   protected GenericProbe() {
41   }
42
43   /**
44    * Gets an object from a Map or bean
45    *
46    * @param object - the object to probe
47    * @param name - the name of the property (or map entry)
48    * @return The value of the property (or map entry)
49    * @see com.ibatis.common.beans.BaseProbe#getObject(java.lang.Object, java.lang.String)
50    */

51   public Object JavaDoc getObject(Object JavaDoc object, String JavaDoc name) {
52     if (object instanceof org.w3c.dom.Document JavaDoc) {
53       return DOM_PROBE.getObject(object, name);
54     } else if (object instanceof List JavaDoc) {
55       return ((List JavaDoc)object).get(new Integer JavaDoc(name.substring(1,name.indexOf(END_INDEX))).intValue());
56     } else {
57       return BEAN_PROBE.getObject(object, name);
58     }
59   }
60
61   /**
62    * Sets an object in a Map or bean
63    *
64    * @param object - the object to probe
65    * @param name - the name of the property (or map entry)
66    * @param value - the new value of the property (or map entry)
67    * @see com.ibatis.common.beans.BaseProbe#setObject(java.lang.Object, java.lang.String, java.lang.Object)
68    */

69   public void setObject(Object JavaDoc object, String JavaDoc name, Object JavaDoc value) {
70     if (object instanceof org.w3c.dom.Document JavaDoc) {
71       DOM_PROBE.setObject(object, name, value);
72     } else {
73       BEAN_PROBE.setObject(object, name, value);
74     }
75   }
76
77   /**
78    * Gets an array of the readable properties in a Map or JavaBean
79    *
80    * @param object - the object to get properties for
81    * @return The array of properties (or map entries)
82    * @see com.ibatis.common.beans.BaseProbe#getReadablePropertyNames(java.lang.Object)
83    */

84   public String JavaDoc[] getReadablePropertyNames(Object JavaDoc object) {
85     if (object instanceof org.w3c.dom.Document JavaDoc) {
86       return DOM_PROBE.getReadablePropertyNames(object);
87     } else {
88       return BEAN_PROBE.getReadablePropertyNames(object);
89     }
90   }
91
92   /**
93    * Gets an array of the writeable properties in a Map or JavaBean
94    *
95    * @param object - the object to get properties for
96    * @return The array of properties (or map entries)
97    * @see com.ibatis.common.beans.BaseProbe#getWriteablePropertyNames(java.lang.Object)
98    */

99   public String JavaDoc[] getWriteablePropertyNames(Object JavaDoc object) {
100     if (object instanceof org.w3c.dom.Document JavaDoc) {
101       return DOM_PROBE.getWriteablePropertyNames(object);
102     } else {
103       return BEAN_PROBE.getWriteablePropertyNames(object);
104     }
105   }
106
107   /**
108    * Returns the class that the setter expects to receive as a parameter when
109    * setting a property value.
110    *
111    * @param object - The class to check
112    * @param name - the name of the property
113    * @return The type of the property
114    * @see com.ibatis.common.beans.Probe#getPropertyTypeForSetter(java.lang.Object, java.lang.String)
115    */

116   public Class JavaDoc getPropertyTypeForSetter(Object JavaDoc object, String JavaDoc name) {
117     if (object instanceof Class JavaDoc) {
118       return getClassPropertyTypeForSetter((Class JavaDoc) object, name);
119     } else if (object instanceof org.w3c.dom.Document JavaDoc) {
120       return DOM_PROBE.getPropertyTypeForSetter(object, name);
121     } else {
122       return BEAN_PROBE.getPropertyTypeForSetter(object, name);
123     }
124   }
125
126   /**
127    * Returns the class that the getter will return when reading a property value.
128    *
129    * @param object The bean to check
130    * @param name The name of the property
131    * @return The type of the property
132    * @see com.ibatis.common.beans.Probe#getPropertyTypeForGetter(java.lang.Object, java.lang.String)
133    */

134   public Class JavaDoc getPropertyTypeForGetter(Object JavaDoc object, String JavaDoc name) {
135     if (object instanceof Class JavaDoc) {
136       return getClassPropertyTypeForGetter((Class JavaDoc) object, name);
137     } else if (object instanceof org.w3c.dom.Document JavaDoc) {
138       return DOM_PROBE.getPropertyTypeForGetter(object, name);
139     } else {
140       return BEAN_PROBE.getPropertyTypeForGetter(object, name);
141     }
142   }
143
144   /**
145    * Checks to see if an object has a writable property by a given name
146    *
147    * @param object The bean to check
148    * @param propertyName The property to check for
149    * @return True if the property exists and is writable
150    * @see com.ibatis.common.beans.Probe#hasWritableProperty(java.lang.Object, java.lang.String)
151    */

152   public boolean hasWritableProperty(Object JavaDoc object, String JavaDoc propertyName) {
153     if (object instanceof org.w3c.dom.Document JavaDoc) {
154       return DOM_PROBE.hasWritableProperty(object, propertyName);
155     } else {
156       return BEAN_PROBE.hasWritableProperty(object, propertyName);
157     }
158   }
159
160   /**
161    * Checks to see if a bean has a readable property by a given name
162    *
163    * @param object The bean to check
164    * @param propertyName The property to check for
165    * @return True if the property exists and is readable
166    * @see com.ibatis.common.beans.Probe#hasReadableProperty(java.lang.Object, java.lang.String)
167    */

168   public boolean hasReadableProperty(Object JavaDoc object, String JavaDoc propertyName) {
169     if (object instanceof org.w3c.dom.Document JavaDoc) {
170       return DOM_PROBE.hasReadableProperty(object, propertyName);
171     } else {
172       return BEAN_PROBE.hasReadableProperty(object, propertyName);
173     }
174   }
175
176   protected void setProperty(Object JavaDoc object, String JavaDoc property, Object JavaDoc value) {
177     if (object instanceof org.w3c.dom.Document JavaDoc) {
178       DOM_PROBE.setProperty(object, property, value);
179     } else {
180       BEAN_PROBE.setProperty(object, property, value);
181     }
182   }
183
184   protected Object JavaDoc getProperty(Object JavaDoc object, String JavaDoc property) {
185     if (object instanceof org.w3c.dom.Document JavaDoc) {
186       return DOM_PROBE.getProperty(object, property);
187     } else {
188       return BEAN_PROBE.getProperty(object, property);
189     }
190   }
191
192   private Class JavaDoc getClassPropertyTypeForGetter(Class JavaDoc type, String JavaDoc name) {
193
194     if (name.indexOf('.') > -1) {
195       StringTokenizer JavaDoc parser = new StringTokenizer JavaDoc(name, ".");
196       while (parser.hasMoreTokens()) {
197         name = parser.nextToken();
198         if (Map JavaDoc.class.isAssignableFrom(type)) {
199           type = Object JavaDoc.class;
200           break;
201         }
202         type = ClassInfo.getInstance(type).getGetterType(name);
203       }
204     } else {
205       type = ClassInfo.getInstance(type).getGetterType(name);
206     }
207
208     return type;
209   }
210
211   /**
212    * Returns the class that the setter expects to receive as a parameter when
213    * setting a property value.
214    *
215    * @param type The class to check
216    * @param name The name of the property
217    * @return The type of the property
218    */

219   private Class JavaDoc getClassPropertyTypeForSetter(Class JavaDoc type, String JavaDoc name) {
220
221     if (name.indexOf('.') > -1) {
222       StringTokenizer JavaDoc parser = new StringTokenizer JavaDoc(name, ".");
223       while (parser.hasMoreTokens()) {
224         name = parser.nextToken();
225         if (Map JavaDoc.class.isAssignableFrom(type)) {
226           type = Object JavaDoc.class;
227           break;
228         }
229         type = ClassInfo.getInstance(type).getSetterType(name);
230       }
231     } else {
232       type = ClassInfo.getInstance(type).getSetterType(name);
233     }
234
235     return type;
236   }
237
238
239 }
240
241
242
Popular Tags