|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| package org.dom4j.bean; |
|
11 |
| |
|
12 |
| import java.beans.BeanInfo; |
|
13 |
| import java.beans.IntrospectionException; |
|
14 |
| import java.beans.Introspector; |
|
15 |
| import java.beans.PropertyDescriptor; |
|
16 |
| import java.lang.reflect.Method; |
|
17 |
| import java.util.HashMap; |
|
18 |
| import java.util.Map; |
|
19 |
| |
|
20 |
| import org.dom4j.DocumentFactory; |
|
21 |
| import org.dom4j.QName; |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| public class BeanMetaData { |
|
29 |
| |
|
30 |
| |
|
31 |
| protected static final Object[] NULL_ARGS = {}; |
|
32 |
| |
|
33 |
| |
|
34 |
| private static Map singletonCache = new HashMap(); |
|
35 |
| |
|
36 |
| private static final DocumentFactory DOCUMENT_FACTORY = BeanDocumentFactory.getInstance(); |
|
37 |
| |
|
38 |
| |
|
39 |
| private Class beanClass; |
|
40 |
| |
|
41 |
| |
|
42 |
| private PropertyDescriptor[] propertyDescriptors; |
|
43 |
| |
|
44 |
| |
|
45 |
| private QName[] qNames; |
|
46 |
| |
|
47 |
| |
|
48 |
| private Method[] readMethods; |
|
49 |
| |
|
50 |
| |
|
51 |
| private Method[] writeMethods; |
|
52 |
| |
|
53 |
| |
|
54 |
| private Map nameMap = new HashMap(); |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
4
| public static BeanMetaData get(Class beanClass) {
|
|
60 |
4
| BeanMetaData answer = (BeanMetaData) singletonCache.get( beanClass );
|
|
61 |
4
| if ( answer == null ) {
|
|
62 |
2
| answer = new BeanMetaData( beanClass );
|
|
63 |
2
| singletonCache.put( beanClass, answer );
|
|
64 |
| } |
|
65 |
4
| return answer;
|
|
66 |
| } |
|
67 |
| |
|
68 |
2
| public BeanMetaData(Class beanClass) {
|
|
69 |
2
| this.beanClass = beanClass;
|
|
70 |
2
| if ( beanClass != null ) {
|
|
71 |
2
| try {
|
|
72 |
2
| BeanInfo beanInfo = Introspector.getBeanInfo( beanClass );
|
|
73 |
2
| propertyDescriptors = beanInfo.getPropertyDescriptors();
|
|
74 |
| } |
|
75 |
| catch (IntrospectionException e) { |
|
76 |
0
| handleException(e);
|
|
77 |
| } |
|
78 |
| } |
|
79 |
2
| if ( propertyDescriptors == null ) {
|
|
80 |
0
| propertyDescriptors = new PropertyDescriptor[0];
|
|
81 |
| } |
|
82 |
2
| int size = propertyDescriptors.length;
|
|
83 |
2
| qNames = new QName[size];
|
|
84 |
2
| readMethods = new Method[size];
|
|
85 |
2
| writeMethods = new Method[size];
|
|
86 |
2
| for ( int i = 0; i < size; i++ ) {
|
|
87 |
118
| PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
|
|
88 |
118
| String name = propertyDescriptor.getName();
|
|
89 |
118
| QName qName = DOCUMENT_FACTORY.createQName( name );
|
|
90 |
118
| qNames[i] = qName;
|
|
91 |
118
| readMethods[i] = propertyDescriptor.getReadMethod();
|
|
92 |
118
| writeMethods[i] = propertyDescriptor.getWriteMethod();
|
|
93 |
| |
|
94 |
118
| Integer index = new Integer(i);
|
|
95 |
118
| nameMap.put( name, index );
|
|
96 |
118
| nameMap.put( qName, index );
|
|
97 |
| } |
|
98 |
| } |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
4
| public int attributeCount() {
|
|
103 |
4
| return propertyDescriptors.length;
|
|
104 |
| } |
|
105 |
| |
|
106 |
0
| public BeanAttributeList createAttributeList(BeanElement parent) {
|
|
107 |
0
| return new BeanAttributeList( parent, this );
|
|
108 |
| } |
|
109 |
| |
|
110 |
0
| public QName getQName(int index) {
|
|
111 |
0
| return qNames[index];
|
|
112 |
| } |
|
113 |
| |
|
114 |
4
| public int getIndex(String name) {
|
|
115 |
4
| Integer index = (Integer) nameMap.get(name);
|
|
116 |
4
| return ( index != null ) ? index.intValue() : -1;
|
|
117 |
| } |
|
118 |
| |
|
119 |
0
| public int getIndex(QName qName) {
|
|
120 |
0
| Integer index = (Integer) nameMap.get(qName);
|
|
121 |
0
| return ( index != null ) ? index.intValue() : -1;
|
|
122 |
| } |
|
123 |
| |
|
124 |
0
| public Object getData(int index, Object bean) {
|
|
125 |
0
| try {
|
|
126 |
0
| Method method = readMethods[index];
|
|
127 |
0
| return method.invoke( bean, NULL_ARGS );
|
|
128 |
| } |
|
129 |
| catch (Exception e) { |
|
130 |
0
| handleException(e);
|
|
131 |
0
| return null;
|
|
132 |
| } |
|
133 |
| } |
|
134 |
| |
|
135 |
4
| public void setData(int index, Object bean, Object data) {
|
|
136 |
4
| try {
|
|
137 |
4
| Method method = writeMethods[index];
|
|
138 |
4
| Object[] args = { data };
|
|
139 |
4
| method.invoke( bean, args );
|
|
140 |
| } |
|
141 |
| catch (Exception e) { |
|
142 |
0
| handleException(e);
|
|
143 |
| } |
|
144 |
| } |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
0
| protected void handleException(Exception e) {
|
|
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 |
| |