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.descriptors;
36
37 import org.exolab.castor.builder.BuilderConfiguration;
38 import org.exolab.castor.builder.SGTypes;
39 import org.exolab.castor.xml.XMLConstants;
40 import org.exolab.javasource.JAnnotation;
41 import org.exolab.javasource.JAnnotationType;
42 import org.exolab.javasource.JClass;
43 import org.exolab.javasource.JConstructor;
44 import org.exolab.javasource.JField;
45 import org.exolab.javasource.JMethod;
46 import org.exolab.javasource.JPrimitiveType;
47 import org.exolab.javasource.JSourceCode;
48 import org.exolab.javasource.JType;
49
50
51
52
53
54
55
56 public final class DescriptorJClass extends JClass {
57
58
59 private static final String XMLCLASS_DESCRIPTOR_IMPL =
60 "org.exolab.castor.xml.util.XMLClassDescriptorImpl";
61
62 private static final String MAPPING_ACCESS_MODE = "org.exolab.castor.mapping.AccessMode";
63
64
65 private static final JClass FIELD_DESCRIPTOR_CLASS;
66
67
68 private static final JClass XML_FIELD_DESCRIPTOR_CLASS;
69
70 private static final JType TYPE_VALIDATOR_CLASS;
71
72 static {
73 FIELD_DESCRIPTOR_CLASS = new JClass("org.exolab.castor.mapping.FieldDescriptor");
74 XML_FIELD_DESCRIPTOR_CLASS = new JClass("org.exolab.castor.xml.XMLFieldDescriptor");
75 TYPE_VALIDATOR_CLASS = new JClass("org.exolab.castor.xml.TypeValidator");
76 }
77
78
79 private final JClass _type;
80
81 private final BuilderConfiguration _config;
82
83
84
85
86
87
88
89
90 public DescriptorJClass(final BuilderConfiguration config, final String className,
91 final JClass type) {
92 super(className);
93 this._config = config;
94 this._type = type;
95 init();
96 }
97
98
99
100
101
102
103
104
105 private void init() {
106
107
108 String superClass = null;
109 if (_config != null) {
110 superClass = _config.getProperty(BuilderConfiguration.Property.SUPER_CLASS, null);
111 }
112
113 boolean extended = false;
114
115 if (_type.getSuperClassQualifiedName() == null
116 || _type.getSuperClassQualifiedName().equals(superClass)) {
117 setSuperClass(XMLCLASS_DESCRIPTOR_IMPL);
118 } else {
119 if (_type.getSuperClass() == null) {
120 setSuperClass(null);
121 } else {
122 extended = true;
123 setSuperClass(getSuperClassName());
124 }
125 }
126 superClass = null;
127
128 if (_type.getPackageName() != null && _type.getPackageName().length() > 0) {
129 addImport(_type.getName());
130 }
131
132 addField(new JField(JType.BOOLEAN, "_elementDefinition"));
133
134 addField(new JField(SGTypes.STRING, "_nsPrefix"));
135 addField(new JField(SGTypes.STRING, "_nsURI"));
136 addField(new JField(SGTypes.STRING, "_xmlName"));
137
138
139 addField(new JField(XML_FIELD_DESCRIPTOR_CLASS, "_identity"));
140
141
142 addDefaultConstructor(extended);
143
144
145
146
147
148
149
150
151 addXMLClassDescriptorImplOverrides();
152
153
154
155
156
157 addXMLClassDescriptorOverrides();
158
159
160
161
162
163 addClassDescriptorOverrides(extended);
164 }
165
166
167
168
169
170
171
172 private String getSuperClassName() {
173 final String superClassName;
174 if (_type.getSuperClass().getPackageName() == null
175 || _type.getSuperClass().getPackageName().equals("")) {
176 if (getPackageName() == null) {
177
178 superClassName = _type.getSuperClass().getLocalName() + XMLConstants.DESCRIPTOR_SUFFIX;
179 } else {
180
181 superClassName = getPackageName() + "." + _type.getSuperClass().getLocalName()
182 + XMLConstants.DESCRIPTOR_SUFFIX;
183 }
184 } else {
185 superClassName =
186 _type.getSuperClass().getPackageName() + "." + XMLConstants.DESCRIPTOR_PACKAGE + "."
187 + _type.getSuperClass().getLocalName() + XMLConstants.DESCRIPTOR_SUFFIX;
188 }
189 return superClassName;
190 }
191
192
193
194
195
196
197 private void addDefaultConstructor(final boolean extended) {
198 addConstructor(createConstructor());
199 JConstructor cons = getConstructor(0);
200 JSourceCode jsc = cons.getSourceCode();
201 jsc.add("super();");
202
203 if (extended) {
204
205 jsc.add("setExtendsWithoutFlatten(");
206 jsc.append("new ");
207 jsc.append(getSuperClassQualifiedName());
208 jsc.append("());");
209 }
210 }
211
212
213
214
215 private void addXMLClassDescriptorImplOverrides() {
216
217 JMethod getElementDefinition = new JMethod("isElementDefinition", JType.BOOLEAN,
218 "true if XML schema definition of this Class is that of a global\n"
219 + "element or element with anonymous type definition.");
220 JSourceCode jsc = getElementDefinition.getSourceCode();
221 jsc.add("return _elementDefinition;");
222 addMethod(getElementDefinition);
223 }
224
225
226
227
228 private void addXMLClassDescriptorOverrides() {
229 JMethod method;
230 JSourceCode jsc;
231
232 method = new JMethod("getNameSpacePrefix", SGTypes.STRING,
233 "the namespace prefix to use when marshaling as XML.");
234
235 if (_config.useJava50()) {
236 method.addAnnotation(new JAnnotation(new JAnnotationType("Override")));
237 }
238
239 jsc = method.getSourceCode();
240 jsc.add("return _nsPrefix;");
241 addMethod(method);
242
243
244 method = new JMethod("getNameSpaceURI", SGTypes.STRING,
245 "the namespace URI used when marshaling and unmarshaling as XML.");
246
247 if (_config.useJava50()) {
248 method.addAnnotation(new JAnnotation(new JAnnotationType("Override")));
249 }
250
251 jsc = method.getSourceCode();
252 jsc.add("return _nsURI;");
253 addMethod(method);
254
255
256 method = new JMethod("getValidator", TYPE_VALIDATOR_CLASS,
257 "a specific validator for the class described" + " by this ClassDescriptor.");
258
259 if (_config.useJava50()) {
260 method.addAnnotation(new JAnnotation(new JAnnotationType("Override")));
261 }
262
263 jsc = method.getSourceCode();
264 jsc.add("return this;");
265 addMethod(method);
266
267
268 method =
269 new JMethod("getXMLName", SGTypes.STRING, "the XML Name for the Class being described.");
270
271 if (_config.useJava50()) {
272 method.addAnnotation(new JAnnotation(new JAnnotationType("Override")));
273 }
274
275 jsc = method.getSourceCode();
276 jsc.add("return _xmlName;");
277 addMethod(method);
278 }
279
280
281
282
283
284
285 private void addClassDescriptorOverrides(final boolean extended) {
286 JSourceCode jsc;
287
288
289 JClass amClass = new JClass(MAPPING_ACCESS_MODE);
290 JMethod getAccessMode =
291 new JMethod("getAccessMode", amClass, "the access mode specified for this class.");
292
293 if (_config.useJava50()) {
294 getAccessMode.addAnnotation(new JAnnotation(new JAnnotationType("Override")));
295 }
296
297 jsc = getAccessMode.getSourceCode();
298 jsc.add("return null;");
299 addMethod(getAccessMode);
300
301
302 JMethod getIdentity = new JMethod("getIdentity", FIELD_DESCRIPTOR_CLASS,
303 "the identity field, null if this class has no identity.");
304
305 if (_config.useJava50()) {
306 getIdentity.addAnnotation(new JAnnotation(new JAnnotationType("Override")));
307 }
308
309 jsc = getIdentity.getSourceCode();
310 if (extended) {
311 jsc.add("if (_identity == null) {");
312 jsc.indent();
313 jsc.add("return super.getIdentity();");
314 jsc.unindent();
315 jsc.add("}");
316 }
317 jsc.add("return _identity;");
318
319
320 addMethod(getIdentity, false);
321
322
323 JMethod getJavaClass = new JMethod("getJavaClass", SGTypes.CLASS,
324 "the Java class represented by this descriptor.");
325
326 if (_config.useJava50()) {
327 getJavaClass.addAnnotation(new JAnnotation(new JAnnotationType("Override")));
328 }
329
330 jsc = getJavaClass.getSourceCode();
331 jsc.add("return ");
332 jsc.append(classType(_type));
333 jsc.append(";");
334
335
336 addMethod(getJavaClass, false);
337 }
338
339
340
341
342
343
344
345 private static String classType(final JType jType) {
346 if (jType.isPrimitive()) {
347 JPrimitiveType primitive = (JPrimitiveType) jType;
348 return primitive.getWrapperName() + ".TYPE";
349 }
350 return jType.toString() + ".class";
351 }
352
353 }