XmlWriter.cs 7.0 KB

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