XmlTextWriter.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 nullEncoding = false;
  20. protected bool openWriter = true;
  21. protected bool openStartElement;
  22. protected bool documentStarted = false;
  23. protected bool namespaces = true;
  24. protected Stack openElements = new Stack ();
  25. protected XmlNamespaceManager namespaceManager = new XmlNamespaceManager (new NameTable ());
  26. protected Formatting formatting = Formatting.None;
  27. protected int indentation = 2;
  28. protected char indentChar = ' ';
  29. protected string indentChars = " ";
  30. protected char quoteChar = '\"';
  31. protected int indentLevel = 0;
  32. protected string indentFormatting;
  33. protected Stream baseStream = null;
  34. #endregion
  35. #region Constructors
  36. public XmlTextWriter (TextWriter w) : base ()
  37. {
  38. this.w = w;
  39. try {
  40. baseStream = ((StreamWriter)w).BaseStream;
  41. }
  42. catch (Exception) { }
  43. }
  44. public XmlTextWriter (Stream w, Encoding encoding) : base ()
  45. {
  46. if (encoding == null) {
  47. nullEncoding = true;
  48. encoding = new UTF8Encoding ();
  49. }
  50. this.w = new StreamWriter(w, encoding);
  51. baseStream = w;
  52. }
  53. public XmlTextWriter (string filename, Encoding encoding) : base ()
  54. {
  55. this.w = new StreamWriter(filename, false, encoding);
  56. baseStream = ((StreamWriter)w).BaseStream;
  57. }
  58. #endregion
  59. #region Properties
  60. public Stream BaseStream {
  61. get { return baseStream; }
  62. }
  63. public Formatting Formatting {
  64. get { return formatting; }
  65. set { formatting = value; }
  66. }
  67. public bool IndentingOverriden
  68. {
  69. get {
  70. if (openElements.Count == 0)
  71. return false;
  72. else
  73. return (((XmlTextWriterOpenElement)openElements.Peek()).IndentingOverriden);
  74. }
  75. set {
  76. if (openElements.Count > 0)
  77. ((XmlTextWriterOpenElement)openElements.Peek()).IndentingOverriden = value;
  78. }
  79. }
  80. public int Indentation {
  81. get { return indentation; }
  82. set {
  83. indentation = value;
  84. UpdateIndentChars ();
  85. }
  86. }
  87. public char IndentChar {
  88. get { return indentChar; }
  89. set {
  90. indentChar = value;
  91. UpdateIndentChars ();
  92. }
  93. }
  94. public bool Namespaces {
  95. get { return namespaces; }
  96. set {
  97. if (ws != WriteState.Start)
  98. throw new InvalidOperationException ("NotInWriteState.");
  99. namespaces = value;
  100. }
  101. }
  102. [MonoTODO]
  103. public char QuoteChar {
  104. get { return quoteChar; }
  105. set {
  106. if ((value != '\'') && (value != '\"'))
  107. throw new ArgumentException ("This is an invalid XML attribute quote character. Valid attribute quote characters are ' and \".");
  108. quoteChar = value;
  109. }
  110. }
  111. public override WriteState WriteState {
  112. get { return ws; }
  113. }
  114. [MonoTODO]
  115. public override string XmlLang {
  116. get { throw new NotImplementedException(); }
  117. }
  118. [MonoTODO]
  119. public override XmlSpace XmlSpace {
  120. get { throw new NotImplementedException(); }
  121. }
  122. #endregion
  123. #region Methods
  124. private void CheckState ()
  125. {
  126. if (!openWriter) {
  127. throw new InvalidOperationException ("The Writer is closed.");
  128. }
  129. if ((documentStarted == true) && (formatting == Formatting.Indented) && (!IndentingOverriden)) {
  130. indentFormatting = "\r\n";
  131. if (indentLevel > 0) {
  132. for (int i = 0; i < indentLevel; i++)
  133. indentFormatting += indentChars;
  134. }
  135. }
  136. else
  137. indentFormatting = "";
  138. documentStarted = true;
  139. }
  140. public override void Close ()
  141. {
  142. while (openElements.Count > 0) {
  143. WriteEndElement();
  144. }
  145. w.Close();
  146. ws = WriteState.Closed;
  147. openWriter = false;
  148. }
  149. private void CloseStartElement ()
  150. {
  151. if (openStartElement)
  152. {
  153. w.Write(">");
  154. ws = WriteState.Content;
  155. openStartElement = false;
  156. }
  157. }
  158. public override void Flush ()
  159. {
  160. w.Flush ();
  161. }
  162. [MonoTODO]
  163. public override string LookupPrefix (string ns)
  164. {
  165. throw new NotImplementedException ();
  166. }
  167. private void UpdateIndentChars ()
  168. {
  169. indentChars = "";
  170. for (int i = 0; i < indentation; i++)
  171. indentChars += indentChar;
  172. }
  173. [MonoTODO]
  174. public override void WriteBase64 (byte[] buffer, int index, int count)
  175. {
  176. throw new NotImplementedException ();
  177. }
  178. [MonoTODO]
  179. public override void WriteBinHex (byte[] buffer, int index, int count)
  180. {
  181. throw new NotImplementedException ();
  182. }
  183. public override void WriteCData (string text)
  184. {
  185. if (text.IndexOf("]]>") > 0)
  186. {
  187. throw new ArgumentException ();
  188. }
  189. CheckState ();
  190. CloseStartElement ();
  191. w.Write("<![CDATA[{0}]]>", text);
  192. }
  193. [MonoTODO]
  194. public override void WriteCharEntity (char ch)
  195. {
  196. throw new NotImplementedException ();
  197. }
  198. [MonoTODO]
  199. public override void WriteChars (char[] buffer, int index, int count)
  200. {
  201. throw new NotImplementedException ();
  202. }
  203. public override void WriteComment (string text)
  204. {
  205. if ((text.EndsWith("-")) || (text.IndexOf("-->") > 0)) {
  206. throw new ArgumentException ();
  207. }
  208. CheckState ();
  209. CloseStartElement ();
  210. w.Write ("<!--{0}-->", text);
  211. }
  212. [MonoTODO]
  213. public override void WriteDocType (string name, string pubid, string sysid, string subset)
  214. {
  215. throw new NotImplementedException ();
  216. }
  217. [MonoTODO]
  218. public override void WriteEndAttribute ()
  219. {
  220. throw new NotImplementedException ();
  221. }
  222. [MonoTODO]
  223. public override void WriteEndDocument ()
  224. {
  225. throw new NotImplementedException ();
  226. }
  227. public override void WriteEndElement ()
  228. {
  229. if (openElements.Count == 0)
  230. throw new InvalidOperationException("There was no XML start tag open.");
  231. indentLevel--;
  232. CheckState ();
  233. if (openStartElement) {
  234. w.Write (" />");
  235. openElements.Pop ();
  236. openStartElement = false;
  237. }
  238. else {
  239. w.Write ("{0}</{1}>", indentFormatting, openElements.Pop ());
  240. namespaceManager.PopScope();
  241. }
  242. }
  243. [MonoTODO]
  244. public override void WriteEntityRef (string name)
  245. {
  246. throw new NotImplementedException ();
  247. }
  248. [MonoTODO]
  249. public override void WriteFullEndElement ()
  250. {
  251. throw new NotImplementedException ();
  252. }
  253. [MonoTODO]
  254. public override void WriteName (string name)
  255. {
  256. throw new NotImplementedException ();
  257. }
  258. [MonoTODO]
  259. public override void WriteNmToken (string name)
  260. {
  261. throw new NotImplementedException ();
  262. }
  263. public override void WriteProcessingInstruction (string name, string text)
  264. {
  265. if ((name == null) || (name == string.Empty) || (name.IndexOf("?>") > 0) || (text.IndexOf("?>") > 0)) {
  266. throw new ArgumentException ();
  267. }
  268. CheckState ();
  269. CloseStartElement ();
  270. w.Write ("{0}<?{1} {2}?>", indentFormatting, name, text);
  271. }
  272. [MonoTODO]
  273. public override void WriteQualifiedName (string localName, string ns)
  274. {
  275. throw new NotImplementedException ();
  276. }
  277. [MonoTODO]
  278. public override void WriteRaw (string data)
  279. {
  280. throw new NotImplementedException ();
  281. }
  282. [MonoTODO]
  283. public override void WriteRaw (char[] buffer, int index, int count)
  284. {
  285. throw new NotImplementedException ();
  286. }
  287. [MonoTODO]
  288. public override void WriteStartAttribute (string prefix, string localName, string ns)
  289. {
  290. throw new NotImplementedException ();
  291. }
  292. public override void WriteStartDocument ()
  293. {
  294. WriteStartDocument ("");
  295. }
  296. public override void WriteStartDocument (bool standalone)
  297. {
  298. string standaloneFormatting;
  299. if (standalone == true)
  300. standaloneFormatting = " standalone=\"yes\"";
  301. else
  302. standaloneFormatting = " standalone=\"no\"";
  303. WriteStartDocument (standaloneFormatting);
  304. }
  305. private void WriteStartDocument (string standaloneFormatting)
  306. {
  307. if (documentStarted == true)
  308. throw new InvalidOperationException("WriteStartDocument should be the first call.");
  309. CheckState ();
  310. string encodingFormatting = "";
  311. if (!nullEncoding)
  312. encodingFormatting = " encoding=\"" + w.Encoding.HeaderName + "\"";
  313. w.Write("<?xml version=\"1.0\"{0}{1}?>", encodingFormatting, standaloneFormatting);
  314. ws = WriteState.Prolog;
  315. }
  316. public override void WriteStartElement (string prefix, string localName, string ns)
  317. {
  318. if (!Namespaces && (((prefix != null) && (prefix != String.Empty))
  319. || ((ns != null) && (ns != String.Empty))))
  320. throw new ArgumentException ("Cannot set the namespace if Namespaces is 'false'.");
  321. WriteStartElementInternal (prefix, localName, ns);
  322. }
  323. protected override void WriteStartElementInternal (string prefix, string localName, string ns)
  324. {
  325. if (prefix == null)
  326. prefix = String.Empty;
  327. if (ns == null)
  328. ns = String.Empty;
  329. if ((prefix != String.Empty) && ((ns == null) || (ns == String.Empty)))
  330. throw new ArgumentException ("Cannot use a prefix with an empty namespace.");
  331. CheckState ();
  332. CloseStartElement ();
  333. string formatXmlns = "";
  334. string formatPrefix = "";
  335. if (ns != String.Empty)
  336. {
  337. string existingPrefix = namespaceManager.LookupPrefix (ns);
  338. if (prefix == String.Empty)
  339. prefix = existingPrefix;
  340. if (prefix != existingPrefix)
  341. formatXmlns = " xmlns:" + prefix + "=\"" + ns + "\"";
  342. else if (existingPrefix == String.Empty)
  343. formatXmlns = " xmlns=\"" + ns + "\"";
  344. }
  345. else if ((prefix == String.Empty) && (namespaceManager.LookupNamespace(prefix) != String.Empty)) {
  346. formatXmlns = " xmlns=\"\"";
  347. }
  348. if (prefix != String.Empty) {
  349. formatPrefix = prefix + ":";
  350. }
  351. w.Write ("{0}<{1}{2}{3}", indentFormatting, formatPrefix, localName, formatXmlns);
  352. openElements.Push (new XmlTextWriterOpenElement (formatPrefix + localName));
  353. ws = WriteState.Element;
  354. openStartElement = true;
  355. namespaceManager.PushScope ();
  356. namespaceManager.AddNamespace (prefix, ns);
  357. indentLevel++;
  358. }
  359. [MonoTODO("Haven't done any entity replacements yet.")]
  360. public override void WriteString (string text)
  361. {
  362. if (text != String.Empty) {
  363. CheckState ();
  364. CloseStartElement ();
  365. w.Write (text);
  366. }
  367. IndentingOverriden = true;
  368. }
  369. [MonoTODO]
  370. public override void WriteSurrogateCharEntity (char lowChar, char highChar)
  371. {
  372. throw new NotImplementedException ();
  373. }
  374. [MonoTODO]
  375. public override void WriteWhitespace (string ws)
  376. {
  377. throw new NotImplementedException ();
  378. }
  379. #endregion
  380. }
  381. }