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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tuscany.sca.databinding.javabeans;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;
import org.apache.tuscany.sca.databinding.PullTransformer;
import org.apache.tuscany.sca.databinding.TransformationContext;
import org.apache.tuscany.sca.databinding.impl.BaseTransformer;
import org.apache.tuscany.sca.databinding.impl.SimpleTypeMapperImpl;
import org.apache.tuscany.sca.interfacedef.DataType;
import org.apache.tuscany.sca.interfacedef.util.XMLType;
/**
* Transformer to convert data from a JavaBean object to xml
*/
public abstract class JavaBean2XMLTransformer<T> extends BaseTransformer<Object, T> implements
PullTransformer<Object, T> {
public static final String GET = "get";
public static final String PREFIX = "n";
public static final String PERIOD = ".";
public static final String FWD_SLASH = "/";
public static final String HTTP = "http://";
private static int prefixCount = 1;
protected SimpleTypeMapperImpl mapper;
public JavaBean2XMLTransformer() {
this.mapper = new SimpleTypeMapperImpl();
}
public T transform(Object source, TransformationContext context) {
QName rootElement = null;
if (context != null) {
DataType type = context.getTargetDataType();
if (type != null) {
Object logical = type.getLogical();
if (logical instanceof XMLType) {
rootElement = ((XMLType)logical).getElementName();
}
}
}
//FIXME See how/if we still need to get the metadata here
//QName rootElementName = (QName)context.getTargetDataType().getMetadata("RootElementName");
//if (rootElementName == null) {
QName rootElementName = new QName(resolveRootElementName(source.getClass()));
//}
T root = createElement(rootElementName);
appendChildElements(root, resolveElementName(source.getClass()), source.getClass(), source, context);
return root;
}
private void appendChildElements(T parent,
QName elementName,
Class javaType,
Object javaObject,
TransformationContext context) {
T element = null;
if (javaObject != null) {
if (javaType.isPrimitive() || isSimpleJavaType(javaObject)) {
appendText(parent, mapper.toXMLLiteral(null, javaObject, context));
} else if (javaType.isArray()) {
int size = Array.getLength(javaObject);
for (int count = 0; count < size; ++count) {
Object item = Array.get(javaObject, count);
element = createElement(elementName);
appendChild(parent, element);
appendChildElements(element, elementName, javaType.getComponentType(), item, context);
}
} else {
Field[] javaFields = javaType.getFields();
for (Field aField : javaFields) {
try {
QName fieldElementName = new QName(aField.getName());
if (!aField.getType().isArray()) {
element = createElement(fieldElementName);
appendChild(parent, element);
appendChildElements(element,
fieldElementName,
aField.getType(),
aField.get(javaObject),
context);
} else {
appendChildElements(parent,
fieldElementName,
aField.getType(),
aField.get(javaObject),
context);
}
} catch (IllegalAccessException e) {
Java2XMLMapperException java2xmlEx = new Java2XMLMapperException(e);
java2xmlEx.setJavaFieldName(aField.getName());
java2xmlEx.setJavaType(javaType);
throw java2xmlEx;
}
}
Method[] methods = javaType.getMethods();
String fieldName = null;
for (Method aMethod : methods) {
try {
if (Modifier.isPublic(aMethod.getModifiers()) && aMethod.getName().startsWith(GET)
&& aMethod.getParameterTypes().length == 0
&& isMappedGetter(aMethod.getName())) {
fieldName = resolveFieldFromMethod(aMethod.getName());
try {
javaType.getField(fieldName);
} catch (NoSuchFieldException e) {
QName fieldElementName = new QName(fieldName);
if (aMethod.getReturnType().isArray()) {
appendChildElements(parent, fieldElementName, aMethod.getReturnType(), aMethod
.invoke(javaObject, new Object[0]), context);
} else {
element = createElement(fieldElementName);
appendChild(parent, element);
appendChildElements(element, fieldElementName, aMethod.getReturnType(), aMethod
.invoke(javaObject, new Object[0]), context);
}
}
}
} catch (IllegalAccessException e) {
Java2XMLMapperException java2xmlEx = new Java2XMLMapperException(e);
java2xmlEx.setJavaFieldName(fieldName);
java2xmlEx.setJavaType(javaType);
throw java2xmlEx;
} catch (InvocationTargetException e) {
Java2XMLMapperException java2xmlEx = new Java2XMLMapperException(e);
java2xmlEx.setJavaFieldName(fieldName);
java2xmlEx.setJavaType(javaType);
throw java2xmlEx;
}
}
}
}
}
/*
* Subclasses can override this method to prevent some getter methods
* from being mapped. The default implementation provided by this class
* maps all getter methods.
*/
protected boolean isMappedGetter(String methodName) {
return true;
}
@Override
public String getSourceDataBinding() {
return JavaBeansDataBinding.NAME;
}
@Override
public Class getSourceType() {
return Object.class;
}
private boolean isSimpleJavaType(Object javaObject) {
if (javaObject instanceof String) {
return true;
}
if (javaObject instanceof Byte || javaObject instanceof Character
|| javaObject instanceof Short
|| javaObject instanceof Integer
|| javaObject instanceof Long
|| javaObject instanceof Float
|| javaObject instanceof Double
|| javaObject instanceof Boolean) {
return true;
}
if (javaObject instanceof GregorianCalendar || javaObject instanceof Date
|| javaObject instanceof XMLGregorianCalendar
|| javaObject instanceof byte[]
|| javaObject instanceof QName) {
return true;
}
return false;
}
private String resolveRootElementName(Class javaType) {
if (javaType.isArray()) {
return javaType.getComponentType().getSimpleName() + "_collection";
} else {
return javaType.getSimpleName() + "_instance";
}
}
private QName resolveElementName(Class javaType) {
if (javaType.isArray()) {
return new QName(javaType.getComponentType().getSimpleName());
} else {
return new QName(javaType.getSimpleName());
}
}
private String resolveFieldFromMethod(String methodName) {
StringBuffer fieldName = new StringBuffer();
fieldName.append(Character.toLowerCase(methodName.charAt(GET.length())));
fieldName.append(methodName.substring(GET.length() + 1));
return fieldName.toString();
}
public String getNexPrefix() {
return PREFIX + prefixCount++;
}
@Override
public int getWeight() {
return JavaBeansDataBinding.HEAVY_WEIGHT;
}
/**
* Create an element with the given name
* @param qName
* @return
* @throws Java2XMLMapperException
*/
public abstract T createElement(QName qName) throws Java2XMLMapperException;
/**
* Create a text node and add it to the parent
* @param parentElement
* @param textData
* @throws Java2XMLMapperException
*/
public abstract void appendText(T parentElement, String textData) throws Java2XMLMapperException;
/**
* Add the child element to the parent
* @param parentElement
* @param childElement
* @throws Java2XMLMapperException
*/
public abstract void appendChild(T parentElement, T childElement) throws Java2XMLMapperException;
}
|