1 16 package com.google.gwt.dev.util.xml; 17 18 import java.lang.reflect.Method ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 22 25 public class HandlerClassInfo { 26 private static final HandlerMethod[] EMPTY_ARRAY_HANDLERMETHOD = new HandlerMethod[0]; 27 private static Map sClassInfoMap = new HashMap (); 28 29 public static synchronized HandlerClassInfo getClassInfo(Class c) { 30 if (sClassInfoMap.containsKey(c)) { 31 return (HandlerClassInfo) sClassInfoMap.get(c); 32 } else { 33 throw new RuntimeException ("The schema class '" + c.getName() 34 + "' should have been registered prior to parsing"); 35 } 36 } 37 38 public static synchronized void registerClass(Class c) { 39 if (sClassInfoMap.containsKey(c)) { 40 return; 41 } 42 43 sClassInfoMap.put(c, null); 47 HandlerClassInfo classInfo = createClassInfo(c); 48 sClassInfoMap.put(c, classInfo); 49 } 50 51 private static HandlerClassInfo createClassInfo(Class c) { 52 Map namedHandlerMethods = new HashMap (); 53 try { 54 loadClassInfoRecursive(namedHandlerMethods, c); 55 } catch (Exception e) { 56 throw new RuntimeException ("Unable to use class '" + c.getName() 57 + "' as a handler", e); 58 } 59 HandlerClassInfo classInfo = new HandlerClassInfo(namedHandlerMethods); 60 return classInfo; 61 } 62 63 private static void loadClassInfoRecursive(Map namedHandlerMethods, Class c) { 64 if (!Schema.class.isAssignableFrom(c)) { 65 return; 68 } 69 70 Method [] methods = c.getDeclaredMethods(); 71 for (int i = 0, n = methods.length; i < n; ++i) { 72 Method method = methods[i]; 73 HandlerMethod handlerMethod = HandlerMethod.tryCreate(method); 74 if (handlerMethod != null) { 75 String name = method.getName(); 79 if (!namedHandlerMethods.containsKey(name)) { 80 namedHandlerMethods.put(name, handlerMethod); 81 } 82 } 83 } 84 85 Class superclass = c.getSuperclass(); 88 if (superclass != null) { 89 loadClassInfoRecursive(namedHandlerMethods, superclass); 90 } 91 } 92 93 private final Map namedHandlerMethods; 94 95 private HandlerClassInfo(Map namedHandlerMethods) { 97 this.namedHandlerMethods = namedHandlerMethods; 98 } 99 100 public HandlerMethod getEndMethod(String localName) { 101 String methodName = "__" + localName.replace('-', '_'); 102 return (HandlerMethod) namedHandlerMethods.get(methodName + "_end"); 103 } 104 105 public HandlerMethod[] getHandlerMethods() { 106 return (HandlerMethod[]) namedHandlerMethods.values().toArray( 107 EMPTY_ARRAY_HANDLERMETHOD); 108 } 109 110 public HandlerMethod getStartMethod(String localName) { 111 String methodName = "__" + localName.replace('-', '_'); 112 return (HandlerMethod) namedHandlerMethods.get(methodName + "_begin"); 113 } 114 115 public HandlerMethod getTextMethod() { 116 return (HandlerMethod) namedHandlerMethods.get("__text"); 117 } 118 } 119 | Popular Tags |