XmlTextReader.cs 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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. throw new NotImplementedException ();
  269. }
  270. public override string GetAttribute (string name)
  271. {
  272. return attributes [name] as string;
  273. }
  274. public override string GetAttribute (string localName, string namespaceURI)
  275. {
  276. foreach (DictionaryEntry entry in attributes)
  277. {
  278. string thisName = entry.Key as string;
  279. int indexOfColon = thisName.IndexOf (':');
  280. if (indexOfColon != -1) {
  281. string thisLocalName = thisName.Substring (indexOfColon + 1);
  282. if (localName == thisLocalName) {
  283. string thisPrefix = thisName.Substring (0, indexOfColon);
  284. string thisNamespaceURI = LookupNamespace (thisPrefix);
  285. if (namespaceURI == thisNamespaceURI)
  286. return attributes [thisName] as string;
  287. }
  288. } else if (localName == "xmlns" && namespaceURI == "http://www.w3.org/2000/xmlns/" && thisName == "xmlns")
  289. return attributes [thisName] as string;
  290. }
  291. return String.Empty;
  292. }
  293. [MonoTODO]
  294. public TextReader GetRemainder ()
  295. {
  296. throw new NotImplementedException ();
  297. }
  298. [MonoTODO]
  299. bool IXmlLineInfo.HasLineInfo ()
  300. {
  301. return false;
  302. }
  303. public override string LookupNamespace (string prefix)
  304. {
  305. return parserContext.NamespaceManager.LookupNamespace (prefix);
  306. }
  307. [MonoTODO]
  308. public override void MoveToAttribute (int i)
  309. {
  310. throw new NotImplementedException ();
  311. }
  312. public override bool MoveToAttribute (string name)
  313. {
  314. MoveToElement ();
  315. bool match = false;
  316. if (attributes == null)
  317. return false;
  318. if (orderedAttributesEnumerator == null) {
  319. SaveProperties ();
  320. orderedAttributesEnumerator = orderedAttributes.GetEnumerator ();
  321. }
  322. while (orderedAttributesEnumerator.MoveNext ()) {
  323. if(name == orderedAttributesEnumerator.Current as string) {
  324. match = true;
  325. break;
  326. }
  327. }
  328. if (match) {
  329. string value = attributes [name] as string;
  330. SetProperties (
  331. XmlNodeType.Attribute, // nodeType
  332. name, // name
  333. false, // isEmptyElement
  334. value, // value
  335. false // clearAttributes
  336. );
  337. }
  338. return match;
  339. }
  340. [MonoTODO]
  341. public override bool MoveToAttribute (string localName, string namespaceName)
  342. {
  343. throw new NotImplementedException ();
  344. }
  345. public override bool MoveToElement ()
  346. {
  347. if (orderedAttributesEnumerator != null) {
  348. orderedAttributesEnumerator = null;
  349. RestoreProperties ();
  350. return true;
  351. }
  352. return false;
  353. }
  354. public override bool MoveToFirstAttribute ()
  355. {
  356. MoveToElement ();
  357. return MoveToNextAttribute ();
  358. }
  359. public override bool MoveToNextAttribute ()
  360. {
  361. if (attributes == null)
  362. return false;
  363. if (orderedAttributesEnumerator == null) {
  364. SaveProperties ();
  365. orderedAttributesEnumerator = orderedAttributes.GetEnumerator ();
  366. }
  367. if (orderedAttributesEnumerator.MoveNext ()) {
  368. string name = orderedAttributesEnumerator.Current as string;
  369. string value = attributes [name] as string;
  370. SetProperties (
  371. XmlNodeType.Attribute, // nodeType
  372. name, // name
  373. false, // isEmptyElement
  374. value, // value
  375. false // clearAttributes
  376. );
  377. return true;
  378. }
  379. return false;
  380. }
  381. public override bool Read ()
  382. {
  383. bool more = false;
  384. readState = ReadState.Interactive;
  385. more = ReadContent ();
  386. return more;
  387. }
  388. [MonoTODO]
  389. public override bool ReadAttributeValue ()
  390. {
  391. throw new NotImplementedException ();
  392. }
  393. [MonoTODO]
  394. public int ReadBase64 (byte [] buffer, int offset, int length)
  395. {
  396. throw new NotImplementedException ();
  397. }
  398. [MonoTODO]
  399. public int ReadBinHex (byte [] buffer, int offset, int length)
  400. {
  401. throw new NotImplementedException ();
  402. }
  403. [MonoTODO]
  404. public int ReadChars (char [] buffer, int offset, int length)
  405. {
  406. throw new NotImplementedException ();
  407. }
  408. [MonoTODO]
  409. public override string ReadInnerXml ()
  410. {
  411. // Still need a Well Formedness check.
  412. // Will wait for Validating reader ;-)
  413. if (NodeType == XmlNodeType.Attribute) {
  414. return Value;
  415. } else {
  416. saveToXmlBuffer = true;
  417. string startname = this.Name;
  418. string endname = string.Empty;
  419. readState = ReadState.Interactive;
  420. while (startname != endname) {
  421. ReadContent ();
  422. endname = this.Name;
  423. }
  424. xmlBuffer.Replace(currentTag.ToString (), "");
  425. saveToXmlBuffer = false;
  426. string InnerXml = xmlBuffer.ToString ();
  427. xmlBuffer.Length = 0;
  428. return InnerXml;
  429. }
  430. }
  431. [MonoTODO]
  432. public override string ReadOuterXml ()
  433. {
  434. // Still need a Well Formedness check.
  435. // Will wait for Validating reader ;-)
  436. if (NodeType == XmlNodeType.Attribute) {
  437. return Name+"=\""+Value+"\"";
  438. } else {
  439. saveToXmlBuffer = true;
  440. xmlBuffer.Append(currentTag.ToString ());
  441. string startname = this.Name;
  442. string endname = string.Empty;
  443. readState = ReadState.Interactive;
  444. while (startname != endname) {
  445. ReadContent ();
  446. endname = this.Name;
  447. }
  448. saveToXmlBuffer = false;
  449. string OuterXml = xmlBuffer.ToString ();
  450. xmlBuffer.Length = 0;
  451. return OuterXml;
  452. }
  453. }
  454. [MonoTODO]
  455. public override string ReadString ()
  456. {
  457. throw new NotImplementedException ();
  458. }
  459. [MonoTODO]
  460. public void ResetState ()
  461. {
  462. throw new NotImplementedException ();
  463. }
  464. public override void ResolveEntity ()
  465. {
  466. // XmlTextReaders don't resolve entities.
  467. throw new InvalidOperationException ("XmlTextReaders don't resolve entities.");
  468. }
  469. #endregion
  470. // privates
  471. private XmlParserContext parserContext;
  472. private TextReader reader;
  473. private ReadState readState;
  474. private int depth;
  475. private bool depthDown;
  476. private bool popScope;
  477. private XmlNodeType nodeType;
  478. private string name;
  479. private string prefix;
  480. private string localName;
  481. private string namespaceURI;
  482. private bool isEmptyElement;
  483. private string value;
  484. private XmlNodeType saveNodeType;
  485. private string saveName;
  486. private string savePrefix;
  487. private string saveLocalName;
  488. private string saveNamespaceURI;
  489. private bool saveIsEmptyElement;
  490. private Hashtable attributes;
  491. private ArrayList orderedAttributes;
  492. private IEnumerator orderedAttributesEnumerator;
  493. private bool returnEntityReference;
  494. private string entityReferenceName;
  495. private char [] nameBuffer;
  496. private int nameLength;
  497. private int nameCapacity;
  498. private const int initialNameCapacity = 256;
  499. private char [] valueBuffer;
  500. private int valueLength;
  501. private int valueCapacity;
  502. private const int initialValueCapacity = 8192;
  503. private StringBuilder xmlBuffer; // This is for Read(Inner|Outer)Xml
  504. private StringBuilder currentTag; // A buffer for ReadContent for ReadOuterXml
  505. private bool saveToXmlBuffer;
  506. private int line = 1;
  507. private int column = 1;
  508. private void Init ()
  509. {
  510. readState = ReadState.Initial;
  511. depth = -1;
  512. depthDown = false;
  513. popScope = false;
  514. nodeType = XmlNodeType.None;
  515. name = String.Empty;
  516. prefix = String.Empty;
  517. localName = string.Empty;
  518. isEmptyElement = false;
  519. value = String.Empty;
  520. attributes = new Hashtable ();
  521. orderedAttributes = new ArrayList ();
  522. orderedAttributesEnumerator = null;
  523. returnEntityReference = false;
  524. entityReferenceName = String.Empty;
  525. nameBuffer = new char [initialNameCapacity];
  526. nameLength = 0;
  527. nameCapacity = initialNameCapacity;
  528. valueBuffer = new char [initialValueCapacity];
  529. valueLength = 0;
  530. valueCapacity = initialValueCapacity;
  531. xmlBuffer = new StringBuilder ();
  532. currentTag = new StringBuilder ();
  533. }
  534. // Use this method rather than setting the properties
  535. // directly so that all the necessary properties can
  536. // be changed in harmony with each other. Maybe the
  537. // fields should be in a seperate class to help enforce
  538. // this.
  539. private void SetProperties (
  540. XmlNodeType nodeType,
  541. string name,
  542. bool isEmptyElement,
  543. string value,
  544. bool clearAttributes)
  545. {
  546. this.nodeType = nodeType;
  547. this.name = name;
  548. this.isEmptyElement = isEmptyElement;
  549. this.value = value;
  550. if (clearAttributes)
  551. ClearAttributes ();
  552. int indexOfColon = name.IndexOf (':');
  553. if (indexOfColon == -1) {
  554. prefix = String.Empty;
  555. localName = name;
  556. } else {
  557. prefix = name.Substring (0, indexOfColon);
  558. localName = name.Substring (indexOfColon + 1);
  559. }
  560. namespaceURI = LookupNamespace (prefix);
  561. }
  562. private void SaveProperties ()
  563. {
  564. saveNodeType = nodeType;
  565. saveName = name;
  566. savePrefix = prefix;
  567. saveLocalName = localName;
  568. saveNamespaceURI = namespaceURI;
  569. saveIsEmptyElement = isEmptyElement;
  570. // An element's value is always String.Empty.
  571. }
  572. private void RestoreProperties ()
  573. {
  574. nodeType = saveNodeType;
  575. name = saveName;
  576. prefix = savePrefix;
  577. localName = saveLocalName;
  578. namespaceURI = saveNamespaceURI;
  579. isEmptyElement = saveIsEmptyElement;
  580. value = String.Empty;
  581. }
  582. private void AddAttribute (string name, string value)
  583. {
  584. attributes.Add (name, value);
  585. orderedAttributes.Add (name);
  586. }
  587. private void ClearAttributes ()
  588. {
  589. if (attributes.Count > 0) {
  590. attributes.Clear ();
  591. orderedAttributes.Clear ();
  592. }
  593. orderedAttributesEnumerator = null;
  594. }
  595. private int PeekChar ()
  596. {
  597. return reader.Peek ();
  598. }
  599. private int ReadChar ()
  600. {
  601. int ch = reader.Read ();
  602. if (ch == '\n') {
  603. line++;
  604. column = 1;
  605. } else {
  606. column++;
  607. }
  608. if (saveToXmlBuffer) {
  609. xmlBuffer.Append ((char) ch);
  610. }
  611. currentTag.Append ((char) ch);
  612. return ch;
  613. }
  614. // This should really keep track of some state so
  615. // that it's not possible to have more than one document
  616. // element or text outside of the document element.
  617. private bool ReadContent ()
  618. {
  619. bool more = false;
  620. currentTag.Length = 0;
  621. if (popScope) {
  622. parserContext.NamespaceManager.PopScope ();
  623. popScope = false;
  624. }
  625. if (depthDown)
  626. --depth;
  627. if (returnEntityReference) {
  628. ++depth;
  629. SetEntityReferenceProperties ();
  630. more = true;
  631. } else {
  632. switch (PeekChar ())
  633. {
  634. case '<':
  635. ReadChar ();
  636. ReadTag ();
  637. more = true;
  638. break;
  639. case -1:
  640. readState = ReadState.EndOfFile;
  641. SetProperties (
  642. XmlNodeType.None, // nodeType
  643. String.Empty, // name
  644. false, // isEmptyElement
  645. String.Empty, // value
  646. true // clearAttributes
  647. );
  648. more = false;
  649. break;
  650. default:
  651. ReadText ();
  652. more = true;
  653. break;
  654. }
  655. }
  656. return more;
  657. }
  658. private void SetEntityReferenceProperties ()
  659. {
  660. SetProperties (
  661. XmlNodeType.EntityReference, // nodeType
  662. entityReferenceName, // name
  663. false, // isEmptyElement
  664. String.Empty, // value
  665. true // clearAttributes
  666. );
  667. returnEntityReference = false;
  668. entityReferenceName = String.Empty;
  669. }
  670. // The leading '<' has already been consumed.
  671. private void ReadTag ()
  672. {
  673. switch (PeekChar ())
  674. {
  675. case '/':
  676. ReadChar ();
  677. ReadEndTag ();
  678. break;
  679. case '?':
  680. ReadChar ();
  681. ReadProcessingInstruction ();
  682. break;
  683. case '!':
  684. ReadChar ();
  685. ReadDeclaration ();
  686. break;
  687. default:
  688. ReadStartTag ();
  689. break;
  690. }
  691. }
  692. // The leading '<' has already been consumed.
  693. private void ReadStartTag ()
  694. {
  695. parserContext.NamespaceManager.PushScope ();
  696. string name = ReadName ();
  697. SkipWhitespace ();
  698. bool isEmptyElement = false;
  699. ClearAttributes ();
  700. if (XmlChar.IsFirstNameChar (PeekChar ()))
  701. ReadAttributes ();
  702. if (PeekChar () == '/') {
  703. ReadChar ();
  704. isEmptyElement = true;
  705. depthDown = true;
  706. popScope = true;
  707. }
  708. Expect ('>');
  709. ++depth;
  710. SetProperties (
  711. XmlNodeType.Element, // nodeType
  712. name, // name
  713. isEmptyElement, // isEmptyElement
  714. String.Empty, // value
  715. false // clearAttributes
  716. );
  717. }
  718. // The reader is positioned on the first character
  719. // of the element's name.
  720. private void ReadEndTag ()
  721. {
  722. string name = ReadName ();
  723. SkipWhitespace ();
  724. Expect ('>');
  725. --depth;
  726. SetProperties (
  727. XmlNodeType.EndElement, // nodeType
  728. name, // name
  729. false, // isEmptyElement
  730. String.Empty, // value
  731. true // clearAttributes
  732. );
  733. popScope = true;
  734. }
  735. private void AppendNameChar (int ch)
  736. {
  737. CheckNameCapacity ();
  738. nameBuffer [nameLength++] = (char)ch;
  739. }
  740. private void CheckNameCapacity ()
  741. {
  742. if (nameLength == nameCapacity) {
  743. nameCapacity = nameCapacity * 2;
  744. char [] oldNameBuffer = nameBuffer;
  745. nameBuffer = new char [nameCapacity];
  746. Array.Copy (oldNameBuffer, nameBuffer, nameLength);
  747. }
  748. }
  749. private string CreateNameString ()
  750. {
  751. return new String (nameBuffer, 0, nameLength);
  752. }
  753. private void AppendValueChar (int ch)
  754. {
  755. CheckValueCapacity ();
  756. valueBuffer [valueLength++] = (char)ch;
  757. }
  758. private void CheckValueCapacity ()
  759. {
  760. if (valueLength == valueCapacity) {
  761. valueCapacity = valueCapacity * 2;
  762. char [] oldValueBuffer = valueBuffer;
  763. valueBuffer = new char [valueCapacity];
  764. Array.Copy (oldValueBuffer, valueBuffer, valueLength);
  765. }
  766. }
  767. private string CreateValueString ()
  768. {
  769. return new String (valueBuffer, 0, valueLength);
  770. }
  771. // The reader is positioned on the first character
  772. // of the text.
  773. private void ReadText ()
  774. {
  775. valueLength = 0;
  776. int ch = PeekChar ();
  777. while (ch != '<' && ch != -1) {
  778. if (ch == '&') {
  779. ReadChar ();
  780. if (ReadReference (false))
  781. break;
  782. } else
  783. AppendValueChar (ReadChar ());
  784. ch = PeekChar ();
  785. }
  786. if (returnEntityReference && valueLength == 0) {
  787. ++depth;
  788. SetEntityReferenceProperties ();
  789. } else {
  790. if (depth >= 0) {
  791. ++depth;
  792. depthDown = true;
  793. }
  794. SetProperties (
  795. XmlNodeType.Text, // nodeType
  796. String.Empty, // name
  797. false, // isEmptyElement
  798. CreateValueString (), // value
  799. true // clearAttributes
  800. );
  801. }
  802. }
  803. // The leading '&' has already been consumed.
  804. // Returns true if the entity reference isn't a simple
  805. // character reference or one of the predefined entities.
  806. // This allows the ReadText method to break so that the
  807. // next call to Read will return the EntityReference node.
  808. private bool ReadReference (bool ignoreEntityReferences)
  809. {
  810. if (PeekChar () == '#') {
  811. ReadChar ();
  812. ReadCharacterReference ();
  813. } else
  814. ReadEntityReference (ignoreEntityReferences);
  815. return returnEntityReference;
  816. }
  817. private void ReadCharacterReference ()
  818. {
  819. int value = 0;
  820. if (PeekChar () == 'x') {
  821. ReadChar ();
  822. while (PeekChar () != ';' && PeekChar () != -1) {
  823. int ch = ReadChar ();
  824. if (ch >= '0' && ch <= '9')
  825. value = (value << 4) + ch - '0';
  826. else if (ch >= 'A' && ch <= 'F')
  827. value = (value << 4) + ch - 'A' + 10;
  828. else if (ch >= 'a' && ch <= 'f')
  829. value = (value << 4) + ch - 'a' + 10;
  830. else
  831. throw new XmlException (
  832. String.Format (
  833. "invalid hexadecimal digit: {0} (#x{1:X})",
  834. (char)ch,
  835. ch));
  836. }
  837. } else {
  838. while (PeekChar () != ';' && PeekChar () != -1) {
  839. int ch = ReadChar ();
  840. if (ch >= '0' && ch <= '9')
  841. value = value * 10 + ch - '0';
  842. else
  843. throw new XmlException (
  844. String.Format (
  845. "invalid decimal digit: {0} (#x{1:X})",
  846. (char)ch,
  847. ch));
  848. }
  849. }
  850. ReadChar (); // ';'
  851. AppendValueChar (value);
  852. }
  853. private void ReadEntityReference (bool ignoreEntityReferences)
  854. {
  855. nameLength = 0;
  856. int ch = PeekChar ();
  857. while (ch != ';' && ch != -1) {
  858. AppendNameChar (ReadChar ());
  859. ch = PeekChar ();
  860. }
  861. Expect (';');
  862. string name = CreateNameString ();
  863. switch (name)
  864. {
  865. case "lt":
  866. AppendValueChar ('<');
  867. break;
  868. case "gt":
  869. AppendValueChar ('>');
  870. break;
  871. case "amp":
  872. AppendValueChar ('&');
  873. break;
  874. case "apos":
  875. AppendValueChar ('\'');
  876. break;
  877. case "quot":
  878. AppendValueChar ('"');
  879. break;
  880. default:
  881. if (ignoreEntityReferences) {
  882. AppendValueChar ('&');
  883. foreach (char ch2 in name) {
  884. AppendValueChar (ch2);
  885. }
  886. AppendValueChar (';');
  887. } else {
  888. returnEntityReference = true;
  889. entityReferenceName = name;
  890. }
  891. break;
  892. }
  893. }
  894. // The reader is positioned on the first character of
  895. // the attribute name.
  896. private void ReadAttributes ()
  897. {
  898. do {
  899. string name = ReadName ();
  900. SkipWhitespace ();
  901. Expect ('=');
  902. SkipWhitespace ();
  903. string value = ReadAttribute ();
  904. SkipWhitespace ();
  905. if (name == "xmlns")
  906. parserContext.NamespaceManager.AddNamespace (String.Empty, value);
  907. else if (name.StartsWith ("xmlns:"))
  908. parserContext.NamespaceManager.AddNamespace (name.Substring (6), value);
  909. AddAttribute (name, value);
  910. } while (PeekChar () != '/' && PeekChar () != '>' && PeekChar () != -1);
  911. }
  912. // The reader is positioned on the quote character.
  913. private string ReadAttribute ()
  914. {
  915. int quoteChar = ReadChar ();
  916. if (quoteChar != '\'' && quoteChar != '\"')
  917. throw new XmlException ("an attribute value was not quoted");
  918. valueLength = 0;
  919. while (PeekChar () != quoteChar) {
  920. int ch = ReadChar ();
  921. switch (ch)
  922. {
  923. case '<':
  924. throw new XmlException ("attribute values cannot contain '<'");
  925. case '&':
  926. ReadReference (true);
  927. break;
  928. case -1:
  929. throw new XmlException ("unexpected end of file in an attribute value");
  930. default:
  931. AppendValueChar (ch);
  932. break;
  933. }
  934. }
  935. ReadChar (); // quoteChar
  936. return CreateValueString ();
  937. }
  938. // The reader is positioned on the first character
  939. // of the target.
  940. private void ReadProcessingInstruction ()
  941. {
  942. string target = ReadName ();
  943. SkipWhitespace ();
  944. valueLength = 0;
  945. while (PeekChar () != -1) {
  946. int ch = ReadChar ();
  947. if (ch == '?' && PeekChar () == '>') {
  948. ReadChar ();
  949. break;
  950. }
  951. AppendValueChar ((char)ch);
  952. }
  953. SetProperties (
  954. XmlNodeType.ProcessingInstruction, // nodeType
  955. target, // name
  956. false, // isEmptyElement
  957. CreateValueString (), // value
  958. true // clearAttributes
  959. );
  960. }
  961. // The reader is positioned on the first character after
  962. // the leading '<!'.
  963. private void ReadDeclaration ()
  964. {
  965. int ch = PeekChar ();
  966. switch (ch)
  967. {
  968. case '-':
  969. Expect ('-');
  970. Expect ('-');
  971. ReadComment ();
  972. break;
  973. case '[':
  974. ReadChar ();
  975. Expect ('C');
  976. Expect ('D');
  977. Expect ('A');
  978. Expect ('T');
  979. Expect ('A');
  980. Expect ('[');
  981. ReadCDATA ();
  982. break;
  983. }
  984. }
  985. // The reader is positioned on the first character after
  986. // the leading '<!--'.
  987. private void ReadComment ()
  988. {
  989. valueLength = 0;
  990. while (PeekChar () != -1) {
  991. int ch = ReadChar ();
  992. if (ch == '-' && PeekChar () == '-') {
  993. ReadChar ();
  994. if (PeekChar () != '>')
  995. throw new XmlException ("comments cannot contain '--'");
  996. ReadChar ();
  997. break;
  998. }
  999. AppendValueChar ((char)ch);
  1000. }
  1001. SetProperties (
  1002. XmlNodeType.Comment, // nodeType
  1003. String.Empty, // name
  1004. false, // isEmptyElement
  1005. CreateValueString (), // value
  1006. true // clearAttributes
  1007. );
  1008. }
  1009. // The reader is positioned on the first character after
  1010. // the leading '<![CDATA['.
  1011. private void ReadCDATA ()
  1012. {
  1013. valueLength = 0;
  1014. while (PeekChar () != -1) {
  1015. int ch = ReadChar ();
  1016. if (ch == ']' && PeekChar () == ']') {
  1017. ch = ReadChar (); // ']'
  1018. if (PeekChar () == '>') {
  1019. ReadChar (); // '>'
  1020. break;
  1021. } else {
  1022. AppendValueChar (']');
  1023. AppendValueChar (']');
  1024. ch = ReadChar ();
  1025. }
  1026. }
  1027. AppendValueChar ((char)ch);
  1028. }
  1029. ++depth;
  1030. SetProperties (
  1031. XmlNodeType.CDATA, // nodeType
  1032. String.Empty, // name
  1033. false, // isEmptyElement
  1034. CreateValueString (), // value
  1035. true // clearAttributes
  1036. );
  1037. }
  1038. // The reader is positioned on the first character
  1039. // of the name.
  1040. private string ReadName ()
  1041. {
  1042. if (!XmlChar.IsFirstNameChar (PeekChar ()))
  1043. throw new XmlException ("a name did not start with a legal character");
  1044. nameLength = 0;
  1045. AppendNameChar (ReadChar ());
  1046. while (XmlChar.IsNameChar (PeekChar ())) {
  1047. AppendNameChar (ReadChar ());
  1048. }
  1049. return CreateNameString ();
  1050. }
  1051. // Read the next character and compare it against the
  1052. // specified character.
  1053. private void Expect (int expected)
  1054. {
  1055. int ch = ReadChar ();
  1056. if (ch != expected) {
  1057. throw new XmlException (
  1058. String.Format (
  1059. "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
  1060. (char)expected,
  1061. expected,
  1062. (char)ch,
  1063. ch));
  1064. }
  1065. }
  1066. // Does not consume the first non-whitespace character.
  1067. private void SkipWhitespace ()
  1068. {
  1069. while (XmlChar.IsWhitespace (PeekChar ()))
  1070. ReadChar ();
  1071. }
  1072. }
  1073. }