2 // System.Xml.XmlTextWriter
5 // Kral Ferch <kral_ferch@hotmail.com>
14 public abstract class XmlWriter
18 protected WriteState ws = WriteState.Start;
19 protected XmlNamespaceManager namespaceManager = new XmlNamespaceManager (new NameTable ());
25 protected XmlWriter () { }
31 public abstract WriteState WriteState { get; }
33 public abstract string XmlLang { get; }
35 public abstract XmlSpace XmlSpace { get; }
41 public abstract void Close ();
43 public abstract void Flush ();
45 public abstract string LookupPrefix (string ns);
47 [MonoTODO("DTDs must be implemented to use 'defattr' parameter.")]
48 public virtual void WriteAttributes (XmlReader reader, bool defattr)
51 throw new ArgumentException("null XmlReader specified.", "reader");
53 switch (reader.NodeType) {
54 case XmlNodeType.XmlDeclaration:
55 string val = reader ["version"];
56 if (val != String.Empty)
57 WriteAttributeString ("version", val);
58 val = reader ["encoding"];
59 if (val != String.Empty)
60 WriteAttributeString ("encoding", val);
61 val = reader ["standalone"];
62 if(val != String.Empty)
63 WriteAttributeString ("standalone", val);
65 case XmlNodeType.Element:
66 if (reader.MoveToFirstAttribute ())
67 goto case XmlNodeType.Attribute;
69 case XmlNodeType.Attribute:
71 // FIXME: use ReadAttributeValue () for strictly write EntityReference.
72 WriteAttributeString (reader.Prefix, reader.LocalName, reader.NamespaceURI, reader.Value);
73 } while (reader.MoveToNextAttribute ());
76 throw new XmlException("NodeType is not one of Element, Attribute, nor XmlDeclaration.");
80 public void WriteAttributeString (string localName, string value)
82 WriteAttributeString ("", localName, "", value);
85 public void WriteAttributeString (string localName, string ns, string value)
87 WriteAttributeString ("", localName, ns, value);
90 public void WriteAttributeString (string prefix, string localName, string ns, string value)
92 if ((prefix == "xmlns") || (localName == "xmlns"))
96 if (prefix == "xmlns" && namespaceManager.HasNamespace (localName))
99 /* Users need to be able to re-declare the default namespace for subnodes
100 else if (localName == "xmlns" && namespaceManager.HasNamespace (String.Empty))
105 WriteStartAttribute (prefix, localName, ns);
107 WriteEndAttribute ();
109 if ((prefix == "xmlns") || (localName == "xmlns"))
111 if (prefix == "xmlns")
112 namespaceManager.AddNamespace (localName, ns);
114 namespaceManager.AddNamespace ("", ns);
119 public abstract void WriteBase64 (byte[] buffer, int index, int count);
121 public abstract void WriteBinHex (byte[] buffer, int index, int count);
123 public abstract void WriteCData (string text);
125 public abstract void WriteCharEntity (char ch);
127 public abstract void WriteChars (char[] buffer, int index, int count);
129 public abstract void WriteComment (string text);
131 public abstract void WriteDocType (string name, string pubid, string sysid, string subset);
133 public void WriteElementString (string localName, string value)
135 WriteStartElement(localName);
140 public void WriteElementString (string localName, string ns, string value)
142 WriteStartElement(localName, ns);
147 public abstract void WriteEndAttribute ();
149 public abstract void WriteEndDocument ();
151 public abstract void WriteEndElement ();
153 public abstract void WriteEntityRef (string name);
155 public abstract void WriteFullEndElement ();
157 public abstract void WriteName (string name);
159 public abstract void WriteNmToken (string name);
161 [MonoTODO("needs to test")]
162 public virtual void WriteNode (XmlReader reader, bool defattr)
165 throw new ArgumentException ();
167 if (reader.ReadState == ReadState.Initial) {
170 WriteNode (reader, defattr);
171 } while (!reader.EOF);
175 switch (reader.NodeType) {
176 case XmlNodeType.Element:
177 WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
178 WriteAttributes (reader, defattr);
179 reader.MoveToElement ();
180 if (reader.IsEmptyElement)
183 int depth = reader.Depth;
186 WriteNode (reader, defattr);
187 } while (depth < reader.Depth);
191 // In case of XmlAttribute, don't proceed reader.
192 case XmlNodeType.Attribute:
194 case XmlNodeType.Text:
195 WriteString (reader.Value);
197 case XmlNodeType.CDATA:
198 WriteCData (reader.Value);
200 case XmlNodeType.EntityReference:
201 WriteEntityRef (reader.Name);
203 case XmlNodeType.ProcessingInstruction:
204 WriteProcessingInstruction (reader.Name, reader.Value);
206 case XmlNodeType.Comment:
207 WriteComment (reader.Value);
209 case XmlNodeType.DocumentType:
210 WriteDocType (reader.Name,
211 reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
213 case XmlNodeType.SignificantWhitespace:
214 goto case XmlNodeType.Whitespace;
215 case XmlNodeType.Whitespace:
216 WriteWhitespace (reader.Value);
218 case XmlNodeType.EndElement:
221 case XmlNodeType.EndEntity:
223 case XmlNodeType.XmlDeclaration:
224 string st = reader.GetAttribute ("standalone");
225 if (st != String.Empty)
226 WriteStartDocument (st.ToLower () == "yes");
228 WriteStartDocument ();
231 throw new NotImplementedException ();
236 public abstract void WriteProcessingInstruction (string name, string text);
238 public abstract void WriteQualifiedName (string localName, string ns);
240 public abstract void WriteRaw (string data);
242 public abstract void WriteRaw (char[] buffer, int index, int count);
244 public void WriteStartAttribute (string localName, string ns)
246 WriteStartAttribute (null, localName, ns);
249 public abstract void WriteStartAttribute (string prefix, string localName, string ns);
251 public abstract void WriteStartDocument ();
253 public abstract void WriteStartDocument (bool standalone);
255 public void WriteStartElement (string localName)
257 WriteStartElement (null, localName, null);
260 public void WriteStartElement (string localName, string ns)
262 WriteStartElement (null, localName, ns);
265 public abstract void WriteStartElement (string prefix, string localName, string ns);
267 public abstract void WriteString (string text);
269 public abstract void WriteSurrogateCharEntity (char lowChar, char highChar);
271 public abstract void WriteWhitespace (string ws);