2002-12-21 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]
163                 public virtual void WriteNode (XmlReader reader, bool defattr)
164                 {
165                         throw new NotImplementedException ();
166                 }
167
168                 public abstract void WriteProcessingInstruction (string name, string text);
169
170                 public abstract void WriteQualifiedName (string localName, string ns);
171
172                 public abstract void WriteRaw (string data);
173
174                 public abstract void WriteRaw (char[] buffer, int index, int count);
175
176                 public void WriteStartAttribute (string localName, string ns)
177                 {
178                         WriteStartAttribute ("", localName, ns);
179                 }
180
181                 public abstract void WriteStartAttribute (string prefix, string localName, string ns);
182
183                 public abstract void WriteStartDocument ();
184
185                 public abstract void WriteStartDocument (bool standalone);
186
187                 public void WriteStartElement (string localName)
188                 {
189                         WriteStartElement (String.Empty, localName, String.Empty);
190                 }
191
192                 public void WriteStartElement (string localName, string ns)
193                 {
194                         WriteStartElement (String.Empty, localName, ns);
195                 }
196
197                 public abstract void WriteStartElement (string prefix, string localName, string ns);
198
199                 public abstract void WriteString (string text);
200
201                 public abstract void WriteSurrogateCharEntity (char lowChar, char highChar);
202
203                 public abstract void WriteWhitespace (string ws);
204
205                 #endregion
206         }
207 }