XmlWriter.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // System.Xml.XmlTextWriter
  3. //
  4. // Author:
  5. // Kral Ferch <[email protected]>
  6. //
  7. // (C) 2002 Kral Ferch
  8. //
  9. using System;
  10. namespace System.Xml
  11. {
  12. public abstract class XmlWriter
  13. {
  14. #region Fields
  15. protected WriteState ws = WriteState.Start;
  16. protected XmlNamespaceManager namespaceManager = new XmlNamespaceManager (new NameTable ());
  17. #endregion
  18. #region Constructors
  19. protected XmlWriter () { }
  20. #endregion
  21. #region Properties
  22. public abstract WriteState WriteState { get; }
  23. public abstract string XmlLang { get; }
  24. public abstract XmlSpace XmlSpace { get; }
  25. #endregion
  26. #region Methods
  27. public abstract void Close ();
  28. public abstract void Flush ();
  29. public abstract string LookupPrefix (string ns);
  30. [MonoTODO("DTDs must be implemented to use 'defattr' parameter.")]
  31. public virtual void WriteAttributes (XmlReader reader, bool defattr)
  32. {
  33. if(reader == null)
  34. throw new ArgumentException("null XmlReader specified.", "reader");
  35. switch (reader.NodeType) {
  36. case XmlNodeType.XmlDeclaration:
  37. case XmlNodeType.Element:
  38. if (reader.MoveToFirstAttribute ())
  39. goto case XmlNodeType.Attribute;
  40. break;
  41. case XmlNodeType.Attribute:
  42. do {
  43. // FIXME: use ReadAttributeValue () for strictly write EntityReference.
  44. WriteAttributeString (reader.Prefix, reader.LocalName, reader.NamespaceURI, reader.Value);
  45. } while (reader.MoveToNextAttribute ());
  46. break;
  47. default:
  48. throw new XmlException("NodeType is not one of Element, Attribute, nor XmlDeclaration.");
  49. }
  50. }
  51. public void WriteAttributeString (string localName, string value)
  52. {
  53. WriteAttributeString ("", localName, "", value);
  54. }
  55. public void WriteAttributeString (string localName, string ns, string value)
  56. {
  57. WriteAttributeString ("", localName, ns, value);
  58. }
  59. public void WriteAttributeString (string prefix, string localName, string ns, string value)
  60. {
  61. if ((prefix == "xmlns") || (prefix == "" && localName == "xmlns"))
  62. {
  63. if (ns == null) ns = "http://www.w3.org/2000/xmlns/";
  64. if (prefix == "xmlns" && namespaceManager.HasNamespace (localName))
  65. return;
  66. }
  67. WriteStartAttribute (prefix, localName, ns);
  68. WriteString (value);
  69. WriteEndAttribute ();
  70. if ((prefix == "xmlns") || (localName == "xmlns"))
  71. {
  72. if (prefix == "xmlns")
  73. {
  74. if (value == string.Empty) throw new ArgumentException ("Cannot use a prefix with an empty namespace");
  75. namespaceManager.AddNamespace (localName, value);
  76. }
  77. else
  78. namespaceManager.AddNamespace ("", value);
  79. }
  80. }
  81. public abstract void WriteBase64 (byte[] buffer, int index, int count);
  82. public abstract void WriteBinHex (byte[] buffer, int index, int count);
  83. public abstract void WriteCData (string text);
  84. public abstract void WriteCharEntity (char ch);
  85. public abstract void WriteChars (char[] buffer, int index, int count);
  86. public abstract void WriteComment (string text);
  87. public abstract void WriteDocType (string name, string pubid, string sysid, string subset);
  88. public void WriteElementString (string localName, string value)
  89. {
  90. WriteStartElement(localName);
  91. WriteString(value);
  92. WriteEndElement();
  93. }
  94. public void WriteElementString (string localName, string ns, string value)
  95. {
  96. WriteStartElement(localName, ns);
  97. WriteString(value);
  98. WriteEndElement();
  99. }
  100. public abstract void WriteEndAttribute ();
  101. public abstract void WriteEndDocument ();
  102. public abstract void WriteEndElement ();
  103. public abstract void WriteEntityRef (string name);
  104. public abstract void WriteFullEndElement ();
  105. public abstract void WriteName (string name);
  106. public abstract void WriteNmToken (string name);
  107. [MonoTODO("needs to test")]
  108. public virtual void WriteNode (XmlReader reader, bool defattr)
  109. {
  110. if (reader == null)
  111. throw new ArgumentException ();
  112. if (reader.ReadState == ReadState.Initial) {
  113. reader.Read ();
  114. do {
  115. WriteNode (reader, defattr);
  116. } while (!reader.EOF);
  117. return;
  118. }
  119. switch (reader.NodeType) {
  120. case XmlNodeType.Element:
  121. WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
  122. WriteAttributes (reader, defattr);
  123. reader.MoveToElement ();
  124. if (reader.IsEmptyElement)
  125. WriteEndElement ();
  126. else {
  127. int depth = reader.Depth;
  128. reader.Read ();
  129. do {
  130. WriteNode (reader, defattr);
  131. } while (depth < reader.Depth);
  132. WriteFullEndElement ();
  133. }
  134. break;
  135. // In case of XmlAttribute, don't proceed reader.
  136. case XmlNodeType.Attribute:
  137. return;
  138. case XmlNodeType.Text:
  139. WriteString (reader.Value);
  140. break;
  141. case XmlNodeType.CDATA:
  142. WriteCData (reader.Value);
  143. break;
  144. case XmlNodeType.EntityReference:
  145. WriteEntityRef (reader.Name);
  146. break;
  147. case XmlNodeType.ProcessingInstruction:
  148. WriteProcessingInstruction (reader.Name, reader.Value);
  149. break;
  150. case XmlNodeType.Comment:
  151. WriteComment (reader.Value);
  152. break;
  153. case XmlNodeType.DocumentType:
  154. WriteDocType (reader.Name,
  155. reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
  156. break;
  157. case XmlNodeType.SignificantWhitespace:
  158. goto case XmlNodeType.Whitespace;
  159. case XmlNodeType.Whitespace:
  160. WriteWhitespace (reader.Value);
  161. break;
  162. case XmlNodeType.EndElement:
  163. WriteEndElement ();
  164. break;
  165. case XmlNodeType.EndEntity:
  166. break;
  167. case XmlNodeType.XmlDeclaration:
  168. string st = reader.GetAttribute ("standalone");
  169. if (st != String.Empty)
  170. WriteStartDocument (st.ToLower () == "yes");
  171. else
  172. WriteStartDocument ();
  173. break;
  174. default:
  175. throw new NotImplementedException ();
  176. }
  177. reader.Read ();
  178. }
  179. public abstract void WriteProcessingInstruction (string name, string text);
  180. public abstract void WriteQualifiedName (string localName, string ns);
  181. public abstract void WriteRaw (string data);
  182. public abstract void WriteRaw (char[] buffer, int index, int count);
  183. public void WriteStartAttribute (string localName, string ns)
  184. {
  185. WriteStartAttribute (null, localName, ns);
  186. }
  187. public abstract void WriteStartAttribute (string prefix, string localName, string ns);
  188. public abstract void WriteStartDocument ();
  189. public abstract void WriteStartDocument (bool standalone);
  190. public void WriteStartElement (string localName)
  191. {
  192. WriteStartElement (null, localName, null);
  193. }
  194. public void WriteStartElement (string localName, string ns)
  195. {
  196. WriteStartElement (null, localName, ns);
  197. }
  198. public abstract void WriteStartElement (string prefix, string localName, string ns);
  199. public abstract void WriteString (string text);
  200. public abstract void WriteSurrogateCharEntity (char lowChar, char highChar);
  201. public abstract void WriteWhitespace (string ws);
  202. #endregion
  203. }
  204. }