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 package org.exolab.castor.xml.schema.reader;
47
48
49 import org.exolab.castor.xml.AttributeSet;
50 import org.exolab.castor.xml.Namespaces;
51 import org.exolab.castor.xml.XMLException;
52 import org.exolab.castor.xml.schema.Annotation;
53 import org.exolab.castor.xml.schema.ComplexType;
54 import org.exolab.castor.xml.schema.ContentType;
55 import org.exolab.castor.xml.schema.SchemaContext;
56 import org.exolab.castor.xml.schema.SchemaNames;
57
58
59
60
61
62
63 public class ComplexContentUnmarshaller extends ComponentReader {
64
65
66
67
68
69
70
71
72 private ComponentReader unmarshaller;
73
74
75
76
77 private int depth = 0;
78
79
80
81
82 private ComplexType _complexType = null;
83
84 private boolean foundAnnotation = false;
85 private boolean foundExtension = false;
86 private boolean foundRestriction = false;
87
88
89
90
91
92
93
94
95
96
97
98 public ComplexContentUnmarshaller(
99 final SchemaContext schemaContext,
100 final ComplexType complexType,
101 final AttributeSet atts)
102 throws XMLException {
103 super(schemaContext);
104
105 _complexType = complexType;
106
107
108 String content = atts.getValue(SchemaNames.MIXED);
109
110 if (content != null) {
111 if (content.equals("true")) {
112 _complexType.setContentType(ContentType.valueOf("mixed"));
113 }
114 if (content.equals("false")) {
115 _complexType.setContentType(ContentType.valueOf("elementOnly"));
116 }
117 }
118
119 }
120
121
122
123
124
125
126
127
128
129
130
131 public String elementName() {
132 return SchemaNames.COMPLEX_CONTENT;
133 }
134
135
136
137
138
139 public Object getObject() {
140 return null;
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 public void startElement(String name, String namespace, AttributeSet atts,
157 Namespaces nsDecls)
158 throws XMLException
159 {
160
161 if (unmarshaller != null) {
162 unmarshaller.startElement(name, namespace, atts, nsDecls);
163 ++depth;
164 return;
165 }
166
167
168 if (SchemaNames.EXTENSION.equals(name)) {
169
170 if (foundExtension)
171 error("Only (1) 'extension' element may appear as a child "+
172 "of 'complexContent' elements.");
173
174 if (foundRestriction)
175 error("Both 'extension' and 'restriction' elements may not "+
176 "appear as children of the same complexContent "+
177 "definition.");
178
179 foundExtension = true;
180
181 ExtensionUnmarshaller extension =
182 new ExtensionUnmarshaller(getSchemaContext(), _complexType, atts);
183 unmarshaller = extension;
184 }
185
186 else if (SchemaNames.RESTRICTION.equals(name)) {
187
188 if (foundRestriction)
189 error("Only (1) 'restriction' element may appear as a child "+
190 "of 'complexContent' elements.");
191
192 if (foundExtension)
193 error("Both 'extension' and 'restriction' elements may not "+
194 "appear as children of the same complexContent "+
195 "definition.");
196
197 foundRestriction = true;
198 unmarshaller=
199 new ComplexContentRestrictionUnmarshaller(getSchemaContext(), _complexType, atts);
200 }
201
202 else if (name.equals(SchemaNames.ANNOTATION)) {
203 if (foundAnnotation)
204 error("Only (1) 'annotation' element may appear as a child "+
205 "of 'complexContent' elements.");
206
207 if (foundRestriction || foundExtension)
208 error("An 'annotation' may only appear as the first child "+
209 "of a 'complexContent' element.");
210
211 foundAnnotation = true;
212 unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
213 }
214 else illegalElement(name);
215
216 }
217
218
219
220
221
222
223
224
225 public void endElement(String name, String namespace)
226 throws XMLException
227 {
228
229
230 if ((unmarshaller != null) && (depth > 0)) {
231 unmarshaller.endElement(name, namespace);
232 --depth;
233 return;
234 }
235
236
237 unmarshaller.finish();
238
239
240 if (SchemaNames.ANNOTATION.equals(name)) {
241 Annotation ann = ((AnnotationUnmarshaller)unmarshaller).getAnnotation();
242 _complexType.addAnnotation(ann);
243 }
244
245 unmarshaller = null;
246 }
247
248 public void characters(char[] ch, int start, int length)
249 throws XMLException
250 {
251
252 if (unmarshaller != null) {
253 unmarshaller.characters(ch, start, length);
254 }
255 }
256
257 }