View Javadoc

1   package org.codehaus.xfire.message;
2   
3   import java.io.OutputStream;
4   import java.util.Calendar;
5   import java.util.Date;
6   
7   import javax.xml.namespace.QName;
8   import javax.xml.stream.XMLOutputFactory;
9   import javax.xml.stream.XMLStreamException;
10  import javax.xml.stream.XMLStreamWriter;
11  
12  import org.codehaus.xfire.XFireRuntimeException;
13  import org.codehaus.xfire.util.DateUtils;
14  
15  /***
16   * LiteralWriter
17   * 
18   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
19   */
20  public class LiteralWriter
21      implements MessageWriter
22  {
23      private XMLStreamWriter writer;
24      
25      private String namespace;
26      
27      private String name;
28  
29      private String prefix;
30  
31      private static Calendar calendar = Calendar.getInstance();
32  
33      /***
34       * Create a LiteralWriter but without writing an element name.
35       * 
36       * @param writer
37       */
38      public LiteralWriter(XMLStreamWriter writer)
39      {
40          this.writer = writer;
41      }
42      
43      public LiteralWriter( XMLStreamWriter writer, String name, String namespace )
44      {
45          this(writer, name, namespace, null);
46      }
47  
48      public LiteralWriter( XMLStreamWriter writer, String name, String namespace, String prefix )
49      {
50          this.writer = writer;
51          this.namespace = namespace;
52          this.name = name;
53          this.prefix = prefix;
54          
55          try
56          {
57              writeStartElement();
58          }
59          catch ( XMLStreamException e )
60          {
61              // TODO: I'm still not sure what should happen here.
62              throw new XFireRuntimeException("Error writing document.", e);
63          }
64      }
65      
66      /***
67       * @param os
68       * @throws XMLStreamException 
69       */
70      public LiteralWriter(OutputStream os, String name, String namespace) 
71          throws XMLStreamException
72      {
73          XMLOutputFactory ofactory = XMLOutputFactory.newInstance();
74          this.writer = ofactory.createXMLStreamWriter(os);
75          
76          this.namespace = namespace;
77          this.name = name;
78          
79          try
80          {
81              writeStartElement();
82          }
83          catch ( XMLStreamException e )
84          {
85              // TODO: I'm still not sure what should happen here.
86              throw new XFireRuntimeException("Error writing document.", e);
87          }
88      }
89  
90      private void writeStartElement() 
91          throws XMLStreamException
92      {
93          if (namespace != null)
94          {
95              if (prefix == null)
96                  prefix = "";
97              
98              writer.setPrefix(prefix, namespace);
99              writer.writeStartElement(prefix, name, namespace);
100             writer.writeNamespace(prefix, namespace);
101         }
102         else
103         {
104             writer.writeStartElement(name);
105         }
106     }
107 
108     /***
109      * @see org.codehaus.xfire.message.MessageWriter#writeValue(java.lang.Object)
110      */
111     public void writeValue(Object value)
112     {
113         try
114         {
115             if ( value != null )
116                 writer.writeCharacters( value.toString() );
117         }
118         catch ( XMLStreamException e )
119         {
120             // TODO: I'm still not sure what should happen here.
121             throw new XFireRuntimeException("Error writing document.", e);
122         }
123     }
124 
125     /***
126      * @see org.codehaus.xfire.message.MessageWriter#writeValue(java.lang.Object)
127      */
128     public void writeValue(Object value, String ns, String attr)
129     {
130         try
131         {
132             if ( value != null )
133                 writer.writeAttribute( ns, attr, value.toString() );
134         }
135         catch ( XMLStreamException e )
136         {
137             // TODO: I'm still not sure what should happen here.
138             throw new XFireRuntimeException("Error writing document.", e);
139         }
140     }
141     
142     /***
143      * @see org.codehaus.xfire.message.MessageWriter#getWriter(java.lang.String)
144      */
145     public MessageWriter getChildWriter(String name)
146     {
147         return new LiteralWriter(writer, name, namespace);
148     }
149 
150     public MessageWriter getChildWriter(String name, String ns)
151     {
152         return new LiteralWriter(writer, name, ns);
153     }
154 
155     public MessageWriter getChildWriter(QName qname)
156     {
157         return new LiteralWriter(writer, 
158                                  qname.getLocalPart(), 
159                                  qname.getNamespaceURI(), 
160                                  qname.getPrefix());
161     }
162 
163     public String getNamespace()
164     {
165         return namespace;
166     }
167 
168     public void setNamespace(String namespace)
169     {
170         this.namespace = namespace;
171     }
172 
173     /***
174      * @see org.codehaus.xfire.message.MessageWriter#writeValueAsCalendar(java.util.Calendar)
175      */
176     public void writeValueAsCalendar(Calendar calendar)
177     {
178          writeValue( DateUtils.formatDateTime( calendar.getTime() ) );
179     }
180     
181     public void writeValueAsCalendar(Calendar calendar, String ns, String attr)
182     {
183          writeValue( DateUtils.formatDateTime( calendar.getTime() ) );
184     }
185     
186     /***
187      * @see org.codehaus.xfire.message.MessageWriter#writeValueAsInt(java.lang.Integer)
188      */
189     public void writeValueAsInt(Integer i)
190     {
191         writeValue( i.toString() );
192     }
193 
194     public void writeValueAsInt(Integer i, String ns, String attr)
195     {
196         writeValue( i.toString() );
197     }
198 
199     /***
200      * @see org.codehaus.xfire.message.MessageWriter#writeValueAsDate(java.util.Date)
201      */
202     public void writeValueAsDateTime(Date date)
203     {
204          writeValue( DateUtils.formatDateTime(date) );
205     }
206 
207     public void writeValueAsDateTime(Date date, String ns, String attr)
208     {
209          writeValue( ns, attr, DateUtils.formatDateTime(date) );
210     }
211     
212     /***
213      * @see org.codehaus.xfire.message.MessageWriter#writeValueAsDate(java.util.Date)
214      */
215     public void writeValueAsDate(Date date)
216     {
217         writeValue(DateUtils.formatDate( calendar.getTime() ));
218     }
219 
220     public void writeValueAsDate(Date date, String ns, String attr)
221     {
222         writeValue(DateUtils.formatDate( calendar.getTime() ), ns, attr);
223     }
224 
225 	/***
226 	 * @see org.codehaus.xfire.message.MessageWriter#writeValueAsDouble(java.lang.Double)
227 	 */
228 	public void writeValueAsDouble(Double d)
229 	{
230 		writeValue( d.toString() );
231 	}
232 
233 	public void writeValueAsDouble(Double d, String ns, String attr)
234 	{
235 		writeValue( d.toString(), ns, attr );
236 	}
237 
238 	/***
239 	 * @see org.codehaus.xfire.message.MessageWriter#writeValueAsLong(java.lang.Long)
240 	 */
241 	public void writeValueAsLong(Long l)
242 	{
243 		writeValue( l.toString() );
244 	}
245 
246 	public void writeValueAsLong(Long l, String ns, String attr)
247 	{
248 		writeValue( l.toString(), ns, attr );
249 	}
250 
251 	/***
252 	 * @see org.codehaus.xfire.message.MessageWriter#writeValueAsFloat(java.lang.Float)
253 	 */
254 	public void writeValueAsFloat(Float f)
255 	{
256 		writeValue( f.toString() );
257 	}
258 
259 	public void writeValueAsFloat(Float f, String ns, String attr)
260 	{
261 		writeValue( f.toString(), ns, attr );
262 	}
263 
264 	/***
265 	 * @see org.codehaus.xfire.message.MessageWriter#writeValueAsBoolean(boolean)
266 	 */
267 	public void writeValueAsBoolean(boolean b)
268 	{
269 		writeValue( Boolean.toString(b) );
270 	}
271 
272 	public void writeValueAsBoolean(boolean b, String ns, String attr)
273 	{
274 		writeValue( Boolean.toString(b), ns, attr );
275 	}
276 
277     public void close()
278     {
279         try
280         {
281             writer.writeEndElement();
282         }
283         catch ( XMLStreamException e )
284         {
285             // TODO: I'm still not sure what should happen here.
286             throw new XFireRuntimeException("Error writing document.", e);
287         }
288     }
289 
290     public void flush() throws XMLStreamException
291     {
292         writer.flush();
293     }
294 
295     public XMLStreamWriter getXMLStreamWriter()
296     {
297         return writer;
298     }
299 }