XmlTextReader.cs 28 KB

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