1 package org.oddjob.designer.arooa; 2 3 import java.lang.reflect.Method ; 4 import java.util.ArrayList ; 5 import java.util.Hashtable ; 6 import java.util.Iterator ; 7 import java.util.LinkedHashMap ; 8 import java.util.List ; 9 import java.util.Map ; 10 11 import org.oddjob.arooa.reflect.IntrospectionHelper; 12 import org.oddjob.designer.model.DesignAttribute; 13 import org.oddjob.designer.model.DesignElement; 14 import org.xml.sax.Attributes ; 15 import org.xml.sax.helpers.AttributesImpl ; 16 17 35 public final class DesignIH { 36 37 private final List getterMethods = new ArrayList (); 38 private final Map attributeTypes = new LinkedHashMap (); 39 private Method elementsMethod; 40 private final Map elementsMethods = new LinkedHashMap (); 41 private final Map elementMethods = new LinkedHashMap (); 42 private Method textMethod = null; 43 private Method unknownInMethod = null; 44 private Method unknownOutMethod = null; 45 46 49 private Class bean; 50 51 54 private static Hashtable helpers = new Hashtable (); 55 56 private DesignIH(final Class bean) { 57 this.bean = bean; 58 59 Method [] methods = bean.getMethods(); 60 for (int i = 0; i < methods.length; i++) { 61 final Method m = methods[i]; 62 String name = m.getName(); 63 Class returnType = m.getReturnType(); 64 Class [] args = m.getParameterTypes(); 65 66 if ("text".equals(name) 67 && String .class.equals(returnType) 68 && args.length == 0) { 69 textMethod = m; 70 } 71 else if ("unknown".equals(name) 72 && String .class.equals(returnType) 73 && args.length == 0) { 74 unknownOutMethod = m; 75 } 76 else if ("unknown".equals(name) 77 && java.lang.Void.TYPE.equals(returnType) 78 && args.length == 1 79 && String .class.equals(args[0])) { 80 unknownInMethod = m; 81 } 82 else if (name.startsWith("get") 83 && args.length == 0) { 84 String rest = getPropertyName(name, "get"); 85 if ("class".equals(rest)) { 86 continue; 87 } 88 attributeTypes.put(rest, returnType); 89 getterMethods.add(m); 90 } 91 else if (name.startsWith("elements") 92 && returnType.isArray() 93 && args.length == 0) { 94 String rest = getPropertyName(name, "elements"); 95 if (rest.length() > 0) { 96 elementsMethods.put(rest, m); 97 } 98 else { 99 elementsMethod = m; 100 } 101 } 102 else if (name.startsWith("element") 103 && !java.lang.Void.TYPE.equals(returnType) 104 && args.length == 0) { 105 String rest = getPropertyName(name, "element"); 106 if (rest.length() > 0) { 107 elementMethods.put(rest, m); 108 } 109 } 110 } } 112 113 114 124 public static synchronized DesignIH getHelper(Class c) { 125 DesignIH ih = (DesignIH) helpers.get(c); 126 if (ih == null) { 127 ih = new DesignIH(c); 128 helpers.put(c, ih); 129 } 130 return ih; 131 } 132 133 137 public Attributes attributes(Object object) { 138 AttributesImpl atts = new AttributesImpl (); 139 for (Iterator it = getterMethods.iterator(); it.hasNext(); ) { 140 Method m = (Method ) it.next(); 141 String propName = getPropertyName(m.getName(), "get"); 142 Class type = (Class ) attributeTypes.get(propName); 143 Object value = null; 144 try { 145 value = m.invoke(object, new Object [0]); 146 } 147 catch (Exception e) { 148 e.printStackTrace(); 149 } 150 if (value != null) { 151 if (DesignAttribute.class.isAssignableFrom(type)) { 152 if (((DesignAttribute)value).attribute() != null) { 153 atts.addAttribute("", propName, propName, null, ((DesignAttribute)value).attribute()); 154 } 155 } 156 else { 157 if (value.toString().length() > 0) { 158 atts.addAttribute("", propName, propName, null, value.toString()); 159 } 160 } 161 } 162 } 163 return atts; 164 } 165 166 169 public String text(Object object) { 170 if (textMethod == null) { 171 return null; 172 } 173 try { 174 return (String ) textMethod.invoke(object, new Object [0]); 175 } 176 catch (Exception e) { 177 e.printStackTrace(); 178 } 179 return null; 180 } 181 182 184 public String unknown(Object object) { 185 if (unknownOutMethod == null) { 186 return null; 187 } 188 try { 189 return (String ) unknownOutMethod.invoke(object, new Object [0]); 190 } 191 catch (Exception e) { 192 e.printStackTrace(); 193 } 194 return null; 195 } 196 197 199 public void unknown(Object object, String text) { 200 if (unknownInMethod == null) { 201 throw new NullPointerException ("No unkown"); 202 } 203 try { 204 unknownInMethod.invoke(object, new Object [] { text }); 205 } 206 catch (Exception e) { 207 e.printStackTrace(); 208 } 209 } 210 211 protected ElementWrapper elementWrapperFor(String name, Object object) { 212 if (object == null) { 213 return null; 214 } 215 if (object instanceof DesignElement) { 216 DesignIH elementDIH = DesignIH.getHelper(object.getClass()); 217 if (!elementDIH.hasDetailData((DesignElement) object)) { 218 return null; 219 } 220 } 221 return new ElementWrapper(name, object); 222 } 223 224 public ElementWrapper[] elements(Object object) { 225 try { 226 if (elementsMethod == null) { 227 List ews = new ArrayList (); 228 for (Iterator it = elementMethods.entrySet().iterator(); it.hasNext(); ) { 229 Map.Entry entry = (Map.Entry ) it.next(); 230 Object obj = (Object ) ((Method ) entry.getValue()).invoke(object, new Object [0]); 231 ElementWrapper wrapper = elementWrapperFor((String )entry.getKey(), obj); 232 if (wrapper != null) { 233 ews.add(new ElementWrapper((String )entry.getKey(), obj)); 234 } 235 } 236 for (Iterator it = elementsMethods.entrySet().iterator(); it.hasNext(); ) { 237 Map.Entry entry = (Map.Entry ) it.next(); 238 Object [] objs = (Object []) ((Method ) entry.getValue()).invoke(object, new Object [0]); 239 if (objs != null) { 240 for (int i = 0; i < objs.length; ++i) { 241 ElementWrapper wrapper = elementWrapperFor((String ) entry.getKey(), objs[i]); 242 if (wrapper != null) { 243 ews.add(new ElementWrapper((String ) entry.getKey(), objs[i])); 244 } 245 } 246 } 247 } 248 for (Iterator it = getterMethods.iterator(); it.hasNext(); ) { 249 Method m = (Method ) it.next(); 250 String propName = getPropertyName(m.getName(), "get"); 251 Class type = (Class ) attributeTypes.get(propName); 252 if (DesignElement.class.isAssignableFrom(type)) { 253 DesignElement value = null; 254 try { 255 value = (DesignElement) m.invoke(object, new Object [0]); 256 } 257 catch (Exception e) { 258 e.printStackTrace(); 259 } 260 if (value == null) { 261 continue; 262 } 263 ElementWrapper wrapper = elementWrapperFor(propName, value); 264 if (wrapper != null) { 265 ews.add(wrapper); 266 } 267 } 268 } 269 return (ElementWrapper[]) ews.toArray(new ElementWrapper[0]); 270 } else { 271 return (ElementWrapper[]) elementsMethod.invoke(object, new Object [0]); 272 } 273 } 274 catch (Exception e) { 275 e.printStackTrace(); 276 } 277 return null; 278 } 279 280 286 public boolean hasDetailData(DesignElement object) { 287 if (object instanceof DesignAttribute 290 && ((DesignAttribute) object).attribute() != null) { 291 return false; 292 } 293 ElementWrapper[] elements = elements(object); 294 if (elements.length > 0) { 295 return true; 296 } 297 Attributes attributes = attributes(object); 298 if (attributes.getLength() > 0) { 299 return true; 300 } 301 if (text(object) != null) { 302 return true; 303 } 304 return false; 305 } 306 307 320 private String getPropertyName(String methodName, String prefix) { 321 return IntrospectionHelper.getPropertyName(methodName, prefix); 322 } 323 324 } 325 | Popular Tags |