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: */package org.apache.cxf.aegis.xml;
019:
020: import javax.xml.namespace.QName;
021: import javax.xml.stream.XMLStreamReader;
022:
023: import org.apache.cxf.aegis.DatabindingException;
024: import org.apache.cxf.aegis.util.XmlConstants;
025:
026: /**
027: * Basic type conversions for reading messages.
028: *
029: * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
030: */
031: public abstract class AbstractMessageReader implements MessageReader {
032: private static final QName XSI_NIL = new QName(XmlConstants.XSI_NS,
033: "nil", XmlConstants.XSI_PREFIX);
034:
035: public AbstractMessageReader() {
036: }
037:
038: public void readToEnd() {
039: readToEnd(this );
040: }
041:
042: private void readToEnd(MessageReader childReader) {
043: while (childReader.hasMoreElementReaders()) {
044: readToEnd(childReader.getNextElementReader());
045: }
046: }
047:
048: public boolean isXsiNil() {
049: MessageReader nilReader = getAttributeReader(XSI_NIL);
050: boolean nil = false;
051: if (nilReader != null) {
052: String value = nilReader.getValue();
053: if (value != null
054: && (value.equals("true") || value.equals("1"))) {
055: return true;
056: }
057: }
058:
059: return nil;
060: }
061:
062: public boolean hasValue() {
063: return getValue() != null;
064: }
065:
066: /**
067: * @see org.apache.cxf.aegis.xml.MessageReader#getValueAsCharacter()
068: */
069: public char getValueAsCharacter() {
070: if (getValue() == null) {
071: return 0;
072: }
073: return getValue().charAt(0);
074: }
075:
076: public int getValueAsInt() {
077: if (getValue() == null) {
078: return 0;
079: }
080:
081: return Integer.parseInt(getValue());
082: }
083:
084: /**
085: * @see org.apache.cxf.aegis.xml.MessageReader#getValueAsLong()
086: */
087: public long getValueAsLong() {
088: if (getValue() == null) {
089: return 0L;
090: }
091:
092: return Long.parseLong(getValue());
093: }
094:
095: /**
096: * @see org.apache.cxf.aegis.xml.MessageReader#getValueAsDouble()
097: */
098: public double getValueAsDouble() {
099: if (getValue() == null) {
100: return 0d;
101: }
102:
103: return Double.parseDouble(getValue());
104: }
105:
106: /**
107: * @see org.apache.cxf.aegis.xml.MessageReader#getValueAsFloat()
108: */
109: public float getValueAsFloat() {
110: if (getValue() == null) {
111: return 0f;
112: }
113:
114: return Float.parseFloat(getValue());
115: }
116:
117: /**
118: * @see org.apache.cxf.aegis.xml.MessageReader#getValueAsBoolean()
119: */
120: public boolean getValueAsBoolean() {
121: String value = getValue();
122: if (value == null) {
123: return false;
124: }
125:
126: if ("true".equalsIgnoreCase(value)
127: || "1".equalsIgnoreCase(value)) {
128: return true;
129: }
130:
131: if ("false".equalsIgnoreCase(value)
132: || "0".equalsIgnoreCase(value)) {
133: return false;
134: }
135:
136: throw new DatabindingException("Invalid boolean value: "
137: + value);
138: }
139:
140: public XMLStreamReader getXMLStreamReader() {
141: throw new UnsupportedOperationException();
142: }
143: }
|