KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > util > xml > HandlerClassInfo


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.util.xml;
17
18 import java.lang.reflect.Method JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * Retains parsed information about a particular schema clas.
24  */

25 public class HandlerClassInfo {
26   private static final HandlerMethod[] EMPTY_ARRAY_HANDLERMETHOD = new HandlerMethod[0];
27   private static Map JavaDoc sClassInfoMap = new HashMap JavaDoc();
28
29   public static synchronized HandlerClassInfo getClassInfo(Class JavaDoc c) {
30     if (sClassInfoMap.containsKey(c)) {
31       return (HandlerClassInfo) sClassInfoMap.get(c);
32     } else {
33       throw new RuntimeException JavaDoc("The schema class '" + c.getName()
34           + "' should have been registered prior to parsing");
35     }
36   }
37
38   public static synchronized void registerClass(Class JavaDoc c) {
39     if (sClassInfoMap.containsKey(c)) {
40       return;
41     }
42
43     // Put a guard null in so that recursive registration of the same
44
// class won't die.
45
//
46
sClassInfoMap.put(c, null);
47     HandlerClassInfo classInfo = createClassInfo(c);
48     sClassInfoMap.put(c, classInfo);
49   }
50
51   private static HandlerClassInfo createClassInfo(Class JavaDoc c) {
52     Map JavaDoc namedHandlerMethods = new HashMap JavaDoc();
53     try {
54       loadClassInfoRecursive(namedHandlerMethods, c);
55     } catch (Exception JavaDoc e) {
56       throw new RuntimeException JavaDoc("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 JavaDoc namedHandlerMethods, Class JavaDoc c) {
64     if (!Schema.class.isAssignableFrom(c)) {
65       // Have gone up as far as we can go.
66
//
67
return;
68     }
69
70     Method JavaDoc[] methods = c.getDeclaredMethods();
71     for (int i = 0, n = methods.length; i < n; ++i) {
72       Method JavaDoc method = methods[i];
73       HandlerMethod handlerMethod = HandlerMethod.tryCreate(method);
74       if (handlerMethod != null) {
75         // Put in the map, but only if that method isn't already there.
76
// (Allows inheritance where most-derived class wins).
77
//
78
String JavaDoc name = method.getName();
79         if (!namedHandlerMethods.containsKey(name)) {
80           namedHandlerMethods.put(name, handlerMethod);
81         }
82       }
83     }
84
85     // Recurse into superclass.
86
//
87
Class JavaDoc superclass = c.getSuperclass();
88     if (superclass != null) {
89       loadClassInfoRecursive(namedHandlerMethods, superclass);
90     }
91   }
92
93   private final Map JavaDoc namedHandlerMethods;
94
95   // Nobody else can create one.
96
private HandlerClassInfo(Map JavaDoc namedHandlerMethods) {
97     this.namedHandlerMethods = namedHandlerMethods;
98   }
99
100   public HandlerMethod getEndMethod(String JavaDoc localName) {
101     String JavaDoc 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 JavaDoc localName) {
111     String JavaDoc 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