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