XmlTextReader.cs 26 KB

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