1 package org.codehaus.xfire.java.message;
2
3 import java.util.Calendar;
4 import java.util.Date;
5
6 import javax.xml.namespace.QName;
7 import javax.xml.stream.XMLStreamException;
8 import javax.xml.stream.XMLStreamReader;
9
10 import org.codehaus.xfire.XFireRuntimeException;
11 import org.codehaus.xfire.util.DateUtils;
12 import org.codehaus.xfire.util.DepthXMLStreamReader;
13
14 /***
15 * Reads literal encoded messages.
16 *
17 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
18 */
19 public class LiteralReader
20 implements MessageReader
21 {
22 private DepthXMLStreamReader root;
23 private String value;
24 private String localName;
25 private QName name;
26 private boolean hasCheckedChildren = false;
27 private boolean hasChildren = false;
28 private boolean hasFoundText = false;
29 int depth;
30
31 /***
32 * Expects the XMLStreamReader in the START_DOCUMENT event.
33 * @param root
34 */
35 public LiteralReader( DepthXMLStreamReader root )
36 {
37 this.root = root;
38 this.localName = root.getLocalName();
39 this.name = root.getName();
40 depth = root.getDepth();
41 }
42
43 public LiteralReader( XMLStreamReader reader )
44 {
45 this.root = new DepthXMLStreamReader(reader);
46 this.localName = root.getLocalName();
47 this.name = root.getName();
48 depth = root.getDepth();
49 }
50
51 /***
52 * @see org.codehaus.xfire.java.message.MessageReader#getValue()
53 */
54 public String getValue()
55 {
56 if ( !hasFoundText )
57 findValue();
58
59 return value;
60 }
61
62 private void findValue()
63 {
64 while( !hasFoundText && checkHasMoreChildReaders() )
65 {
66 }
67 }
68
69 /***
70 * @see org.codehaus.xfire.java.message.MessageReader#getValueAsInt()
71 */
72 public int getValueAsInt()
73 {
74 return Integer.parseInt( getValue() );
75 }
76
77 /***
78 * @see org.codehaus.xfire.java.message.MessageReader#getValueAsDate()
79 */
80 public Date getValueAsDate()
81 {
82 if ( getValue() == null )
83 return null;
84
85 return DateUtils.parseDate( getValue() );
86 }
87
88 /***
89 * @see org.codehaus.xfire.java.message.MessageReader#getValueAsDateTime()
90 */
91 public Date getValueAsDateTime()
92 {
93 if ( getValue() == null )
94 return null;
95
96 return DateUtils.parseDateTime( getValue() );
97 }
98
99
100 /***
101 * @see org.codehaus.xfire.java.message.MessageReader#getValueAsCalendar()
102 */
103 public Calendar getValueAsCalendar()
104 {
105 if ( getValue() == null )
106 return null;
107
108 Calendar calendar = Calendar.getInstance();
109 calendar.setTime( DateUtils.parseDateTime(getValue()) );
110 return calendar;
111 }
112
113 /***
114 * @see org.codehaus.xfire.java.message.MessageReader#getValueAsLong()
115 */
116 public long getValueAsLong()
117 {
118 return Long.parseLong( getValue() );
119 }
120
121 /***
122 * @see org.codehaus.xfire.java.message.MessageReader#getValueAsDouble()
123 */
124 public double getValueAsDouble()
125 {
126 return Double.parseDouble( getValue() );
127 }
128
129 /***
130 * @see org.codehaus.xfire.java.message.MessageReader#getValueAsFloat()
131 */
132 public float getValueAsFloat()
133 {
134 return Float.parseFloat( getValue() );
135 }
136
137 /***
138 * @see org.codehaus.xfire.java.message.MessageReader#getValueAsBoolean()
139 */
140 public boolean getValueAsBoolean()
141 {
142 return Boolean.valueOf( getValue() ).booleanValue();
143 }
144
145 public boolean hasMoreChildReaders()
146 {
147
148
149 if ( !hasCheckedChildren )
150 checkHasMoreChildReaders();
151
152 return hasChildren;
153 }
154
155 private boolean checkHasMoreChildReaders()
156 {
157 try
158 {
159 int event = root.getEventType();
160 while ( true )
161 {
162 switch( event )
163 {
164 case XMLStreamReader.START_ELEMENT:
165 if ( root.getDepth() > depth )
166 {
167 hasCheckedChildren = true;
168 hasChildren = true;
169 return true;
170 }
171 break;
172 case XMLStreamReader.END_ELEMENT:
173 if ( root.getDepth() <= depth )
174 {
175 hasCheckedChildren = true;
176 hasChildren = false;
177 root.next();
178 return false;
179 }
180 break;
181 case XMLStreamReader.CHARACTERS:
182 value = root.getText();
183 hasFoundText = true;
184 break;
185 case XMLStreamReader.END_DOCUMENT:
186
187 hasCheckedChildren = true;
188 hasChildren = false;
189 return false;
190 default:
191 break;
192 }
193 event = root.next();
194 }
195 }
196 catch (XMLStreamException e)
197 {
198
199 throw new XFireRuntimeException("Error parsing document.", e);
200 }
201 }
202
203 public MessageReader getNextChildReader()
204 {
205 if ( !hasCheckedChildren )
206 checkHasMoreChildReaders();
207
208 if ( !hasChildren )
209 return null;
210
211 hasCheckedChildren = false;
212
213 return new LiteralReader( root );
214 }
215
216 public QName getName()
217 {
218 return name;
219 }
220
221 public String getLocalName()
222 {
223 return localName;
224 }
225 }