Xml.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. namespace FastReport.Utils
  6. {
  7. /// <summary>
  8. /// Represents a xml property.
  9. /// </summary>
  10. #if READONLY_STRUCTS
  11. public readonly struct XmlProperty
  12. #else
  13. public struct XmlProperty
  14. #endif
  15. {
  16. private readonly string key;
  17. private readonly string value;
  18. /// <summary>
  19. /// Represents a property key.
  20. /// </summary>
  21. public string Key
  22. {
  23. get { return key; }
  24. }
  25. /// <summary>
  26. /// Represents a property value.
  27. /// </summary>
  28. public string Value
  29. {
  30. get { return value; }
  31. }
  32. private XmlProperty(string key, string value)
  33. {
  34. this.key = key;
  35. this.value = value;
  36. }
  37. /// <summary>
  38. /// Creates new property and assigns value
  39. /// </summary>
  40. /// <param name="key">Property key</param>
  41. /// <param name="value">Property value</param>
  42. public static XmlProperty Create(string key, string value)
  43. {
  44. return new XmlProperty(key, value);
  45. }
  46. }
  47. /// <summary>
  48. /// Represents a xml node.
  49. /// </summary>
  50. public class XmlItem : IDisposable
  51. {
  52. private List<XmlItem> items;
  53. private XmlItem parent;
  54. private string name;
  55. private string value;
  56. private XmlProperty[] properties;
  57. /// <summary>
  58. /// Gets a number of children in this node.
  59. /// </summary>
  60. public int Count
  61. {
  62. get { return items == null ? 0 : items.Count; }
  63. }
  64. /// <summary>
  65. /// Gets a list of children in this node.
  66. /// </summary>
  67. public List<XmlItem> Items
  68. {
  69. get
  70. {
  71. if (items == null)
  72. items = new List<XmlItem>();
  73. return items;
  74. }
  75. }
  76. /// <summary>
  77. /// Gets a child node with specified index.
  78. /// </summary>
  79. /// <param name="index">Index of node.</param>
  80. /// <returns>The node with specified index.</returns>
  81. public XmlItem this[int index]
  82. {
  83. get { return Items[index]; }
  84. }
  85. /// <summary>
  86. /// Gets or sets the node name.
  87. /// </summary>
  88. /// <remarks>
  89. /// This property will return "Node" for a node like <c>&lt;Node Text="" Left="0"/&gt;</c>
  90. /// </remarks>
  91. public string Name
  92. {
  93. get { return name; }
  94. set { name = value; }
  95. }
  96. /// <summary>
  97. /// Gets or sets a list of properties in this node.
  98. /// </summary>
  99. public XmlProperty[] Properties
  100. {
  101. get
  102. {
  103. if (properties == null)
  104. properties = new XmlProperty[0];
  105. return properties;
  106. }
  107. set
  108. {
  109. properties = value;
  110. }
  111. }
  112. /// <summary>
  113. /// Gets or sets the parent for this node.
  114. /// </summary>
  115. public XmlItem Parent
  116. {
  117. get { return parent; }
  118. set
  119. {
  120. if (parent != value)
  121. {
  122. if (parent != null)
  123. parent.Items.Remove(this);
  124. if (value != null)
  125. value.Items.Add(this);
  126. }
  127. parent = value;
  128. }
  129. }
  130. /// <summary>
  131. /// Gets or sets the node value.
  132. /// </summary>
  133. /// <remarks>
  134. /// This property will return "ABC" for a node like <c>&lt;Node&gt;ABC&lt;/Node&gt;</c>
  135. /// </remarks>
  136. public string Value
  137. {
  138. get { return value; }
  139. set { this.value = value; }
  140. }
  141. /// <summary>
  142. /// Gets the root node which owns this node.
  143. /// </summary>
  144. public XmlItem Root
  145. {
  146. get
  147. {
  148. XmlItem result = this;
  149. while (result.Parent != null)
  150. {
  151. result = result.Parent;
  152. }
  153. return result;
  154. }
  155. }
  156. /// <summary>
  157. /// Clears the child nodes of this node.
  158. /// </summary>
  159. public void Clear()
  160. {
  161. if (items != null)
  162. {
  163. items.Clear();
  164. /* while (Items.Count > 0)
  165. {
  166. Items[0].Dispose();
  167. } */
  168. items = null;
  169. }
  170. }
  171. /// <summary>
  172. /// Adds a new child node to this node.
  173. /// </summary>
  174. /// <returns>The new child node.</returns>
  175. public XmlItem Add()
  176. {
  177. XmlItem result = new XmlItem();
  178. AddItem(result);
  179. return result;
  180. }
  181. /// <summary>
  182. /// Adds a specified node to this node.
  183. /// </summary>
  184. /// <param name="item">The node to add.</param>
  185. public void AddItem(XmlItem item)
  186. {
  187. item.Parent = this;
  188. }
  189. /// <summary>
  190. /// Inserts a specified node to this node.
  191. /// </summary>
  192. /// <param name="index">Position to insert.</param>
  193. /// <param name="item">Node to insert.</param>
  194. public void InsertItem(int index, XmlItem item)
  195. {
  196. AddItem(item);
  197. Items.RemoveAt(Count - 1);
  198. Items.Insert(index, item);
  199. }
  200. /// <summary>
  201. /// Finds the node with specified name.
  202. /// </summary>
  203. /// <param name="name">The name of node to find.</param>
  204. /// <returns>The node with specified name, if found; <b>null</b> otherwise.</returns>
  205. public int Find(string name)
  206. {
  207. for (int i = 0; i < Count; i++)
  208. {
  209. if (String.Compare(Items[i].Name, name, true) == 0)
  210. return i;
  211. }
  212. return -1;
  213. }
  214. /// <summary>
  215. /// Finds the node with specified name.
  216. /// </summary>
  217. /// <param name="name">The name of node to find.</param>
  218. /// <returns>The node with specified name, if found; the new node otherwise.</returns>
  219. /// <remarks>
  220. /// This method adds the node with specified name to the child nodes if it cannot find the node.
  221. /// Do not dispose items, which has been created by this method
  222. /// </remarks>
  223. public XmlItem FindItem(string name)
  224. {
  225. XmlItem result = null;
  226. int i = Find(name);
  227. if (i == -1)
  228. {
  229. result = Add();
  230. result.Name = name;
  231. }
  232. else
  233. result = Items[i];
  234. return result;
  235. }
  236. /// <summary>
  237. /// Gets the index of specified node in the child nodes list.
  238. /// </summary>
  239. /// <param name="item">The node to find.</param>
  240. /// <returns>Zero-based index of node, if found; <b>-1</b> otherwise.</returns>
  241. public int IndexOf(XmlItem item)
  242. {
  243. return Items.IndexOf(item);
  244. }
  245. /// <summary>
  246. /// Gets a property with specified name.
  247. /// </summary>
  248. /// <param name="key">The property name.</param>
  249. /// <returns>The value of property, if found; empty string otherwise.</returns>
  250. /// <remarks>
  251. /// This property will return "0" when you request the "Left" property for a node
  252. /// like <c>&lt;Node Text="" Left="0"/&gt;</c>
  253. /// </remarks>
  254. public string GetProp(string key)
  255. {
  256. return GetProp(key, true);
  257. }
  258. internal string GetProp(string key, bool convertFromXml)
  259. {
  260. if (properties == null || properties.Length == 0)
  261. return "";
  262. // property key should be trimmed
  263. key = key.Trim();
  264. foreach (XmlProperty kv in properties)
  265. if (kv.Key == key)
  266. return kv.Value;
  267. return "";
  268. }
  269. internal void WriteProps(FastString sb)
  270. {
  271. if (properties == null || properties.Length == 0)
  272. return;
  273. sb.Append(" ");
  274. foreach (XmlProperty kv in properties)
  275. {
  276. //if (string.IsNullOrWhiteSpace(kv.Key))
  277. if (String.IsNullOrEmpty(kv.Key) || kv.Key.Trim().Length == 0)
  278. continue;
  279. sb.Append(kv.Key);
  280. sb.Append("=\"");
  281. sb.Append(Converter.ToXml(kv.Value));
  282. sb.Append("\" ");
  283. }
  284. sb.Length--;
  285. }
  286. /// <summary>
  287. /// Removes all properties.
  288. /// </summary>
  289. public void ClearProps()
  290. {
  291. properties = null;
  292. }
  293. internal void CopyPropsTo(XmlItem item)
  294. {
  295. if (properties == null)
  296. {
  297. item.properties = null;
  298. return;
  299. }
  300. item.properties = (XmlProperty[])properties.Clone();
  301. }
  302. internal bool IsNullOrEmptyProps()
  303. {
  304. return properties == null || properties.Length == 0;
  305. }
  306. /// <summary>
  307. /// Sets the value for a specified property.
  308. /// </summary>
  309. /// <param name="key">The property name.</param>
  310. /// <param name="value">Value to set.</param>
  311. /// <remarks>
  312. /// For example, you have a node like <c>&lt;Node Text="" Left="0"/&gt;</c>. When you set the
  313. /// "Text" property to "test", the node will be <c>&lt;Node Text="test" Left="0"/&gt;</c>.
  314. /// If property with specified name is not exist, it will be added.
  315. /// </remarks>
  316. public void SetProp(string key, string value)
  317. {
  318. // property key should be trimmed
  319. key = key.Trim();
  320. if (properties == null)
  321. {
  322. properties = new XmlProperty[1];
  323. properties[0] = XmlProperty.Create(key, value);
  324. return;
  325. }
  326. for (int i = 0; i < properties.Length; i++)
  327. {
  328. if (properties[i].Key == key)
  329. {
  330. properties[i] = XmlProperty.Create(key, value);
  331. return;
  332. }
  333. }
  334. Array.Resize<XmlProperty>(ref properties, properties.Length + 1);
  335. properties[properties.Length - 1] = XmlProperty.Create(key, value);
  336. }
  337. /// <summary>
  338. /// Removes a property with specified name.
  339. /// </summary>
  340. /// <param name="key">The property name.</param>
  341. /// <returns>Returns true if property is removed, false otherwise.</returns>
  342. public bool RemoveProp(string key)
  343. {
  344. if (properties == null || properties.Length == 0)
  345. return false;
  346. // property key should be trimmed
  347. key = key.Trim();
  348. if (properties.Length == 1 && properties[0].Key == key)
  349. {
  350. properties = null;
  351. return true;
  352. }
  353. if (properties[properties.Length - 1].Key == key)
  354. {
  355. Array.Resize<XmlProperty>(ref properties, properties.Length - 1);
  356. return true;
  357. }
  358. int target = -1;
  359. for (int i = 0; i < properties.Length; i++)
  360. {
  361. if (properties[i].Key == key)
  362. {
  363. target = i;
  364. break;
  365. }
  366. }
  367. if (target != -1)
  368. {
  369. for (int i = target; i < properties.Length - 1; i++)
  370. {
  371. properties[i] = properties[i + 1];
  372. }
  373. Array.Resize<XmlProperty>(ref properties, properties.Length - 1);
  374. return true;
  375. }
  376. return false;
  377. }
  378. /// <summary>
  379. /// Disposes the node and all its children.
  380. /// </summary>
  381. public void Dispose()
  382. {
  383. Clear();
  384. Parent = null;
  385. }
  386. /// <summary>
  387. /// Initializes a new instance of the <b>XmlItem</b> class with default settings.
  388. /// </summary>
  389. public XmlItem()
  390. {
  391. name = "";
  392. value = "";
  393. }
  394. }
  395. /// <summary>
  396. /// Represents a xml document that contains the root xml node.
  397. /// </summary>
  398. /// <remarks>
  399. /// Use <b>Load</b> and <b>Save</b> methods to load/save the document. To access the root node
  400. /// of the document, use the <see cref="Root"/> property.
  401. /// </remarks>
  402. public class XmlDocument : IDisposable
  403. {
  404. private bool autoIndent;
  405. private bool writeHeader;
  406. private XmlItem root;
  407. /// <summary>
  408. /// Gets or sets a value indicating whether is necessary to indent the document
  409. /// when saving it to a file/stream.
  410. /// </summary>
  411. public bool AutoIndent
  412. {
  413. get { return autoIndent; }
  414. set { autoIndent = value; }
  415. }
  416. /// <summary>
  417. /// Gets or sets a value indicating whether is necessary to add xml header.
  418. /// </summary>
  419. public bool WriteHeader
  420. {
  421. get { return writeHeader; }
  422. set { writeHeader = value; }
  423. }
  424. /// <summary>
  425. /// Gets the root node of the document.
  426. /// </summary>
  427. public XmlItem Root
  428. {
  429. get { return root; }
  430. }
  431. /// <summary>
  432. /// Clears the document.
  433. /// </summary>
  434. public void Clear()
  435. {
  436. root.Clear();
  437. }
  438. /// <summary>
  439. /// Saves the document to a stream.
  440. /// </summary>
  441. /// <param name="stream">Stream to save to.</param>
  442. public void Save(Stream stream)
  443. {
  444. XmlWriter wr = new XmlWriter(stream);
  445. wr.AutoIndent = autoIndent;
  446. wr.IsWriteHeader = WriteHeader;
  447. wr.Write(root);
  448. }
  449. /// <summary>
  450. /// Saves the document to a string.
  451. /// </summary>
  452. /// <param name="textWriter">Writer to save to.</param>
  453. public void Save(TextWriter textWriter)
  454. {
  455. XmlWriter wr = new XmlWriter(textWriter);
  456. wr.AutoIndent = autoIndent;
  457. wr.IsWriteHeader = WriteHeader;
  458. wr.Write(root);
  459. }
  460. /// <summary>
  461. /// Loads the document from a stream.
  462. /// </summary>
  463. /// <param name="stream">Stream to load from.</param>
  464. public void Load(Stream stream)
  465. {
  466. XmlReader rd = new XmlReader(stream);
  467. root.Clear();
  468. rd.Read(root);
  469. }
  470. /// <summary>
  471. /// Saves the document to a file.
  472. /// </summary>
  473. /// <param name="fileName">The name of file to save to.</param>
  474. public void Save(string fileName)
  475. {
  476. FileStream s = new FileStream(fileName, FileMode.Create);
  477. Save(s);
  478. s.Close();
  479. }
  480. /// <summary>
  481. /// Loads the document from a file.
  482. /// </summary>
  483. /// <param name="fileName">The name of file to load from.</param>
  484. public void Load(string fileName)
  485. {
  486. FileStream s = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
  487. Load(s);
  488. s.Close();
  489. }
  490. /// <summary>
  491. /// Disposes resources used by the document.
  492. /// </summary>
  493. public void Dispose()
  494. {
  495. root.Dispose();
  496. }
  497. /// <summary>
  498. /// Initializes a new instance of the <b>XmlDocument</b> class with default settings.
  499. /// </summary>
  500. public XmlDocument()
  501. {
  502. root = new XmlItem();
  503. writeHeader = true;
  504. }
  505. /// <inheritdoc/>
  506. public override string ToString()
  507. {
  508. using (TextWriter tw = new StringWriter())
  509. {
  510. this.Save(tw);
  511. tw.Flush();
  512. return tw.ToString();
  513. }
  514. }
  515. }
  516. internal class XmlReader
  517. {
  518. private StreamReader reader;
  519. private Stream stream;
  520. private string lastName;
  521. private enum ReadState { FindLeft, FindRight, FindComment, FindCloseItem, Done }
  522. private enum ItemState { Begin, End, Complete }
  523. private int symbolInBuffer;
  524. Dictionary<string, string> stringPool;
  525. private ItemState ReadItem(XmlItem item)
  526. {
  527. FastString builder;
  528. if (Config.IsStringOptimization)
  529. builder = new FastStringWithPool(stringPool);
  530. else
  531. builder = new FastString();
  532. ReadState state = ReadState.FindLeft;
  533. int comment = 0;
  534. int i = 0;
  535. //string tempAttrName = null;
  536. //int lc = -1;
  537. int c = -1;
  538. // find <
  539. c = readNextSymbol();
  540. while (c != -1)
  541. {
  542. if (c == '<')
  543. break;
  544. c = readNextSymbol();
  545. }
  546. //while not end
  547. while (state != ReadState.Done && c != -1)
  548. {
  549. // find name or comment;
  550. c = readNextSymbol();
  551. i = 0;
  552. while (c != -1)
  553. {
  554. if (i <= comment)
  555. {
  556. switch (comment)
  557. {
  558. case 0: if (c == '!') comment++; break;
  559. case 1: if (c == '-') comment++; break;
  560. case 2: if (c == '-') state = ReadState.FindComment; break;
  561. default: comment = -1; break;
  562. }
  563. if (state == ReadState.FindComment) break;
  564. }
  565. i++;
  566. switch (c)
  567. {
  568. case '>': state = ReadState.Done; break; //Found name
  569. case ' ': state = ReadState.FindRight; break; //Found name
  570. case '<': RaiseException(); break;
  571. default: builder.Append((char)c); break;
  572. }
  573. if (state != ReadState.FindLeft) break;
  574. c = readNextSymbol();
  575. }
  576. switch (state)
  577. {
  578. case ReadState.FindComment:
  579. comment = 0;
  580. while (c != -1)
  581. {
  582. c = readNextSymbol();
  583. if (comment > 1)
  584. {
  585. if (c == '>')
  586. {
  587. state = ReadState.FindLeft;
  588. break;
  589. }
  590. }
  591. else
  592. {
  593. if (c == '-')
  594. comment++;
  595. else
  596. comment = 0;
  597. }
  598. }
  599. comment = 0;
  600. builder.Length = 0;
  601. while (c != -1)
  602. {
  603. if (c == '<')
  604. break;
  605. c = readNextSymbol();
  606. }
  607. break;
  608. case ReadState.Done:
  609. string result = builder.ToString();
  610. if (result[0] == '/')
  611. {
  612. item.Name = result.Substring(1);
  613. return ItemState.End;
  614. }
  615. if (result[result.Length - 1] == '/')
  616. {
  617. item.Name = result.Substring(0, result.Length - 1);
  618. return ItemState.Complete;
  619. }
  620. item.Name = result;
  621. return ItemState.Begin;
  622. case ReadState.FindRight:
  623. if (builder[0] == '/')
  624. {
  625. builder.Remove(0, 1);
  626. item.Name = builder.ToString();
  627. return ItemState.End;
  628. }
  629. item.Name = builder.ToString();
  630. builder.Length = 0;
  631. while (c != -1 && c != '>')
  632. {
  633. c = readNextSymbol();
  634. while (c != -1)
  635. {
  636. if (c == ' ')
  637. {
  638. builder.Length = 0;
  639. c = readNextSymbol();
  640. continue;
  641. }
  642. if (c == '=' || c == '>')
  643. break;
  644. builder.Append((char)c);
  645. c = readNextSymbol();
  646. }
  647. if (c == '>')
  648. {
  649. if (builder.Length > 0 && builder[builder.Length - 1] == '/')
  650. return ItemState.Complete;
  651. return ItemState.Begin;
  652. }
  653. c = readNextSymbol();
  654. if (c != '"')
  655. continue;
  656. string attrName = builder.ToString();
  657. builder.Length = 0;
  658. while (c != -1)
  659. {
  660. c = readNextSymbol();
  661. if (c == '"')
  662. break;
  663. builder.Append((char)c);
  664. }
  665. item.SetProp(attrName, Converter.FromXml(builder.ToString()));
  666. builder.Length = 0;
  667. }
  668. break;
  669. }
  670. }
  671. //just for errors
  672. return ItemState.Begin;
  673. }
  674. private int readNextSymbol()
  675. {
  676. if (symbolInBuffer != -1)
  677. {
  678. int temp = symbolInBuffer;
  679. symbolInBuffer = -1;
  680. return temp;
  681. }
  682. return reader.Read();
  683. }
  684. private bool ReadValue(XmlItem item)
  685. {
  686. FastString builder;
  687. if (Config.IsStringOptimization)
  688. builder = new FastStringWithPool(stringPool);
  689. else
  690. builder = new FastString();
  691. ReadState state = ReadState.FindLeft;
  692. string lastName = "</" + this.lastName + ">";
  693. int lastNameLength = lastName.Length;
  694. do
  695. {
  696. int c = reader.Read();
  697. if (c == -1)
  698. RaiseException();
  699. builder.Append((char)c);
  700. if (state == ReadState.FindLeft)
  701. {
  702. if (c == '<')
  703. {
  704. symbolInBuffer = '<';
  705. return false;
  706. }
  707. else if (c != ' ' && c != '\r' && c != '\n' && c != '\t')
  708. state = ReadState.FindCloseItem;
  709. }
  710. else if (state == ReadState.FindCloseItem)
  711. {
  712. if (builder.Length >= lastNameLength)
  713. {
  714. bool match = true;
  715. for (int j = 0; j < lastNameLength; j++)
  716. {
  717. if (builder[builder.Length - lastNameLength + j] != lastName[j])
  718. {
  719. match = false;
  720. break;
  721. }
  722. }
  723. if (match)
  724. {
  725. builder.Length -= lastNameLength;
  726. item.Value = Converter.FromXml(builder.ToString());
  727. return true;
  728. }
  729. }
  730. }
  731. }
  732. while (true);
  733. }
  734. private bool DoRead(XmlItem rootItem)
  735. {
  736. ItemState itemState = ReadItem(rootItem);
  737. lastName = rootItem.Name;
  738. if (itemState == ItemState.End)
  739. return true;
  740. else if (itemState == ItemState.Complete)
  741. return false;
  742. if (ReadValue(rootItem))
  743. return false;
  744. bool done = false;
  745. do
  746. {
  747. XmlItem childItem = new XmlItem();
  748. done = DoRead(childItem);
  749. if (!done)
  750. rootItem.AddItem(childItem);
  751. else
  752. childItem.Dispose();
  753. }
  754. while (!done);
  755. if (lastName != "" && String.Compare(lastName, rootItem.Name, true) != 0)
  756. RaiseException();
  757. return false;
  758. }
  759. private void RaiseException()
  760. {
  761. throw new FileFormatException();
  762. }
  763. private void ReadHeader()
  764. {
  765. using (XmlItem item = new XmlItem())
  766. {
  767. ReadItem(item);
  768. if (item.Name.IndexOf("?xml") != 0)
  769. RaiseException();
  770. }
  771. }
  772. public void Read(XmlItem item)
  773. {
  774. ReadHeader();
  775. DoRead(item);
  776. }
  777. public XmlReader(Stream stream)
  778. {
  779. this.stream = stream;
  780. reader = new StreamReader(this.stream, Encoding.UTF8);
  781. if (Config.IsStringOptimization)
  782. stringPool = new Dictionary<string, string>();
  783. lastName = "";
  784. symbolInBuffer = -1;
  785. }
  786. }
  787. internal class XmlWriter
  788. {
  789. private bool autoIndent;
  790. private bool isWriteHeader;
  791. //private Stream FStream;
  792. private TextWriter writer;
  793. public bool AutoIndent
  794. {
  795. get { return autoIndent; }
  796. set { autoIndent = value; }
  797. }
  798. public bool IsWriteHeader
  799. {
  800. get { return isWriteHeader; }
  801. set { isWriteHeader = value; }
  802. }
  803. private void WriteLn(string s)
  804. {
  805. if (!autoIndent)
  806. writer.Write(s);
  807. else
  808. writer.Write(s + "\r\n");
  809. }
  810. private string Dup(int num)
  811. {
  812. string s = "";
  813. return s.PadLeft(num);
  814. }
  815. private void WriteItem(XmlItem item, int level)
  816. {
  817. FastString sb = new FastString();
  818. // start
  819. if (autoIndent)
  820. sb.Append(Dup(level));
  821. sb.Append("<");
  822. sb.Append(item.Name);
  823. // text
  824. item.WriteProps(sb);
  825. // end
  826. if (item.Count == 0 && item.Value == "")
  827. sb.Append("/>");
  828. else
  829. sb.Append(">");
  830. // value
  831. if (item.Count == 0 && item.Value != "")
  832. {
  833. sb.Append(Converter.ToXml(item.Value,false));
  834. sb.Append("</");
  835. sb.Append(item.Name);
  836. sb.Append(">");
  837. }
  838. WriteLn(sb.ToString());
  839. }
  840. private void DoWrite(XmlItem rootItem, int level)
  841. {
  842. if (!autoIndent)
  843. level = 0;
  844. WriteItem(rootItem, level);
  845. for (int i = 0; i < rootItem.Count; i++)
  846. DoWrite(rootItem[i], level + 2);
  847. if (rootItem.Count > 0)
  848. {
  849. if (!autoIndent)
  850. WriteLn("</" + rootItem.Name + ">");
  851. else
  852. WriteLn(Dup(level) + "</" + rootItem.Name + ">");
  853. }
  854. }
  855. private void WriteHeader()
  856. {
  857. WriteLn("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  858. }
  859. public void Write(XmlItem rootItem)
  860. {
  861. if (IsWriteHeader)
  862. WriteHeader();
  863. DoWrite(rootItem, 0);
  864. writer.Flush();
  865. }
  866. public XmlWriter(Stream stream)
  867. {
  868. //FStream = stream;
  869. writer = new StreamWriter(stream, Encoding.UTF8);
  870. isWriteHeader = true;
  871. }
  872. public XmlWriter(TextWriter textWriter)
  873. {
  874. //FStream = null;
  875. writer = textWriter;
  876. isWriteHeader = true;
  877. }
  878. }
  879. }