Repository navigation
Expand file tree
/
Copy pathXmlBeanModule.java
More file actions
227 lines (186 loc) · 5.48 KB
/
Copy pathXmlBeanModule.java
File metadata and controls
227 lines (186 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package example.xml;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Binder;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import safesax.Element;
import safesax.ElementListener;
import safesax.Parsers;
import safesax.RootElement;
import safesax.StartElementListener;
public class XmlBeanModule implements Module {
final URL xmlUrl;
Locator locator;
Binder originalBinder;
BeanBuilder beanBuilder;
public XmlBeanModule(URL xmlUrl) {
this.xmlUrl = xmlUrl;
}
public void configure(Binder binder) {
this.originalBinder = binder;
try {
RootElement beans = new RootElement("beans");
locator = beans.getLocator();
Element bean = beans.getChild("bean");
bean.setElementListener(new BeanListener());
Element property = bean.getChild("property");
property.setStartElementListener(new PropertyListener());
Reader in = new InputStreamReader(xmlUrl.openStream());
Parsers.parse(in, beans.getContentHandler());
}
catch (Exception e) {
originalBinder.addError(e);
}
}
/** Handles "binding" elements. */
class BeanListener implements ElementListener {
public void start(final Attributes attributes) {
Binder sourced = originalBinder.withSource(xmlSource());
String typeString = attributes.getValue("type");
// Make sure 'type' is present.
if (typeString == null) {
sourced.addError("Missing 'type' attribute.");
return;
}
// Resolve 'type'.
Class<?> type;
try {
type = Class.forName(typeString);
}
catch (ClassNotFoundException e) {
sourced.addError(e);
return;
}
// Look for a no-arg constructor.
try {
type.getConstructor();
}
catch (NoSuchMethodException e) {
sourced.addError("%s doesn't have a no-arg constructor.");
return;
}
// Create a bean builder for the given type.
beanBuilder = new BeanBuilder(type);
}
public void end() {
if (beanBuilder != null) {
beanBuilder.build();
beanBuilder = null;
}
}
}
/** Handles "property" elements. */
class PropertyListener implements StartElementListener {
public void start(final Attributes attributes) {
Binder sourced = originalBinder.withSource(xmlSource());
if (beanBuilder == null) {
// We must have already run into an error.
return;
}
// Check for 'name'.
String name = attributes.getValue("name");
if (name == null) {
sourced.addError("Missing attribute name.");
return;
}
Class<?> type = beanBuilder.type;
// Find setter method for the given property name.
String setterName = "set" + capitalize(name);
Method setter = null;
for (Method method : type.getMethods()) {
if (method.getName().equals(setterName)) {
setter = method;
break;
}
}
if (setter == null) {
sourced.addError("%s.%s() not found.", type.getName(), setterName);
return;
}
// Validate number of parameters.
Type[] parameterTypes = setter.getGenericParameterTypes();
if (parameterTypes.length != 1) {
sourced.addError("%s.%s() must take one argument.",
setterName, type.getName());
return;
}
// Add property descriptor to builder.
Provider<?> provider = sourced.getProvider(Key.get(parameterTypes[0]));
beanBuilder.properties.add(
new Property(setter, provider));
}
}
static String capitalize(String s) {
return Character.toUpperCase(s.charAt(0)) +
s.substring(1);
}
static class Property {
final Method setter;
final Provider<?> provider;
Property(Method setter, Provider<?> provider) {
this.setter = setter;
this.provider = provider;
}
void setOn(Object o) {
try {
setter.invoke(o, provider.get());
}
catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
class BeanBuilder {
final List<Property> properties = new ArrayList<Property>();
final Class<?> type;
BeanBuilder(Class<?> type) {
this.type = type;
}
void build() {
addBinding(type);
}
<T> void addBinding(Class<T> type) {
originalBinder.withSource(xmlSource())
.bind(type).toProvider(new BeanProvider<T>(type, properties));
}
}
static class BeanProvider<T> implements Provider<T> {
final Class<T> type;
final List<Property> properties;
BeanProvider(Class<T> type, List<Property> properties) {
this.type = type;
this.properties = properties;
}
public T get() {
try {
T t = type.newInstance();
for (Property property : properties) {
property.setOn(t);
}
return t;
}
catch (InstantiationException e) {
throw new RuntimeException(e);
}
catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
Object xmlSource() {
return xmlUrl + ":" + locator.getLineNumber();
}
}