1 package org.exolab.castor.xml.parsing;
2
3 import org.exolab.castor.types.AnyNode;
4 import org.exolab.castor.xml.util.SAX2ANY;
5 import org.xml.sax.AttributeList;
6 import org.xml.sax.Attributes;
7 import org.xml.sax.SAXException;
8
9 public class AnyNodeUnmarshalHandler {
10
11 private NamespaceHandling _namespaceHandling;
12
13
14
15
16 private SAX2ANY _anyUnmarshaller = null;
17
18
19
20
21 private int _depth = 0;
22
23
24
25
26 private ElementInfo _elemInfo = null;
27
28 public AnyNodeUnmarshalHandler(NamespaceHandling namespaceHandling) {
29 _namespaceHandling = namespaceHandling;
30 }
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public Object commonStartElement(String name, String namespace,
45 boolean wsPreserve) throws SAXException {
46
47 _anyUnmarshaller = new SAX2ANY(_namespaceHandling.getNamespaceStack(), wsPreserve);
48
49 if (_elemInfo._attributeList != null) {
50
51 startElement(_elemInfo._qName, _elemInfo._attributeList);
52 } else {
53
54 startElement(namespace, name, _elemInfo._qName,
55 _elemInfo._attributes);
56 }
57
58 _depth = 1;
59 return _anyUnmarshaller.getStartingNode();
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73 public void ignorableWhitespace(char[] ch, int start, int length)
74 throws SAXException {
75 _anyUnmarshaller.ignorableWhitespace(ch, start, length);
76 }
77
78
79
80
81
82
83
84
85
86 public void startElement(String name, AttributeList attList)
87 throws SAXException {
88 _depth++;
89 _anyUnmarshaller.startElement(name, attList);
90 return;
91 }
92
93
94
95
96
97
98
99
100
101
102
103 public void startElement(String namespaceURI, String localName,
104 String qName, Attributes atts) throws SAXException {
105 _depth++;
106 _anyUnmarshaller.startElement(namespaceURI, localName, qName, atts);
107 return;
108 }
109
110
111
112
113
114
115
116 public void endElement(String name) throws SAXException {
117 _anyUnmarshaller.endElement(name);
118 _depth--;
119 }
120
121
122
123
124
125
126
127
128
129 public void characters(char[] ch, int start, int length)
130 throws SAXException {
131 _anyUnmarshaller.characters(ch, start, length);
132 }
133
134
135
136
137
138
139
140
141 public void startPrefixMapping(String prefix, String uri)
142 throws SAXException {
143 _anyUnmarshaller.startPrefixMapping(prefix, uri);
144 }
145
146
147
148
149
150
151
152 public void endPrefixMapping(String prefix) throws SAXException {
153 _anyUnmarshaller.endPrefixMapping(prefix);
154 }
155
156
157
158
159
160
161 public boolean hasAnyUnmarshaller() {
162 return _anyUnmarshaller != null;
163 }
164
165
166
167
168
169
170 public boolean isStartingNode() {
171 return _depth == 0;
172 }
173
174
175
176
177
178
179 public AnyNode getStartingNode() {
180 AnyNode startingNode = _anyUnmarshaller.getStartingNode();
181 _anyUnmarshaller = null;
182 return startingNode;
183 }
184
185
186
187
188
189
190
191
192
193 public void preservePassedArguments(String name, Attributes atts) {
194 _elemInfo = setElementInfo(_elemInfo, name, atts);
195 }
196
197
198
199
200
201
202
203
204
205 public void preservePassedArguments(String name, AttributeList attList) {
206 _elemInfo = setElementInfo(_elemInfo, name, attList);
207 }
208
209
210
211
212
213
214
215
216
217
218
219
220 private ElementInfo setElementInfo(ElementInfo element, String name,
221 AttributeList attList) {
222 if (element == null) {
223 return new ElementInfo(name, attList);
224 }
225 element.clear();
226 element._qName = name;
227 element._attributeList = attList;
228 return element;
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242 private ElementInfo setElementInfo(ElementInfo element, String name,
243 Attributes attributes) {
244 if (element == null) {
245 return new ElementInfo(name, attributes);
246 }
247 element.clear();
248 element._qName = name;
249 element._attributes = attributes;
250 return element;
251 }
252
253
254
255
256
257 class ElementInfo {
258 String _qName = null;
259 Attributes _attributes = null;
260 AttributeList _attributeList = null;
261
262 ElementInfo() {
263 super();
264 }
265
266 ElementInfo(String qName, Attributes atts) {
267 super();
268 _qName = qName;
269 _attributes = atts;
270 }
271
272 ElementInfo(String qName, AttributeList atts) {
273 super();
274 _qName = qName;
275 _attributeList = atts;
276 }
277
278 void clear() {
279 _qName = null;
280 _attributes = null;
281 _attributeList = null;
282 }
283
284 }
285
286 }