2003-01-26 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[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                 protected XmlNamespaceManager namespaceManager = new XmlNamespaceManager (new NameTable ());
20
21                 #endregion
22
23                 #region Constructors
24
25                 protected XmlWriter () { }
26
27                 #endregion
28
29                 #region Properties
30
31                 public abstract WriteState WriteState { get; }
32                 
33                 public abstract string XmlLang { get; }
34
35                 public abstract XmlSpace XmlSpace { get; }
36
37                 #endregion
38
39                 #region Methods
40
41                 public abstract void Close ();
42
43                 public abstract void Flush ();
44
45                 public abstract string LookupPrefix (string ns);
46
47                 [MonoTODO("DTDs must be implemented to use 'defattr' parameter.")]
48                 public virtual void WriteAttributes (XmlReader reader, bool defattr)
49                 {
50                         if(reader == null)
51                                 throw new ArgumentException("null XmlReader specified.", "reader");
52
53                         switch(reader.NodeType)
54                         {
55                                 case XmlNodeType.XmlDeclaration:
56                                         // this method doesn't write "<?xml " and "?>", at least MS .NET Framework as yet.
57                                         XmlDeclaration decl = new XmlDeclaration("1.0", String.Empty, String.Empty, null);
58                                         decl.Value = reader.Value;
59                                         if(decl.Version != null && decl.Version != String.Empty) WriteAttributeString("version", decl.Version);
60                                         if(decl.Encoding != null && decl.Encoding != String.Empty) WriteAttributeString("encoding", decl.Encoding);
61                                         if(decl.Standalone != null && decl.Standalone != String.Empty) WriteAttributeString("standalone", decl.Standalone);
62                                         break;
63                                 case XmlNodeType.Element:
64                                         while (reader.MoveToNextAttribute ()) 
65                                         {
66                                                 WriteAttributeString(reader.Prefix, reader.LocalName, reader.NamespaceURI, reader.Value);
67                                         }
68                                         break;
69                                 case XmlNodeType.Attribute:
70                                         do
71                                         {
72                                                 WriteAttributeString(reader.Prefix, reader.LocalName, reader.NamespaceURI, reader.Value);
73                                         }
74                                         while (reader.MoveToNextAttribute ()) ;
75                                         break;
76                                 default:
77                                         throw new XmlException("NodeType is not one of Element, Attribute, nor XmlDeclaration.");
78                         }
79                 }
80
81                 public void WriteAttributeString (string localName, string value)
82                 {
83                         WriteAttributeString ("", localName, "", value);
84                 }
85
86                 public void WriteAttributeString (string localName, string ns, string value)
87                 {
88                         WriteAttributeString ("", localName, ns, value);
89                 }
90
91                 public void WriteAttributeString (string prefix, string localName, string ns, string value)
92                 {
93                         if ((prefix == "xmlns") || (localName == "xmlns"))
94                           {
95                                 ns = value;
96                                 
97                                 if (prefix == "xmlns" && namespaceManager.HasNamespace (localName))
98                                         return;
99                                 
100                                 /* Users need to be able to re-declare the default namespace for subnodes
101                                 else if (localName == "xmlns" && namespaceManager.HasNamespace (String.Empty))
102                                         return;
103                                 */
104                           }
105                         
106                         WriteStartAttribute (prefix, localName, ns);
107                         WriteString (value);
108                         WriteEndAttribute ();
109
110                         if ((prefix == "xmlns") || (localName == "xmlns")) 
111                         {
112                                 if (prefix == "xmlns")
113                                         namespaceManager.AddNamespace (localName, ns);
114                                 else
115                                         namespaceManager.AddNamespace ("", ns);
116                         }
117                         
118                 }
119
120                 public abstract void WriteBase64 (byte[] buffer, int index, int count);
121
122                 public abstract void WriteBinHex (byte[] buffer, int index, int count);
123
124                 public abstract void WriteCData (string text);
125
126                 public abstract void WriteCharEntity (char ch);
127
128                 public abstract void WriteChars (char[] buffer, int index, int count);
129
130                 public abstract void WriteComment (string text);
131
132                 public abstract void WriteDocType (string name, string pubid, string sysid, string subset);
133
134                 public void WriteElementString (string localName, string value)
135                 {
136                         WriteStartElement(localName);
137                         WriteString(value);
138                         WriteEndElement();
139                 }
140
141                 public void WriteElementString (string localName, string ns, string value)
142                 {
143                         WriteStartElement(localName, ns);
144                         WriteString(value);
145                         WriteEndElement();
146                 }
147
148                 public abstract void WriteEndAttribute ();
149
150                 public abstract void WriteEndDocument ();
151
152                 public abstract void WriteEndElement ();
153
154                 public abstract void WriteEntityRef (string name);
155
156                 public abstract void WriteFullEndElement ();
157
158                 public abstract void WriteName (string name);
159
160                 public abstract void WriteNmToken (string name);
161
162                 [MonoTODO("needs to test")]
163                 public virtual void WriteNode (XmlReader reader, bool defattr)
164                 {
165                         if (reader == null)
166                                 throw new ArgumentException ();
167
168                         if (reader.ReadState == ReadState.Initial) {
169                                 while (reader.Read ())
170                                         WriteNode (reader, defattr);
171                         }
172                         else {
173                                 switch (reader.NodeType) {
174                                 case XmlNodeType.Element:
175                                         WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
176                                         WriteAttributes (reader, defattr);
177                                         if (reader.IsEmptyElement)
178                                                 WriteEndElement ();
179                                         break;
180                                 case XmlNodeType.Attribute:
181                                         break;
182                                 case XmlNodeType.Text:
183                                         WriteString (reader.Value);
184                                         break;
185                                 case XmlNodeType.CDATA:
186                                         WriteCData (reader.Value);
187                                         break;
188                                 case XmlNodeType.EntityReference:
189                                         WriteEntityRef (reader.Name);
190                                         break;
191                                 case XmlNodeType.ProcessingInstruction:
192                                         WriteProcessingInstruction (reader.Name, reader.Value);
193                                         break;
194                                 case XmlNodeType.Comment:
195                                         WriteComment (reader.Value);
196                                         break;
197                                 case XmlNodeType.DocumentType:
198                                         WriteDocType (reader.Name,
199                                                 reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
200                                         break;
201                                 case XmlNodeType.SignificantWhitespace:
202                                         goto case XmlNodeType.Whitespace;
203                                 case XmlNodeType.Whitespace:
204                                         WriteWhitespace (reader.Value);
205                                         break;
206                                 case XmlNodeType.EndElement:
207                                         break;
208                                 case XmlNodeType.EndEntity:
209                                         break;
210                                 case XmlNodeType.XmlDeclaration:
211                                         WriteStartDocument (reader.GetAttribute  ("standalone").ToLower () == "yes");
212                                         break;
213                                 default:
214                                         throw new NotImplementedException ();
215                                 }
216                         }
217                 }
218
219                 public abstract void WriteProcessingInstruction (string name, string text);
220
221                 public abstract void WriteQualifiedName (string localName, string ns);
222
223                 public abstract void WriteRaw (string data);
224
225                 public abstract void WriteRaw (char[] buffer, int index, int count);
226
227                 public void WriteStartAttribute (string localName, string ns)
228                 {
229                         WriteStartAttribute (null, localName, ns);
230                 }
231
232                 public abstract void WriteStartAttribute (string prefix, string localName, string ns);
233
234                 public abstract void WriteStartDocument ();
235
236                 public abstract void WriteStartDocument (bool standalone);
237
238                 public void WriteStartElement (string localName)
239                 {
240                         WriteStartElement (null, localName, null);
241                 }
242
243                 public void WriteStartElement (string localName, string ns)
244                 {
245                         WriteStartElement (null, localName, ns);
246                 }
247
248                 public abstract void WriteStartElement (string prefix, string localName, string ns);
249
250                 public abstract void WriteString (string text);
251
252                 public abstract void WriteSurrogateCharEntity (char lowChar, char highChar);
253
254                 public abstract void WriteWhitespace (string ws);
255
256                 #endregion
257         }
258 }