1 16 package com.ibatis.common.beans; 17 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.StringTokenizer ; 21 22 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 START_INDEX = "["; 38 private static final String END_INDEX = "]"; 39 40 protected GenericProbe() { 41 } 42 43 51 public Object getObject(Object object, String name) { 52 if (object instanceof org.w3c.dom.Document ) { 53 return DOM_PROBE.getObject(object, name); 54 } else if (object instanceof List ) { 55 return ((List )object).get(new Integer (name.substring(1,name.indexOf(END_INDEX))).intValue()); 56 } else { 57 return BEAN_PROBE.getObject(object, name); 58 } 59 } 60 61 69 public void setObject(Object object, String name, Object value) { 70 if (object instanceof org.w3c.dom.Document ) { 71 DOM_PROBE.setObject(object, name, value); 72 } else { 73 BEAN_PROBE.setObject(object, name, value); 74 } 75 } 76 77 84 public String [] getReadablePropertyNames(Object object) { 85 if (object instanceof org.w3c.dom.Document ) { 86 return DOM_PROBE.getReadablePropertyNames(object); 87 } else { 88 return BEAN_PROBE.getReadablePropertyNames(object); 89 } 90 } 91 92 99 public String [] getWriteablePropertyNames(Object object) { 100 if (object instanceof org.w3c.dom.Document ) { 101 return DOM_PROBE.getWriteablePropertyNames(object); 102 } else { 103 return BEAN_PROBE.getWriteablePropertyNames(object); 104 } 105 } 106 107 116 public Class getPropertyTypeForSetter(Object object, String name) { 117 if (object instanceof Class ) { 118 return getClassPropertyTypeForSetter((Class ) object, name); 119 } else if (object instanceof org.w3c.dom.Document ) { 120 return DOM_PROBE.getPropertyTypeForSetter(object, name); 121 } else { 122 return BEAN_PROBE.getPropertyTypeForSetter(object, name); 123 } 124 } 125 126 134 public Class getPropertyTypeForGetter(Object object, String name) { 135 if (object instanceof Class ) { 136 return getClassPropertyTypeForGetter((Class ) object, name); 137 } else if (object instanceof org.w3c.dom.Document ) { 138 return DOM_PROBE.getPropertyTypeForGetter(object, name); 139 } else { 140 return BEAN_PROBE.getPropertyTypeForGetter(object, name); 141 } 142 } 143 144 152 public boolean hasWritableProperty(Object object, String propertyName) { 153 if (object instanceof org.w3c.dom.Document ) { 154 return DOM_PROBE.hasWritableProperty(object, propertyName); 155 } else { 156 return BEAN_PROBE.hasWritableProperty(object, propertyName); 157 } 158 } 159 160 168 public boolean hasReadableProperty(Object object, String propertyName) { 169 if (object instanceof org.w3c.dom.Document ) { 170 return DOM_PROBE.hasReadableProperty(object, propertyName); 171 } else { 172 return BEAN_PROBE.hasReadableProperty(object, propertyName); 173 } 174 } 175 176 protected void setProperty(Object object, String property, Object value) { 177 if (object instanceof org.w3c.dom.Document ) { 178 DOM_PROBE.setProperty(object, property, value); 179 } else { 180 BEAN_PROBE.setProperty(object, property, value); 181 } 182 } 183 184 protected Object getProperty(Object object, String property) { 185 if (object instanceof org.w3c.dom.Document ) { 186 return DOM_PROBE.getProperty(object, property); 187 } else { 188 return BEAN_PROBE.getProperty(object, property); 189 } 190 } 191 192 private Class getClassPropertyTypeForGetter(Class type, String name) { 193 194 if (name.indexOf('.') > -1) { 195 StringTokenizer parser = new StringTokenizer (name, "."); 196 while (parser.hasMoreTokens()) { 197 name = parser.nextToken(); 198 if (Map .class.isAssignableFrom(type)) { 199 type = Object .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 219 private Class getClassPropertyTypeForSetter(Class type, String name) { 220 221 if (name.indexOf('.') > -1) { 222 StringTokenizer parser = new StringTokenizer (name, "."); 223 while (parser.hasMoreTokens()) { 224 name = parser.nextToken(); 225 if (Map .class.isAssignableFrom(type)) { 226 type = Object .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 |