283d462ea1fd835704afc508584b14ca209d307f
[mono.git] / mcs / class / System.XML / System.Xml / XmlTextWriter.cs
1 //
2 // System.Xml.XmlTextWriter
3 //
4 // Author:
5 //   Kral Ferch <kral_ferch@hotmail.com>
6 //
7 // (C) 2002 Kral Ferch
8 //
9
10 using System;
11 using System.Collections;
12 using System.IO;
13 using System.Text;
14
15 namespace System.Xml
16 {
17         public class XmlTextWriter : XmlWriter
18         {
19                 #region Fields
20
21                 protected TextWriter w;
22                 protected bool openWriter = true;
23                 protected bool openStartElement;
24                 protected Stack openElements = new Stack();
25
26                 #endregion
27
28                 #region Constructors
29
30                 public XmlTextWriter (TextWriter w) : base ()
31                 {
32                         this.w = w;
33                 }
34
35                 public XmlTextWriter (Stream w, Encoding encoding) : base ()
36                 {
37                         this.w = new StreamWriter(w, encoding);
38                 }
39
40                 public XmlTextWriter (string filename, Encoding encoding) : base ()
41                 {
42                         this.w = new StreamWriter(filename, false, encoding);
43                 }
44
45                 #endregion
46
47                 #region Properties
48
49                 [MonoTODO]
50                 public Stream BaseStream {
51                         get { throw new NotImplementedException(); }
52                 }
53
54
55                 [MonoTODO]
56                 public Formatting Formatting {
57                         get { throw new NotImplementedException(); }
58                         set { throw new NotImplementedException(); }
59                 }
60
61                 [MonoTODO]
62                 public int Indentation {
63                         get { throw new NotImplementedException(); }
64                         set { throw new NotImplementedException(); }
65                 }
66
67                 [MonoTODO]
68                 public char IndentChar {
69                         get { throw new NotImplementedException(); }
70                         set { throw new NotImplementedException(); }
71                 }
72
73                 [MonoTODO]
74                 public bool Namespaces {
75                         get { throw new NotImplementedException(); }
76                         set { throw new NotImplementedException(); }
77                 }
78
79                 [MonoTODO]
80                 public char QuoteChar {
81                         get { throw new NotImplementedException(); }
82                         set { throw new NotImplementedException(); }
83                 }
84
85                 [MonoTODO]
86                 public override WriteState WriteState {
87                         get { throw new NotImplementedException(); }
88                 }
89                 
90                 [MonoTODO]
91                 public override string XmlLang {
92                         get { throw new NotImplementedException(); }
93                 }
94
95                 [MonoTODO]
96                 public override XmlSpace XmlSpace {
97                         get { throw new NotImplementedException(); }
98                 }
99
100                 #endregion
101
102                 #region Methods
103
104                 private void CheckOpenWriter ()
105                 {
106                         if (!openWriter) {
107                                 throw new InvalidOperationException ("The Writer is closed.");
108                         }
109                 }
110
111                 public override void Close ()
112                 {
113                         while (openElements.Count > 0) {
114                                 WriteEndElement();
115                         }
116
117                         w.Close();
118
119                         openWriter = false;
120                 }
121
122                 private void CloseStartElement ()
123                 {
124                         if (openStartElement) 
125                         {
126                                 w.Write(">");
127                                 openStartElement = false;
128                         }
129                 }
130
131                 [MonoTODO]
132                 public override void Flush ()
133                 {
134                         throw new NotImplementedException ();
135                 }
136
137                 [MonoTODO]
138                 public override string LookupPrefix (string ns)
139                 {
140                         throw new NotImplementedException ();
141                 }
142
143                 [MonoTODO]
144                 public override void WriteBase64 (byte[] buffer, int index, int count)
145                 {
146                         throw new NotImplementedException ();
147                 }
148
149                 [MonoTODO]
150                 public override void WriteBinHex (byte[] buffer, int index, int count)
151                 {
152                         throw new NotImplementedException ();
153                 }
154
155                 public override void WriteCData (string text)
156                 {
157                         if (text.IndexOf("]]>") > 0) 
158                         {
159                                 throw new ArgumentException ();
160                         }
161
162                         CheckOpenWriter ();
163                         CloseStartElement ();
164
165                         w.Write("<![CDATA[{0}]]>", text);
166                 }
167
168                 [MonoTODO]
169                 public override void WriteCharEntity (char ch)
170                 {
171                         throw new NotImplementedException ();
172                 }
173
174                 [MonoTODO]
175                 public override void WriteChars (char[] buffer, int index, int count)
176                 {
177                         throw new NotImplementedException ();
178                 }
179
180                 public override void WriteComment (string text)
181                 {
182                         if ((text.EndsWith("-")) || (text.IndexOf("-->") > 0)) {
183                                 throw new ArgumentException ();
184                         }
185
186                         CheckOpenWriter ();
187                         CloseStartElement ();
188
189                         w.Write ("<!--{0}-->", text);
190                 }
191
192                 [MonoTODO]
193                 public override void WriteDocType (string name, string pubid, string sysid, string subset)
194                 {
195                         throw new NotImplementedException ();
196                 }
197
198                 [MonoTODO]
199                 public override void WriteEndAttribute ()
200                 {
201                         throw new NotImplementedException ();
202                 }
203
204                 [MonoTODO]
205                 public override void WriteEndDocument ()
206                 {
207                         throw new NotImplementedException ();
208                 }
209
210                 public override void WriteEndElement ()
211                 {
212                         if (openStartElement) {
213                                 w.Write (" />");
214                                 openElements.Pop ();
215                                 openStartElement = false;
216                         }
217                         else {
218                                 w.Write ("</{0}>", openElements.Pop ());
219                         }
220                 }
221
222                 [MonoTODO]
223                 public override void WriteEntityRef (string name)
224                 {
225                         throw new NotImplementedException ();
226                 }
227
228                 [MonoTODO]
229                 public override void WriteFullEndElement ()
230                 {
231                         throw new NotImplementedException ();
232                 }
233
234                 [MonoTODO]
235                 public override void WriteName (string name)
236                 {
237                         throw new NotImplementedException ();
238                 }
239
240                 [MonoTODO]
241                 public override void WriteNmToken (string name)
242                 {
243                         throw new NotImplementedException ();
244                 }
245
246                 public override void WriteProcessingInstruction (string name, string text)
247                 {
248                         if ((name == null) || (name == string.Empty) || (name.IndexOf("?>") > 0) || (text.IndexOf("?>") > 0)) {
249                                 throw new ArgumentException ();
250                         }
251
252                         CheckOpenWriter ();
253                         CloseStartElement ();
254
255                         w.Write ("<?{0} {1}?>", name, text);
256                 }
257
258                 [MonoTODO]
259                 public override void WriteQualifiedName (string localName, string ns)
260                 {
261                         throw new NotImplementedException ();
262                 }
263
264                 [MonoTODO]
265                 public override void WriteRaw (string data)
266                 {
267                         throw new NotImplementedException ();
268                 }
269
270                 [MonoTODO]
271                 public override void WriteRaw (char[] buffer, int index, int count)
272                 {
273                         throw new NotImplementedException ();
274                 }
275
276                 [MonoTODO]
277                 public override void WriteStartAttribute (string prefix, string localName, string ns)
278                 {
279                         throw new NotImplementedException ();
280                 }
281
282                 [MonoTODO]
283                 public override void WriteStartDocument ()
284                 {
285                         throw new NotImplementedException ();
286                 }
287
288                 [MonoTODO]
289                 public override void WriteStartDocument (bool standalone)
290                 {
291                         throw new NotImplementedException ();
292                 }
293
294                 [MonoTODO("Not dealing with prefix and ns yet.")]
295                 public override void WriteStartElement (string prefix, string localName, string ns)
296                 {
297                         CheckOpenWriter ();
298                         CloseStartElement ();
299                         w.Write ("<{0}", localName);
300                         openElements.Push (localName);
301                         openStartElement = true;
302                 }
303
304                 [MonoTODO("Haven't done any entity replacements yet.")]
305                 public override void WriteString (string text)
306                 {
307                         CheckOpenWriter ();
308                         CloseStartElement ();
309                         w.Write (text);
310                 }
311
312                 [MonoTODO]
313                 public override void WriteSurrogateCharEntity (char lowChar, char highChar)
314                 {
315                         throw new NotImplementedException ();
316                 }
317
318                 [MonoTODO]
319                 public override void WriteWhitespace (string ws)
320                 {
321                         throw new NotImplementedException ();
322                 }
323
324                 #endregion
325         }
326 }