XmlTextReader.cs 23 KB

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