XmlTextReader.cs 23 KB

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