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.Facet;
44 import org.exolab.castor.xml.schema.Schema;
45 import org.exolab.castor.xml.schema.SchemaContext;
46 import org.exolab.castor.xml.schema.SchemaNames;
47 import org.exolab.castor.xml.schema.SimpleType;
48 import org.exolab.castor.xml.schema.Structure;
49 import org.exolab.castor.xml.schema.XMLType;
50
51
52
53
54
55
56
57 public class SimpleTypeRestrictionUnmarshaller extends ComponentReader {
58
59
60
61
62
63
64
65
66
67 private ComponentReader unmarshaller;
68
69
70
71
72 private int depth = 0;
73
74
75
76
77 private SimpleTypeDefinition _typeDefinition = null;
78
79 private Schema _schema = null;
80 private boolean foundAnnotation = false;
81 private boolean foundSimpleType = false;
82 private boolean foundFacets = false;
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public SimpleTypeRestrictionUnmarshaller(final SchemaContext schemaContext,
101 final SimpleTypeDefinition typeDefinition, final AttributeSet atts) throws XMLException {
102 super(schemaContext);
103
104 _typeDefinition = typeDefinition;
105 _schema = typeDefinition.getSchema();
106
107
108 String base = atts.getValue(SchemaNames.BASE_ATTR);
109 if ((base != null) && (base.length() > 0)) {
110
111 XMLType baseType = _schema.getType(base);
112 if (baseType == null)
113 _typeDefinition.setBaseTypeName(base);
114 else if (baseType.getStructureType() == Structure.COMPLEX_TYPE) {
115 String err = "The base type of a simpleType cannot " + "be a complexType.";
116 throw new IllegalStateException(err);
117 } else
118 _typeDefinition.setBaseType((SimpleType) baseType);
119 }
120
121
122 }
123
124
125
126
127
128
129
130
131
132
133 public String elementName() {
134 return SchemaNames.RESTRICTION;
135 }
136
137
138
139
140
141
142 public Object getObject() {
143 return null;
144 }
145
146
147
148
149
150
151
152
153
154
155
156 public void startElement(String name, String namespace, AttributeSet atts, Namespaces nsDecls)
157 throws XMLException {
158
159 if (unmarshaller != null) {
160 unmarshaller.startElement(name, namespace, atts, nsDecls);
161 ++depth;
162 return;
163 }
164
165
166
167 if (name.equals(SchemaNames.ANNOTATION)) {
168
169 if (foundFacets || foundSimpleType)
170 error("An annotation must appear as the first child " + "of 'restriction' elements.");
171
172 if (foundAnnotation)
173 error("Only one (1) annotation may appear as a child of " + "'restriction' elements.");
174
175 foundAnnotation = true;
176 unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts);
177 } else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
178 if (foundSimpleType)
179 error("Only one (1) 'simpleType' may appear as a child of " + "'restriction' elements.");
180
181 if (foundFacets)
182 error("A 'simpleType', as a child of 'restriction' "
183 + "elements, must appear before any facets.");
184
185 foundSimpleType = true;
186 unmarshaller = new SimpleTypeUnmarshaller(getSchemaContext(), _schema, atts);
187
188 } else if (FacetUnmarshaller.isFacet(name)) {
189 foundFacets = true;
190 unmarshaller = new FacetUnmarshaller(getSchemaContext(), name, atts);
191 } else
192 illegalElement(name);
193
194 unmarshaller.setDocumentLocator(getDocumentLocator());
195 }
196
197
198
199
200
201
202
203
204 public void endElement(String name, String namespace) throws XMLException {
205
206
207 if ((unmarshaller != null) && (depth > 0)) {
208 unmarshaller.endElement(name, namespace);
209 --depth;
210 return;
211 }
212
213
214 unmarshaller.finish();
215
216
217 if (SchemaNames.ANNOTATION.equals(name)) {
218 Annotation ann = ((AnnotationUnmarshaller) unmarshaller).getAnnotation();
219 _typeDefinition.setAnnotation(ann);
220 } else if (SchemaNames.SIMPLE_TYPE.equals(name)) {
221 SimpleType type = (SimpleType) unmarshaller.getObject();
222 _typeDefinition.setBaseType(type);
223 } else {
224 Facet facet = (Facet) unmarshaller.getObject();
225 facet.setOwningType((SimpleType) _typeDefinition.getBaseType());
226 _typeDefinition.addFacet(facet);
227 }
228
229
230 unmarshaller = null;
231 }
232
233 public void characters(char[] ch, int start, int length) throws XMLException {
234
235 if (unmarshaller != null) {
236 unmarshaller.characters(ch, start, length);
237 }
238 }
239
240 }