View Javadoc

1   package org.codehaus.xfire.message;
2   
3   import java.io.InputStream;
4   import java.util.Calendar;
5   import java.util.Date;
6   
7   import javax.xml.namespace.QName;
8   import javax.xml.stream.XMLInputFactory;
9   import javax.xml.stream.XMLStreamException;
10  import javax.xml.stream.XMLStreamReader;
11  
12  import org.codehaus.xfire.XFireRuntimeException;
13  import org.codehaus.xfire.util.DateUtils;
14  import org.codehaus.xfire.util.DepthXMLStreamReader;
15  
16  /***
17   * Reads literal encoded messages.
18   * 
19   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
20   */
21  public class LiteralReader
22      implements MessageReader
23  {
24      private DepthXMLStreamReader root;
25      private String value;
26      private String localName;
27      private QName name;
28      private boolean hasCheckedChildren = false;
29      private boolean hasChildren = false;
30      private boolean hasFoundText = false;
31      private String namespace;
32      private int depth;
33      
34      /***
35       * Expects the XMLStreamReader in the START_DOCUMENT event.
36       * @param root
37       */
38      public LiteralReader(DepthXMLStreamReader root)
39      {
40          this.root = root;
41          this.localName = root.getLocalName();
42          this.name = root.getName();
43          this.namespace = root.getNamespaceURI();
44          
45          depth = root.getDepth();
46      }
47      
48      public LiteralReader(XMLStreamReader reader)
49      {
50          this.root = new DepthXMLStreamReader(reader);
51          this.localName = root.getLocalName();
52          this.name = root.getName();
53          this.namespace = root.getNamespaceURI();
54          
55          depth = root.getDepth();
56      }
57  
58      /***
59       * @param resourceAsStream
60       * @throws XMLStreamException 
61       */
62      public LiteralReader(InputStream is) 
63          throws XMLStreamException
64      {
65          XMLInputFactory factory = XMLInputFactory.newInstance();
66          XMLStreamReader xmlReader = factory.createXMLStreamReader(is);
67          
68          while ( xmlReader.getEventType() != XMLStreamReader.START_ELEMENT )
69              xmlReader.next();
70          
71          this.root = new DepthXMLStreamReader(xmlReader);
72          this.localName = root.getLocalName();
73          this.name = root.getName();
74          this.namespace = root.getNamespaceURI();
75          
76          depth = root.getDepth();
77      }
78  
79      /***
80       * @see org.codehaus.xfire.message.MessageReader#getValue()
81       */
82      public String getValue()
83      {
84          while( !hasFoundText && checkHasMoreChildReaders() )
85          {
86          }
87  
88          return value;
89      }
90      
91      public String getValue( String ns, String attr )
92      {
93          return root.getAttributeValue(ns, attr);
94      }
95      
96      /***
97       * @see org.codehaus.xfire.message.MessageReader#getValueAsInt()
98       */
99      public int getValueAsInt()
100     {
101         return Integer.parseInt( getValue() );
102     }
103 
104     public int getValueAsInt(String ns, String attr)
105     {
106         return Integer.parseInt( getValue(ns, attr) );
107     }
108 
109     /***
110      * @see org.codehaus.xfire.message.MessageReader#getValueAsDate()
111      */
112     public Date getValueAsDate()
113     {
114         if ( getValue() == null )
115             return null;
116         
117         return DateUtils.parseDate( getValue() );
118     }
119 
120     public Date getValueAsDate(String ns, String attr)
121     {
122         String value = getValue(ns, attr);
123         if ( value == null )
124             return null;
125         
126         return DateUtils.parseDate( value );
127     }
128     
129     /***
130      * @see org.codehaus.xfire.message.MessageReader#getValueAsDateTime()
131      */
132     public Date getValueAsDateTime()
133     {
134         if ( getValue() == null )
135             return null;
136 
137         return DateUtils.parseDateTime( getValue() );
138     }
139 
140     public Date getValueAsDateTime(String ns, String attr)
141     {
142         String value = getValue(ns, attr);
143         if ( value == null )
144             return null;
145         
146         return DateUtils.parseDateTime( value );
147     }
148 
149     /***
150 	 * @see org.codehaus.xfire.message.MessageReader#getValueAsCalendar()
151 	 */
152     public Calendar getValueAsCalendar()
153     {
154         if ( getValue() == null )
155             return null;
156 
157         Calendar calendar = Calendar.getInstance();
158         calendar.setTime( DateUtils.parseDateTime(getValue()) );
159         return calendar;
160     }
161 
162     public Calendar getValueAsCalendar(String ns, String attr)
163     {
164         String value = getValue(ns, attr);
165         if ( value == null )
166             return null;
167 
168         Calendar calendar = Calendar.getInstance();
169         calendar.setTime( DateUtils.parseDateTime(getValue(ns, attr)) );
170         return calendar;
171     }
172 
173 	/***
174 	 * @see org.codehaus.xfire.message.MessageReader#getValueAsLong()
175 	 */
176 	public long getValueAsLong()
177 	{
178         return Long.parseLong( getValue() );
179 	}
180 
181 	public long getValueAsLong(String ns, String att)
182 	{
183         return Long.parseLong( getValue(ns, att) );
184 	}
185 
186 	/***
187 	 * @see org.codehaus.xfire.message.MessageReader#getValueAsDouble()
188 	 */
189 	public double getValueAsDouble()
190 	{
191 		return Double.parseDouble( getValue() );
192 	}
193 
194 	public double getValueAsDouble(String ns, String attr)
195 	{
196 		return Double.parseDouble( getValue(ns, attr) );
197 	}
198 
199 	/***
200 	 * @see org.codehaus.xfire.message.MessageReader#getValueAsFloat()
201 	 */
202 	public float getValueAsFloat()
203 	{
204 		return Float.parseFloat( getValue() );
205 	}
206 
207 	public float getValueAsFloat(String ns, String attr)
208 	{
209 		return Float.parseFloat( getValue(ns, attr) );
210 	}
211 
212 	/***
213 	 * @see org.codehaus.xfire.message.MessageReader#getValueAsBoolean()
214 	 */
215 	public boolean getValueAsBoolean()
216 	{
217 		return Boolean.valueOf( getValue() ).booleanValue();
218 	}
219 
220 	public boolean getValueAsBoolean(String ns, String attr)
221 	{
222 		return Boolean.valueOf( getValue(ns, attr) ).booleanValue();
223 	}
224 	
225     public boolean hasMoreChildReaders()
226     {
227         // Check to see if we checked before, 
228         // so we don't mess up the stream position.
229         if ( !hasCheckedChildren )
230             checkHasMoreChildReaders();
231         
232         return hasChildren;
233     }
234     
235     private boolean checkHasMoreChildReaders()
236     {
237         try
238         {
239             int event = root.getEventType();
240             while ( true )
241             {
242                 switch( event )
243                 {
244                 case XMLStreamReader.START_ELEMENT:
245                     if ( root.getDepth() > depth )
246                     {
247                         hasCheckedChildren = true;
248                         hasChildren = true;
249                         return true;
250                     }
251                     break;
252                 case XMLStreamReader.END_ELEMENT:
253                     if ( root.getDepth() <= depth )
254                     {
255                         hasCheckedChildren = true;
256                         hasChildren = false;
257                         root.next();
258                         return false;
259                     }
260                     break;
261                 case XMLStreamReader.CHARACTERS:
262                     value = root.getText();
263                     hasFoundText = true;
264                     break;
265                 case XMLStreamReader.END_DOCUMENT:
266                     // We should never get here...
267                     hasCheckedChildren = true;
268                     hasChildren = false;
269                     return false;
270                 default:
271                     break;
272                 }
273                 event = root.next();
274             }
275         }
276         catch (XMLStreamException e)
277         {
278             // TODO: I'm still not sure what should happen here.
279             throw new XFireRuntimeException("Error parsing document.", e);
280         }
281     }
282 
283     public MessageReader getNextChildReader()
284     {
285         if ( !hasCheckedChildren )
286             checkHasMoreChildReaders();
287             
288         if ( !hasChildren )
289             return null;
290         
291         hasCheckedChildren = false;
292 
293         return new LiteralReader( root );
294     }
295     
296     public QName getName()
297     {
298         return name;
299     }
300 
301     public String getLocalName()
302     {
303         return localName;
304     }
305 
306     public String getNamespace()
307     {
308         return namespace;
309     }
310 
311     public XMLStreamReader getXMLStreamReader()
312     {
313         return root;
314     }
315 }