XmlTextWriter.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. //
  2. // System.Xml.XmlTextWriter
  3. //
  4. // Author:
  5. // Kral Ferch <[email protected]>
  6. //
  7. // (C) 2002 Kral Ferch
  8. //
  9. // [FIXME]
  10. // Document state should be considered.
  11. //
  12. using System;
  13. using System.Collections;
  14. using System.IO;
  15. using System.Text;
  16. namespace System.Xml
  17. {
  18. public class XmlTextWriter : XmlWriter
  19. {
  20. #region Fields
  21. const string XmlnsNamespace = "http://www.w3.org/2000/xmlns/";
  22. TextWriter w;
  23. bool nullEncoding = false;
  24. bool openWriter = true;
  25. bool openStartElement = false;
  26. bool documentStarted = false;
  27. bool namespaces = true;
  28. bool openAttribute = false;
  29. bool attributeWrittenForElement = false;
  30. Stack openElements = new Stack ();
  31. Formatting formatting = Formatting.None;
  32. int indentation = 2;
  33. char indentChar = ' ';
  34. string indentChars = " ";
  35. char quoteChar = '\"';
  36. int indentLevel = 0;
  37. string indentFormatting;
  38. Stream baseStream = null;
  39. string xmlLang = null;
  40. XmlSpace xmlSpace = XmlSpace.None;
  41. bool openXmlLang = false;
  42. bool openXmlSpace = false;
  43. string openElementPrefix;
  44. string openElementNS;
  45. bool hasRoot = false;
  46. Hashtable newAttributeNamespaces = new Hashtable ();
  47. XmlNamespaceManager namespaceManager = new XmlNamespaceManager (new NameTable ());
  48. string savingAttributeValue = String.Empty;
  49. bool saveAttributeValue = false;
  50. string savedAttributePrefix;
  51. #endregion
  52. #region Constructors
  53. public XmlTextWriter (TextWriter w) : base ()
  54. {
  55. this.w = w;
  56. nullEncoding = (w.Encoding == null);
  57. StreamWriter sw = w as StreamWriter;
  58. if (sw != null)
  59. baseStream = ((StreamWriter)w).BaseStream;
  60. }
  61. public XmlTextWriter (Stream w, Encoding encoding) : base ()
  62. {
  63. if (encoding == null) {
  64. nullEncoding = true;
  65. encoding = new UTF8Encoding ();
  66. }
  67. this.w = new StreamWriter(w, encoding);
  68. baseStream = w;
  69. }
  70. public XmlTextWriter (string filename, Encoding encoding) :
  71. this (new FileStream (filename, FileMode.Create, FileAccess.Write, FileShare.None), encoding)
  72. {
  73. }
  74. #endregion
  75. #region Properties
  76. public Stream BaseStream {
  77. get { return baseStream; }
  78. }
  79. public Formatting Formatting {
  80. get { return formatting; }
  81. set { formatting = value; }
  82. }
  83. private bool IndentingOverriden
  84. {
  85. get {
  86. if (openElements.Count == 0)
  87. return false;
  88. else
  89. return (((XmlTextWriterOpenElement)openElements.Peek()).IndentingOverriden);
  90. }
  91. set {
  92. if (openElements.Count > 0)
  93. ((XmlTextWriterOpenElement)openElements.Peek()).IndentingOverriden = value;
  94. }
  95. }
  96. public int Indentation {
  97. get { return indentation; }
  98. set {
  99. indentation = value;
  100. UpdateIndentChars ();
  101. }
  102. }
  103. public char IndentChar {
  104. get { return indentChar; }
  105. set {
  106. indentChar = value;
  107. UpdateIndentChars ();
  108. }
  109. }
  110. public bool Namespaces {
  111. get { return namespaces; }
  112. set {
  113. if (ws != WriteState.Start)
  114. throw new InvalidOperationException ("NotInWriteState.");
  115. namespaces = value;
  116. }
  117. }
  118. public char QuoteChar {
  119. get { return quoteChar; }
  120. set {
  121. if ((value != '\'') && (value != '\"'))
  122. throw new ArgumentException ("This is an invalid XML attribute quote character. Valid attribute quote characters are ' and \".");
  123. quoteChar = value;
  124. }
  125. }
  126. public override WriteState WriteState {
  127. get { return ws; }
  128. }
  129. public override string XmlLang {
  130. get {
  131. string xmlLang = null;
  132. int i;
  133. for (i = 0; i < openElements.Count; i++)
  134. {
  135. xmlLang = ((XmlTextWriterOpenElement)openElements.ToArray().GetValue(i)).XmlLang;
  136. if (xmlLang != null)
  137. break;
  138. }
  139. return xmlLang;
  140. }
  141. }
  142. public override XmlSpace XmlSpace {
  143. get {
  144. XmlSpace xmlSpace = XmlSpace.None;
  145. int i;
  146. for (i = 0; i < openElements.Count; i++)
  147. {
  148. xmlSpace = ((XmlTextWriterOpenElement)openElements.ToArray().GetValue(i)).XmlSpace;
  149. if (xmlSpace != XmlSpace.None)
  150. break;
  151. }
  152. return xmlSpace;
  153. }
  154. }
  155. #endregion
  156. #region Methods
  157. private void AddMissingElementXmlns ()
  158. {
  159. // output namespace declaration if not exist.
  160. string prefix = openElementPrefix;
  161. string ns = openElementNS;
  162. openElementPrefix = null;
  163. openElementNS = null;
  164. // LAMESPEC: If prefix was already assigned another nsuri, then this element's nsuri goes away!
  165. if (ns != null)
  166. {
  167. string formatXmlns = String.Empty;
  168. if (ns != String.Empty)
  169. {
  170. string existingPrefix = namespaceManager.LookupPrefix (ns);
  171. if (existingPrefix != prefix)
  172. {
  173. if (prefix != string.Empty)
  174. formatXmlns = String.Format ("xmlns:{0}={1}{2}{1}", prefix, quoteChar, ns);
  175. else
  176. formatXmlns = String.Format ("xmlns={0}{1}{0}", quoteChar, ns);
  177. namespaceManager.AddNamespace (prefix, ns);
  178. }
  179. }
  180. else if ((prefix == String.Empty) && (namespaceManager.LookupNamespace (prefix) != ns))
  181. {
  182. namespaceManager.AddNamespace (prefix, ns);
  183. formatXmlns = String.Format ("xmlns={0}{0}", quoteChar);
  184. }
  185. if(formatXmlns != String.Empty) {
  186. w.Write (' ');
  187. w.Write(formatXmlns);
  188. }
  189. }
  190. if (newAttributeNamespaces.Count > 0)
  191. {
  192. foreach (DictionaryEntry ent in newAttributeNamespaces)
  193. {
  194. string ans = (string) ent.Value;
  195. string aprefix = (string) ent.Key;
  196. string formatXmlns = String.Format (" xmlns:{0}={1}{2}{1}", aprefix, quoteChar, ans);
  197. w.Write(formatXmlns);
  198. }
  199. }
  200. newAttributeNamespaces.Clear ();
  201. }
  202. private void CheckState ()
  203. {
  204. if (!openWriter) {
  205. throw new InvalidOperationException ("The Writer is closed.");
  206. }
  207. if ((documentStarted == true) && (formatting == Formatting.Indented) && (!IndentingOverriden)) {
  208. indentFormatting = w.NewLine;
  209. if (indentLevel > 0) {
  210. for (int i = 0; i < indentLevel; i++)
  211. indentFormatting += indentChars;
  212. }
  213. }
  214. else
  215. indentFormatting = "";
  216. documentStarted = true;
  217. }
  218. public override void Close ()
  219. {
  220. CloseOpenAttributeAndElements ();
  221. w.Close();
  222. ws = WriteState.Closed;
  223. openWriter = false;
  224. }
  225. private void CloseOpenAttributeAndElements ()
  226. {
  227. if (openAttribute)
  228. WriteEndAttribute ();
  229. while (openElements.Count > 0) {
  230. WriteEndElement();
  231. }
  232. }
  233. private void CloseStartElement ()
  234. {
  235. if (!openStartElement)
  236. return;
  237. AddMissingElementXmlns ();
  238. w.Write (">");
  239. ws = WriteState.Content;
  240. openStartElement = false;
  241. attributeWrittenForElement = false;
  242. newAttributeNamespaces.Clear ();
  243. }
  244. public override void Flush ()
  245. {
  246. w.Flush ();
  247. }
  248. public override string LookupPrefix (string ns)
  249. {
  250. string prefix = namespaceManager.LookupPrefix (ns);
  251. // XmlNamespaceManager has changed to return null when NSURI not found.
  252. // (Contradiction to the documentation.)
  253. //if (prefix == String.Empty)
  254. // prefix = null;
  255. return prefix;
  256. }
  257. private void UpdateIndentChars ()
  258. {
  259. indentChars = "";
  260. for (int i = 0; i < indentation; i++)
  261. indentChars += indentChar;
  262. }
  263. public override void WriteBase64 (byte[] buffer, int index, int count)
  264. {
  265. CheckState ();
  266. if (!openAttribute) {
  267. IndentingOverriden = true;
  268. CloseStartElement ();
  269. }
  270. w.Write (Convert.ToBase64String (buffer, index, count));
  271. }
  272. // BinHex??
  273. public override void WriteBinHex (byte[] buffer, int index, int count)
  274. {
  275. CheckState ();
  276. if (!openAttribute) {
  277. IndentingOverriden = true;
  278. CloseStartElement ();
  279. }
  280. if (index < 0)
  281. throw new ArgumentOutOfRangeException ("index", index, "index must be non negative integer.");
  282. if (count < 0)
  283. throw new ArgumentOutOfRangeException ("count", count, "count must be non negative integer.");
  284. if (buffer.Length < index + count)
  285. throw new ArgumentOutOfRangeException ("index and count must be smaller than the length of the buffer.");
  286. for (int i = index; i < count; i++) {
  287. int val = buffer [i];
  288. int high = val >> 4;
  289. int low = val & 15;
  290. if (high > 9)
  291. w.Write ((char) (high + 55));
  292. else
  293. w.Write ((char) (high + 0x30));
  294. if (low > 9)
  295. w.Write ((char) (low + 55));
  296. else
  297. w.Write ((char) (low + 0x30));
  298. }
  299. }
  300. public override void WriteCData (string text)
  301. {
  302. if (text.IndexOf("]]>") > 0)
  303. throw new ArgumentException ();
  304. CheckState ();
  305. CloseStartElement ();
  306. w.Write ("<![CDATA[");
  307. w.Write (text);
  308. w.Write ("]]>");
  309. }
  310. public override void WriteCharEntity (char ch)
  311. {
  312. Int16 intCh = (Int16)ch;
  313. // Make sure the character is not in the surrogate pair
  314. // character range, 0xd800- 0xdfff
  315. if ((intCh >= -10240) && (intCh <= -8193))
  316. throw new ArgumentException ("Surrogate Pair is invalid.");
  317. w.Write("&#x{0:X};", intCh);
  318. }
  319. public override void WriteChars (char[] buffer, int index, int count)
  320. {
  321. CheckState ();
  322. if (!openAttribute) {
  323. IndentingOverriden = true;
  324. CloseStartElement ();
  325. }
  326. w.Write (buffer, index, count);
  327. }
  328. public override void WriteComment (string text)
  329. {
  330. if ((text.EndsWith("-")) || (text.IndexOf("-->") > 0)) {
  331. throw new ArgumentException ();
  332. }
  333. CheckState ();
  334. CloseStartElement ();
  335. w.Write ("<!--");
  336. w.Write (text);
  337. w.Write ("-->");
  338. }
  339. public override void WriteDocType (string name, string pubid, string sysid, string subset)
  340. {
  341. if (name == null || name.Trim ().Length == 0)
  342. throw new ArgumentException ("Invalid DOCTYPE name", "name");
  343. w.Write ("<!DOCTYPE ");
  344. w.Write (name);
  345. if (pubid != null) {
  346. w.Write (" PUBLIC ");
  347. w.Write (quoteChar);
  348. w.Write (pubid);
  349. w.Write (quoteChar);
  350. w.Write (' ');
  351. w.Write (quoteChar);
  352. w.Write (sysid);
  353. w.Write (quoteChar);
  354. } else if (sysid != null) {
  355. w.Write (" SYSTEM ");
  356. w.Write (quoteChar);
  357. w.Write (sysid);
  358. w.Write (quoteChar);
  359. }
  360. if (subset != null) {
  361. w.Write ('[');
  362. w.Write (subset);
  363. w.Write (']');
  364. }
  365. w.Write('>');
  366. }
  367. public override void WriteEndAttribute ()
  368. {
  369. if (!openAttribute)
  370. throw new InvalidOperationException("Token EndAttribute in state Start would result in an invalid XML document.");
  371. CheckState ();
  372. if (openXmlLang) {
  373. w.Write (xmlLang);
  374. openXmlLang = false;
  375. ((XmlTextWriterOpenElement)openElements.Peek()).XmlLang = xmlLang;
  376. }
  377. if (openXmlSpace)
  378. {
  379. w.Write (xmlSpace.ToString ().ToLower ());
  380. openXmlSpace = false;
  381. ((XmlTextWriterOpenElement)openElements.Peek()).XmlSpace = xmlSpace;
  382. }
  383. w.Write (quoteChar);
  384. openAttribute = false;
  385. if (saveAttributeValue) {
  386. // add namespace
  387. namespaceManager.AddNamespace (
  388. savedAttributePrefix, savingAttributeValue);
  389. saveAttributeValue = false;
  390. savedAttributePrefix = String.Empty;
  391. savingAttributeValue = String.Empty;
  392. }
  393. }
  394. public override void WriteEndDocument ()
  395. {
  396. CloseOpenAttributeAndElements ();
  397. if (!hasRoot)
  398. throw new ArgumentException ("This document does not have a root element.");
  399. ws = WriteState.Start;
  400. hasRoot = false;
  401. }
  402. public override void WriteEndElement ()
  403. {
  404. WriteEndElementInternal (false);
  405. }
  406. private void WriteEndElementInternal (bool fullEndElement)
  407. {
  408. if (openElements.Count == 0)
  409. throw new InvalidOperationException("There was no XML start tag open.");
  410. indentLevel--;
  411. CheckState ();
  412. AddMissingElementXmlns ();
  413. if (openStartElement) {
  414. if (openAttribute)
  415. WriteEndAttribute ();
  416. if (fullEndElement) {
  417. w.Write ("></");
  418. XmlTextWriterOpenElement el = (XmlTextWriterOpenElement) openElements.Peek ();
  419. if (el.Prefix != String.Empty) {
  420. w.Write (el.Prefix);
  421. w.Write (':');
  422. }
  423. w.Write (el.LocalName);
  424. w.Write ('>');
  425. } else
  426. w.Write (" />");
  427. openElements.Pop ();
  428. openStartElement = false;
  429. } else {
  430. w.Write (indentFormatting);
  431. w.Write ("</");
  432. XmlTextWriterOpenElement el = (XmlTextWriterOpenElement) openElements.Pop ();
  433. if (el.Prefix != String.Empty) {
  434. w.Write (el.Prefix);
  435. w.Write (':');
  436. }
  437. w.Write (el.LocalName);
  438. w.Write ('>');
  439. }
  440. namespaceManager.PopScope();
  441. }
  442. public override void WriteEntityRef (string name)
  443. {
  444. WriteRaw ("&");
  445. WriteStringInternal (name, true);
  446. WriteRaw (";");
  447. }
  448. public override void WriteFullEndElement ()
  449. {
  450. WriteEndElementInternal (true);
  451. }
  452. public override void WriteName (string name)
  453. {
  454. if (!XmlChar.IsName (name))
  455. throw new ArgumentException ("There is an invalid character: '" + name [0] +
  456. "'", "name");
  457. w.Write (name);
  458. }
  459. public override void WriteNmToken (string name)
  460. {
  461. if (!XmlChar.IsNmToken (name))
  462. throw new ArgumentException ("There is an invalid character: '" + name [0] +
  463. "'", "name");
  464. w.Write (name);
  465. }
  466. public override void WriteProcessingInstruction (string name, string text)
  467. {
  468. if ((name == null) || (name == string.Empty) || (name.IndexOf("?>") > 0) || (text.IndexOf("?>") > 0)) {
  469. throw new ArgumentException ();
  470. }
  471. CheckState ();
  472. CloseStartElement ();
  473. w.Write (indentFormatting);
  474. w.Write ("<?");
  475. w.Write (name);
  476. w.Write (' ');
  477. w.Write (text);
  478. w.Write ("?>");
  479. }
  480. public override void WriteQualifiedName (string localName, string ns)
  481. {
  482. if (localName == null || localName == String.Empty)
  483. throw new ArgumentException ();
  484. CheckState ();
  485. if (!openAttribute)
  486. CloseStartElement ();
  487. w.Write (namespaceManager.LookupPrefix (ns));
  488. w.Write (':');
  489. w.Write (localName);
  490. }
  491. public override void WriteRaw (string data)
  492. {
  493. WriteStringInternal (data, false);
  494. }
  495. public override void WriteRaw (char[] buffer, int index, int count)
  496. {
  497. // WriteRawInternal (new string (buffer, index, count));
  498. WriteStringInternal (new string (buffer, index, count), false);
  499. }
  500. public override void WriteStartAttribute (string prefix, string localName, string ns)
  501. {
  502. if ((prefix == "xml") && (localName == "lang")) {
  503. ns = XmlNamespaceManager.XmlnsXml;
  504. openXmlLang = true;
  505. }
  506. if ((prefix == "xml") && (localName == "space")) {
  507. ns = XmlNamespaceManager.XmlnsXml;
  508. openXmlSpace = true;
  509. }
  510. if ((prefix == "xmlns") && (localName.ToLower ().StartsWith ("xml")))
  511. throw new ArgumentException ("Prefixes beginning with \"xml\" (regardless of whether the characters are uppercase, lowercase, or some combination thereof) are reserved for use by XML: " + prefix + ":" + localName);
  512. if ((prefix == "xmlns") && (ns != XmlnsNamespace))
  513. throw new ArgumentException (String.Format ("The 'xmlns' attribute is bound to the reserved namespace '{0}'", XmlnsNamespace));
  514. // ignore non-namespaced node's prefix.
  515. if (ns == null)
  516. ns = String.Empty;
  517. if (ns == String.Empty)
  518. prefix = String.Empty;
  519. CheckState ();
  520. if (ws == WriteState.Content)
  521. throw new InvalidOperationException ("Token StartAttribute in state " + WriteState + " would result in an invalid XML document.");
  522. if (prefix == null)
  523. prefix = String.Empty;
  524. if (ns == null)
  525. ns = String.Empty;
  526. string formatPrefix = "";
  527. string formatSpace = "";
  528. if (ns != String.Empty && prefix != "xmlns")
  529. {
  530. string existingPrefix = namespaceManager.LookupPrefix (ns);
  531. if (existingPrefix == null || existingPrefix == "")
  532. {
  533. if (prefix == "" || namespaceManager.LookupNamespace (prefix) != null)
  534. prefix = "d" + indentLevel + "p" + (newAttributeNamespaces.Count + 1);
  535. newAttributeNamespaces.Add (prefix, ns);
  536. }
  537. if (prefix == String.Empty && ns != XmlnsNamespace)
  538. prefix = (existingPrefix == null) ?
  539. String.Empty : existingPrefix;
  540. }
  541. if (prefix != String.Empty)
  542. {
  543. formatPrefix = prefix + ":";
  544. }
  545. if (openStartElement || attributeWrittenForElement)
  546. formatSpace = " ";
  547. w.Write (formatSpace);
  548. w.Write (formatPrefix);
  549. w.Write (localName);
  550. w.Write ('=');
  551. w.Write (quoteChar);
  552. openAttribute = true;
  553. attributeWrittenForElement = true;
  554. ws = WriteState.Attribute;
  555. if (prefix == "xmlns" || prefix == String.Empty && localName == "xmlns") {
  556. saveAttributeValue = true;
  557. savedAttributePrefix = (prefix == "xmlns") ? localName : String.Empty;
  558. savingAttributeValue = String.Empty;
  559. }
  560. }
  561. public override void WriteStartDocument ()
  562. {
  563. WriteStartDocument ("");
  564. }
  565. public override void WriteStartDocument (bool standalone)
  566. {
  567. string standaloneFormatting;
  568. if (standalone == true)
  569. standaloneFormatting = String.Format (" standalone={0}yes{0}", quoteChar);
  570. else
  571. standaloneFormatting = String.Format (" standalone={0}no{0}", quoteChar);
  572. WriteStartDocument (standaloneFormatting);
  573. }
  574. private void WriteStartDocument (string standaloneFormatting)
  575. {
  576. if (documentStarted == true)
  577. throw new InvalidOperationException("WriteStartDocument should be the first call.");
  578. if (hasRoot)
  579. throw new XmlException ("WriteStartDocument called twice.");
  580. CheckState ();
  581. string encodingFormatting = "";
  582. if (!nullEncoding)
  583. encodingFormatting = String.Format (" encoding={0}{1}{0}", quoteChar, w.Encoding.WebName);
  584. w.Write("<?xml version={0}1.0{0}{1}{2}?>", quoteChar, encodingFormatting, standaloneFormatting);
  585. ws = WriteState.Prolog;
  586. }
  587. public override void WriteStartElement (string prefix, string localName, string ns)
  588. {
  589. if (!Namespaces && (((prefix != null) && (prefix != String.Empty))
  590. || ((ns != null) && (ns != String.Empty))))
  591. throw new ArgumentException ("Cannot set the namespace if Namespaces is 'false'.");
  592. if ((prefix != null && prefix != String.Empty) && ((ns == null) || (ns == String.Empty)))
  593. throw new ArgumentException ("Cannot use a prefix with an empty namespace.");
  594. // ignore non-namespaced node's prefix.
  595. if (ns == null)
  596. ns = String.Empty;
  597. if (ns == String.Empty)
  598. prefix = String.Empty;
  599. WriteStartElementInternal (prefix, localName, ns);
  600. }
  601. private void WriteStartElementInternal (string prefix, string localName, string ns)
  602. {
  603. hasRoot = true;
  604. CheckState ();
  605. CloseStartElement ();
  606. newAttributeNamespaces.Clear ();
  607. if (prefix == null)
  608. prefix = namespaceManager.LookupPrefix (ns);
  609. if (prefix == null)
  610. prefix = String.Empty;
  611. w.Write (indentFormatting);
  612. w.Write ('<');
  613. if (prefix != String.Empty) {
  614. w.Write (prefix);
  615. w.Write (':');
  616. }
  617. w.Write (localName);
  618. openElements.Push (new XmlTextWriterOpenElement (prefix, localName));
  619. ws = WriteState.Element;
  620. openStartElement = true;
  621. openElementNS = ns;
  622. openElementPrefix = prefix;
  623. namespaceManager.PushScope ();
  624. indentLevel++;
  625. }
  626. public override void WriteString (string text)
  627. {
  628. if (ws == WriteState.Prolog)
  629. throw new InvalidOperationException ("Token content in state Prolog would result in an invalid XML document.");
  630. WriteStringInternal (text, true);
  631. // MS.NET (1.0) saves attribute value only at WriteString.
  632. if (saveAttributeValue)
  633. // In most cases it will be called one time, so simply use string + string.
  634. savingAttributeValue += text;
  635. }
  636. private string NormalizeAttributeString (string value)
  637. {
  638. return value.Replace ("\r", "&#xD;").Replace ("\n", "&#xA;");
  639. }
  640. private void WriteStringInternal (string text, bool entitize)
  641. {
  642. if (text == null)
  643. text = String.Empty;
  644. if (text != String.Empty) {
  645. CheckState ();
  646. if (entitize)
  647. {
  648. text = text.Replace ("&", "&amp;");
  649. text = text.Replace ("<", "&lt;");
  650. text = text.Replace (">", "&gt;");
  651. if (openAttribute)
  652. {
  653. if (quoteChar == '"')
  654. text = text.Replace ("\"", "&quot;");
  655. else
  656. text = text.Replace ("'", "&apos;");
  657. text = NormalizeAttributeString (text);
  658. }
  659. }
  660. if (!openAttribute)
  661. {
  662. IndentingOverriden = true;
  663. CloseStartElement ();
  664. }
  665. if (!openXmlLang && !openXmlSpace)
  666. w.Write (text);
  667. else
  668. {
  669. if (openXmlLang)
  670. xmlLang = text;
  671. else
  672. {
  673. switch (text)
  674. {
  675. case "default":
  676. xmlSpace = XmlSpace.Default;
  677. break;
  678. case "preserve":
  679. xmlSpace = XmlSpace.Preserve;
  680. break;
  681. default:
  682. throw new ArgumentException ("'{0}' is an invalid xml:space value.");
  683. }
  684. }
  685. }
  686. }
  687. }
  688. [MonoTODO]
  689. public override void WriteSurrogateCharEntity (char lowChar, char highChar)
  690. {
  691. throw new NotImplementedException ();
  692. }
  693. public override void WriteWhitespace (string ws)
  694. {
  695. if (!XmlChar.IsWhitespace (ws))
  696. throw new ArgumentException ("Invalid Whitespace");
  697. CheckState ();
  698. if (!openAttribute) {
  699. IndentingOverriden = true;
  700. CloseStartElement ();
  701. }
  702. w.Write (ws);
  703. }
  704. #endregion
  705. }
  706. }