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 package org.exolab.castor.builder.factory;
46
47 import org.exolab.castor.builder.BuilderConfiguration;
48 import org.exolab.castor.builder.info.ClassInfo;
49 import org.exolab.castor.builder.info.CollectionInfo;
50 import org.exolab.castor.builder.info.FieldInfo;
51 import org.exolab.castor.builder.info.NodeType;
52 import org.exolab.castor.builder.info.nature.XMLInfoNature;
53 import org.exolab.castor.builder.types.XSType;
54 import org.exolab.castor.mapping.xml.BindXml;
55 import org.exolab.castor.mapping.xml.ClassChoice;
56 import org.exolab.castor.mapping.xml.ClassMapping;
57 import org.exolab.castor.mapping.xml.FieldMapping;
58 import org.exolab.castor.mapping.xml.MapTo;
59 import org.exolab.castor.mapping.xml.types.BindXmlNodeType;
60 import org.exolab.javasource.JClass;
61 import org.exolab.javasource.JType;
62
63
64
65
66
67
68
69 public final class MappingFileSourceFactory {
70
71
72
73
74
75
76 public MappingFileSourceFactory(final BuilderConfiguration config) {
77 if (config == null) {
78 String err = "The argument 'config' must not be null.";
79 throw new IllegalArgumentException(err);
80 }
81 }
82
83
84
85
86
87
88
89 public ClassMapping createMapping(final ClassInfo classInfo) {
90 JClass jClass = classInfo.getJClass();
91 String className = jClass.getName();
92
93 ClassMapping classMapping = new ClassMapping();
94 classMapping.setName(className);
95
96
97 MapTo mapTo = new MapTo();
98 classMapping.setMapTo(mapTo);
99
100 XMLInfoNature xmlNature = new XMLInfoNature(classInfo);
101
102 String nsPrefix = xmlNature.getNamespacePrefix();
103 if ((nsPrefix != null) && (nsPrefix.length() > 0)) {
104 mapTo.setNsPrefix(nsPrefix);
105 }
106
107
108 String nsURI = xmlNature.getNamespaceURI();
109 if ((nsURI != null) && (nsURI.length() > 0)) {
110 mapTo.setNsUri(nsURI);
111 }
112
113
114 mapTo.setXml(xmlNature.getNodeName());
115
116
117 mapTo.setElementDefinition(xmlNature.isElementDefinition());
118
119
120 if (xmlNature.isChoice()) {
121
122 }
123
124 boolean isAbstract = classInfo.isAbstract();
125 if (!isAbstract) {
126 isAbstract = jClass.getModifiers().isAbstract();
127 }
128 classInfo.setAbstract(isAbstract);
129
130
131
132 if (classInfo.getFieldCount() == 0) {
133 return classMapping;
134 }
135
136
137 if (classInfo.allowContent()) {
138 createFieldMapping(classMapping, classInfo.getTextField(), null);
139 }
140
141 ClassInfo base = classInfo.getBaseClass();
142 if (base != null) {
143 classMapping.setExtends(base.getJClass().getName());
144 }
145
146 FieldInfo[] atts = classInfo.getAttributeFields();
147
148
149
150
151
152 for (int i = 0; i < atts.length; i++) {
153 FieldInfo member = atts[i];
154
155 if (member.isTransient()) {
156 continue;
157 }
158
159
160 if (base != null && base.getAttributeField(xmlNature.getNodeName()) != null) {
161 continue;
162 }
163
164 createFieldMapping(classMapping, member, nsURI);
165 }
166
167
168
169
170
171 FieldInfo[] elements = classInfo.getElementFields();
172 for (int i = 0; i < elements.length; i++) {
173 FieldInfo member = elements[i];
174
175 if (member.isTransient()) {
176 continue;
177 }
178
179
180 if (base != null && base.getElementField(xmlNature.getNodeName()) != null) {
181 continue;
182 }
183
184 createFieldMapping(classMapping, member, nsURI);
185 }
186
187 return classMapping;
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201 private void createFieldMapping(final ClassMapping classMapping, final FieldInfo member,
202 final String nsURI) {
203 XMLInfoNature xmlNature = new XMLInfoNature(member);
204
205 XSType xsType = xmlNature.getSchemaType();
206
207 boolean any = false;
208 NodeType nodeType = xmlNature.getNodeType();
209 boolean isAttribute = (nodeType == NodeType.ATTRIBUTE);
210 boolean isText = (nodeType == NodeType.TEXT);
211
212
213 if (member.getName().equals("_anyObject")) {
214 any = true;
215 }
216
217
218 if (xsType.isCollection()) {
219 xsType = new XMLInfoNature(((CollectionInfo) member).getContent()).getSchemaType();
220 }
221
222
223 ClassChoice classChoice = classMapping.getClassChoice();
224 if (classChoice == null) {
225 classChoice = new ClassChoice();
226 classMapping.setClassChoice(classChoice);
227 }
228
229
230 FieldMapping fieldMap = new FieldMapping();
231 classChoice.addFieldMapping(fieldMap);
232 String fieldName = member.getName();
233 if (fieldName.charAt(0) == '_') {
234 fieldName = fieldName.substring(1);
235 }
236 fieldMap.setName(fieldName);
237 String className = getClassName(xsType.getJType());
238 if (className.equals("byte[]")) {
239 className = "bytes";
240 }
241 fieldMap.setType(className);
242
243 BindXml bindXml = new BindXml();
244 fieldMap.setBindXml(bindXml);
245
246 String nodeName = xmlNature.getNodeName();
247 if ((nodeName != null) && (!isText)) {
248 bindXml.setName(nodeName);
249 }
250
251 if (isAttribute) {
252 bindXml.setNode(BindXmlNodeType.ATTRIBUTE);
253 } else if (isText) {
254 bindXml.setNode(BindXmlNodeType.TEXT);
255 } else {
256 bindXml.setNode(BindXmlNodeType.ELEMENT);
257 }
258
259 switch (xsType.getType()) {
260 case XSType.IDREF_TYPE :
261 bindXml.setReference(true);
262 break;
263 case XSType.ID_TYPE :
264 classMapping.addIdentity(member.getName());
265 break;
266 case XSType.QNAME_TYPE :
267 bindXml.setType("QName");
268 default:
269 break;
270 }
271
272
273 fieldMap.setHandler(member.getXMLFieldHandler());
274
275
276 if (member.isContainer()) {
277 fieldMap.setContainer(true);
278 }
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295 if (xmlNature.isRequired()) {
296 fieldMap.setRequired(true);
297 }
298
299
300 if (member.isNillable()) {
301
302 }
303
304
305 if (any) {
306 bindXml.setMatches("*");
307 }
308
309
310
311
312 }
313
314
315
316
317
318
319
320
321
322 private static String getClassName(final JType jType) {
323
324
325 if (jType.isPrimitive()) {
326 return jType.getName();
327 }
328 return jType.toString();
329 }
330 }