XmlWriter.cs 6.9 KB

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