XmlTextReader.cs 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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 '\r':
  641. ReadChar ();
  642. return ReadContent ();
  643. case '\n':
  644. ReadChar ();
  645. return ReadContent ();
  646. case ' ':
  647. SkipWhitespace ();
  648. return ReadContent ();
  649. case -1:
  650. readState = ReadState.EndOfFile;
  651. SetProperties (
  652. XmlNodeType.None, // nodeType
  653. String.Empty, // name
  654. false, // isEmptyElement
  655. String.Empty, // value
  656. true // clearAttributes
  657. );
  658. more = false;
  659. break;
  660. default:
  661. ReadText ();
  662. more = true;
  663. break;
  664. }
  665. }
  666. return more;
  667. }
  668. private void SetEntityReferenceProperties ()
  669. {
  670. SetProperties (
  671. XmlNodeType.EntityReference, // nodeType
  672. entityReferenceName, // name
  673. false, // isEmptyElement
  674. String.Empty, // value
  675. true // clearAttributes
  676. );
  677. returnEntityReference = false;
  678. entityReferenceName = String.Empty;
  679. }
  680. // The leading '<' has already been consumed.
  681. private void ReadTag ()
  682. {
  683. switch (PeekChar ())
  684. {
  685. case '/':
  686. ReadChar ();
  687. ReadEndTag ();
  688. break;
  689. case '?':
  690. ReadChar ();
  691. ReadProcessingInstruction ();
  692. break;
  693. case '!':
  694. ReadChar ();
  695. ReadDeclaration ();
  696. break;
  697. default:
  698. ReadStartTag ();
  699. break;
  700. }
  701. }
  702. // The leading '<' has already been consumed.
  703. private void ReadStartTag ()
  704. {
  705. parserContext.NamespaceManager.PushScope ();
  706. string name = ReadName ();
  707. SkipWhitespace ();
  708. bool isEmptyElement = false;
  709. ClearAttributes ();
  710. if (XmlChar.IsFirstNameChar (PeekChar ()))
  711. ReadAttributes ();
  712. if (PeekChar () == '/') {
  713. ReadChar ();
  714. isEmptyElement = true;
  715. depthDown = true;
  716. popScope = true;
  717. }
  718. Expect ('>');
  719. if (!depthDown)
  720. ++depth;
  721. else
  722. depthDown = false;
  723. SetProperties (
  724. XmlNodeType.Element, // nodeType
  725. name, // name
  726. isEmptyElement, // isEmptyElement
  727. String.Empty, // value
  728. false // clearAttributes
  729. );
  730. }
  731. // The reader is positioned on the first character
  732. // of the element's name.
  733. private void ReadEndTag ()
  734. {
  735. string name = ReadName ();
  736. SkipWhitespace ();
  737. Expect ('>');
  738. --depth;
  739. SetProperties (
  740. XmlNodeType.EndElement, // nodeType
  741. name, // name
  742. false, // isEmptyElement
  743. String.Empty, // value
  744. true // clearAttributes
  745. );
  746. popScope = true;
  747. }
  748. private void AppendNameChar (int ch)
  749. {
  750. CheckNameCapacity ();
  751. nameBuffer [nameLength++] = (char)ch;
  752. }
  753. private void CheckNameCapacity ()
  754. {
  755. if (nameLength == nameCapacity) {
  756. nameCapacity = nameCapacity * 2;
  757. char [] oldNameBuffer = nameBuffer;
  758. nameBuffer = new char [nameCapacity];
  759. Array.Copy (oldNameBuffer, nameBuffer, nameLength);
  760. }
  761. }
  762. private string CreateNameString ()
  763. {
  764. return new String (nameBuffer, 0, nameLength);
  765. }
  766. private void AppendValueChar (int ch)
  767. {
  768. CheckValueCapacity ();
  769. valueBuffer [valueLength++] = (char)ch;
  770. }
  771. private void CheckValueCapacity ()
  772. {
  773. if (valueLength == valueCapacity) {
  774. valueCapacity = valueCapacity * 2;
  775. char [] oldValueBuffer = valueBuffer;
  776. valueBuffer = new char [valueCapacity];
  777. Array.Copy (oldValueBuffer, valueBuffer, valueLength);
  778. }
  779. }
  780. private string CreateValueString ()
  781. {
  782. return new String (valueBuffer, 0, valueLength);
  783. }
  784. // The reader is positioned on the first character
  785. // of the text.
  786. private void ReadText ()
  787. {
  788. valueLength = 0;
  789. int ch = PeekChar ();
  790. while (ch != '<' && ch != -1) {
  791. if (ch == '&') {
  792. ReadChar ();
  793. if (ReadReference (false))
  794. break;
  795. } else
  796. AppendValueChar (ReadChar ());
  797. ch = PeekChar ();
  798. }
  799. if (returnEntityReference && valueLength == 0) {
  800. ++depth;
  801. SetEntityReferenceProperties ();
  802. } else {
  803. if (depth >= 0) {
  804. ++depth;
  805. depthDown = true;
  806. }
  807. SetProperties (
  808. XmlNodeType.Text, // nodeType
  809. String.Empty, // name
  810. false, // isEmptyElement
  811. CreateValueString (), // value
  812. true // clearAttributes
  813. );
  814. }
  815. }
  816. // The leading '&' has already been consumed.
  817. // Returns true if the entity reference isn't a simple
  818. // character reference or one of the predefined entities.
  819. // This allows the ReadText method to break so that the
  820. // next call to Read will return the EntityReference node.
  821. private bool ReadReference (bool ignoreEntityReferences)
  822. {
  823. if (PeekChar () == '#') {
  824. ReadChar ();
  825. ReadCharacterReference ();
  826. } else
  827. ReadEntityReference (ignoreEntityReferences);
  828. return returnEntityReference;
  829. }
  830. private void ReadCharacterReference ()
  831. {
  832. int value = 0;
  833. if (PeekChar () == 'x') {
  834. ReadChar ();
  835. while (PeekChar () != ';' && PeekChar () != -1) {
  836. int ch = ReadChar ();
  837. if (ch >= '0' && ch <= '9')
  838. value = (value << 4) + ch - '0';
  839. else if (ch >= 'A' && ch <= 'F')
  840. value = (value << 4) + ch - 'A' + 10;
  841. else if (ch >= 'a' && ch <= 'f')
  842. value = (value << 4) + ch - 'a' + 10;
  843. else
  844. throw new XmlException (
  845. String.Format (
  846. "invalid hexadecimal digit: {0} (#x{1:X})",
  847. (char)ch,
  848. ch));
  849. }
  850. } else {
  851. while (PeekChar () != ';' && PeekChar () != -1) {
  852. int ch = ReadChar ();
  853. if (ch >= '0' && ch <= '9')
  854. value = value * 10 + ch - '0';
  855. else
  856. throw new XmlException (
  857. String.Format (
  858. "invalid decimal digit: {0} (#x{1:X})",
  859. (char)ch,
  860. ch));
  861. }
  862. }
  863. ReadChar (); // ';'
  864. AppendValueChar (value);
  865. }
  866. private void ReadEntityReference (bool ignoreEntityReferences)
  867. {
  868. nameLength = 0;
  869. int ch = PeekChar ();
  870. while (ch != ';' && ch != -1) {
  871. AppendNameChar (ReadChar ());
  872. ch = PeekChar ();
  873. }
  874. Expect (';');
  875. string name = CreateNameString ();
  876. switch (name)
  877. {
  878. case "lt":
  879. AppendValueChar ('<');
  880. break;
  881. case "gt":
  882. AppendValueChar ('>');
  883. break;
  884. case "amp":
  885. AppendValueChar ('&');
  886. break;
  887. case "apos":
  888. AppendValueChar ('\'');
  889. break;
  890. case "quot":
  891. AppendValueChar ('"');
  892. break;
  893. default:
  894. if (ignoreEntityReferences) {
  895. AppendValueChar ('&');
  896. foreach (char ch2 in name) {
  897. AppendValueChar (ch2);
  898. }
  899. AppendValueChar (';');
  900. } else {
  901. returnEntityReference = true;
  902. entityReferenceName = name;
  903. }
  904. break;
  905. }
  906. }
  907. // The reader is positioned on the first character of
  908. // the attribute name.
  909. private void ReadAttributes ()
  910. {
  911. do {
  912. string name = ReadName ();
  913. SkipWhitespace ();
  914. Expect ('=');
  915. SkipWhitespace ();
  916. string value = ReadAttribute ();
  917. SkipWhitespace ();
  918. if (name == "xmlns")
  919. parserContext.NamespaceManager.AddNamespace (String.Empty, value);
  920. else if (name.StartsWith ("xmlns:"))
  921. parserContext.NamespaceManager.AddNamespace (name.Substring (6), value);
  922. AddAttribute (name, value);
  923. } while (PeekChar () != '/' && PeekChar () != '>' && PeekChar () != -1);
  924. }
  925. // The reader is positioned on the quote character.
  926. private string ReadAttribute ()
  927. {
  928. int quoteChar = ReadChar ();
  929. if (quoteChar != '\'' && quoteChar != '\"')
  930. throw new XmlException ("an attribute value was not quoted");
  931. valueLength = 0;
  932. while (PeekChar () != quoteChar) {
  933. int ch = ReadChar ();
  934. switch (ch)
  935. {
  936. case '<':
  937. throw new XmlException ("attribute values cannot contain '<'");
  938. case '&':
  939. ReadReference (true);
  940. break;
  941. case -1:
  942. throw new XmlException ("unexpected end of file in an attribute value");
  943. default:
  944. AppendValueChar (ch);
  945. break;
  946. }
  947. }
  948. ReadChar (); // quoteChar
  949. return CreateValueString ();
  950. }
  951. // The reader is positioned on the first character
  952. // of the target.
  953. private void ReadProcessingInstruction ()
  954. {
  955. string target = ReadName ();
  956. SkipWhitespace ();
  957. valueLength = 0;
  958. while (PeekChar () != -1) {
  959. int ch = ReadChar ();
  960. if (ch == '?' && PeekChar () == '>') {
  961. ReadChar ();
  962. break;
  963. }
  964. AppendValueChar ((char)ch);
  965. }
  966. SetProperties (
  967. XmlNodeType.ProcessingInstruction, // nodeType
  968. target, // name
  969. false, // isEmptyElement
  970. CreateValueString (), // value
  971. true // clearAttributes
  972. );
  973. }
  974. // The reader is positioned on the first character after
  975. // the leading '<!'.
  976. private void ReadDeclaration ()
  977. {
  978. int ch = PeekChar ();
  979. switch (ch)
  980. {
  981. case '-':
  982. Expect ('-');
  983. Expect ('-');
  984. ReadComment ();
  985. break;
  986. case '[':
  987. ReadChar ();
  988. Expect ('C');
  989. Expect ('D');
  990. Expect ('A');
  991. Expect ('T');
  992. Expect ('A');
  993. Expect ('[');
  994. ReadCDATA ();
  995. break;
  996. }
  997. }
  998. // The reader is positioned on the first character after
  999. // the leading '<!--'.
  1000. private void ReadComment ()
  1001. {
  1002. valueLength = 0;
  1003. while (PeekChar () != -1) {
  1004. int ch = ReadChar ();
  1005. if (ch == '-' && PeekChar () == '-') {
  1006. ReadChar ();
  1007. if (PeekChar () != '>')
  1008. throw new XmlException ("comments cannot contain '--'");
  1009. ReadChar ();
  1010. break;
  1011. }
  1012. AppendValueChar ((char)ch);
  1013. }
  1014. SetProperties (
  1015. XmlNodeType.Comment, // nodeType
  1016. String.Empty, // name
  1017. false, // isEmptyElement
  1018. CreateValueString (), // value
  1019. true // clearAttributes
  1020. );
  1021. }
  1022. // The reader is positioned on the first character after
  1023. // the leading '<![CDATA['.
  1024. private void ReadCDATA ()
  1025. {
  1026. valueLength = 0;
  1027. while (PeekChar () != -1) {
  1028. int ch = ReadChar ();
  1029. if (ch == ']' && PeekChar () == ']') {
  1030. ch = ReadChar (); // ']'
  1031. if (PeekChar () == '>') {
  1032. ReadChar (); // '>'
  1033. break;
  1034. } else {
  1035. AppendValueChar (']');
  1036. AppendValueChar (']');
  1037. ch = ReadChar ();
  1038. }
  1039. }
  1040. AppendValueChar ((char)ch);
  1041. }
  1042. ++depth;
  1043. SetProperties (
  1044. XmlNodeType.CDATA, // nodeType
  1045. String.Empty, // name
  1046. false, // isEmptyElement
  1047. CreateValueString (), // value
  1048. true // clearAttributes
  1049. );
  1050. }
  1051. // The reader is positioned on the first character
  1052. // of the name.
  1053. private string ReadName ()
  1054. {
  1055. if (!XmlChar.IsFirstNameChar (PeekChar ()))
  1056. throw new XmlException ("a name did not start with a legal character");
  1057. nameLength = 0;
  1058. AppendNameChar (ReadChar ());
  1059. while (XmlChar.IsNameChar (PeekChar ())) {
  1060. AppendNameChar (ReadChar ());
  1061. }
  1062. return CreateNameString ();
  1063. }
  1064. // Read the next character and compare it against the
  1065. // specified character.
  1066. private void Expect (int expected)
  1067. {
  1068. int ch = ReadChar ();
  1069. if (ch != expected) {
  1070. throw new XmlException (
  1071. String.Format (
  1072. "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
  1073. (char)expected,
  1074. expected,
  1075. (char)ch,
  1076. ch));
  1077. }
  1078. }
  1079. // Does not consume the first non-whitespace character.
  1080. private void SkipWhitespace ()
  1081. {
  1082. while (XmlChar.IsWhitespace (PeekChar ()))
  1083. ReadChar ();
  1084. }
  1085. }
  1086. }