XmlWriter.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //
  2. // System.Xml.XmlWriter
  3. //
  4. // Authors:
  5. // Kral Ferch <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. //
  8. // (C) 2002 Kral Ferch
  9. // (C) 2002-2003 Atsushi Enomoto
  10. //
  11. using System;
  12. namespace System.Xml
  13. {
  14. #if NET_1_2
  15. public abstract class XmlWriter : IDisposable
  16. #else
  17. public abstract class XmlWriter
  18. #endif
  19. {
  20. #region Constructors
  21. protected XmlWriter () { }
  22. #endregion
  23. #region Properties
  24. public abstract WriteState WriteState { get; }
  25. public abstract string XmlLang { get; }
  26. public abstract XmlSpace XmlSpace { get; }
  27. #endregion
  28. #region Methods
  29. public abstract void Close ();
  30. #if NET_1_2
  31. public virtual void Dispose ()
  32. {
  33. Close ();
  34. }
  35. #endif
  36. public abstract void Flush ();
  37. public abstract string LookupPrefix (string ns);
  38. private void WriteAttribute (XmlReader reader, bool defattr)
  39. {
  40. if (!defattr && reader.IsDefault)
  41. return;
  42. WriteStartAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI);
  43. while (reader.ReadAttributeValue ()) {
  44. switch (reader.NodeType) {
  45. case XmlNodeType.Text:
  46. WriteString (reader.Value);
  47. break;
  48. case XmlNodeType.EntityReference:
  49. WriteEntityRef (reader.Name);
  50. break;
  51. }
  52. }
  53. WriteEndAttribute ();
  54. }
  55. public virtual void WriteAttributes (XmlReader reader, bool defattr)
  56. {
  57. if(reader == null)
  58. throw new ArgumentException("null XmlReader specified.", "reader");
  59. switch (reader.NodeType) {
  60. case XmlNodeType.XmlDeclaration:
  61. WriteAttributeString ("version", reader ["version"]);
  62. if (reader ["encoding"] != null)
  63. WriteAttributeString ("encoding", reader ["encoding"]);
  64. if (reader ["standalone"] != null)
  65. WriteAttributeString ("standalone", reader ["standalone"]);
  66. break;
  67. case XmlNodeType.Element:
  68. if (reader.MoveToFirstAttribute ())
  69. goto case XmlNodeType.Attribute;
  70. break;
  71. case XmlNodeType.Attribute:
  72. do {
  73. WriteAttribute (reader, defattr);
  74. } while (reader.MoveToNextAttribute ());
  75. break;
  76. default:
  77. throw new XmlException("NodeType is not one of Element, Attribute, nor XmlDeclaration.");
  78. }
  79. }
  80. public void WriteAttributeString (string localName, string value)
  81. {
  82. WriteAttributeString ("", localName, null, value);
  83. }
  84. public void WriteAttributeString (string localName, string ns, string value)
  85. {
  86. WriteAttributeString ("", localName, ns, value);
  87. }
  88. public void WriteAttributeString (string prefix, string localName, string ns, string value)
  89. {
  90. // In MS.NET (1.0), this check is done *here*, not at WriteStartAttribute.
  91. // (XmlTextWriter.WriteStartAttribute("xmlns", "anyname", null) throws an exception.
  92. #if NET_1_0
  93. if ((prefix == "xmlns" || (prefix == "" && localName == "xmlns")) && ns == null)
  94. ns = "http://www.w3.org/2000/xmlns/";
  95. #endif
  96. WriteStartAttribute (prefix, localName, ns);
  97. WriteString (value);
  98. WriteEndAttribute ();
  99. }
  100. public abstract void WriteBase64 (byte[] buffer, int index, int count);
  101. public abstract void WriteBinHex (byte[] buffer, int index, int count);
  102. public abstract void WriteCData (string text);
  103. public abstract void WriteCharEntity (char ch);
  104. public abstract void WriteChars (char[] buffer, int index, int count);
  105. public abstract void WriteComment (string text);
  106. public abstract void WriteDocType (string name, string pubid, string sysid, string subset);
  107. public void WriteElementString (string localName, string value)
  108. {
  109. WriteStartElement(localName);
  110. WriteString(value);
  111. WriteEndElement();
  112. }
  113. public void WriteElementString (string localName, string ns, string value)
  114. {
  115. WriteStartElement(localName, ns);
  116. WriteString(value);
  117. WriteEndElement();
  118. }
  119. public abstract void WriteEndAttribute ();
  120. public abstract void WriteEndDocument ();
  121. public abstract void WriteEndElement ();
  122. public abstract void WriteEntityRef (string name);
  123. public abstract void WriteFullEndElement ();
  124. public abstract void WriteName (string name);
  125. public abstract void WriteNmToken (string name);
  126. public virtual void WriteNode (XmlReader reader, bool defattr)
  127. {
  128. if (reader == null)
  129. throw new ArgumentException ();
  130. if (reader.ReadState == ReadState.Initial) {
  131. reader.Read ();
  132. do {
  133. WriteNode (reader, defattr);
  134. } while (!reader.EOF);
  135. return;
  136. }
  137. switch (reader.NodeType) {
  138. case XmlNodeType.Element:
  139. WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
  140. WriteAttributes (reader, defattr);
  141. reader.MoveToElement ();
  142. if (reader.IsEmptyElement)
  143. WriteEndElement ();
  144. else {
  145. int depth = reader.Depth;
  146. reader.Read ();
  147. if (reader.NodeType != XmlNodeType.EndElement) {
  148. do {
  149. WriteNode (reader, defattr);
  150. } while (depth < reader.Depth);
  151. }
  152. WriteFullEndElement ();
  153. }
  154. break;
  155. // In case of XmlAttribute, don't proceed reader.
  156. case XmlNodeType.Attribute:
  157. return;
  158. case XmlNodeType.Text:
  159. WriteString (reader.Value);
  160. break;
  161. case XmlNodeType.CDATA:
  162. WriteCData (reader.Value);
  163. break;
  164. case XmlNodeType.EntityReference:
  165. WriteEntityRef (reader.Name);
  166. break;
  167. case XmlNodeType.XmlDeclaration:
  168. // LAMESPEC: It means that XmlWriter implementation _must not_ check
  169. // whether PI name is "xml" (it is XML error) or not.
  170. case XmlNodeType.ProcessingInstruction:
  171. WriteProcessingInstruction (reader.Name, reader.Value);
  172. break;
  173. case XmlNodeType.Comment:
  174. WriteComment (reader.Value);
  175. break;
  176. case XmlNodeType.DocumentType:
  177. WriteDocType (reader.Name,
  178. reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
  179. break;
  180. case XmlNodeType.SignificantWhitespace:
  181. goto case XmlNodeType.Whitespace;
  182. case XmlNodeType.Whitespace:
  183. WriteWhitespace (reader.Value);
  184. break;
  185. case XmlNodeType.EndElement:
  186. WriteFullEndElement ();
  187. break;
  188. case XmlNodeType.EndEntity:
  189. break;
  190. case XmlNodeType.None:
  191. return; // Do nothing, nor reporting errors.
  192. default:
  193. throw new XmlException ("Unexpected node " + reader.Name + " of type " + reader.NodeType);
  194. }
  195. reader.Read ();
  196. }
  197. public abstract void WriteProcessingInstruction (string name, string text);
  198. public abstract void WriteQualifiedName (string localName, string ns);
  199. public abstract void WriteRaw (string data);
  200. public abstract void WriteRaw (char[] buffer, int index, int count);
  201. public void WriteStartAttribute (string localName, string ns)
  202. {
  203. WriteStartAttribute (null, localName, ns);
  204. }
  205. public abstract void WriteStartAttribute (string prefix, string localName, string ns);
  206. public abstract void WriteStartDocument ();
  207. public abstract void WriteStartDocument (bool standalone);
  208. public void WriteStartElement (string localName)
  209. {
  210. WriteStartElement (null, localName, null);
  211. }
  212. public void WriteStartElement (string localName, string ns)
  213. {
  214. WriteStartElement (null, localName, ns);
  215. }
  216. public abstract void WriteStartElement (string prefix, string localName, string ns);
  217. public abstract void WriteString (string text);
  218. public abstract void WriteSurrogateCharEntity (char lowChar, char highChar);
  219. public abstract void WriteWhitespace (string ws);
  220. #endregion
  221. }
  222. }