XmlTextReader.cs 26 KB

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