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/NameValuePairStreamReader.java
blob: 4abf9b3dcf3ea4d6494f8072cf45d0afca6d4d8a (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
/*
 * 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 javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.stream.Location;
import javax.xml.stream.XMLStreamException;



public class NameValuePairStreamReader implements XMLFragmentStreamReader {

    private static final int START_ELEMENT_STATE = 0;
    private static final int TEXT_STATE = 1;
    private static final int END_ELEMENT_STATE = 2;

    private DelegatingNamespaceContext namespaceContext = new DelegatingNamespaceContext();

    private QName name;
    private String value;

    private int state = START_ELEMENT_STATE;
    // initiate at the start element state

    // keeps track whether the namespace is declared
    // false by default
    private boolean nsDeclared;

    public NameValuePairStreamReader(QName name, String value) {
        this.name = name;
        this.value = value;
    }

    public Object getProperty(String key) throws IllegalArgumentException {
        return null;
    }

    public int next() throws XMLStreamException {
        // no need to handle null here. it should have been handled
        // already
        switch (state) {
            case START_ELEMENT_STATE:
                state = TEXT_STATE;
                return CHARACTERS;
            case END_ELEMENT_STATE:
                // oops, not supposed to happen!
                throw new XMLStreamException("end already reached!");
            case TEXT_STATE:
                state = END_ELEMENT_STATE;
                return END_ELEMENT;
            default:
                throw new XMLStreamException("unknown event type!");
        }
    }

    public void require(int i, String string, String string1) throws XMLStreamException {
        // not implemented
    }

    public String getElementText() throws XMLStreamException {
        if (state == START_ELEMENT) {
            // move to the end state and return the value
            state = END_ELEMENT_STATE;
            return value;
        } else {
            throw new XMLStreamException();
        }

    }

    public int nextTag() throws XMLStreamException {
        return 0; // TODO
    }

    public boolean hasNext() throws XMLStreamException {
        return state != END_ELEMENT_STATE;
    }

    public void close() throws XMLStreamException {
        // Do nothing - we've nothing to free here
    }

    public String getNamespaceURI(String prefix) {
        return namespaceContext.getNamespaceURI(prefix);
    }

    public boolean isStartElement() {
        return state == START_ELEMENT_STATE;
    }

    public boolean isEndElement() {
        return state == END_ELEMENT_STATE;
    }

    public boolean isCharacters() {
        return state == TEXT_STATE;
    }

    public boolean isWhiteSpace() {
        return false; // no whitespaces here
    }

    public String getAttributeValue(String string, String string1) {
        return null;
    }

    public int getAttributeCount() {
        return 0;
    }

    public QName getAttributeName(int i) {
        return null;
    }

    public String getAttributeNamespace(int i) {
        return null;
    }

    public String getAttributeLocalName(int i) {
        return null;
    }

    public String getAttributePrefix(int i) {
        return null;
    }

    public String getAttributeType(int i) {
        return null;
    }

    public String getAttributeValue(int i) {
        return null;
    }

    public boolean isAttributeSpecified(int i) {
        return false; // no attributes here
    }

    public int getNamespaceCount() {
        return nsDeclared ? 1 : 0;
    }

    public String getNamespacePrefix(int i) {
        return (nsDeclared && i == 0) ? name.getPrefix() : null;
    }

    public String getNamespaceURI(int i) {
        return (nsDeclared && i == 0) ? name.getNamespaceURI() : null;
    }

    public NamespaceContext getNamespaceContext() {
        return this.namespaceContext;
    }

    public int getEventType() {
        switch (state) {
            case START_ELEMENT_STATE:
                return START_ELEMENT;
            case END_ELEMENT_STATE:
                return END_ELEMENT;
            case TEXT_STATE:
                return CHARACTERS;
            default:
                throw new UnsupportedOperationException();
                // we've no idea what this is!!!!!
        }

    }

    public String getText() {
        if (state == TEXT_STATE) {
            return value;
        } else {
            throw new IllegalStateException();
        }
    }

    public char[] getTextCharacters() {
        if (state == TEXT_STATE) {
            return value.toCharArray();
        } else {
            throw new IllegalStateException();
        }
    }

    public int getTextCharacters(int i, char[] chars, int i1, int i2) throws XMLStreamException {
        // not implemented
        throw new UnsupportedOperationException();
    }

    public int getTextStart() {
        if (state == TEXT_STATE) {
            return 0;
        } else {
            throw new IllegalStateException();
        }
    }

    public int getTextLength() {
        if (state == TEXT_STATE) {
            return value.length();
        } else {
            throw new IllegalStateException();
        }

    }

    public String getEncoding() {
        return null;
    }

    public boolean hasText() {
        return state == TEXT_STATE;
    }

    public Location getLocation() {
        return new Location() {
            public int getLineNumber() {
                return 0;
            }

            public int getColumnNumber() {
                return 0;
            }

            public int getCharacterOffset() {
                return 0;
            }

            public String getPublicId() {
                return null;
            }

            public String getSystemId() {
                return null;
            }
        };
    }

    public QName getName() {
        if (state != TEXT_STATE) {
            return name;
        } else {
            return null;
        }
    }

    public String getLocalName() {
        if (state != TEXT_STATE) {
            return name.getLocalPart();
        } else {
            return null;
        }
    }

    public boolean hasName() {
        return state != TEXT_STATE;

    }

    public String getNamespaceURI() {
        if (state != TEXT_STATE) {
            return name.getNamespaceURI();
        } else {
            return null;
        }

    }

    public String getPrefix() {
        if (state != TEXT_STATE) {
            return name.getPrefix();
        } else {
            return null;
        }
    }

    public String getVersion() {
        return null; // TODO 1.0 ?
    }

    public boolean isStandalone() {
        return false;
    }

    public boolean standaloneSet() {
        return false;
    }

    public String getCharacterEncodingScheme() {
        return null;
    }

    public String getPITarget() {
        return null;
    }

    public String getPIData() {
        return null;
    }

    public boolean isDone() {
        return state == END_ELEMENT_STATE;
    }

    public void setParentNamespaceContext(NamespaceContext nsContext) {
        this.namespaceContext.setParentNsContext(nsContext);
    }

    public void init() {
        // just add the current elements namespace and prefix to the this
        // elements nscontext
        addToNsMap(name.getPrefix(), name.getNamespaceURI());

    }

    /**
     * @param prefix
     * @param uri
     */
    private void addToNsMap(String prefix, String uri) {
        // TODO - need to fix this up to cater for cases where
        // namespaces are having no prefixes
        if (!uri.equals(namespaceContext.getNamespaceURI(prefix))) {
            // this namespace is not there. Need to declare it
            namespaceContext.pushNamespace(prefix, uri);
            nsDeclared = true;
        }
    }

}