XmlTextReader.cs 26 KB

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