summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/tags/2.0.1-RC1/modules/common-xml/src/main/java/org/apache/tuscany/sca/common/xml/stax/reader/XMLDocumentStreamReader.java
blob: a020a8a61e5d9f9c8662cf38d515e9d8b42efd08 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.sca.common.xml.stax.reader;

import java.util.NoSuchElementException;

import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.stream.Location;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/**
 * This class is derived from Apache Axis2 class
 * org.apache.axis2.util.StreamWrapper</a>. It's used wrap a XMLStreamReader to
 * create a XMLStreamReader representing a document and it will produce
 * START_DOCUMENT, END_DOCUMENT events.
 *
 * @version $Rev$ $Date$
 */
public class XMLDocumentStreamReader implements XMLStreamReader {
    private static final int STATE_COMPLETE_AT_NEXT = 2; // The wrapper
    // will produce
    // END_DOCUMENT

    private static final int STATE_COMPLETED = 3; // Done

    private static final int STATE_INIT = 0; // The wrapper will produce
    // START_DOCUMENT

    private static final int STATE_SWITCHED = 1; // The real reader will
    // produce events

    private XMLStreamReader realReader;
    private boolean fragment;
    private int level = 1;

    private int state = STATE_INIT;

    public XMLDocumentStreamReader(XMLStreamReader realReader) {
        if (realReader == null) {
            throw new UnsupportedOperationException("Reader cannot be null");
        }

        this.realReader = realReader;
        
        if (realReader instanceof XMLFragmentStreamReader) {
            ((XMLFragmentStreamReader)realReader).init();
        }

        // If the real reader is positioned at START_DOCUMENT, always use
        // the real reader
        if (realReader.getEventType() == START_DOCUMENT) {
            fragment = false;
            state = STATE_SWITCHED;
        }
    }

    public void close() throws XMLStreamException {
        realReader.close();
    }

    public int getAttributeCount() {
        if (isDelegating()) {
            return realReader.getAttributeCount();
        } else {
            throw new IllegalStateException();
        }
    }

    public String getAttributeLocalName(int i) {
        if (isDelegating()) {
            return realReader.getAttributeLocalName(i);
        } else {
            throw new IllegalStateException();
        }
    }

    public QName getAttributeName(int i) {
        if (isDelegating()) {
            return realReader.getAttributeName(i);
        } else {
            throw new IllegalStateException();
        }
    }

    public String getAttributeNamespace(int i) {
        if (isDelegating()) {
            return realReader.getAttributeNamespace(i);
        } else {
            throw new IllegalStateException();
        }
    }

    public String getAttributePrefix(int i) {
        if (isDelegating()) {
            return realReader.getAttributePrefix(i);
        } else {
            throw new IllegalStateException();
        }
    }

    public String getAttributeType(int i) {
        if (isDelegating()) {
            return realReader.getAttributeType(i);
        } else {
            throw new IllegalStateException();
        }
    }

    public String getAttributeValue(int i) {
        if (isDelegating()) {
            return realReader.getAttributeValue(i);
        } else {
            throw new IllegalStateException();
        }
    }

    public String getAttributeValue(String s, String s1) {
        if (isDelegating()) {
            return realReader.getAttributeValue(s, s1);
        } else {
            throw new IllegalStateException();
        }
    }

    public String getCharacterEncodingScheme() {
        return realReader.getCharacterEncodingScheme();
    }

    public String getElementText() throws XMLStreamException {
        if (isDelegating()) {
            return realReader.getElementText();
        } else {
            throw new XMLStreamException();
        }
    }

    public String getEncoding() {
        return realReader.getEncoding();
    }

    public int getEventType() {
        int event = -1;
        switch (state) {
            case STATE_SWITCHED:
            case STATE_COMPLETE_AT_NEXT:
                event = realReader.getEventType();
                break;
            case STATE_INIT:
                event = START_DOCUMENT;
                break;
            case STATE_COMPLETED:
                event = END_DOCUMENT;
                break;
        }
        return event;
    }

    public String getLocalName() {
        if (isDelegating()) {
            return realReader.getLocalName();
        } else {
            throw new IllegalStateException();
        }
    }

    public Location getLocation() {
        if (isDelegating()) {
            return realReader.getLocation();
        } else {
            return null;
        }
    }

    public QName getName() {
        if (isDelegating()) {
            return realReader.getName();
        } else {
            throw new IllegalStateException();
        }
    }

    public NamespaceContext getNamespaceContext() {
        return realReader.getNamespaceContext();
    }

    public int getNamespaceCount() {
        if (isDelegating()) {
            return realReader.getNamespaceCount();
        } else {
            throw new IllegalStateException();
        }
    }

    public String getNamespacePrefix(int i) {
        if (isDelegating()) {
            return realReader.getNamespacePrefix(i);
        } else {
            throw new IllegalStateException();
        }
    }

    public String getNamespaceURI() {
        if (isDelegating()) {
            return realReader.getNamespaceURI();
        } else {
            throw new IllegalStateException();
        }
    }

    public String getNamespaceURI(int i) {
        if (isDelegating()) {
            return realReader.getNamespaceURI(i);
        } else {
            throw new IllegalStateException();
        }
    }

    public String getNamespaceURI(String s) {
        if (isDelegating()) {
            return realReader.getNamespaceURI(s);
        } else {
            throw new IllegalStateException();
        }
    }

    public String getPIData() {
        if (isDelegating()) {
            return realReader.getPIData();
        } else {
            throw new IllegalStateException();
        }
    }

    public String getPITarget() {
        if (isDelegating()) {
            return realReader.getPITarget();
        } else {
            throw new IllegalStateException();
        }
    }

    public String getPrefix() {
        if (isDelegating()) {
            return realReader.getPrefix();
        } else {
            throw new IllegalStateException();
        }
    }

    public Object getProperty(String s) throws IllegalArgumentException {
        if (isDelegating()) {
            return realReader.getProperty(s);
        } else {
            throw new IllegalArgumentException();
        }
    }

    public String getText() {
        if (isDelegating()) {
            return realReader.getText();
        } else {
            throw new IllegalStateException();
        }
    }

    public char[] getTextCharacters() {
        if (isDelegating()) {
            return realReader.getTextCharacters();
        } else {
            throw new IllegalStateException();
        }
    }

    public int getTextCharacters(int i, char[] chars, int i1, int i2) throws XMLStreamException {
        if (isDelegating()) {
            return realReader.getTextCharacters(i, chars, i1, i2);
        } else {
            throw new IllegalStateException();
        }
    }

    public int getTextLength() {
        if (isDelegating()) {
            return realReader.getTextLength();
        } else {
            throw new IllegalStateException();
        }
    }

    public int getTextStart() {
        if (isDelegating()) {
            return realReader.getTextStart();
        } else {
            throw new IllegalStateException();
        }
    }

    public String getVersion() {
        if (isDelegating()) {
            return realReader.getVersion();
        } else {
            return null;
        }
    }

    public boolean hasName() {
        if (isDelegating()) {
            return realReader.hasName();
        } else {
            return false;
        }
    }

    public boolean hasNext() throws XMLStreamException {
        if (state == STATE_COMPLETE_AT_NEXT) {
            return true;
        } else if (state == STATE_COMPLETED) {
            return false;
        } else if (state == STATE_SWITCHED) {
            return realReader.hasNext();
        } else {
            return true;
        }
    }

    public boolean hasText() {
        if (isDelegating()) {
            return realReader.hasText();
        } else {
            return false;
        }
    }

    public boolean isAttributeSpecified(int i) {
        if (isDelegating()) {
            return realReader.isAttributeSpecified(i);
        } else {
            return false;
        }
    }

    public boolean isCharacters() {
        if (isDelegating()) {
            return realReader.isCharacters();
        } else {
            return false;
        }
    }

    private boolean isDelegating() {
        return state == STATE_SWITCHED || state == STATE_COMPLETE_AT_NEXT;
    }

    public boolean isEndElement() {
        if (isDelegating()) {
            return realReader.isEndElement();
        } else {
            return false;
        }
    }

    public boolean isStandalone() {
        if (isDelegating()) {
            return realReader.isStandalone();
        } else {
            return false;
        }
    }

    public boolean isStartElement() {
        if (isDelegating()) {
            return realReader.isStartElement();
        } else {
            return false;
        }
    }

    public boolean isWhiteSpace() {
        if (isDelegating()) {
            return realReader.isWhiteSpace();
        } else {
            return false;
        }
    }

    public int next() throws XMLStreamException {
        int returnEvent;

        switch (state) {
            case STATE_SWITCHED:
                returnEvent = realReader.next();
                if (returnEvent == END_DOCUMENT) {
                    state = STATE_COMPLETED;
                } else if (!realReader.hasNext()) {
                    state = STATE_COMPLETE_AT_NEXT;
                } 
                if (fragment && returnEvent == END_ELEMENT) {
                    level--;
                    if (level == 0) {
                        // We are now at the end of the top-level element in the fragment
                        state = STATE_COMPLETE_AT_NEXT;
                    }
                }
                if (fragment && returnEvent == START_ELEMENT) {
                    level++;
                }
                break;
            case STATE_INIT:
                state = STATE_SWITCHED;
                returnEvent = realReader.getEventType();
                if (returnEvent == START_ELEMENT) {
                    // The real reader is positioned at the top-level element in the fragment
                    level = 0;
                    fragment = true;
                }
                break;
            case STATE_COMPLETE_AT_NEXT:
                state = STATE_COMPLETED;
                returnEvent = END_DOCUMENT;
                break;
            case STATE_COMPLETED:
                // oops - no way we can go beyond this
                throw new NoSuchElementException("End of stream has reached.");
            default:
                throw new UnsupportedOperationException();
        }

        return returnEvent;
    }

    public int nextTag() throws XMLStreamException {
        if (isDelegating()) {
            int returnEvent = realReader.nextTag();
            if (fragment && returnEvent == END_ELEMENT) {
                level--;
                if (level == 0) {
                    // We are now at the end of the top-level element in the fragment
                    state = STATE_COMPLETE_AT_NEXT;
                }
            }
            if (fragment && returnEvent == START_ELEMENT) {
                level++;
            }
            return returnEvent;
        } else {
            throw new XMLStreamException();
        }
    }

    public void require(int i, String s, String s1) throws XMLStreamException {
        if (isDelegating()) {
            realReader.require(i, s, s1);
        }
    }

    public boolean standaloneSet() {
        if (isDelegating()) {
            return realReader.standaloneSet();
        } else {
            return false;
        }
    }
}