/[dgee]/dgee/cslib/DotGNU/XmlRpc/XmlRpcWriter.cs
ViewVC logotype

Diff of /dgee/cslib/DotGNU/XmlRpc/XmlRpcWriter.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by npg, Mon Jul 21 14:46:58 2003 UTC revision 1.2 by csmith, Sun Sep 21 10:55:47 2003 UTC
# Line 0  Line 1 
1    /*
2     * DotGNU XmlRpc implementation
3     *
4     * Copyright (C) 2003  Free Software Foundation, Inc.
5     *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19     *
20     * $Revision$  $Date$
21     *
22     * --------------------------------------------------------------------------
23     */
24    using System.Collections;
25    using System.IO;
26    using System.Text;
27    using System.Xml;
28    
29    namespace DotGNU.XmlRpc
30    {  
31      // For now, indentation is the default since it makes it easier for
32      // a human to read the output.  later the output should not be
33      // formatted at all.  ideally it should be an unindented block.
34      // Ideally XmlTextWriter should write the tags, but it's indentation
35      // is too broken so that its more human readable and debuggable.
36      // Grrr Grrr Grrrrr
37      public class XmlRpcWriter : XmlTextWriter
38      {
39    
40        // TODO: build node tree in memory, then flush this tree at the
41        // end which avoids us to do the WriteEndElements and allows us
42        // for consistency checking
43    
44        private StringWriter sw;
45        private XmlRpcMethod m;
46    
47        public XmlRpcWriter( TextWriter w ) : base( w )
48        {
49        }
50    
51        public XmlRpcWriter( Stream stream, Encoding encoding ) : base( stream, encoding )
52        {
53        }
54    
55        public XmlRpcWriter( String filename, Encoding encoding ) : base( filename, encoding )
56        {
57        }
58    
59        public void Write( XmlRpcMethod method )
60        {
61          WriteStartDocument();
62          WriteStartElement( "methodCall" );
63          WriteElementString( "methodName", method.Name );
64          WriteParams();
65          foreach( object o in method ) {
66            WriteParam( o );
67          }
68          WriteEndDocument();
69        }
70    
71        private void WriteMethodResponse()
72        {
73          WriteStartElement( "methodResponse" );  
74        }
75    
76        public void Write( XmlRpcResponse response )
77        {
78          WriteStartDocument();
79          WriteMethodResponse();
80          WriteParams();
81          foreach( object o in response ) {
82            WriteParam( o );
83          }
84          WriteEndDocument();
85        }
86        
87        public void Write( XmlRpcException e )
88        {
89          WriteStartDocument();
90          WriteMethodResponse();
91          WriteStartElement( "fault" );
92          XmlRpcStruct s = new XmlRpcStruct();
93          s.Add( "faultCode", e.FaultCode );
94          s.Add( "faultString", e.Message );
95          WriteValue( s );
96          WriteEndElement();
97          WriteEndDocument();
98        }
99    
100        public void WriteMethodResponse( Exception e )
101        {
102          XmlRpcException ex = new XmlRpcException( e );
103          WriteMethodResponse( ex );
104        }
105        
106        private void WriteParams()
107        {
108          WriteStartElement( "params" );
109        }
110        
111        private void WriteParam( object o )
112        {
113          WriteStartElement( "param" );
114          WriteValue( o );
115          WriteEndElement();
116        }
117        
118        private void WriteValue()
119        {
120          WriteStartElement( "value" );
121        }
122    
123        private void WriteInt( int v )
124        {
125          WriteElementString( "i4", XmlConvert.ToString( v ) );
126        }
127        
128        private void WriteStringValue( string v)
129        {
130          WriteElementString( "string", v );
131        }
132        
133        private void WriteDouble( double v )
134        {
135          // XmlConvert is HORRID doing this!!!!  Try this with a value
136          // of 2.0 and see what i mean
137          WriteElementString( "double",  v.ToString() );
138        }
139        
140        private void WriteBoolean( bool v )
141        {
142          WriteElementString( "boolean", XmlConvert.ToString( v ) );
143        }
144    
145        private void WriteDateTime( DateTime v )
146        {
147          // ensure this is iso compliant
148          Console.Out.WriteLine( "Writing DateTime: " + v );
149    
150          // Why has MS got no clue about ISO 8601???  Horrid.  All
151          // these newbies working on specs.  this is as close as it
152          // gets but not exactly since the 'T' can be left out
153          WriteElementString( "dateTime.iso8601", v.ToString( "s", null ) );
154        }
155        
156        private void WriteBase64( byte[] v )
157        {
158          WriteStartElement( "base64" );
159          WriteBase64( v, 0, v.Length);
160          WriteEndElement();
161        }
162    
163        private void WriteStruct( XmlRpcStruct v )
164        {
165          WriteStartElement( "struct" );
166          
167          foreach( DictionaryEntry entry in v ) {
168            WriteStartElement( "member" );
169            WriteElementString( "name", (string)entry.Key );
170            WriteValue( entry.Value );
171            WriteEndElement(); // member
172          }
173          WriteEndElement(); // struct
174        }
175    
176        private void WriteArray( XmlRpcArray v )
177        {
178          WriteStartElement( "array" );
179          WriteStartElement( "data" );
180          foreach( object entry in v ) {
181            WriteValue( entry );
182          }
183          WriteEndElement(); // data
184          WriteEndElement(); // array
185        }
186    
187        private void WriteValue( object o )
188        {
189          WriteStartElement( "value" );
190          if( o is int ) {
191            WriteInt( (int)o );
192          }
193          else if( o is double ) {
194            WriteDouble( (double)o );
195          }
196          else if( o is string ) {
197            WriteStringValue( (string)o );
198          }
199          else if( o is bool ) {
200            WriteBoolean( (bool)o );
201          }
202          else if( o is DateTime ) {
203            WriteDateTime( (DateTime)o );
204          }
205          else if( o is byte[] ) {
206            WriteBase64( (byte[])o );
207          }
208          else if( o is XmlRpcStruct ) {
209            WriteStruct( (XmlRpcStruct)o );
210          }
211          else if( o is XmlRpcArray ) {
212            WriteArray( (XmlRpcArray)o );
213          }
214          WriteEndElement();
215        }
216      }
217    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26