XmlTextReader.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. // -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2. //
  3. // System.Xml.XmlTextReader.cs
  4. //
  5. // Author:
  6. // Jason Diamond ([email protected])
  7. //
  8. // (C) 2001 Jason Diamond http://injektilo.org/
  9. //
  10. // FIXME:
  11. // This can only parse basic XML: elements, attributes, processing
  12. // instructions, and comments are OK but there's no support for
  13. // namespaces yet.
  14. //
  15. // It barfs on DOCTYPE declarations.
  16. //
  17. // There's also no checking being done for either well-formedness
  18. // or validity.
  19. //
  20. // ParserContext and NameTables aren't being used yet.
  21. //
  22. // Some thought needs to be given to performance. There's too many
  23. // strings being allocated.
  24. //
  25. // None of the MoveTo methods have been implemented yet.
  26. //
  27. // LineNumber and LinePosition aren't being tracked.
  28. //
  29. // xml:space, xml:lang, and xml:base aren't being tracked.
  30. //
  31. // Depth isn't being tracked.
  32. using System;
  33. using System.Collections;
  34. using System.IO;
  35. using System.Net;
  36. using System.Text;
  37. namespace System.Xml
  38. {
  39. public class XmlTextReader : XmlReader, IXmlLineInfo
  40. {
  41. // constructors
  42. protected XmlTextReader()
  43. {
  44. Init();
  45. }
  46. public XmlTextReader(Stream input)
  47. {
  48. Init();
  49. reader = new StreamReader(
  50. input,
  51. Encoding.UTF8,
  52. true);
  53. }
  54. public XmlTextReader(string url)
  55. {
  56. Init();
  57. WebClient client = new WebClient();
  58. reader = new StreamReader(
  59. client.OpenRead(url),
  60. Encoding.UTF8,
  61. true);
  62. }
  63. public XmlTextReader(TextReader input)
  64. {
  65. Init();
  66. reader = input;
  67. }
  68. public XmlTextReader(Stream input, XmlNameTable nameTable)
  69. {
  70. // TODO: implement me.
  71. throw new NotImplementedException();
  72. }
  73. public XmlTextReader(string baseURI, Stream input)
  74. {
  75. // TODO: implement me.
  76. throw new NotImplementedException();
  77. }
  78. public XmlTextReader(string baseURI, TextReader input)
  79. {
  80. // TODO: implement me.
  81. throw new NotImplementedException();
  82. }
  83. public XmlTextReader(string url, XmlNameTable nameTable)
  84. {
  85. // TODO: implement me.
  86. throw new NotImplementedException();
  87. }
  88. public XmlTextReader(
  89. TextReader input,
  90. XmlNameTable nameTable)
  91. {
  92. // TODO: implement me.
  93. throw new NotImplementedException();
  94. }
  95. public XmlTextReader(
  96. Stream inputFragment,
  97. XmlNodeType fragmentType,
  98. XmlParserContext context)
  99. {
  100. // TODO: implement me.
  101. throw new NotImplementedException();
  102. }
  103. public XmlTextReader(
  104. string baseURI,
  105. Stream input,
  106. XmlNameTable nameTable)
  107. {
  108. // TODO: implement me.
  109. throw new NotImplementedException();
  110. }
  111. public XmlTextReader(
  112. string baseURI,
  113. TextReader input,
  114. XmlNameTable nameTable)
  115. {
  116. // TODO: implement me.
  117. throw new NotImplementedException();
  118. }
  119. public XmlTextReader(
  120. string fragment,
  121. XmlNodeType fragmentType,
  122. XmlParserContext context)
  123. {
  124. // TODO: implement me.
  125. throw new NotImplementedException();
  126. }
  127. // properties
  128. public override int AttributeCount
  129. {
  130. get
  131. {
  132. return attributes.Count;
  133. }
  134. }
  135. public override string BaseURI
  136. {
  137. get
  138. {
  139. // TODO: implement me.
  140. return null;
  141. }
  142. }
  143. public override bool CanResolveEntity
  144. {
  145. get
  146. {
  147. // XmlTextReaders don't resolve entities.
  148. return false;
  149. }
  150. }
  151. public override int Depth
  152. {
  153. get
  154. {
  155. // TODO: implement me.
  156. return depth > 0 ? depth : 0;
  157. }
  158. }
  159. public Encoding Encoding
  160. {
  161. get
  162. {
  163. // TODO: implement me.
  164. return null;
  165. }
  166. }
  167. public override bool EOF
  168. {
  169. get
  170. {
  171. return
  172. readState == ReadState.EndOfFile ||
  173. readState == ReadState.Closed;
  174. }
  175. }
  176. public override bool HasValue
  177. {
  178. get
  179. {
  180. return value != String.Empty;
  181. }
  182. }
  183. public override bool IsDefault
  184. {
  185. get
  186. {
  187. // XmlTextReader does not expand default attributes.
  188. return false;
  189. }
  190. }
  191. public override bool IsEmptyElement
  192. {
  193. get
  194. {
  195. return isEmptyElement;
  196. }
  197. }
  198. public override string this[int i]
  199. {
  200. get
  201. {
  202. return GetAttribute(i);
  203. }
  204. }
  205. public override string this[string name]
  206. {
  207. get
  208. {
  209. return GetAttribute(name);
  210. }
  211. }
  212. public override string this[
  213. string localName,
  214. string namespaceName]
  215. {
  216. get
  217. {
  218. return GetAttribute(localName, namespaceName);
  219. }
  220. }
  221. public int LineNumber
  222. {
  223. get
  224. {
  225. // TODO: implement me.
  226. return 0;
  227. }
  228. }
  229. public int LinePosition
  230. {
  231. get
  232. {
  233. // TODO: implement me.
  234. return 0;
  235. }
  236. }
  237. public override string LocalName
  238. {
  239. get
  240. {
  241. return localName;
  242. }
  243. }
  244. public override string Name
  245. {
  246. get
  247. {
  248. return name;
  249. }
  250. }
  251. public bool Namespaces
  252. {
  253. get
  254. {
  255. // TODO: implement me.
  256. return false;
  257. }
  258. set
  259. {
  260. // TODO: implement me.
  261. }
  262. }
  263. public override string NamespaceURI
  264. {
  265. get
  266. {
  267. // TODO: implement me.
  268. return null;
  269. }
  270. }
  271. public override XmlNameTable NameTable
  272. {
  273. get
  274. {
  275. // TODO: implement me.
  276. return null;
  277. }
  278. }
  279. public override XmlNodeType NodeType
  280. {
  281. get
  282. {
  283. return nodeType;
  284. }
  285. }
  286. public bool Normalization
  287. {
  288. get
  289. {
  290. // TODO: implement me.
  291. return false;
  292. }
  293. set
  294. {
  295. // TODO: implement me.
  296. }
  297. }
  298. public override string Prefix
  299. {
  300. get
  301. {
  302. return prefix;
  303. }
  304. }
  305. public override char QuoteChar
  306. {
  307. get
  308. {
  309. // TODO: implement me.
  310. return '"';
  311. }
  312. }
  313. public override ReadState ReadState
  314. {
  315. get
  316. {
  317. return readState;
  318. }
  319. }
  320. public override string Value
  321. {
  322. get
  323. {
  324. return value;
  325. }
  326. }
  327. public WhitespaceHandling WhitespaceHandling
  328. {
  329. get
  330. {
  331. // TODO: implement me.
  332. return WhitespaceHandling.All;
  333. }
  334. set
  335. {
  336. // TODO: implement me.
  337. }
  338. }
  339. public override string XmlLang
  340. {
  341. get
  342. {
  343. // TODO: implement me.
  344. return null;
  345. }
  346. }
  347. public XmlResolver XmlResolver
  348. {
  349. set
  350. {
  351. // TODO: implement me.
  352. }
  353. }
  354. public override XmlSpace XmlSpace
  355. {
  356. get
  357. {
  358. // TODO: implement me.
  359. return XmlSpace.Default;
  360. }
  361. }
  362. // methods
  363. public override void Close()
  364. {
  365. readState = ReadState.Closed;
  366. }
  367. public override string GetAttribute(int i)
  368. {
  369. // TODO: implement me.
  370. return null;
  371. }
  372. public override string GetAttribute(string name)
  373. {
  374. return (string)attributes[name];
  375. }
  376. public override string GetAttribute(
  377. string localName,
  378. string namespaceName)
  379. {
  380. // TODO: implement me.
  381. return null;
  382. }
  383. public TextReader GetRemainder()
  384. {
  385. // TODO: implement me.
  386. return null;
  387. }
  388. // Why does this use explicit interface implementation?
  389. bool IXmlLineInfo.HasLineInfo()
  390. {
  391. // TODO: implement me.
  392. return false;
  393. }
  394. public override string LookupNamespace(string prefix)
  395. {
  396. // TODO: implement me.
  397. return null;
  398. }
  399. public override void MoveToAttribute(int i)
  400. {
  401. // TODO: implement me.
  402. }
  403. public override bool MoveToAttribute(string name)
  404. {
  405. // TODO: implement me.
  406. return false;
  407. }
  408. public override bool MoveToAttribute(
  409. string localName,
  410. string namespaceName)
  411. {
  412. // TODO: implement me.
  413. return false;
  414. }
  415. public override bool MoveToElement()
  416. {
  417. // TODO: implement me.
  418. return false;
  419. }
  420. public override bool MoveToFirstAttribute()
  421. {
  422. // TODO: implement me.
  423. return false;
  424. }
  425. public override bool MoveToNextAttribute()
  426. {
  427. // TODO: implement me.
  428. return false;
  429. }
  430. public override bool Read()
  431. {
  432. bool more = false;
  433. readState = ReadState.Interactive;
  434. more = ReadContent();
  435. return more;
  436. }
  437. public override bool ReadAttributeValue()
  438. {
  439. // TODO: implement me.
  440. return false;
  441. }
  442. public int ReadBase64(byte[] buffer, int offset, int length)
  443. {
  444. // TODO: implement me.
  445. return 0;
  446. }
  447. public int ReadBinHex(byte[] buffer, int offset, int length)
  448. {
  449. // TODO: implement me.
  450. return 0;
  451. }
  452. public int ReadChars(char[] buffer, int offset, int length)
  453. {
  454. // TODO: implement me.
  455. return 0;
  456. }
  457. public override string ReadInnerXml()
  458. {
  459. // TODO: implement me.
  460. return null;
  461. }
  462. public override string ReadOuterXml()
  463. {
  464. // TODO: implement me.
  465. return null;
  466. }
  467. public override string ReadString()
  468. {
  469. // TODO: implement me.
  470. return null;
  471. }
  472. public override void ResolveEntity()
  473. {
  474. // XmlTextReaders don't resolve entities.
  475. throw new InvalidOperationException("XmlTextReaders don't resolve entities.");
  476. }
  477. // privates
  478. private TextReader reader;
  479. private ReadState readState;
  480. private int depth;
  481. private bool depthDown;
  482. private XmlNodeType nodeType;
  483. private string name;
  484. private string prefix;
  485. private string localName;
  486. private bool isEmptyElement;
  487. private string value;
  488. private Hashtable attributes;
  489. private bool returnEntityReference;
  490. private string entityReferenceName;
  491. private char[] nameBuffer;
  492. private int nameLength;
  493. private int nameCapacity;
  494. private const int initialNameCapacity = 256;
  495. private char[] valueBuffer;
  496. private int valueLength;
  497. private int valueCapacity;
  498. private const int initialValueCapacity = 8192;
  499. private void Init()
  500. {
  501. readState = ReadState.Initial;
  502. depth = -1;
  503. depthDown = false;
  504. nodeType = XmlNodeType.None;
  505. name = String.Empty;
  506. prefix = String.Empty;
  507. localName = string.Empty;
  508. isEmptyElement = false;
  509. value = String.Empty;
  510. attributes = new Hashtable();
  511. returnEntityReference = false;
  512. entityReferenceName = String.Empty;
  513. nameBuffer = new char[initialNameCapacity];
  514. nameLength = 0;
  515. nameCapacity = initialNameCapacity;
  516. valueBuffer = new char[initialValueCapacity];
  517. valueLength = 0;
  518. valueCapacity = initialValueCapacity;
  519. }
  520. // Use this method rather than setting the properties
  521. // directly so that all the necessary properties can
  522. // be changed in harmony with each other. Maybe the
  523. // fields should be in a seperate class to help enforce
  524. // this.
  525. private void SetProperties(
  526. XmlNodeType nodeType,
  527. string name,
  528. bool isEmptyElement,
  529. string value,
  530. bool clearAttributes)
  531. {
  532. this.nodeType = nodeType;
  533. this.name = name;
  534. this.isEmptyElement = isEmptyElement;
  535. this.value = value;
  536. if (clearAttributes)
  537. {
  538. ClearAttributes();
  539. }
  540. int indexOfColon = name.IndexOf(':');
  541. if (indexOfColon == -1)
  542. {
  543. prefix = String.Empty;
  544. localName = name;
  545. }
  546. else
  547. {
  548. prefix = name.Substring(0, indexOfColon);
  549. localName = name.Substring(indexOfColon + 1);
  550. }
  551. }
  552. private void AddAttribute(string name, string value)
  553. {
  554. attributes.Add(name, value);
  555. }
  556. private void ClearAttributes()
  557. {
  558. if (attributes.Count > 0)
  559. {
  560. attributes.Clear();
  561. }
  562. }
  563. private int PeekChar()
  564. {
  565. return reader.Peek();
  566. }
  567. private int ReadChar()
  568. {
  569. return reader.Read();
  570. }
  571. // This should really keep track of some state so
  572. // that it's not possible to have more than one document
  573. // element or text outside of the document element.
  574. private bool ReadContent()
  575. {
  576. bool more = false;
  577. if (depthDown)
  578. {
  579. --depth;
  580. }
  581. if (returnEntityReference)
  582. {
  583. ++depth;
  584. SetEntityReferenceProperties();
  585. more = true;
  586. }
  587. else
  588. {
  589. switch (PeekChar())
  590. {
  591. case '<':
  592. ReadChar();
  593. ReadTag();
  594. more = true;
  595. break;
  596. case -1:
  597. readState = ReadState.EndOfFile;
  598. SetProperties(
  599. XmlNodeType.None, // nodeType
  600. String.Empty, // name
  601. false, // isEmptyElement
  602. String.Empty, // value
  603. true // clearAttributes
  604. );
  605. more = false;
  606. break;
  607. default:
  608. ReadText();
  609. more = true;
  610. break;
  611. }
  612. }
  613. return more;
  614. }
  615. private void SetEntityReferenceProperties()
  616. {
  617. SetProperties(
  618. XmlNodeType.EntityReference, // nodeType
  619. entityReferenceName, // name
  620. false, // isEmptyElement
  621. String.Empty, // value
  622. true // clearAttributes
  623. );
  624. returnEntityReference = false;
  625. entityReferenceName = String.Empty;
  626. }
  627. // The leading '<' has already been consumed.
  628. private void ReadTag()
  629. {
  630. switch (PeekChar())
  631. {
  632. case '/':
  633. ReadChar();
  634. ReadEndTag();
  635. break;
  636. case '?':
  637. ReadChar();
  638. ReadProcessingInstruction();
  639. break;
  640. case '!':
  641. ReadChar();
  642. ReadDeclaration();
  643. break;
  644. default:
  645. ReadStartTag();
  646. break;
  647. }
  648. }
  649. // The leading '<' has already been consumed.
  650. private void ReadStartTag()
  651. {
  652. string name = ReadName();
  653. SkipWhitespace();
  654. bool isEmptyElement = false;
  655. ClearAttributes();
  656. if (XmlChar.IsFirstNameChar(PeekChar()))
  657. {
  658. ReadAttributes();
  659. }
  660. if (PeekChar() == '/')
  661. {
  662. ReadChar();
  663. isEmptyElement = true;
  664. depthDown = true;
  665. }
  666. Expect('>');
  667. ++depth;
  668. SetProperties(
  669. XmlNodeType.Element, // nodeType
  670. name, // name
  671. isEmptyElement, // isEmptyElement
  672. String.Empty, // value
  673. false // clearAttributes
  674. );
  675. }
  676. // The reader is positioned on the first character
  677. // of the element's name.
  678. private void ReadEndTag()
  679. {
  680. string name = ReadName();
  681. SkipWhitespace();
  682. Expect('>');
  683. --depth;
  684. SetProperties(
  685. XmlNodeType.EndElement, // nodeType
  686. name, // name
  687. false, // isEmptyElement
  688. String.Empty, // value
  689. true // clearAttributes
  690. );
  691. }
  692. private void AppendNameChar(int ch)
  693. {
  694. CheckNameCapacity();
  695. nameBuffer[nameLength++] = (char)ch;
  696. }
  697. private void CheckNameCapacity()
  698. {
  699. if (nameLength == nameCapacity)
  700. {
  701. nameCapacity = nameCapacity * 2;
  702. char[] oldNameBuffer = nameBuffer;
  703. nameBuffer = new char[nameCapacity];
  704. Array.Copy(oldNameBuffer, nameBuffer, nameLength);
  705. }
  706. }
  707. private string CreateNameString()
  708. {
  709. return new String(nameBuffer, 0, nameLength);
  710. }
  711. private void AppendValueChar(int ch)
  712. {
  713. CheckValueCapacity();
  714. valueBuffer[valueLength++] = (char)ch;
  715. }
  716. private void CheckValueCapacity()
  717. {
  718. if (valueLength == valueCapacity)
  719. {
  720. valueCapacity = valueCapacity * 2;
  721. char[] oldValueBuffer = valueBuffer;
  722. valueBuffer = new char[valueCapacity];
  723. Array.Copy(oldValueBuffer, valueBuffer, valueLength);
  724. }
  725. }
  726. private string CreateValueString()
  727. {
  728. return new String(valueBuffer, 0, valueLength);
  729. }
  730. // The reader is positioned on the first character
  731. // of the text.
  732. private void ReadText()
  733. {
  734. valueLength = 0;
  735. int ch = PeekChar();
  736. while (ch != '<' && ch != -1)
  737. {
  738. if (ch == '&')
  739. {
  740. ReadChar();
  741. if (ReadReference(false))
  742. {
  743. break;
  744. }
  745. }
  746. else
  747. {
  748. AppendValueChar(ReadChar());
  749. }
  750. ch = PeekChar();
  751. }
  752. if (returnEntityReference && valueLength == 0)
  753. {
  754. ++depth;
  755. SetEntityReferenceProperties();
  756. }
  757. else
  758. {
  759. if (depth >= 0)
  760. {
  761. ++depth;
  762. depthDown = true;
  763. }
  764. SetProperties(
  765. XmlNodeType.Text, // nodeType
  766. String.Empty, // name
  767. false, // isEmptyElement
  768. CreateValueString(), // value
  769. true // clearAttributes
  770. );
  771. }
  772. }
  773. // The leading '&' has already been consumed.
  774. // Returns true if the entity reference isn't a simple
  775. // character reference or one of the predefined entities.
  776. // This allows the ReadText method to break so that the
  777. // next call to Read will return the EntityReference node.
  778. private bool ReadReference(bool ignoreEntityReferences)
  779. {
  780. if (PeekChar() == '#')
  781. {
  782. ReadChar();
  783. ReadCharacterReference();
  784. }
  785. else
  786. {
  787. ReadEntityReference(ignoreEntityReferences);
  788. }
  789. return returnEntityReference;
  790. }
  791. private void ReadCharacterReference()
  792. {
  793. int value = 0;
  794. if (PeekChar() == 'x')
  795. {
  796. ReadChar();
  797. while (PeekChar() != ';' && PeekChar() != -1)
  798. {
  799. int ch = ReadChar();
  800. if (ch >= '0' && ch <= '9')
  801. {
  802. value = (value << 4) + ch - '0';
  803. }
  804. else if (ch >= 'A' && ch <= 'F')
  805. {
  806. value = (value << 4) + ch - 'A' + 10;
  807. }
  808. else if (ch >= 'a' && ch <= 'f')
  809. {
  810. value = (value << 4) + ch - 'a' + 10;
  811. }
  812. else
  813. {
  814. throw new Exception(
  815. String.Format(
  816. "invalid hexadecimal digit: {0} (#x{1:X})",
  817. (char)ch,
  818. ch));
  819. }
  820. }
  821. }
  822. else
  823. {
  824. while (PeekChar() != ';' && PeekChar() != -1)
  825. {
  826. int ch = ReadChar();
  827. if (ch >= '0' && ch <= '9')
  828. {
  829. value = value * 10 + ch - '0';
  830. }
  831. else
  832. {
  833. throw new Exception(
  834. String.Format(
  835. "invalid decimal digit: {0} (#x{1:X})",
  836. (char)ch,
  837. ch));
  838. }
  839. }
  840. }
  841. ReadChar(); // ';'
  842. AppendValueChar(value);
  843. }
  844. private void ReadEntityReference(bool ignoreEntityReferences)
  845. {
  846. nameLength = 0;
  847. int ch = PeekChar();
  848. while (ch != ';' && ch != -1)
  849. {
  850. AppendNameChar(ReadChar());
  851. ch = PeekChar();
  852. }
  853. Expect(';');
  854. string name = CreateNameString();
  855. switch (name)
  856. {
  857. case "lt":
  858. AppendValueChar('<');
  859. break;
  860. case "gt":
  861. AppendValueChar('>');
  862. break;
  863. case "amp":
  864. AppendValueChar('&');
  865. break;
  866. case "apos":
  867. AppendValueChar('\'');
  868. break;
  869. case "quot":
  870. AppendValueChar('"');
  871. break;
  872. default:
  873. if (ignoreEntityReferences)
  874. {
  875. AppendValueChar('&');
  876. foreach (char ch2 in name)
  877. {
  878. AppendValueChar(ch2);
  879. }
  880. AppendValueChar(';');
  881. }
  882. else
  883. {
  884. returnEntityReference = true;
  885. entityReferenceName = name;
  886. }
  887. break;
  888. }
  889. }
  890. // The reader is positioned on the first character of
  891. // the attribute name.
  892. private void ReadAttributes()
  893. {
  894. do
  895. {
  896. string name = ReadName();
  897. SkipWhitespace();
  898. Expect('=');
  899. SkipWhitespace();
  900. string value = ReadAttribute();
  901. SkipWhitespace();
  902. AddAttribute(name, value);
  903. }
  904. while (PeekChar() != '/' && PeekChar() != '>' && PeekChar() != -1);
  905. }
  906. // The reader is positioned on the quote character.
  907. private string ReadAttribute()
  908. {
  909. int quoteChar = ReadChar();
  910. if (quoteChar != '\'' && quoteChar != '\"')
  911. {
  912. throw new Exception("an attribute value was not quoted");
  913. }
  914. valueLength = 0;
  915. while (PeekChar() != quoteChar)
  916. {
  917. int ch = ReadChar();
  918. switch (ch)
  919. {
  920. case '<':
  921. throw new Exception("attribute values cannot contain '<'");
  922. case '&':
  923. ReadReference(true);
  924. break;
  925. case -1:
  926. throw new Exception("unexpected end of file in an attribute value");
  927. default:
  928. AppendValueChar(ch);
  929. break;
  930. }
  931. }
  932. ReadChar(); // quoteChar
  933. return CreateValueString();
  934. }
  935. // The reader is positioned on the first character
  936. // of the target.
  937. private void ReadProcessingInstruction()
  938. {
  939. string target = ReadName();
  940. SkipWhitespace();
  941. valueLength = 0;
  942. while (PeekChar() != -1)
  943. {
  944. int ch = ReadChar();
  945. if (ch == '?' && PeekChar() == '>')
  946. {
  947. ReadChar();
  948. break;
  949. }
  950. AppendValueChar((char)ch);
  951. }
  952. SetProperties(
  953. XmlNodeType.ProcessingInstruction, // nodeType
  954. target, // name
  955. false, // isEmptyElement
  956. CreateValueString(), // value
  957. true // clearAttributes
  958. );
  959. }
  960. // The reader is positioned on the first character after
  961. // the leading '<!'.
  962. private void ReadDeclaration()
  963. {
  964. int ch = PeekChar();
  965. switch (ch)
  966. {
  967. case '-':
  968. Expect('-');
  969. Expect('-');
  970. ReadComment();
  971. break;
  972. case '[':
  973. ReadChar();
  974. Expect('C');
  975. Expect('D');
  976. Expect('A');
  977. Expect('T');
  978. Expect('A');
  979. Expect('[');
  980. ReadCDATA();
  981. break;
  982. }
  983. }
  984. // The reader is positioned on the first character after
  985. // the leading '<!--'.
  986. private void ReadComment()
  987. {
  988. valueLength = 0;
  989. while (PeekChar() != -1)
  990. {
  991. int ch = ReadChar();
  992. if (ch == '-' && PeekChar() == '-')
  993. {
  994. ReadChar();
  995. if (PeekChar() != '>')
  996. {
  997. throw new Exception("comments cannot contain '--'");
  998. }
  999. ReadChar();
  1000. break;
  1001. }
  1002. AppendValueChar((char)ch);
  1003. }
  1004. SetProperties(
  1005. XmlNodeType.Comment, // nodeType
  1006. String.Empty, // name
  1007. false, // isEmptyElement
  1008. CreateValueString(), // value
  1009. true // clearAttributes
  1010. );
  1011. }
  1012. // The reader is positioned on the first character after
  1013. // the leading '<![CDATA['.
  1014. private void ReadCDATA()
  1015. {
  1016. valueLength = 0;
  1017. while (PeekChar() != -1)
  1018. {
  1019. int ch = ReadChar();
  1020. if (ch == ']' && PeekChar() == ']')
  1021. {
  1022. ch = ReadChar(); // ']'
  1023. if (PeekChar() == '>')
  1024. {
  1025. ReadChar(); // '>'
  1026. break;
  1027. }
  1028. else
  1029. {
  1030. AppendValueChar(']');
  1031. AppendValueChar(']');
  1032. ch = ReadChar();
  1033. }
  1034. }
  1035. AppendValueChar((char)ch);
  1036. }
  1037. ++depth;
  1038. SetProperties(
  1039. XmlNodeType.CDATA, // nodeType
  1040. String.Empty, // name
  1041. false, // isEmptyElement
  1042. CreateValueString(), // value
  1043. true // clearAttributes
  1044. );
  1045. }
  1046. // The reader is positioned on the first character
  1047. // of the name.
  1048. private string ReadName()
  1049. {
  1050. if (!XmlChar.IsFirstNameChar(PeekChar()))
  1051. {
  1052. throw new Exception("a name did not start with a legal character");
  1053. }
  1054. nameLength = 0;
  1055. AppendNameChar(ReadChar());
  1056. while (XmlChar.IsNameChar(PeekChar()))
  1057. {
  1058. AppendNameChar(ReadChar());
  1059. }
  1060. return CreateNameString();
  1061. }
  1062. // Read the next character and compare it against the
  1063. // specified character.
  1064. private void Expect(int expected)
  1065. {
  1066. int ch = ReadChar();
  1067. if (ch != expected)
  1068. {
  1069. throw new Exception(String.Format(
  1070. "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
  1071. (char)expected,
  1072. expected,
  1073. (char)ch,
  1074. ch));
  1075. }
  1076. }
  1077. // Does not consume the first non-whitespace character.
  1078. private void SkipWhitespace()
  1079. {
  1080. while (XmlChar.IsWhitespace(PeekChar()))
  1081. {
  1082. ReadChar();
  1083. }
  1084. }
  1085. }
  1086. }