XmlTextReader.cs 26 KB

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