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 NameValuePairStreamReader 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:
032: private ADBNamespaceContext namespaceContext = new ADBNamespaceContext();
033:
034: private QName name;
035: private String value;
036:
037: private int state = START_ELEMENT_STATE;
038: //initiate at the start element state
039:
040: //keeps track whether the namespace is declared
041: //false by default
042: private boolean nsDeclared = false;
043:
044: public NameValuePairStreamReader(QName name, String value) {
045: this .name = name;
046: this .value = value;
047: }
048:
049: public Object getProperty(String key)
050: throws IllegalArgumentException {
051: //since optimization is a global property
052: //we've to implement it everywhere
053: if (OPTIMIZATION_ENABLED.equals(key)) {
054: return Boolean.TRUE;
055: } else if (state == TEXT_STATE) {
056: if (IS_BINARY.equals(key)) {
057: return Boolean.FALSE;
058: } else {
059: return null;
060: }
061: } else {
062: return null;
063: }
064: }
065:
066: public int next() throws XMLStreamException {
067: //no need to handle null here. it should have been handled
068: //already
069: switch (state) {
070: case START_ELEMENT_STATE:
071: state = TEXT_STATE;
072: return CHARACTERS;
073: case END_ELEMENT_STATE:
074: //oops, not supposed to happen!
075: throw new XMLStreamException("end already reached!");
076: case TEXT_STATE:
077: state = END_ELEMENT_STATE;
078: return END_ELEMENT;
079: default:
080: throw new XMLStreamException("unknown event type!");
081: }
082: }
083:
084: public void require(int i, String string, String string1)
085: throws XMLStreamException {
086: //not implemented
087: }
088:
089: public String getElementText() throws XMLStreamException {
090: if (state == START_ELEMENT) {
091: //move to the end state and return the value
092: state = END_ELEMENT_STATE;
093: return value;
094: } else {
095: throw new XMLStreamException();
096: }
097:
098: }
099:
100: public int nextTag() throws XMLStreamException {
101: return 0;//todo
102: }
103:
104: public boolean hasNext() throws XMLStreamException {
105: return (state != END_ELEMENT_STATE);
106: }
107:
108: public void close() throws XMLStreamException {
109: //Do nothing - we've nothing to free here
110: }
111:
112: public String getNamespaceURI(String prefix) {
113: return namespaceContext.getNamespaceURI(prefix);
114: }
115:
116: public boolean isStartElement() {
117: return (state == START_ELEMENT_STATE);
118: }
119:
120: public boolean isEndElement() {
121: return (state == END_ELEMENT_STATE);
122: }
123:
124: public boolean isCharacters() {
125: return (state == TEXT_STATE);
126: }
127:
128: public boolean isWhiteSpace() {
129: return false; //no whitespaces here
130: }
131:
132: public String getAttributeValue(String string, String string1) {
133: return null;
134: }
135:
136: public int getAttributeCount() {
137: return 0;
138: }
139:
140: public QName getAttributeName(int i) {
141: return null;
142: }
143:
144: public String getAttributeNamespace(int i) {
145: return null;
146: }
147:
148: public String getAttributeLocalName(int i) {
149: return null;
150: }
151:
152: public String getAttributePrefix(int i) {
153: return null;
154: }
155:
156: public String getAttributeType(int i) {
157: return null;
158: }
159:
160: public String getAttributeValue(int i) {
161: return null;
162: }
163:
164: public boolean isAttributeSpecified(int i) {
165: return false; //no attribs here
166: }
167:
168: public int getNamespaceCount() {
169: return (nsDeclared) ? 1 : 0;
170: }
171:
172: public String getNamespacePrefix(int i) {
173: return (nsDeclared && i == 0) ? name.getPrefix() : null;
174: }
175:
176: public String getNamespaceURI(int i) {
177: return (nsDeclared && i == 0) ? name.getNamespaceURI() : null;
178: }
179:
180: public NamespaceContext getNamespaceContext() {
181: return this .namespaceContext;
182: }
183:
184: public int getEventType() {
185: switch (state) {
186: case START_ELEMENT_STATE:
187: return START_ELEMENT;
188: case END_ELEMENT_STATE:
189: return END_ELEMENT;
190: case TEXT_STATE:
191: return CHARACTERS;
192: default:
193: throw new UnsupportedOperationException();
194: //we've no idea what this is!!!!!
195: }
196:
197: }
198:
199: public String getText() {
200: if (state == TEXT_STATE) {
201: return value;
202: } else {
203: throw new IllegalStateException();
204: }
205: }
206:
207: public char[] getTextCharacters() {
208: if (state == TEXT_STATE) {
209: return value.toCharArray();
210: } else {
211: throw new IllegalStateException();
212: }
213: }
214:
215: public int getTextCharacters(int i, char[] chars, int i1, int i2)
216: throws XMLStreamException {
217: //not implemented
218: throw new UnsupportedOperationException();
219: }
220:
221: public int getTextStart() {
222: if (state == TEXT_STATE) {
223: return 0;
224: } else {
225: throw new IllegalStateException();
226: }
227: }
228:
229: public int getTextLength() {
230: if (state == TEXT_STATE) {
231: return value.length();
232: } else {
233: throw new IllegalStateException();
234: }
235:
236: }
237:
238: public String getEncoding() {
239: return null;
240: }
241:
242: public boolean hasText() {
243: return (state == TEXT_STATE);
244: }
245:
246: public Location getLocation() {
247: return new Location() {
248: public int getLineNumber() {
249: return 0;
250: }
251:
252: public int getColumnNumber() {
253: return 0;
254: }
255:
256: public int getCharacterOffset() {
257: return 0;
258: }
259:
260: public String getPublicId() {
261: return null;
262: }
263:
264: public String getSystemId() {
265: return null;
266: }
267: };
268: }
269:
270: public QName getName() {
271: if (state != TEXT_STATE) {
272: return name;
273: } else {
274: return null;
275: }
276: }
277:
278: public String getLocalName() {
279: if (state != TEXT_STATE) {
280: return name.getLocalPart();
281: } else {
282: return null;
283: }
284: }
285:
286: public boolean hasName() {
287: return (state != TEXT_STATE);
288:
289: }
290:
291: public String getNamespaceURI() {
292: if (state != TEXT_STATE) {
293: return name.getNamespaceURI();
294: } else {
295: return null;
296: }
297:
298: }
299:
300: public String getPrefix() {
301: if (state != TEXT_STATE) {
302: return name.getPrefix();
303: } else {
304: return null;
305: }
306: }
307:
308: public String getVersion() {
309: return null; //todo 1.0 ?
310: }
311:
312: public boolean isStandalone() {
313: return false;
314: }
315:
316: public boolean standaloneSet() {
317: return false;
318: }
319:
320: public String getCharacterEncodingScheme() {
321: return null;
322: }
323:
324: public String getPITarget() {
325: return null;
326: }
327:
328: public String getPIData() {
329: return null;
330: }
331:
332: public boolean isDone() {
333: return (state == END_ELEMENT_STATE);
334: }
335:
336: public void addNamespaceContext(NamespaceContext nsContext) {
337: this .namespaceContext.setParentNsContext(nsContext);
338: }
339:
340: public void init() {
341: //just add the current elements namespace and prefix to the this
342: //elements nscontext
343: addToNsMap(name.getPrefix(), name.getNamespaceURI());
344:
345: }
346:
347: /**
348: * @param prefix
349: * @param uri
350: */
351: private void addToNsMap(String prefix, String uri) {
352: //todo - need to fix this up to cater for cases where
353: //namespaces are having no prefixes
354: if (!uri.equals(namespaceContext.getNamespaceURI(prefix))) {
355: //this namespace is not there. Need to declare it
356: namespaceContext.pushNamespace(prefix, uri);
357: nsDeclared = true;
358: }
359: }
360:
361: }
|