XmlWriter.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. namespace System.Xml
  33. {
  34. #if NET_2_0
  35. public abstract class XmlWriter : IDisposable
  36. #else
  37. public abstract class XmlWriter
  38. #endif
  39. {
  40. #region Constructors
  41. protected XmlWriter () { }
  42. #endregion
  43. #region Properties
  44. public abstract WriteState WriteState { get; }
  45. public abstract string XmlLang { get; }
  46. public abstract XmlSpace XmlSpace { get; }
  47. #endregion
  48. #region Methods
  49. public abstract void Close ();
  50. #if NET_2_0
  51. public virtual void Dispose ()
  52. {
  53. Close ();
  54. }
  55. #endif
  56. public abstract void Flush ();
  57. public abstract string LookupPrefix (string ns);
  58. private void WriteAttribute (XmlReader reader, bool defattr)
  59. {
  60. if (!defattr && reader.IsDefault)
  61. return;
  62. WriteStartAttribute (reader.Prefix, reader.LocalName, reader.NamespaceURI);
  63. while (reader.ReadAttributeValue ()) {
  64. switch (reader.NodeType) {
  65. case XmlNodeType.Text:
  66. WriteString (reader.Value);
  67. break;
  68. case XmlNodeType.EntityReference:
  69. WriteEntityRef (reader.Name);
  70. break;
  71. }
  72. }
  73. WriteEndAttribute ();
  74. }
  75. public virtual void WriteAttributes (XmlReader reader, bool defattr)
  76. {
  77. if(reader == null)
  78. throw new ArgumentException("null XmlReader specified.", "reader");
  79. switch (reader.NodeType) {
  80. case XmlNodeType.XmlDeclaration:
  81. WriteAttributeString ("version", reader ["version"]);
  82. if (reader ["encoding"] != null)
  83. WriteAttributeString ("encoding", reader ["encoding"]);
  84. if (reader ["standalone"] != null)
  85. WriteAttributeString ("standalone", reader ["standalone"]);
  86. break;
  87. case XmlNodeType.Element:
  88. if (reader.MoveToFirstAttribute ())
  89. goto case XmlNodeType.Attribute;
  90. break;
  91. case XmlNodeType.Attribute:
  92. do {
  93. WriteAttribute (reader, defattr);
  94. } while (reader.MoveToNextAttribute ());
  95. break;
  96. default:
  97. throw new XmlException("NodeType is not one of Element, Attribute, nor XmlDeclaration.");
  98. }
  99. }
  100. public void WriteAttributeString (string localName, string value)
  101. {
  102. WriteAttributeString ("", localName, null, value);
  103. }
  104. public void WriteAttributeString (string localName, string ns, string value)
  105. {
  106. WriteAttributeString ("", localName, ns, value);
  107. }
  108. public void WriteAttributeString (string prefix, string localName, string ns, string value)
  109. {
  110. // In MS.NET (1.0), this check is done *here*, not at WriteStartAttribute.
  111. // (XmlTextWriter.WriteStartAttribute("xmlns", "anyname", null) throws an exception.
  112. #if NET_1_0
  113. if ((prefix == "xmlns" || (prefix == "" && localName == "xmlns")) && ns == null)
  114. ns = "http://www.w3.org/2000/xmlns/";
  115. #endif
  116. WriteStartAttribute (prefix, localName, ns);
  117. WriteString (value);
  118. WriteEndAttribute ();
  119. }
  120. public abstract void WriteBase64 (byte[] buffer, int index, int count);
  121. public abstract void WriteBinHex (byte[] buffer, int index, int count);
  122. public abstract void WriteCData (string text);
  123. public abstract void WriteCharEntity (char ch);
  124. public abstract void WriteChars (char[] buffer, int index, int count);
  125. public abstract void WriteComment (string text);
  126. public abstract void WriteDocType (string name, string pubid, string sysid, string subset);
  127. public void WriteElementString (string localName, string value)
  128. {
  129. WriteStartElement(localName);
  130. WriteString(value);
  131. WriteEndElement();
  132. }
  133. public void WriteElementString (string localName, string ns, string value)
  134. {
  135. WriteStartElement(localName, ns);
  136. WriteString(value);
  137. WriteEndElement();
  138. }
  139. #if NET_2_0
  140. public void WriteElementString (string prefix, string localName, string ns, string value)
  141. {
  142. WriteStartElement(prefix, localName, ns);
  143. WriteString(value);
  144. WriteEndElement();
  145. }
  146. #endif
  147. public abstract void WriteEndAttribute ();
  148. public abstract void WriteEndDocument ();
  149. public abstract void WriteEndElement ();
  150. public abstract void WriteEntityRef (string name);
  151. public abstract void WriteFullEndElement ();
  152. public abstract void WriteName (string name);
  153. public abstract void WriteNmToken (string name);
  154. public virtual void WriteNode (XmlReader reader, bool defattr)
  155. {
  156. if (reader == null)
  157. throw new ArgumentException ();
  158. if (reader.ReadState == ReadState.Initial) {
  159. reader.Read ();
  160. do {
  161. WriteNode (reader, defattr);
  162. } while (!reader.EOF);
  163. return;
  164. }
  165. switch (reader.NodeType) {
  166. case XmlNodeType.Element:
  167. WriteStartElement (reader.Prefix, reader.LocalName, reader.NamespaceURI);
  168. #if false
  169. WriteAttributes (reader, defattr);
  170. reader.MoveToElement ();
  171. #else
  172. // Well, I found that MS.NET took this way, since
  173. // there was a error-prone SgmlReader that fails
  174. // MoveToNextAttribute().
  175. if (reader.HasAttributes) {
  176. for (int i = 0; i < reader.AttributeCount; i++) {
  177. reader.MoveToAttribute (i);
  178. WriteAttribute (reader, defattr);
  179. }
  180. reader.MoveToElement ();
  181. }
  182. #endif
  183. if (reader.IsEmptyElement)
  184. WriteEndElement ();
  185. else {
  186. int depth = reader.Depth;
  187. reader.Read ();
  188. if (reader.NodeType != XmlNodeType.EndElement) {
  189. do {
  190. WriteNode (reader, defattr);
  191. } while (depth < reader.Depth);
  192. }
  193. WriteFullEndElement ();
  194. }
  195. break;
  196. // In case of XmlAttribute, don't proceed reader.
  197. case XmlNodeType.Attribute:
  198. return;
  199. case XmlNodeType.Text:
  200. WriteString (reader.Value);
  201. break;
  202. case XmlNodeType.CDATA:
  203. WriteCData (reader.Value);
  204. break;
  205. case XmlNodeType.EntityReference:
  206. WriteEntityRef (reader.Name);
  207. break;
  208. case XmlNodeType.XmlDeclaration:
  209. // LAMESPEC: It means that XmlWriter implementation _must not_ check
  210. // whether PI name is "xml" (it is XML error) or not.
  211. case XmlNodeType.ProcessingInstruction:
  212. WriteProcessingInstruction (reader.Name, reader.Value);
  213. break;
  214. case XmlNodeType.Comment:
  215. WriteComment (reader.Value);
  216. break;
  217. case XmlNodeType.DocumentType:
  218. WriteDocType (reader.Name,
  219. reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
  220. break;
  221. case XmlNodeType.SignificantWhitespace:
  222. goto case XmlNodeType.Whitespace;
  223. case XmlNodeType.Whitespace:
  224. WriteWhitespace (reader.Value);
  225. break;
  226. case XmlNodeType.EndElement:
  227. WriteFullEndElement ();
  228. break;
  229. case XmlNodeType.EndEntity:
  230. break;
  231. case XmlNodeType.None:
  232. return; // Do nothing, nor reporting errors.
  233. default:
  234. throw new XmlException ("Unexpected node " + reader.Name + " of type " + reader.NodeType);
  235. }
  236. reader.Read ();
  237. }
  238. public abstract void WriteProcessingInstruction (string name, string text);
  239. public abstract void WriteQualifiedName (string localName, string ns);
  240. public abstract void WriteRaw (string data);
  241. public abstract void WriteRaw (char[] buffer, int index, int count);
  242. public void WriteStartAttribute (string localName, string ns)
  243. {
  244. WriteStartAttribute (null, localName, ns);
  245. }
  246. public abstract void WriteStartAttribute (string prefix, string localName, string ns);
  247. public abstract void WriteStartDocument ();
  248. public abstract void WriteStartDocument (bool standalone);
  249. public void WriteStartElement (string localName)
  250. {
  251. WriteStartElement (null, localName, null);
  252. }
  253. public void WriteStartElement (string localName, string ns)
  254. {
  255. WriteStartElement (null, localName, ns);
  256. }
  257. public abstract void WriteStartElement (string prefix, string localName, string ns);
  258. public abstract void WriteString (string text);
  259. public abstract void WriteSurrogateCharEntity (char lowChar, char highChar);
  260. public abstract void WriteWhitespace (string ws);
  261. #endregion
  262. }
  263. }