04fe7f64853bbb4a9d37e3ad21f95b7327792154
[mono.git] / mcs / class / Microsoft.Web.Services / Microsoft.Web.Services / SoapEnvelope.cs
1 //
2 // SoapEnvelope.cs: Soap Envelope
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using System;
11 using System.IO;
12 using System.Xml;
13 using System.Text;
14
15 namespace Microsoft.Web.Services {
16
17         public class SoapEnvelope : XmlDocument {
18
19                 private SoapContext context;
20                 private XmlElement envelope;
21                 private XmlElement body;
22                 private XmlElement header;
23 #if WSE2
24                 private Encoding _encoding;
25 #endif
26
27                 public SoapEnvelope ()
28                 {
29                         envelope = CreateElement (Soap.Prefix, Soap.ElementNames.Envelope, Soap.NamespaceURI);
30                         AppendChild (envelope);
31                 }
32
33                 internal SoapEnvelope (SoapContext context) : this ()
34                 {
35                         this.context = context;
36                 }
37
38 #if WSE2
39                 public Encoding Encoding {
40                         get {
41                                 if(_encoding == null) {
42                                         return new UTF8Encoding (false);
43                                 }
44                                 return _encoding;
45                         }
46                         set {
47                                 _encoding = value;
48                                 if(_encoding is UTF8Encoding) {
49                                         _encoding = new UTF8Encoding (false);
50                                 }
51                         }
52                 }
53 #endif
54
55                 public XmlElement Body {
56                         get {
57                                 if (body == null) {
58                                         XmlNodeList xnl = GetElementsByTagName (Soap.ElementNames.Body, Soap.NamespaceURI);
59                                         body = (XmlElement)xnl[0];
60                                 }
61                                 return body;
62                         }
63                 }
64
65                 public SoapContext Context { 
66                         get { 
67                                 if (context == null)
68                                         context = new SoapContext (this);
69                                 return context; 
70                         }
71                 }
72
73                 public XmlElement Envelope { 
74                         get { return envelope; }
75                 }
76
77                 public XmlElement Header { 
78                         get {
79                                 if (header == null) {
80                                         XmlNodeList xnl = GetElementsByTagName (Soap.ElementNames.Header, Soap.NamespaceURI);
81                                         header = (XmlElement)xnl[0];
82                                 }
83                                 return header;
84                         }
85                 }
86
87                 public XmlElement CreateBody () 
88                 {
89                         if (body == null) {
90                                 body = CreateElement (Soap.Prefix, Soap.ElementNames.Body, Soap.NamespaceURI);
91                                 DocumentElement.AppendChild (body);
92                         }
93                         return body;
94                 }
95
96                 public XmlElement CreateHeader () 
97                 {
98                         if (header == null) {
99                                 header = CreateElement (Soap.Prefix, Soap.ElementNames.Header, Soap.NamespaceURI);
100                                 // be sure Header comes before the Body
101                                 DocumentElement.PrependChild (header);
102                         }
103                         return header;
104                 }
105
106                 private void InvalidateCache () 
107                 {
108                         envelope = DocumentElement;
109                         header = null;
110                         body = null;
111                 }
112
113                 public override void Load (Stream stream) 
114                 {
115                         base.Load (stream);
116                         InvalidateCache ();
117                 }
118
119                 public override void Load (string filename)
120                 {
121                         base.Load (filename);
122                         InvalidateCache ();
123                 }
124
125                 public override void Load (TextReader txtReader) 
126                 {
127                         base.Load (txtReader);
128                         InvalidateCache ();
129                 }
130
131                 public override void Load (XmlReader xmlReader) 
132                 {
133                         base.Load (xmlReader);
134                         InvalidateCache ();
135                 }
136
137                 [MonoTODO("why?")]
138                 public override void Save (Stream stream) 
139                 {
140                         base.Save (stream);
141                 }
142
143                 [MonoTODO("why?")]
144                 public override void Save (string str) 
145                 {
146 #if WSE2
147                         base.Save (new XmlTextWriter (str, Encoding));
148 #else
149                         base.Save (str);
150 #endif
151                 }
152         }
153 }