XmlWriter.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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_2_0
  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_2_0
  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. #if NET_2_0
  120. public void WriteElementString (string prefix, string localName, string ns, string value)
  121. {
  122. WriteStartElement(prefix, localName, ns);
  123. WriteString(value);
  124. WriteEndElement();
  125. }
  126. #endif
  127. public abstract void WriteEndAttribute ();
  128. public abstract void WriteEndDocument ();
  129. public abstract void WriteEndElement ();
  130. public abstract void WriteEntityRef (string name);
  131. public abstract void WriteFullEndElement ();
  132. public abstract void WriteName (string name);
  133. public abstract void WriteNmToken (string name);
  134. public virtual void WriteNode (XmlReader reader, bool defattr)
  135. {
  136. if (reader == null)
  137. throw new ArgumentException ();
  138. if (reader.ReadState == ReadState.Initial) {
  139. reader.Read ();
  140. do {
  141. WriteNode (reader, defattr);
  142. } while (!reader.EOF);
  143. return;
  144. }
  145. switch (reader.NodeType) {
  146. case XmlNodeType.Element:
  147. WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
  148. #if false
  149. WriteAttributes (reader, defattr);
  150. reader.MoveToElement ();
  151. #else
  152. // Well, I found that MS.NET took this way, since
  153. // there was a error-prone SgmlReader that fails
  154. // MoveToNextAttribute().
  155. if (reader.HasAttributes) {
  156. for (int i = 0; i < reader.AttributeCount; i++) {
  157. reader.MoveToAttribute (i);
  158. WriteAttribute (reader, defattr);
  159. }
  160. reader.MoveToElement ();
  161. }
  162. #endif
  163. if (reader.IsEmptyElement)
  164. WriteEndElement ();
  165. else {
  166. int depth = reader.Depth;
  167. reader.Read ();
  168. if (reader.NodeType != XmlNodeType.EndElement) {
  169. do {
  170. WriteNode (reader, defattr);
  171. } while (depth < reader.Depth);
  172. }
  173. WriteFullEndElement ();
  174. }
  175. break;
  176. // In case of XmlAttribute, don't proceed reader.
  177. case XmlNodeType.Attribute:
  178. return;
  179. case XmlNodeType.Text:
  180. WriteString (reader.Value);
  181. break;
  182. case XmlNodeType.CDATA:
  183. WriteCData (reader.Value);
  184. break;
  185. case XmlNodeType.EntityReference:
  186. WriteEntityRef (reader.Name);
  187. break;
  188. case XmlNodeType.XmlDeclaration:
  189. // LAMESPEC: It means that XmlWriter implementation _must not_ check
  190. // whether PI name is "xml" (it is XML error) or not.
  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. WriteFullEndElement ();
  208. break;
  209. case XmlNodeType.EndEntity:
  210. break;
  211. case XmlNodeType.None:
  212. return; // Do nothing, nor reporting errors.
  213. default:
  214. throw new XmlException ("Unexpected node " + reader.Name + " of type " + reader.NodeType);
  215. }
  216. reader.Read ();
  217. }
  218. public abstract void WriteProcessingInstruction (string name, string text);
  219. public abstract void WriteQualifiedName (string localName, string ns);
  220. public abstract void WriteRaw (string data);
  221. public abstract void WriteRaw (char[] buffer, int index, int count);
  222. public void WriteStartAttribute (string localName, string ns)
  223. {
  224. WriteStartAttribute (null, localName, ns);
  225. }
  226. public abstract void WriteStartAttribute (string prefix, string localName, string ns);
  227. public abstract void WriteStartDocument ();
  228. public abstract void WriteStartDocument (bool standalone);
  229. public void WriteStartElement (string localName)
  230. {
  231. WriteStartElement (null, localName, null);
  232. }
  233. public void WriteStartElement (string localName, string ns)
  234. {
  235. WriteStartElement (null, localName, ns);
  236. }
  237. public abstract void WriteStartElement (string prefix, string localName, string ns);
  238. public abstract void WriteString (string text);
  239. public abstract void WriteSurrogateCharEntity (char lowChar, char highChar);
  240. public abstract void WriteWhitespace (string ws);
  241. #endregion
  242. }
  243. }