001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.databinding.utils.reader;
020:
021: import javax.xml.namespace.NamespaceContext;
022: import javax.xml.namespace.QName;
023: import javax.xml.stream.Location;
024: import javax.xml.stream.XMLStreamException;
025:
026: public class NameValueArrayStreamReader implements ADBXMLStreamReader {
027:
028: private static final int START_ELEMENT_STATE = 0;
029: private static final int TEXT_STATE = 1;
030: private static final int END_ELEMENT_STATE = 2;
031: private static final int FINAL_END_ELEMENT_STATE = 3;
032: private static final int START_ELEMENT_STATE_WITH_NULL = 4;
033:
034: private static final QName NIL_QNAME = new QName(
035: "http://www.w3.org/2001/XMLSchema-instance", "nil", "xsi");
036: private static final String NIL_VALUE_TRUE = "true";
037:
038: private ADBNamespaceContext namespaceContext = new ADBNamespaceContext();
039: //the index of the array
040: private int arrayIndex = 0;
041:
042: private QName name;
043: private String[] values;
044:
045: //start element is the default state
046: private int state = START_ELEMENT_STATE;
047:
048: public NameValueArrayStreamReader(QName name, String[] values) {
049: this .name = name;
050: this .values = values;
051: }
052:
053: public void addNamespaceContext(NamespaceContext nsContext) {
054: this .namespaceContext.setParentNsContext(nsContext);
055: }
056:
057: public void init() {
058: //todo what if the Qname namespace has not been declared
059: }
060:
061: public Object getProperty(String string)
062: throws IllegalArgumentException {
063: return null;
064: }
065:
066: /** @throws XMLStreamException */
067: public int next() throws XMLStreamException {
068: switch (state) {
069: case START_ELEMENT_STATE:
070: if (values.length > 0) {
071: state = TEXT_STATE;
072: return CHARACTERS;
073: } else {
074: state = FINAL_END_ELEMENT_STATE;
075: return END_ELEMENT;
076: }
077:
078: case START_ELEMENT_STATE_WITH_NULL:
079: if (arrayIndex == (values.length - 1)) {
080: state = FINAL_END_ELEMENT_STATE;
081: } else {
082: state = END_ELEMENT_STATE;
083: }
084: return END_ELEMENT;
085: case FINAL_END_ELEMENT_STATE:
086: //oops, not supposed to happen!
087: throw new XMLStreamException("end already reached!");
088: case END_ELEMENT_STATE:
089: //we've to have more values since this is not the
090: //last value
091: //increment the counter
092: arrayIndex++;
093: if (values[arrayIndex] == null) {
094: state = START_ELEMENT_STATE_WITH_NULL;
095: } else {
096: state = START_ELEMENT_STATE;
097: }
098: return START_ELEMENT;
099: case TEXT_STATE:
100: if (arrayIndex == (values.length - 1)) {
101: state = FINAL_END_ELEMENT_STATE;
102: return END_ELEMENT;
103: } else {
104: state = END_ELEMENT_STATE;
105: return END_ELEMENT;
106: }
107:
108: default:
109: throw new XMLStreamException("unknown event type!");
110: }
111: }
112:
113: public void require(int i, String string, String string1)
114: throws XMLStreamException {
115: //nothing done here
116: }
117:
118: public String getElementText() throws XMLStreamException {
119: return null; //not implemented
120: }
121:
122: public int nextTag() throws XMLStreamException {
123: return 0; //not implemented
124: }
125:
126: public String getAttributeValue(String string, String string1) {
127: if (state == TEXT_STATE) {
128: //todo something
129: return null;
130: } else {
131: return null;
132: }
133:
134: }
135:
136: public int getAttributeCount() {
137: if (state == START_ELEMENT_STATE_WITH_NULL)
138: return 1;
139: if (state == START_ELEMENT_STATE) {
140: return 0;
141: } else {
142: throw new IllegalStateException();
143: }
144:
145: }
146:
147: public QName getAttributeName(int i) {
148: if (state == START_ELEMENT_STATE_WITH_NULL && i == 0)
149: return NIL_QNAME;
150: if (state == START_ELEMENT_STATE) {
151: return null;
152: } else {
153: throw new IllegalStateException();
154: }
155: }
156:
157: public String getAttributeNamespace(int i) {
158: if (state == START_ELEMENT_STATE_WITH_NULL && i == 0)
159: return NIL_QNAME.getNamespaceURI();
160: if (state == START_ELEMENT_STATE) {
161: return null;
162: } else {
163: throw new IllegalStateException();
164: }
165: }
166:
167: public String getAttributeLocalName(int i) {
168: if (state == START_ELEMENT_STATE_WITH_NULL && i == 0)
169: return NIL_QNAME.getLocalPart();
170: if (state == START_ELEMENT_STATE) {
171: return null;
172: } else {
173: throw new IllegalStateException();
174: }
175: }
176:
177: public String getAttributePrefix(int i) {
178: if (state == START_ELEMENT_STATE_WITH_NULL && i == 0)
179: return NIL_QNAME.getPrefix();
180: if (state == START_ELEMENT_STATE) {
181: return null;
182: } else {
183: throw new IllegalStateException();
184: }
185: }
186:
187: public String getAttributeType(int i) {
188: return null; //not implemented
189: }
190:
191: public String getAttributeValue(int i) {
192: if (state == START_ELEMENT_STATE_WITH_NULL && i == 0)
193: return NIL_VALUE_TRUE;
194: if (state == START_ELEMENT_STATE) {
195: return null;
196: } else {
197: throw new IllegalStateException();
198: }
199: }
200:
201: public boolean isAttributeSpecified(int i) {
202: return false; //not supported
203: }
204:
205: public int getNamespaceCount() {
206: if (state == START_ELEMENT_STATE_WITH_NULL
207: && isXsiNamespacePresent())
208: return 1;
209: else
210: return 0;
211:
212: }
213:
214: public String getNamespacePrefix(int i) {
215: if (state == START_ELEMENT_STATE_WITH_NULL
216: && isXsiNamespacePresent() && i == 0)
217: return NIL_QNAME.getPrefix();
218: else
219: return null;
220: }
221:
222: public String getNamespaceURI(int i) {
223: if (state == START_ELEMENT_STATE_WITH_NULL
224: && isXsiNamespacePresent() && i == 0)
225: return NIL_QNAME.getNamespaceURI();
226: else
227: return null;
228: }
229:
230: public NamespaceContext getNamespaceContext() {
231: return this .namespaceContext;
232: }
233:
234: public boolean isDone() {
235: return (state == FINAL_END_ELEMENT_STATE);
236: }
237:
238: public int getEventType() {
239: switch (state) {
240: case START_ELEMENT_STATE:
241: return START_ELEMENT;
242: case END_ELEMENT_STATE:
243: return END_ELEMENT;
244: case TEXT_STATE:
245: return CHARACTERS;
246: case FINAL_END_ELEMENT_STATE:
247: return END_ELEMENT;
248: default:
249: throw new UnsupportedOperationException();
250: //we've no idea what this is!!!!!
251: }
252:
253: }
254:
255: public String getText() {
256: if (state == TEXT_STATE) {
257: return values[arrayIndex];
258: } else {
259: throw new IllegalStateException();
260: }
261: }
262:
263: public char[] getTextCharacters() {
264: if (state == TEXT_STATE) {
265: return values[arrayIndex].toCharArray();
266: } else {
267: throw new IllegalStateException();
268: }
269: }
270:
271: public int getTextCharacters(int i, char[] chars, int i1, int i2)
272: throws XMLStreamException {
273: //not implemented
274: throw new UnsupportedOperationException();
275: }
276:
277: public int getTextStart() {
278: if (state == TEXT_STATE) {
279: return 0;
280: } else {
281: throw new IllegalStateException();
282: }
283: }
284:
285: public int getTextLength() {
286: if (state == TEXT_STATE) {
287: return values[arrayIndex].length();
288: } else {
289: throw new IllegalStateException();
290: }
291:
292: }
293:
294: public String getEncoding() {
295: return null;
296: }
297:
298: public boolean hasText() {
299: return (state == TEXT_STATE);
300: }
301:
302: public Location getLocation() {
303: return null; //not supported
304: }
305:
306: public QName getName() {
307: if (state != TEXT_STATE) {
308: return name;
309: } else {
310: return null;
311: }
312: }
313:
314: public String getLocalName() {
315: if (state != TEXT_STATE) {
316: return name.getLocalPart();
317: } else {
318: return null;
319: }
320: }
321:
322: public boolean hasName() {
323: return (state != TEXT_STATE);
324:
325: }
326:
327: public String getNamespaceURI() {
328: if (state != TEXT_STATE) {
329: return name.getNamespaceURI();
330: } else {
331: return null;
332: }
333:
334: }
335:
336: public String getPrefix() {
337: if (state != TEXT_STATE) {
338: return name.getPrefix();
339: } else {
340: return null;
341: }
342: }
343:
344: public String getVersion() {
345: return null; //todo 1.0 ?
346: }
347:
348: public boolean isStandalone() {
349: return false;
350: }
351:
352: public boolean standaloneSet() {
353: return false;
354: }
355:
356: public String getCharacterEncodingScheme() {
357: return null;
358: }
359:
360: public String getPITarget() {
361: return null;
362: }
363:
364: public String getPIData() {
365: return null;
366: }
367:
368: public boolean hasNext() throws XMLStreamException {
369: return (state != FINAL_END_ELEMENT_STATE);
370: }
371:
372: public void close() throws XMLStreamException {
373: //Do nothing - we've nothing to free here
374: }
375:
376: public String getNamespaceURI(String prefix) {
377: return namespaceContext.getNamespaceURI(prefix);
378: }
379:
380: public boolean isStartElement() {
381: return (state == START_ELEMENT_STATE);
382: }
383:
384: public boolean isEndElement() {
385: return (state == END_ELEMENT_STATE);
386: }
387:
388: public boolean isCharacters() {
389: return (state == TEXT_STATE);
390: }
391:
392: public boolean isWhiteSpace() {
393: return false; //no whitespaces here
394: }
395:
396: /**
397: * @param prefix
398: * @param uri
399: */
400: private void addToNsMap(String prefix, String uri) {
401: //todo - need to fix this up to cater for cases where
402: //namespaces are having no prefixes
403: if (!uri.equals(namespaceContext.getNamespaceURI(prefix))) {
404: //this namespace is not there. Need to declare it
405: namespaceContext.pushNamespace(prefix, uri);
406: }
407: }
408:
409: /** Test whether the xsi namespace is present */
410: private boolean isXsiNamespacePresent() {
411: return (namespaceContext.getNamespaceURI(NIL_QNAME.getPrefix()) != null);
412: }
413: }
|