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