Xml.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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 readonly XmlItem root;
  407. private bool readHeader = true;
  408. /// <summary>
  409. /// Gets or sets a value indicating whether is necessary to indent the document
  410. /// when saving it to a file/stream.
  411. /// </summary>
  412. public bool AutoIndent
  413. {
  414. get { return autoIndent; }
  415. set { autoIndent = value; }
  416. }
  417. /// <summary>
  418. /// Gets or sets a value indicating whether is necessary to add xml header.
  419. /// </summary>
  420. public bool WriteHeader
  421. {
  422. get { return writeHeader; }
  423. set { writeHeader = value; }
  424. }
  425. /// <summary>
  426. /// Gets or sets a value indicating whether is necessary to read xml header.
  427. /// </summary>
  428. public bool ReadHeader
  429. {
  430. get { return readHeader; }
  431. set { readHeader = value; }
  432. }
  433. /// <summary>
  434. /// Gets the root node of the document.
  435. /// </summary>
  436. public XmlItem Root
  437. {
  438. get { return root; }
  439. }
  440. /// <summary>
  441. /// Clears the document.
  442. /// </summary>
  443. public void Clear()
  444. {
  445. root.Clear();
  446. }
  447. /// <summary>
  448. /// Saves the document to a stream.
  449. /// </summary>
  450. /// <param name="stream">Stream to save to.</param>
  451. public void Save(Stream stream)
  452. {
  453. XmlWriter wr = new XmlWriter(stream);
  454. wr.AutoIndent = autoIndent;
  455. wr.IsWriteHeader = WriteHeader;
  456. wr.Write(root);
  457. }
  458. /// <summary>
  459. /// Saves the document to a string.
  460. /// </summary>
  461. /// <param name="textWriter">Writer to save to.</param>
  462. public void Save(TextWriter textWriter)
  463. {
  464. XmlWriter wr = new XmlWriter(textWriter);
  465. wr.AutoIndent = autoIndent;
  466. wr.IsWriteHeader = WriteHeader;
  467. wr.Write(root);
  468. }
  469. /// <summary>
  470. /// Loads the document from a stream.
  471. /// </summary>
  472. /// <param name="stream">Stream to load from.</param>
  473. public void Load(Stream stream)
  474. {
  475. XmlReader rd = new XmlReader(stream);
  476. root.Clear();
  477. rd.Read(root);
  478. }
  479. public void Load(TextReader reader)
  480. {
  481. XmlReader rd = new XmlReader(reader);
  482. rd.IsReadHeader = ReadHeader;
  483. root.Clear();
  484. rd.Read(root);
  485. }
  486. /// <summary>
  487. /// Saves the document to a file.
  488. /// </summary>
  489. /// <param name="fileName">The name of file to save to.</param>
  490. public void Save(string fileName)
  491. {
  492. FileStream s = new FileStream(fileName, FileMode.Create);
  493. Save(s);
  494. s.Close();
  495. }
  496. /// <summary>
  497. /// Loads the document from a file.
  498. /// </summary>
  499. /// <param name="fileName">The name of file to load from.</param>
  500. public void Load(string fileName)
  501. {
  502. FileStream s = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
  503. Load(s);
  504. s.Close();
  505. }
  506. /// <summary>
  507. /// Disposes resources used by the document.
  508. /// </summary>
  509. public void Dispose()
  510. {
  511. root.Dispose();
  512. }
  513. /// <summary>
  514. /// Initializes a new instance of the <b>XmlDocument</b> class with default settings.
  515. /// </summary>
  516. public XmlDocument()
  517. {
  518. root = new XmlItem();
  519. writeHeader = true;
  520. }
  521. /// <inheritdoc/>
  522. public override string ToString()
  523. {
  524. using (TextWriter tw = new StringWriter())
  525. {
  526. this.Save(tw);
  527. tw.Flush();
  528. return tw.ToString();
  529. }
  530. }
  531. }
  532. internal class XmlReader
  533. {
  534. private readonly TextReader reader;
  535. private string lastName;
  536. private enum ReadState { FindLeft, FindRight, FindComment, FindCloseItem, Done }
  537. private enum ItemState { Begin, End, Complete }
  538. private int symbolInBuffer;
  539. Dictionary<string, string> stringPool;
  540. private bool isReadHeader = true;
  541. public bool IsReadHeader
  542. {
  543. get
  544. {
  545. return isReadHeader;
  546. }
  547. set
  548. {
  549. isReadHeader = value;
  550. }
  551. }
  552. private ItemState ReadItem(XmlItem item)
  553. {
  554. FastString builder;
  555. if (Config.IsStringOptimization)
  556. builder = new FastStringWithPool(stringPool);
  557. else
  558. builder = new FastString();
  559. ReadState state = ReadState.FindLeft;
  560. int comment = 0;
  561. int i = 0;
  562. //string tempAttrName = null;
  563. //int lc = -1;
  564. int c = -1;
  565. // find <
  566. c = readNextSymbol();
  567. while (c != -1)
  568. {
  569. if (c == '<')
  570. break;
  571. c = readNextSymbol();
  572. }
  573. //while not end
  574. while (state != ReadState.Done && c != -1)
  575. {
  576. // find name or comment;
  577. c = readNextSymbol();
  578. i = 0;
  579. while (c != -1)
  580. {
  581. if (i <= comment)
  582. {
  583. switch (comment)
  584. {
  585. case 0: if (c == '!') comment++; break;
  586. case 1: if (c == '-') comment++; break;
  587. case 2: if (c == '-') state = ReadState.FindComment; break;
  588. default: comment = -1; break;
  589. }
  590. if (state == ReadState.FindComment) break;
  591. }
  592. i++;
  593. switch (c)
  594. {
  595. case '>': state = ReadState.Done; break; //Found name
  596. case ' ': state = ReadState.FindRight; break; //Found name
  597. case '<': RaiseException(); break;
  598. default: builder.Append((char)c); break;
  599. }
  600. if (state != ReadState.FindLeft) break;
  601. c = readNextSymbol();
  602. }
  603. switch (state)
  604. {
  605. case ReadState.FindComment:
  606. comment = 0;
  607. while (c != -1)
  608. {
  609. c = readNextSymbol();
  610. if (comment > 1)
  611. {
  612. if (c == '>')
  613. {
  614. state = ReadState.FindLeft;
  615. break;
  616. }
  617. }
  618. else
  619. {
  620. if (c == '-')
  621. comment++;
  622. else
  623. comment = 0;
  624. }
  625. }
  626. comment = 0;
  627. builder.Length = 0;
  628. while (c != -1)
  629. {
  630. if (c == '<')
  631. break;
  632. c = readNextSymbol();
  633. }
  634. break;
  635. case ReadState.Done:
  636. string result = builder.ToString();
  637. if (result[0] == '/')
  638. {
  639. item.Name = result.Substring(1);
  640. return ItemState.End;
  641. }
  642. if (result[result.Length - 1] == '/')
  643. {
  644. item.Name = result.Substring(0, result.Length - 1);
  645. return ItemState.Complete;
  646. }
  647. item.Name = result;
  648. return ItemState.Begin;
  649. case ReadState.FindRight:
  650. if (builder[0] == '/')
  651. {
  652. builder.Remove(0, 1);
  653. item.Name = builder.ToString();
  654. return ItemState.End;
  655. }
  656. item.Name = builder.ToString();
  657. builder.Length = 0;
  658. while (c != -1 && c != '>')
  659. {
  660. c = readNextSymbol();
  661. while (c != -1)
  662. {
  663. if (c == ' ')
  664. {
  665. builder.Length = 0;
  666. c = readNextSymbol();
  667. continue;
  668. }
  669. if (c == '=' || c == '>')
  670. break;
  671. builder.Append((char)c);
  672. c = readNextSymbol();
  673. }
  674. if (c == '>')
  675. {
  676. if (builder.Length > 0 && builder[builder.Length - 1] == '/')
  677. return ItemState.Complete;
  678. return ItemState.Begin;
  679. }
  680. c = readNextSymbol();
  681. if (c != '"')
  682. continue;
  683. string attrName = builder.ToString();
  684. builder.Length = 0;
  685. while (c != -1)
  686. {
  687. c = readNextSymbol();
  688. if (c == '"')
  689. break;
  690. builder.Append((char)c);
  691. }
  692. item.SetProp(attrName, Converter.FromXml(builder.ToString()));
  693. builder.Length = 0;
  694. }
  695. break;
  696. }
  697. }
  698. //just for errors
  699. return ItemState.Begin;
  700. }
  701. private int readNextSymbol()
  702. {
  703. if (symbolInBuffer != -1)
  704. {
  705. int temp = symbolInBuffer;
  706. symbolInBuffer = -1;
  707. return temp;
  708. }
  709. return reader.Read();
  710. }
  711. private bool ReadValue(XmlItem item)
  712. {
  713. FastString builder;
  714. if (Config.IsStringOptimization)
  715. builder = new FastStringWithPool(stringPool);
  716. else
  717. builder = new FastString();
  718. ReadState state = ReadState.FindLeft;
  719. string lastName = "</" + this.lastName + ">";
  720. int lastNameLength = lastName.Length;
  721. do
  722. {
  723. int c = reader.Read();
  724. if (c == -1)
  725. RaiseException();
  726. builder.Append((char)c);
  727. if (state == ReadState.FindLeft)
  728. {
  729. if (c == '<')
  730. {
  731. symbolInBuffer = '<';
  732. return false;
  733. }
  734. else if (c != ' ' && c != '\r' && c != '\n' && c != '\t')
  735. state = ReadState.FindCloseItem;
  736. }
  737. else if (state == ReadState.FindCloseItem)
  738. {
  739. if (builder.Length >= lastNameLength)
  740. {
  741. bool match = true;
  742. for (int j = 0; j < lastNameLength; j++)
  743. {
  744. if (builder[builder.Length - lastNameLength + j] != lastName[j])
  745. {
  746. match = false;
  747. break;
  748. }
  749. }
  750. if (match)
  751. {
  752. builder.Length -= lastNameLength;
  753. item.Value = Converter.FromXml(builder.ToString());
  754. return true;
  755. }
  756. }
  757. }
  758. }
  759. while (true);
  760. }
  761. private bool DoRead(XmlItem rootItem)
  762. {
  763. ItemState itemState = ReadItem(rootItem);
  764. lastName = rootItem.Name;
  765. if (itemState == ItemState.End)
  766. return true;
  767. else if (itemState == ItemState.Complete)
  768. return false;
  769. if (ReadValue(rootItem))
  770. return false;
  771. bool done = false;
  772. do
  773. {
  774. XmlItem childItem = new XmlItem();
  775. done = DoRead(childItem);
  776. if (!done)
  777. rootItem.AddItem(childItem);
  778. else
  779. childItem.Dispose();
  780. }
  781. while (!done);
  782. if (lastName != "" && String.Compare(lastName, rootItem.Name, true) != 0)
  783. RaiseException();
  784. return false;
  785. }
  786. private static void RaiseException()
  787. {
  788. throw new FileFormatException();
  789. }
  790. private void ReadHeader()
  791. {
  792. if (isReadHeader)
  793. {
  794. using (XmlItem item = new XmlItem())
  795. {
  796. ReadItem(item);
  797. if (!item.Name.StartsWith("?xml"))
  798. RaiseException();
  799. }
  800. }
  801. }
  802. public void Read(XmlItem item)
  803. {
  804. ReadHeader();
  805. DoRead(item);
  806. }
  807. public XmlReader(Stream stream)
  808. : this(new StreamReader(stream, Encoding.UTF8))
  809. {
  810. }
  811. public XmlReader(TextReader reader)
  812. {
  813. this.reader = reader;
  814. if (Config.IsStringOptimization)
  815. stringPool = new Dictionary<string, string>();
  816. lastName = "";
  817. symbolInBuffer = -1;
  818. }
  819. }
  820. internal class XmlWriter
  821. {
  822. private bool autoIndent;
  823. private bool isWriteHeader;
  824. //private Stream FStream;
  825. private TextWriter writer;
  826. public bool AutoIndent
  827. {
  828. get { return autoIndent; }
  829. set { autoIndent = value; }
  830. }
  831. public bool IsWriteHeader
  832. {
  833. get { return isWriteHeader; }
  834. set { isWriteHeader = value; }
  835. }
  836. private void WriteLn(string s)
  837. {
  838. if (!autoIndent)
  839. writer.Write(s);
  840. else
  841. writer.Write(s + "\r\n");
  842. }
  843. private string Dup(int num)
  844. {
  845. string s = "";
  846. return s.PadLeft(num);
  847. }
  848. private void WriteItem(XmlItem item, int level)
  849. {
  850. FastString sb = new FastString();
  851. // start
  852. if (autoIndent)
  853. sb.Append(Dup(level));
  854. sb.Append("<");
  855. sb.Append(item.Name);
  856. // text
  857. item.WriteProps(sb);
  858. // end
  859. if (item.Count == 0 && item.Value == "")
  860. sb.Append("/>");
  861. else
  862. sb.Append(">");
  863. // value
  864. if (item.Count == 0 && item.Value != "")
  865. {
  866. sb.Append(Converter.ToXml(item.Value, false));
  867. sb.Append("</");
  868. sb.Append(item.Name);
  869. sb.Append(">");
  870. }
  871. WriteLn(sb.ToString());
  872. }
  873. private void DoWrite(XmlItem rootItem, int level)
  874. {
  875. if (!autoIndent)
  876. level = 0;
  877. WriteItem(rootItem, level);
  878. for (int i = 0; i < rootItem.Count; i++)
  879. DoWrite(rootItem[i], level + 2);
  880. if (rootItem.Count > 0)
  881. {
  882. if (!autoIndent)
  883. WriteLn("</" + rootItem.Name + ">");
  884. else
  885. WriteLn(Dup(level) + "</" + rootItem.Name + ">");
  886. }
  887. }
  888. private void WriteHeader()
  889. {
  890. WriteLn("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  891. }
  892. public void Write(XmlItem rootItem)
  893. {
  894. if (IsWriteHeader)
  895. WriteHeader();
  896. DoWrite(rootItem, 0);
  897. writer.Flush();
  898. }
  899. public XmlWriter(Stream stream)
  900. {
  901. //FStream = stream;
  902. writer = new StreamWriter(stream, Encoding.UTF8);
  903. isWriteHeader = true;
  904. }
  905. public XmlWriter(TextWriter textWriter)
  906. {
  907. //FStream = null;
  908. writer = textWriter;
  909. isWriteHeader = true;
  910. }
  911. }
  912. }