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.xml.util;
36
37 import org.castor.xml.BackwardCompatibilityContext;
38 import org.castor.xml.InternalContext;
39 import org.castor.xml.JavaNaming;
40 import org.castor.xml.XMLNaming;
41 import org.exolab.castor.mapping.ClassDescriptor;
42 import org.exolab.castor.mapping.FieldDescriptor;
43 import org.exolab.castor.mapping.MappingException;
44 import org.exolab.castor.mapping.loader.ClassDescriptorImpl;
45 import org.exolab.castor.xml.NodeType;
46 import org.exolab.castor.xml.XMLClassDescriptor;
47 import org.exolab.castor.xml.XMLFieldDescriptor;
48
49
50
51
52
53
54
55 public class XMLClassDescriptorAdapter extends XMLClassDescriptorImpl {
56 private InternalContext _internalContext;
57
58 public XMLClassDescriptorAdapter() {
59 super();
60 _internalContext = new BackwardCompatibilityContext();
61 }
62
63
64
65
66
67
68
69 public XMLClassDescriptorAdapter(final ClassDescriptor classDesc, final String xmlName)
70 throws org.exolab.castor.mapping.MappingException {
71 this(classDesc, xmlName, null);
72 }
73
74
75
76
77
78
79
80
81 public XMLClassDescriptorAdapter(final ClassDescriptor classDesc, String xmlName,
82 NodeType primitiveNodeType) throws MappingException {
83 this();
84
85 if (classDesc == null) {
86 String err =
87 "The ClassDescriptor argument to " + "XMLClassDescriptorAdapter must not be null.";
88 throw new IllegalArgumentException(err);
89 }
90
91 if (primitiveNodeType == null) {
92 primitiveNodeType = new BackwardCompatibilityContext().getPrimitiveNodeType();
93 }
94
95 if (primitiveNodeType == null) {
96 primitiveNodeType = NodeType.Attribute;
97 }
98
99 process(classDesc, primitiveNodeType);
100 setJavaClass(classDesc.getJavaClass());
101
102 if (xmlName == null) {
103 if (classDesc instanceof XMLClassDescriptor) {
104 xmlName = ((XMLClassDescriptor) classDesc).getXMLName();
105 } else {
106 String clsName = _internalContext.getJavaNaming().getClassName(classDesc.getJavaClass());
107 xmlName = _internalContext.getXMLNaming().toXMLName(clsName);
108 }
109 }
110 setXMLName(xmlName);
111 }
112
113
114
115
116
117
118 private void process(ClassDescriptor classDesc, NodeType primitiveNodeType)
119 throws MappingException {
120 if (classDesc instanceof XMLClassDescriptor) {
121
122 process((XMLClassDescriptor) classDesc);
123 return;
124 }
125
126
127 XMLClassDescriptor xmlClassDesc = null;
128 ClassDescriptor extendsDesc = classDesc.getExtends();
129 if (extendsDesc != null) {
130 if (extendsDesc instanceof XMLClassDescriptor) {
131 xmlClassDesc = (XMLClassDescriptor) extendsDesc;
132 } else {
133 xmlClassDesc = new XMLClassDescriptorAdapter(extendsDesc, null, primitiveNodeType);
134 }
135 }
136 setExtends(xmlClassDesc);
137
138 FieldDescriptor identity = classDesc.getIdentity();
139 FieldDescriptor[] fields = classDesc.getFields();
140
141
142
143
144
145
146
147
148 if (classDesc instanceof ClassDescriptorImpl) {
149 ClassDescriptorImpl cdImpl = (ClassDescriptorImpl) classDesc;
150 FieldDescriptor[] identities = cdImpl.getIdentities();
151 if ((identities != null) && (identities.length > 1)) {
152 int size = fields.length + identities.length;
153 FieldDescriptor[] newFields = new FieldDescriptor[size];
154 System.arraycopy(fields, 0, newFields, 0, fields.length);
155 System.arraycopy(identities, 0, newFields, fields.length, identities.length);
156 fields = newFields;
157 }
158 }
159
160
161 for (int i = 0; i < fields.length; i++) {
162 FieldDescriptor fieldDesc = fields[i];
163 if (fieldDesc == null) {
164 continue;
165 }
166 if (fieldDesc instanceof XMLFieldDescriptorImpl) {
167 if (identity == fieldDesc) {
168 setIdentity((XMLFieldDescriptorImpl) fieldDesc);
169 identity = null;
170 } else {
171 addFieldDescriptor((XMLFieldDescriptorImpl) fieldDesc);
172 }
173 } else {
174 String name = fieldDesc.getFieldName();
175 String xmlFieldName = _internalContext.getXMLNaming().toXMLName(name);
176
177 if (identity == fieldDesc) {
178 setIdentity(new XMLFieldDescriptorImpl(fieldDesc, xmlFieldName, NodeType.Attribute,
179 primitiveNodeType));
180 identity = null;
181 } else {
182 NodeType nodeType = NodeType.Element;
183 if (isPrimitive(fieldDesc.getFieldType())) {
184 nodeType = primitiveNodeType;
185 }
186
187 addFieldDescriptor(
188 new XMLFieldDescriptorImpl(fieldDesc, xmlFieldName, nodeType, primitiveNodeType));
189 }
190 }
191 }
192
193
194
195
196
197 if (identity != null) {
198 String xmlFieldName;
199 if (identity instanceof XMLFieldDescriptor) {
200 setIdentity((XMLFieldDescriptor) identity);
201 } else {
202 xmlFieldName = _internalContext.getXMLNaming().toXMLName(identity.getFieldName());
203 setIdentity(new XMLFieldDescriptorImpl(identity, xmlFieldName, NodeType.Attribute,
204 primitiveNodeType));
205 }
206 }
207 }
208
209
210
211
212
213
214 private void process(XMLClassDescriptor classDesc) {
215 FieldDescriptor identity = classDesc.getIdentity();
216 FieldDescriptor[] fields = classDesc.getFields();
217 for (int i = 0; i < fields.length; i++) {
218 if (identity == fields[i]) {
219 setIdentity((XMLFieldDescriptor) fields[i]);
220 identity = null;
221 } else {
222 addFieldDescriptor((XMLFieldDescriptor) fields[i]);
223 }
224 }
225
226
227 if (identity != null) {
228 setIdentity((XMLFieldDescriptor) identity);
229 }
230 setXMLName(classDesc.getXMLName());
231 setExtendsWithoutFlatten((XMLClassDescriptor) classDesc.getExtends());
232 }
233 }