XmlTextReader.cs 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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. [MonoTODO]
  40. protected XmlTextReader ()
  41. {
  42. throw new NotImplementedException ();
  43. }
  44. [MonoTODO]
  45. public XmlTextReader (Stream input)
  46. {
  47. throw new NotImplementedException ();
  48. }
  49. [MonoTODO]
  50. public XmlTextReader (string url)
  51. {
  52. XmlNameTable nt = new NameTable ();
  53. XmlNamespaceManager nsMgr = new XmlNamespaceManager (nt);
  54. parserContext = new XmlParserContext (null, nsMgr, null, XmlSpace.None);
  55. Init ();
  56. reader = new StreamReader(url);
  57. }
  58. [MonoTODO]
  59. public XmlTextReader (TextReader input)
  60. {
  61. XmlNameTable nt = new NameTable ();
  62. XmlNamespaceManager nsMgr = new XmlNamespaceManager (nt);
  63. parserContext = new XmlParserContext (null, nsMgr, null, XmlSpace.None);
  64. Init ();
  65. reader = input;
  66. }
  67. [MonoTODO]
  68. protected XmlTextReader (XmlNameTable nt)
  69. {
  70. throw new NotImplementedException ();
  71. }
  72. [MonoTODO]
  73. public XmlTextReader (Stream input, XmlNameTable nt)
  74. {
  75. throw new NotImplementedException ();
  76. }
  77. [MonoTODO]
  78. public XmlTextReader (string url, Stream input)
  79. {
  80. throw new NotImplementedException ();
  81. }
  82. [MonoTODO]
  83. public XmlTextReader (string url, TextReader input)
  84. {
  85. throw new NotImplementedException ();
  86. }
  87. [MonoTODO]
  88. public XmlTextReader (string url, XmlNameTable nt)
  89. {
  90. throw new NotImplementedException ();
  91. }
  92. [MonoTODO]
  93. public XmlTextReader (TextReader input, XmlNameTable nt)
  94. {
  95. throw new NotImplementedException ();
  96. }
  97. [MonoTODO]
  98. public XmlTextReader (Stream xmlFragment, XmlNodeType fragType, XmlParserContext context)
  99. {
  100. throw new NotImplementedException ();
  101. }
  102. [MonoTODO]
  103. public XmlTextReader (string url, Stream input, XmlNameTable nt)
  104. {
  105. throw new NotImplementedException ();
  106. }
  107. [MonoTODO]
  108. public XmlTextReader (string url, TextReader input, XmlNameTable nt)
  109. {
  110. throw new NotImplementedException ();
  111. }
  112. [MonoTODO]
  113. public XmlTextReader (string xmlFragment, XmlNodeType fragType, XmlParserContext context)
  114. {
  115. //Waiting for Validating reader for fragType rules.
  116. parserContext = context;
  117. Init ();
  118. reader = new StringReader(xmlFragment);
  119. }
  120. #endregion
  121. #region Properties
  122. public override int AttributeCount
  123. {
  124. get { return attributes.Count; }
  125. }
  126. [MonoTODO]
  127. public override string BaseURI
  128. {
  129. get { throw new NotImplementedException (); }
  130. }
  131. public override int Depth
  132. {
  133. get { return depth > 0 ? depth : 0; }
  134. }
  135. [MonoTODO]
  136. public Encoding Encoding
  137. {
  138. get { throw new NotImplementedException (); }
  139. }
  140. public override bool EOF
  141. {
  142. get
  143. {
  144. return
  145. readState == ReadState.EndOfFile ||
  146. readState == ReadState.Closed;
  147. }
  148. }
  149. public override bool HasValue
  150. {
  151. get { return value != String.Empty; }
  152. }
  153. public override bool IsDefault
  154. {
  155. get
  156. {
  157. // XmlTextReader does not expand default attributes.
  158. return false;
  159. }
  160. }
  161. public override bool IsEmptyElement
  162. {
  163. get { return isEmptyElement; }
  164. }
  165. public override string this [int i]
  166. {
  167. get { return GetAttribute (i); }
  168. }
  169. public override string this [string name]
  170. {
  171. get { return GetAttribute (name); }
  172. }
  173. public override string this [string localName, string namespaceName]
  174. {
  175. get { return GetAttribute (localName, namespaceName); }
  176. }
  177. [MonoTODO]
  178. public int LineNumber
  179. {
  180. get { throw new NotImplementedException (); }
  181. }
  182. [MonoTODO]
  183. public int LinePosition
  184. {
  185. get { throw new NotImplementedException (); }
  186. }
  187. public override string LocalName
  188. {
  189. get { return localName; }
  190. }
  191. public override string Name
  192. {
  193. get { return name; }
  194. }
  195. [MonoTODO]
  196. public bool Namespaces
  197. {
  198. get { throw new NotImplementedException (); }
  199. set { throw new NotImplementedException (); }
  200. }
  201. public override string NamespaceURI
  202. {
  203. get { return namespaceURI; }
  204. }
  205. public override XmlNameTable NameTable
  206. {
  207. get { return parserContext.NameTable; }
  208. }
  209. public override XmlNodeType NodeType
  210. {
  211. get { return nodeType; }
  212. }
  213. [MonoTODO]
  214. public bool Normalization
  215. {
  216. get { throw new NotImplementedException (); }
  217. set { throw new NotImplementedException (); }
  218. }
  219. public override string Prefix
  220. {
  221. get { return prefix; }
  222. }
  223. [MonoTODO]
  224. public override char QuoteChar
  225. {
  226. get { throw new NotImplementedException (); }
  227. }
  228. public override ReadState ReadState
  229. {
  230. get { return readState; }
  231. }
  232. public override string Value
  233. {
  234. get { return value; }
  235. }
  236. [MonoTODO]
  237. public WhitespaceHandling WhitespaceHandling
  238. {
  239. get { throw new NotImplementedException (); }
  240. set { throw new NotImplementedException (); }
  241. }
  242. [MonoTODO]
  243. public override string XmlLang
  244. {
  245. get { throw new NotImplementedException (); }
  246. }
  247. [MonoTODO]
  248. public XmlResolver XmlResolver
  249. {
  250. set { throw new NotImplementedException (); }
  251. }
  252. [MonoTODO]
  253. public override XmlSpace XmlSpace
  254. {
  255. get { throw new NotImplementedException (); }
  256. }
  257. #endregion
  258. #region Methods
  259. [MonoTODO]
  260. public override void Close ()
  261. {
  262. readState = ReadState.Closed;
  263. }
  264. [MonoTODO]
  265. public override string GetAttribute (int i)
  266. {
  267. throw new NotImplementedException ();
  268. }
  269. public override string GetAttribute (string name)
  270. {
  271. return attributes [name] as string;
  272. }
  273. public override string GetAttribute (string localName, string namespaceURI)
  274. {
  275. foreach (DictionaryEntry entry in attributes)
  276. {
  277. string thisName = entry.Key as string;
  278. int indexOfColon = thisName.IndexOf (':');
  279. if (indexOfColon != -1) {
  280. string thisLocalName = thisName.Substring (indexOfColon + 1);
  281. if (localName == thisLocalName) {
  282. string thisPrefix = thisName.Substring (0, indexOfColon);
  283. string thisNamespaceURI = LookupNamespace (thisPrefix);
  284. if (namespaceURI == thisNamespaceURI)
  285. return attributes [thisName] as string;
  286. }
  287. } else if (localName == "xmlns" && namespaceURI == "http://www.w3.org/2000/xmlns/" && thisName == "xmlns")
  288. return attributes [thisName] as string;
  289. }
  290. return String.Empty;
  291. }
  292. [MonoTODO]
  293. public TextReader GetRemainder ()
  294. {
  295. throw new NotImplementedException ();
  296. }
  297. [MonoTODO]
  298. bool IXmlLineInfo.HasLineInfo ()
  299. {
  300. throw new NotImplementedException ();
  301. }
  302. public override string LookupNamespace (string prefix)
  303. {
  304. return parserContext.NamespaceManager.LookupNamespace (prefix);
  305. }
  306. [MonoTODO]
  307. public override void MoveToAttribute (int i)
  308. {
  309. throw new NotImplementedException ();
  310. }
  311. public override bool MoveToAttribute (string name)
  312. {
  313. MoveToElement ();
  314. bool match = false;
  315. if (attributes == null)
  316. return false;
  317. if (attributeEnumerator == null) {
  318. SaveProperties ();
  319. attributeEnumerator = attributes.GetEnumerator ();
  320. }
  321. while (attributeEnumerator.MoveNext ()) {
  322. if(name == attributeEnumerator.Key as string) {
  323. match = true;
  324. break;
  325. }
  326. }
  327. if (match) {
  328. string attname = attributeEnumerator.Key as string;
  329. string value = attributeEnumerator.Value as string;
  330. SetProperties (
  331. XmlNodeType.Attribute, // nodeType
  332. attname, // 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 (attributeEnumerator != null) {
  348. attributeEnumerator = 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 (attributeEnumerator == null) {
  364. SaveProperties ();
  365. attributeEnumerator = attributes.GetEnumerator ();
  366. }
  367. if (attributeEnumerator.MoveNext ()) {
  368. string name = attributeEnumerator.Key as string;
  369. string value = attributeEnumerator.Value 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 IDictionaryEnumerator attributeEnumerator;
  492. private bool returnEntityReference;
  493. private string entityReferenceName;
  494. private char [] nameBuffer;
  495. private int nameLength;
  496. private int nameCapacity;
  497. private const int initialNameCapacity = 256;
  498. private char [] valueBuffer;
  499. private int valueLength;
  500. private int valueCapacity;
  501. private const int initialValueCapacity = 8192;
  502. private StringBuilder xmlBuffer; // This is for Read(Inner|Outer)Xml
  503. private StringBuilder currentTag; // A buffer for ReadContent for ReadOuterXml
  504. private bool saveToXmlBuffer;
  505. private void Init ()
  506. {
  507. readState = ReadState.Initial;
  508. depth = -1;
  509. depthDown = false;
  510. popScope = false;
  511. nodeType = XmlNodeType.None;
  512. name = String.Empty;
  513. prefix = String.Empty;
  514. localName = string.Empty;
  515. isEmptyElement = false;
  516. value = String.Empty;
  517. attributes = new Hashtable ();
  518. attributeEnumerator = null;
  519. returnEntityReference = false;
  520. entityReferenceName = String.Empty;
  521. nameBuffer = new char [initialNameCapacity];
  522. nameLength = 0;
  523. nameCapacity = initialNameCapacity;
  524. valueBuffer = new char [initialValueCapacity];
  525. valueLength = 0;
  526. valueCapacity = initialValueCapacity;
  527. xmlBuffer = new StringBuilder ();
  528. currentTag = new StringBuilder ();
  529. }
  530. // Use this method rather than setting the properties
  531. // directly so that all the necessary properties can
  532. // be changed in harmony with each other. Maybe the
  533. // fields should be in a seperate class to help enforce
  534. // this.
  535. private void SetProperties (
  536. XmlNodeType nodeType,
  537. string name,
  538. bool isEmptyElement,
  539. string value,
  540. bool clearAttributes)
  541. {
  542. this.nodeType = nodeType;
  543. this.name = name;
  544. this.isEmptyElement = isEmptyElement;
  545. this.value = value;
  546. if (clearAttributes)
  547. ClearAttributes ();
  548. int indexOfColon = name.IndexOf (':');
  549. if (indexOfColon == -1) {
  550. prefix = String.Empty;
  551. localName = name;
  552. } else {
  553. prefix = name.Substring (0, indexOfColon);
  554. localName = name.Substring (indexOfColon + 1);
  555. }
  556. namespaceURI = LookupNamespace (prefix);
  557. }
  558. private void SaveProperties ()
  559. {
  560. saveNodeType = nodeType;
  561. saveName = name;
  562. savePrefix = prefix;
  563. saveLocalName = localName;
  564. saveNamespaceURI = namespaceURI;
  565. saveIsEmptyElement = isEmptyElement;
  566. // An element's value is always String.Empty.
  567. }
  568. private void RestoreProperties ()
  569. {
  570. nodeType = saveNodeType;
  571. name = saveName;
  572. prefix = savePrefix;
  573. localName = saveLocalName;
  574. namespaceURI = saveNamespaceURI;
  575. isEmptyElement = saveIsEmptyElement;
  576. value = String.Empty;
  577. }
  578. private void AddAttribute (string name, string value)
  579. {
  580. attributes.Add (name, value);
  581. }
  582. private void ClearAttributes ()
  583. {
  584. if (attributes.Count > 0)
  585. attributes.Clear ();
  586. attributeEnumerator = null;
  587. }
  588. private int PeekChar ()
  589. {
  590. return reader.Peek ();
  591. }
  592. private int ReadChar ()
  593. {
  594. int ch = reader.Read ();
  595. if (saveToXmlBuffer) {
  596. xmlBuffer.Append ((char) ch);
  597. }
  598. currentTag.Append ((char) ch);
  599. return ch;
  600. }
  601. // This should really keep track of some state so
  602. // that it's not possible to have more than one document
  603. // element or text outside of the document element.
  604. private bool ReadContent ()
  605. {
  606. bool more = false;
  607. currentTag.Length = 0;
  608. if (popScope) {
  609. parserContext.NamespaceManager.PopScope ();
  610. popScope = false;
  611. }
  612. if (depthDown)
  613. --depth;
  614. if (returnEntityReference) {
  615. ++depth;
  616. SetEntityReferenceProperties ();
  617. more = true;
  618. } else {
  619. switch (PeekChar ())
  620. {
  621. case '<':
  622. ReadChar ();
  623. ReadTag ();
  624. more = true;
  625. break;
  626. case -1:
  627. readState = ReadState.EndOfFile;
  628. SetProperties (
  629. XmlNodeType.None, // nodeType
  630. String.Empty, // name
  631. false, // isEmptyElement
  632. String.Empty, // value
  633. true // clearAttributes
  634. );
  635. more = false;
  636. break;
  637. default:
  638. ReadText ();
  639. more = true;
  640. break;
  641. }
  642. }
  643. return more;
  644. }
  645. private void SetEntityReferenceProperties ()
  646. {
  647. SetProperties (
  648. XmlNodeType.EntityReference, // nodeType
  649. entityReferenceName, // name
  650. false, // isEmptyElement
  651. String.Empty, // value
  652. true // clearAttributes
  653. );
  654. returnEntityReference = false;
  655. entityReferenceName = String.Empty;
  656. }
  657. // The leading '<' has already been consumed.
  658. private void ReadTag ()
  659. {
  660. switch (PeekChar ())
  661. {
  662. case '/':
  663. ReadChar ();
  664. ReadEndTag ();
  665. break;
  666. case '?':
  667. ReadChar ();
  668. ReadProcessingInstruction ();
  669. break;
  670. case '!':
  671. ReadChar ();
  672. ReadDeclaration ();
  673. break;
  674. default:
  675. ReadStartTag ();
  676. break;
  677. }
  678. }
  679. // The leading '<' has already been consumed.
  680. private void ReadStartTag ()
  681. {
  682. parserContext.NamespaceManager.PushScope ();
  683. string name = ReadName ();
  684. SkipWhitespace ();
  685. bool isEmptyElement = false;
  686. ClearAttributes ();
  687. if (XmlChar.IsFirstNameChar (PeekChar ()))
  688. ReadAttributes ();
  689. if (PeekChar () == '/') {
  690. ReadChar ();
  691. isEmptyElement = true;
  692. depthDown = true;
  693. popScope = true;
  694. }
  695. Expect ('>');
  696. ++depth;
  697. SetProperties (
  698. XmlNodeType.Element, // nodeType
  699. name, // name
  700. isEmptyElement, // isEmptyElement
  701. String.Empty, // value
  702. false // clearAttributes
  703. );
  704. }
  705. // The reader is positioned on the first character
  706. // of the element's name.
  707. private void ReadEndTag ()
  708. {
  709. string name = ReadName ();
  710. SkipWhitespace ();
  711. Expect ('>');
  712. --depth;
  713. SetProperties (
  714. XmlNodeType.EndElement, // nodeType
  715. name, // name
  716. false, // isEmptyElement
  717. String.Empty, // value
  718. true // clearAttributes
  719. );
  720. popScope = true;
  721. }
  722. private void AppendNameChar (int ch)
  723. {
  724. CheckNameCapacity ();
  725. nameBuffer [nameLength++] = (char)ch;
  726. }
  727. private void CheckNameCapacity ()
  728. {
  729. if (nameLength == nameCapacity) {
  730. nameCapacity = nameCapacity * 2;
  731. char [] oldNameBuffer = nameBuffer;
  732. nameBuffer = new char [nameCapacity];
  733. Array.Copy (oldNameBuffer, nameBuffer, nameLength);
  734. }
  735. }
  736. private string CreateNameString ()
  737. {
  738. return new String (nameBuffer, 0, nameLength);
  739. }
  740. private void AppendValueChar (int ch)
  741. {
  742. CheckValueCapacity ();
  743. valueBuffer [valueLength++] = (char)ch;
  744. }
  745. private void CheckValueCapacity ()
  746. {
  747. if (valueLength == valueCapacity) {
  748. valueCapacity = valueCapacity * 2;
  749. char [] oldValueBuffer = valueBuffer;
  750. valueBuffer = new char [valueCapacity];
  751. Array.Copy (oldValueBuffer, valueBuffer, valueLength);
  752. }
  753. }
  754. private string CreateValueString ()
  755. {
  756. return new String (valueBuffer, 0, valueLength);
  757. }
  758. // The reader is positioned on the first character
  759. // of the text.
  760. private void ReadText ()
  761. {
  762. valueLength = 0;
  763. int ch = PeekChar ();
  764. while (ch != '<' && ch != -1) {
  765. if (ch == '&') {
  766. ReadChar ();
  767. if (ReadReference (false))
  768. break;
  769. } else
  770. AppendValueChar (ReadChar ());
  771. ch = PeekChar ();
  772. }
  773. if (returnEntityReference && valueLength == 0) {
  774. ++depth;
  775. SetEntityReferenceProperties ();
  776. } else {
  777. if (depth >= 0) {
  778. ++depth;
  779. depthDown = true;
  780. }
  781. SetProperties (
  782. XmlNodeType.Text, // nodeType
  783. String.Empty, // name
  784. false, // isEmptyElement
  785. CreateValueString (), // value
  786. true // clearAttributes
  787. );
  788. }
  789. }
  790. // The leading '&' has already been consumed.
  791. // Returns true if the entity reference isn't a simple
  792. // character reference or one of the predefined entities.
  793. // This allows the ReadText method to break so that the
  794. // next call to Read will return the EntityReference node.
  795. private bool ReadReference (bool ignoreEntityReferences)
  796. {
  797. if (PeekChar () == '#') {
  798. ReadChar ();
  799. ReadCharacterReference ();
  800. } else
  801. ReadEntityReference (ignoreEntityReferences);
  802. return returnEntityReference;
  803. }
  804. private void ReadCharacterReference ()
  805. {
  806. int value = 0;
  807. if (PeekChar () == 'x') {
  808. ReadChar ();
  809. while (PeekChar () != ';' && PeekChar () != -1) {
  810. int ch = ReadChar ();
  811. if (ch >= '0' && ch <= '9')
  812. value = (value << 4) + ch - '0';
  813. else if (ch >= 'A' && ch <= 'F')
  814. value = (value << 4) + ch - 'A' + 10;
  815. else if (ch >= 'a' && ch <= 'f')
  816. value = (value << 4) + ch - 'a' + 10;
  817. else
  818. throw new XmlException (
  819. String.Format (
  820. "invalid hexadecimal digit: {0} (#x{1:X})",
  821. (char)ch,
  822. ch));
  823. }
  824. } else {
  825. while (PeekChar () != ';' && PeekChar () != -1) {
  826. int ch = ReadChar ();
  827. if (ch >= '0' && ch <= '9')
  828. value = value * 10 + ch - '0';
  829. else
  830. throw new XmlException (
  831. String.Format (
  832. "invalid decimal digit: {0} (#x{1:X})",
  833. (char)ch,
  834. ch));
  835. }
  836. }
  837. ReadChar (); // ';'
  838. AppendValueChar (value);
  839. }
  840. private void ReadEntityReference (bool ignoreEntityReferences)
  841. {
  842. nameLength = 0;
  843. int ch = PeekChar ();
  844. while (ch != ';' && ch != -1) {
  845. AppendNameChar (ReadChar ());
  846. ch = PeekChar ();
  847. }
  848. Expect (';');
  849. string name = CreateNameString ();
  850. switch (name)
  851. {
  852. case "lt":
  853. AppendValueChar ('<');
  854. break;
  855. case "gt":
  856. AppendValueChar ('>');
  857. break;
  858. case "amp":
  859. AppendValueChar ('&');
  860. break;
  861. case "apos":
  862. AppendValueChar ('\'');
  863. break;
  864. case "quot":
  865. AppendValueChar ('"');
  866. break;
  867. default:
  868. if (ignoreEntityReferences) {
  869. AppendValueChar ('&');
  870. foreach (char ch2 in name) {
  871. AppendValueChar (ch2);
  872. }
  873. AppendValueChar (';');
  874. } else {
  875. returnEntityReference = true;
  876. entityReferenceName = name;
  877. }
  878. break;
  879. }
  880. }
  881. // The reader is positioned on the first character of
  882. // the attribute name.
  883. private void ReadAttributes ()
  884. {
  885. do {
  886. string name = ReadName ();
  887. SkipWhitespace ();
  888. Expect ('=');
  889. SkipWhitespace ();
  890. string value = ReadAttribute ();
  891. SkipWhitespace ();
  892. if (name == "xmlns")
  893. parserContext.NamespaceManager.AddNamespace (String.Empty, value);
  894. else if (name.StartsWith ("xmlns:"))
  895. parserContext.NamespaceManager.AddNamespace (name.Substring (6), value);
  896. AddAttribute (name, value);
  897. } while (PeekChar () != '/' && PeekChar () != '>' && PeekChar () != -1);
  898. }
  899. // The reader is positioned on the quote character.
  900. private string ReadAttribute ()
  901. {
  902. int quoteChar = ReadChar ();
  903. if (quoteChar != '\'' && quoteChar != '\"')
  904. throw new XmlException ("an attribute value was not quoted");
  905. valueLength = 0;
  906. while (PeekChar () != quoteChar) {
  907. int ch = ReadChar ();
  908. switch (ch)
  909. {
  910. case '<':
  911. throw new XmlException ("attribute values cannot contain '<'");
  912. case '&':
  913. ReadReference (true);
  914. break;
  915. case -1:
  916. throw new XmlException ("unexpected end of file in an attribute value");
  917. default:
  918. AppendValueChar (ch);
  919. break;
  920. }
  921. }
  922. ReadChar (); // quoteChar
  923. return CreateValueString ();
  924. }
  925. // The reader is positioned on the first character
  926. // of the target.
  927. private void ReadProcessingInstruction ()
  928. {
  929. string target = ReadName ();
  930. SkipWhitespace ();
  931. valueLength = 0;
  932. while (PeekChar () != -1) {
  933. int ch = ReadChar ();
  934. if (ch == '?' && PeekChar () == '>') {
  935. ReadChar ();
  936. break;
  937. }
  938. AppendValueChar ((char)ch);
  939. }
  940. SetProperties (
  941. XmlNodeType.ProcessingInstruction, // nodeType
  942. target, // name
  943. false, // isEmptyElement
  944. CreateValueString (), // value
  945. true // clearAttributes
  946. );
  947. }
  948. // The reader is positioned on the first character after
  949. // the leading '<!'.
  950. private void ReadDeclaration ()
  951. {
  952. int ch = PeekChar ();
  953. switch (ch)
  954. {
  955. case '-':
  956. Expect ('-');
  957. Expect ('-');
  958. ReadComment ();
  959. break;
  960. case '[':
  961. ReadChar ();
  962. Expect ('C');
  963. Expect ('D');
  964. Expect ('A');
  965. Expect ('T');
  966. Expect ('A');
  967. Expect ('[');
  968. ReadCDATA ();
  969. break;
  970. }
  971. }
  972. // The reader is positioned on the first character after
  973. // the leading '<!--'.
  974. private void ReadComment ()
  975. {
  976. valueLength = 0;
  977. while (PeekChar () != -1) {
  978. int ch = ReadChar ();
  979. if (ch == '-' && PeekChar () == '-') {
  980. ReadChar ();
  981. if (PeekChar () != '>')
  982. throw new XmlException ("comments cannot contain '--'");
  983. ReadChar ();
  984. break;
  985. }
  986. AppendValueChar ((char)ch);
  987. }
  988. SetProperties (
  989. XmlNodeType.Comment, // nodeType
  990. String.Empty, // name
  991. false, // isEmptyElement
  992. CreateValueString (), // value
  993. true // clearAttributes
  994. );
  995. }
  996. // The reader is positioned on the first character after
  997. // the leading '<![CDATA['.
  998. private void ReadCDATA ()
  999. {
  1000. valueLength = 0;
  1001. while (PeekChar () != -1) {
  1002. int ch = ReadChar ();
  1003. if (ch == ']' && PeekChar () == ']') {
  1004. ch = ReadChar (); // ']'
  1005. if (PeekChar () == '>') {
  1006. ReadChar (); // '>'
  1007. break;
  1008. } else {
  1009. AppendValueChar (']');
  1010. AppendValueChar (']');
  1011. ch = ReadChar ();
  1012. }
  1013. }
  1014. AppendValueChar ((char)ch);
  1015. }
  1016. ++depth;
  1017. SetProperties (
  1018. XmlNodeType.CDATA, // nodeType
  1019. String.Empty, // name
  1020. false, // isEmptyElement
  1021. CreateValueString (), // value
  1022. true // clearAttributes
  1023. );
  1024. }
  1025. // The reader is positioned on the first character
  1026. // of the name.
  1027. private string ReadName ()
  1028. {
  1029. if (!XmlChar.IsFirstNameChar (PeekChar ()))
  1030. throw new XmlException ("a name did not start with a legal character");
  1031. nameLength = 0;
  1032. AppendNameChar (ReadChar ());
  1033. while (XmlChar.IsNameChar (PeekChar ())) {
  1034. AppendNameChar (ReadChar ());
  1035. }
  1036. return CreateNameString ();
  1037. }
  1038. // Read the next character and compare it against the
  1039. // specified character.
  1040. private void Expect (int expected)
  1041. {
  1042. int ch = ReadChar ();
  1043. if (ch != expected) {
  1044. throw new XmlException (
  1045. String.Format (
  1046. "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
  1047. (char)expected,
  1048. expected,
  1049. (char)ch,
  1050. ch));
  1051. }
  1052. }
  1053. // Does not consume the first non-whitespace character.
  1054. private void SkipWhitespace ()
  1055. {
  1056. while (XmlChar.IsWhitespace (PeekChar ()))
  1057. ReadChar ();
  1058. }
  1059. }
  1060. }