When compiling CORLIB: pass the INSIDE_CORLIB setting, so X509Certificate class is...
[mono.git] / mcs / class / System.XML / System.Xml / XmlWriter.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
12 namespace System.Xml
13 {
14         public abstract class XmlWriter
15         {
16                 #region Fields
17
18                 protected WriteState ws = WriteState.Start;
19
20                 #endregion
21
22                 #region Constructors
23
24                 protected XmlWriter () { }
25
26                 #endregion
27
28                 #region Properties
29
30                 public abstract WriteState WriteState { get; }
31                 
32                 public abstract string XmlLang { get; }
33
34                 public abstract XmlSpace XmlSpace { get; }
35
36                 #endregion
37
38                 #region Methods
39
40                 public abstract void Close ();
41
42                 public abstract void Flush ();
43
44                 public abstract string LookupPrefix (string ns);
45
46                 private void WriteAttribute (XmlReader reader, bool defattr)
47                 {
48                         if (!defattr && reader.IsDefault)
49                                 return;
50
51                         WriteStartAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI);
52                         while (reader.ReadAttributeValue ()) {
53                                 switch (reader.NodeType) {
54                                 case XmlNodeType.Text:
55                                         WriteString (reader.Value);
56                                         break;
57                                 case XmlNodeType.EntityReference:
58                                         WriteEntityRef (reader.Name);
59                                         break;
60                                 }
61                         }
62                         WriteEndAttribute ();
63                 }
64
65                 public virtual void WriteAttributes (XmlReader reader, bool defattr)
66                 {
67                         if(reader == null)
68                                 throw new ArgumentException("null XmlReader specified.", "reader");
69
70                         switch (reader.NodeType) {
71                         case XmlNodeType.XmlDeclaration:
72                                 WriteAttributeString ("version", reader ["version"]);
73                                 if (reader ["encoding"] != null)
74                                         WriteAttributeString ("encoding", reader ["encoding"]);
75                                 if (reader ["standalone"] != null)
76                                         WriteAttributeString ("standalone", reader ["standalone"]);
77                                 break;
78                         case XmlNodeType.Element:
79                                 if (reader.MoveToFirstAttribute ())
80                                         goto case XmlNodeType.Attribute;
81                                 break;
82                         case XmlNodeType.Attribute:
83                                 do {
84                                         WriteAttribute (reader, defattr);
85                                 } while (reader.MoveToNextAttribute ());
86                                 break;
87                         default:
88                                 throw new XmlException("NodeType is not one of Element, Attribute, nor XmlDeclaration.");
89                         }
90                 }
91
92                 public void WriteAttributeString (string localName, string value)
93                 {
94                         WriteAttributeString ("", localName, "", value);
95                 }
96
97                 public void WriteAttributeString (string localName, string ns, string value)
98                 {
99                         WriteAttributeString ("", localName, ns, value);
100                 }
101
102                 public void WriteAttributeString (string prefix, string localName, string ns, string value)
103                 {
104                         // In MS.NET (1.0), this check is done *here*, not at WriteStartAttribute.
105                         // (XmlTextWriter.WriteStartAttribute("xmlns", "anyname", null) throws an exception.
106                         if ((prefix == "xmlns") || (prefix == "" && localName == "xmlns"))
107                                 if (ns == null)
108                                         ns = "http://www.w3.org/2000/xmlns/";
109
110                         WriteStartAttribute (prefix, localName, ns);
111                         WriteString (value);
112                         WriteEndAttribute ();
113                 }
114
115                 public abstract void WriteBase64 (byte[] buffer, int index, int count);
116
117                 public abstract void WriteBinHex (byte[] buffer, int index, int count);
118
119                 public abstract void WriteCData (string text);
120
121                 public abstract void WriteCharEntity (char ch);
122
123                 public abstract void WriteChars (char[] buffer, int index, int count);
124
125                 public abstract void WriteComment (string text);
126
127                 public abstract void WriteDocType (string name, string pubid, string sysid, string subset);
128
129                 public void WriteElementString (string localName, string value)
130                 {
131                         WriteStartElement(localName);
132                         WriteString(value);
133                         WriteEndElement();
134                 }
135
136                 public void WriteElementString (string localName, string ns, string value)
137                 {
138                         WriteStartElement(localName, ns);
139                         WriteString(value);
140                         WriteEndElement();
141                 }
142
143                 public abstract void WriteEndAttribute ();
144
145                 public abstract void WriteEndDocument ();
146
147                 public abstract void WriteEndElement ();
148
149                 public abstract void WriteEntityRef (string name);
150
151                 public abstract void WriteFullEndElement ();
152
153                 public abstract void WriteName (string name);
154
155                 public abstract void WriteNmToken (string name);
156
157                 public virtual void WriteNode (XmlReader reader, bool defattr)
158                 {
159                         if (reader == null)
160                                 throw new ArgumentException ();
161
162                         if (reader.ReadState == ReadState.Initial) {
163                                 reader.Read ();
164                                 do {
165                                         WriteNode (reader, defattr);
166                                 } while (!reader.EOF);
167                                 return;
168                         }
169
170                         switch (reader.NodeType) {
171                         case XmlNodeType.Element:
172                                 WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
173                                 WriteAttributes (reader, defattr);
174                                 reader.MoveToElement ();
175                                 if (reader.IsEmptyElement)
176                                         WriteEndElement ();
177                                 else {
178                                         int depth = reader.Depth;
179                                         reader.Read ();
180                                         do {
181                                                 WriteNode (reader, defattr);
182                                         } while (depth < reader.Depth);
183                                         WriteFullEndElement ();
184                                 }
185                                 break;
186                         // In case of XmlAttribute, don't proceed reader.
187                         case XmlNodeType.Attribute:
188                                 return;
189                         case XmlNodeType.Text:
190                                 WriteString (reader.Value);
191                                 break;
192                         case XmlNodeType.CDATA:
193                                 WriteCData (reader.Value);
194                                 break;
195                         case XmlNodeType.EntityReference:
196                                 WriteEntityRef (reader.Name);
197                                 break;
198                         case XmlNodeType.ProcessingInstruction:
199                                 WriteProcessingInstruction (reader.Name, reader.Value);
200                                 break;
201                         case XmlNodeType.Comment:
202                                 WriteComment (reader.Value);
203                                 break;
204                         case XmlNodeType.DocumentType:
205                                 WriteDocType (reader.Name,
206                                         reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
207                                 break;
208                         case XmlNodeType.SignificantWhitespace:
209                                 goto case XmlNodeType.Whitespace;
210                         case XmlNodeType.Whitespace:
211                                 WriteWhitespace (reader.Value);
212                                 break;
213                         case XmlNodeType.EndElement:
214                                 WriteFullEndElement ();
215                                 break;
216                         case XmlNodeType.EndEntity:
217                                 break;
218                         case XmlNodeType.XmlDeclaration:
219                                 // FIXME: It seems different from MS way, but I have 
220                                 // no other idea to write start document statefully.
221                                 string st = reader.GetAttribute ("standalone");
222                                 if (st != null && st != String.Empty)
223                                         WriteStartDocument (st.ToLower () == "yes");
224                                 else
225                                         WriteStartDocument ();
226                                 break;
227                         default:
228                                 throw new XmlException ("Unexpected node " + reader.Name + " of type " + reader.NodeType);
229                         }
230                         reader.Read ();
231                 }
232
233                 public abstract void WriteProcessingInstruction (string name, string text);
234
235                 public abstract void WriteQualifiedName (string localName, string ns);
236
237                 public abstract void WriteRaw (string data);
238
239                 public abstract void WriteRaw (char[] buffer, int index, int count);
240
241                 public void WriteStartAttribute (string localName, string ns)
242                 {
243                         WriteStartAttribute (null, localName, ns);
244                 }
245
246                 public abstract void WriteStartAttribute (string prefix, string localName, string ns);
247
248                 public abstract void WriteStartDocument ();
249
250                 public abstract void WriteStartDocument (bool standalone);
251
252                 public void WriteStartElement (string localName)
253                 {
254                         WriteStartElement (null, localName, null);
255                 }
256
257                 public void WriteStartElement (string localName, string ns)
258                 {
259                         WriteStartElement (null, localName, ns);
260                 }
261
262                 public abstract void WriteStartElement (string prefix, string localName, string ns);
263
264                 public abstract void WriteString (string text);
265
266                 public abstract void WriteSurrogateCharEntity (char lowChar, char highChar);
267
268                 public abstract void WriteWhitespace (string ws);
269
270                 #endregion
271         }
272 }