XmlTextWriter.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. //
  2. // System.Xml.XmlTextWriter
  3. //
  4. // Author:
  5. // Kral Ferch <[email protected]>
  6. //
  7. // (C) 2002 Kral Ferch
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Text;
  13. namespace System.Xml
  14. {
  15. public class XmlTextWriter : XmlWriter
  16. {
  17. #region Fields
  18. protected TextWriter w;
  19. protected bool openWriter = true;
  20. protected bool openStartElement;
  21. protected Stack openElements = new Stack ();
  22. protected XmlNamespaceManager namespaceManager = new XmlNamespaceManager (new NameTable ());
  23. #endregion
  24. #region Constructors
  25. public XmlTextWriter (TextWriter w) : base ()
  26. {
  27. this.w = w;
  28. }
  29. public XmlTextWriter (Stream w, Encoding encoding) : base ()
  30. {
  31. this.w = new StreamWriter(w, encoding);
  32. }
  33. public XmlTextWriter (string filename, Encoding encoding) : base ()
  34. {
  35. this.w = new StreamWriter(filename, false, encoding);
  36. }
  37. #endregion
  38. #region Properties
  39. [MonoTODO]
  40. public Stream BaseStream {
  41. get { throw new NotImplementedException(); }
  42. }
  43. [MonoTODO]
  44. public Formatting Formatting {
  45. get { throw new NotImplementedException(); }
  46. set { throw new NotImplementedException(); }
  47. }
  48. [MonoTODO]
  49. public int Indentation {
  50. get { throw new NotImplementedException(); }
  51. set { throw new NotImplementedException(); }
  52. }
  53. [MonoTODO]
  54. public char IndentChar {
  55. get { throw new NotImplementedException(); }
  56. set { throw new NotImplementedException(); }
  57. }
  58. [MonoTODO]
  59. public bool Namespaces {
  60. get { throw new NotImplementedException(); }
  61. set { throw new NotImplementedException(); }
  62. }
  63. [MonoTODO]
  64. public char QuoteChar {
  65. get { throw new NotImplementedException(); }
  66. set { throw new NotImplementedException(); }
  67. }
  68. [MonoTODO]
  69. public override WriteState WriteState {
  70. get { throw new NotImplementedException(); }
  71. }
  72. [MonoTODO]
  73. public override string XmlLang {
  74. get { throw new NotImplementedException(); }
  75. }
  76. [MonoTODO]
  77. public override XmlSpace XmlSpace {
  78. get { throw new NotImplementedException(); }
  79. }
  80. #endregion
  81. #region Methods
  82. private void CheckOpenWriter ()
  83. {
  84. if (!openWriter) {
  85. throw new InvalidOperationException ("The Writer is closed.");
  86. }
  87. }
  88. public override void Close ()
  89. {
  90. while (openElements.Count > 0) {
  91. WriteEndElement();
  92. }
  93. w.Close();
  94. openWriter = false;
  95. }
  96. private void CloseStartElement ()
  97. {
  98. if (openStartElement)
  99. {
  100. w.Write(">");
  101. openStartElement = false;
  102. }
  103. }
  104. [MonoTODO]
  105. public override void Flush ()
  106. {
  107. throw new NotImplementedException ();
  108. }
  109. [MonoTODO]
  110. public override string LookupPrefix (string ns)
  111. {
  112. throw new NotImplementedException ();
  113. }
  114. [MonoTODO]
  115. public override void WriteBase64 (byte[] buffer, int index, int count)
  116. {
  117. throw new NotImplementedException ();
  118. }
  119. [MonoTODO]
  120. public override void WriteBinHex (byte[] buffer, int index, int count)
  121. {
  122. throw new NotImplementedException ();
  123. }
  124. public override void WriteCData (string text)
  125. {
  126. if (text.IndexOf("]]>") > 0)
  127. {
  128. throw new ArgumentException ();
  129. }
  130. CheckOpenWriter ();
  131. CloseStartElement ();
  132. w.Write("<![CDATA[{0}]]>", text);
  133. }
  134. [MonoTODO]
  135. public override void WriteCharEntity (char ch)
  136. {
  137. throw new NotImplementedException ();
  138. }
  139. [MonoTODO]
  140. public override void WriteChars (char[] buffer, int index, int count)
  141. {
  142. throw new NotImplementedException ();
  143. }
  144. public override void WriteComment (string text)
  145. {
  146. if ((text.EndsWith("-")) || (text.IndexOf("-->") > 0)) {
  147. throw new ArgumentException ();
  148. }
  149. CheckOpenWriter ();
  150. CloseStartElement ();
  151. w.Write ("<!--{0}-->", text);
  152. }
  153. [MonoTODO]
  154. public override void WriteDocType (string name, string pubid, string sysid, string subset)
  155. {
  156. throw new NotImplementedException ();
  157. }
  158. [MonoTODO]
  159. public override void WriteEndAttribute ()
  160. {
  161. throw new NotImplementedException ();
  162. }
  163. [MonoTODO]
  164. public override void WriteEndDocument ()
  165. {
  166. throw new NotImplementedException ();
  167. }
  168. public override void WriteEndElement ()
  169. {
  170. if (openStartElement) {
  171. w.Write (" />");
  172. openElements.Pop ();
  173. openStartElement = false;
  174. }
  175. else {
  176. w.Write ("</{0}>", openElements.Pop ());
  177. namespaceManager.PopScope();
  178. }
  179. }
  180. [MonoTODO]
  181. public override void WriteEntityRef (string name)
  182. {
  183. throw new NotImplementedException ();
  184. }
  185. [MonoTODO]
  186. public override void WriteFullEndElement ()
  187. {
  188. throw new NotImplementedException ();
  189. }
  190. [MonoTODO]
  191. public override void WriteName (string name)
  192. {
  193. throw new NotImplementedException ();
  194. }
  195. [MonoTODO]
  196. public override void WriteNmToken (string name)
  197. {
  198. throw new NotImplementedException ();
  199. }
  200. public override void WriteProcessingInstruction (string name, string text)
  201. {
  202. if ((name == null) || (name == string.Empty) || (name.IndexOf("?>") > 0) || (text.IndexOf("?>") > 0)) {
  203. throw new ArgumentException ();
  204. }
  205. CheckOpenWriter ();
  206. CloseStartElement ();
  207. w.Write ("<?{0} {1}?>", name, text);
  208. }
  209. [MonoTODO]
  210. public override void WriteQualifiedName (string localName, string ns)
  211. {
  212. throw new NotImplementedException ();
  213. }
  214. [MonoTODO]
  215. public override void WriteRaw (string data)
  216. {
  217. throw new NotImplementedException ();
  218. }
  219. [MonoTODO]
  220. public override void WriteRaw (char[] buffer, int index, int count)
  221. {
  222. throw new NotImplementedException ();
  223. }
  224. [MonoTODO]
  225. public override void WriteStartAttribute (string prefix, string localName, string ns)
  226. {
  227. throw new NotImplementedException ();
  228. }
  229. [MonoTODO]
  230. public override void WriteStartDocument ()
  231. {
  232. throw new NotImplementedException ();
  233. }
  234. [MonoTODO]
  235. public override void WriteStartDocument (bool standalone)
  236. {
  237. throw new NotImplementedException ();
  238. }
  239. public override void WriteStartElement (string prefix, string localName, string ns)
  240. {
  241. string formatXmlns = "";
  242. string formatPrefix = "";
  243. if ((prefix != String.Empty) && (ns == String.Empty))
  244. throw new ArgumentException ("Cannot use a prefix with an empty namespace.");
  245. CheckOpenWriter ();
  246. CloseStartElement ();
  247. if (ns != String.Empty) {
  248. string existingPrefix = namespaceManager.LookupPrefix (ns);
  249. if (prefix == String.Empty)
  250. prefix = existingPrefix;
  251. if (prefix != existingPrefix)
  252. formatXmlns = " xmlns:" + prefix + "=\"" + ns + "\"";
  253. else if (existingPrefix == String.Empty)
  254. formatXmlns = " xmlns=\"" + ns + "\"";
  255. }
  256. else if ((prefix == String.Empty) && (namespaceManager.LookupNamespace(prefix) != String.Empty)) {
  257. formatXmlns = " xmlns=\"\"";
  258. }
  259. if (prefix != String.Empty) {
  260. formatPrefix = prefix + ":";
  261. }
  262. w.Write ("<{0}{1}{2}", formatPrefix, localName, formatXmlns);
  263. openElements.Push (formatPrefix + localName);
  264. openStartElement = true;
  265. namespaceManager.PushScope ();
  266. namespaceManager.AddNamespace (prefix, ns);
  267. }
  268. [MonoTODO("Haven't done any entity replacements yet.")]
  269. public override void WriteString (string text)
  270. {
  271. if (text != String.Empty) {
  272. CheckOpenWriter ();
  273. CloseStartElement ();
  274. w.Write (text);
  275. }
  276. }
  277. [MonoTODO]
  278. public override void WriteSurrogateCharEntity (char lowChar, char highChar)
  279. {
  280. throw new NotImplementedException ();
  281. }
  282. [MonoTODO]
  283. public override void WriteWhitespace (string ws)
  284. {
  285. throw new NotImplementedException ();
  286. }
  287. #endregion
  288. }
  289. }