XmlTextReader.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  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. // => No barfing, but parsing is incomplete.
  16. // DTD nodes are not still created.
  17. //
  18. // There's also no checking being done for either well-formedness
  19. // or validity.
  20. //
  21. // NameTables aren't being used everywhere yet.
  22. //
  23. // Some thought needs to be given to performance. There's too many
  24. // strings being allocated.
  25. //
  26. // Some of the MoveTo methods haven't been implemented yet.
  27. //
  28. // LineNumber and LinePosition aren't being tracked.
  29. //
  30. // xml:space, xml:lang, and xml:base aren't being tracked.
  31. //
  32. using System;
  33. using System.Collections;
  34. using System.IO;
  35. using System.Text;
  36. namespace System.Xml
  37. {
  38. public class XmlTextReader : XmlReader, IXmlLineInfo
  39. {
  40. WhitespaceHandling whitespaceHandling = WhitespaceHandling.All;
  41. #region Constructors
  42. protected XmlTextReader ()
  43. {
  44. }
  45. public XmlTextReader (Stream input)
  46. : this (new StreamReader (input))
  47. {
  48. }
  49. public XmlTextReader (string url)
  50. : this(url, new NameTable ())
  51. {
  52. }
  53. public XmlTextReader (TextReader input)
  54. : this (input, new NameTable ())
  55. {
  56. }
  57. protected XmlTextReader (XmlNameTable nt)
  58. : this (String.Empty, null, XmlNodeType.None, null)
  59. {
  60. }
  61. public XmlTextReader (Stream input, XmlNameTable nt)
  62. : this(new StreamReader (input), nt)
  63. {
  64. }
  65. public XmlTextReader (string url, Stream input)
  66. : this (url, new StreamReader (input))
  67. {
  68. }
  69. public XmlTextReader (string url, TextReader input)
  70. : this (url, input, new NameTable ())
  71. {
  72. }
  73. [MonoTODO("Non-filename-url must be supported. Waiting for WebClient")]
  74. public XmlTextReader (string url, XmlNameTable nt)
  75. // : this(url, new StreamReader ((Stream)new XmlUrlResolver ().GetEntity (new Uri (url), null, typeof(Stream))), nt)
  76. : this (url, new StreamReader (url), nt)
  77. {
  78. }
  79. public XmlTextReader (TextReader input, XmlNameTable nt)
  80. : this(String.Empty, input, nt)
  81. {
  82. }
  83. public XmlTextReader (Stream xmlFragment, XmlNodeType fragType, XmlParserContext context)
  84. : this (String.Empty, new StreamReader (xmlFragment), fragType, context)
  85. {
  86. }
  87. public XmlTextReader (string url, Stream input, XmlNameTable nt)
  88. : this (url, new StreamReader (input), nt)
  89. {
  90. }
  91. public XmlTextReader (string url, TextReader input, XmlNameTable nt)
  92. : this (url, input, XmlNodeType.Document, new XmlParserContext (nt, new XmlNamespaceManager (nt), null, XmlSpace.None))
  93. {
  94. }
  95. [MonoTODO("TODO as same as private XmlTextReader(TextReader, XmlNodeType, XmlParserContext)")]
  96. public XmlTextReader (string xmlFragment, XmlNodeType fragType, XmlParserContext context)
  97. : this (String.Empty, new StringReader (xmlFragment), fragType, context)
  98. {
  99. }
  100. // TODO still remains as described at head of this file,
  101. // but it might not be TODO of the constructors...
  102. XmlTextReader (string url, TextReader fragment, XmlNodeType fragType, XmlParserContext context)
  103. {
  104. this.SetReaderContext(url, context);
  105. this.SetReaderFragment(fragment, fragType);
  106. }
  107. #endregion
  108. #region Properties
  109. public override int AttributeCount
  110. {
  111. get { return attributes.Count; }
  112. }
  113. public override string BaseURI
  114. {
  115. get { return parserContext.BaseURI; }
  116. }
  117. public override int Depth
  118. {
  119. get {
  120. return elementDepth;
  121. }
  122. }
  123. public Encoding Encoding
  124. {
  125. get { return parserContext.Encoding; }
  126. }
  127. public override bool EOF
  128. {
  129. get
  130. {
  131. return
  132. readState == ReadState.EndOfFile ||
  133. readState == ReadState.Closed;
  134. }
  135. }
  136. public override bool HasValue
  137. {
  138. get { return value != String.Empty; }
  139. }
  140. public override bool IsDefault
  141. {
  142. get
  143. {
  144. // XmlTextReader does not expand default attributes.
  145. return false;
  146. }
  147. }
  148. public override bool IsEmptyElement
  149. {
  150. get { return isEmptyElement; }
  151. }
  152. public override string this [int i]
  153. {
  154. get { return GetAttribute (i); }
  155. }
  156. public override string this [string name]
  157. {
  158. get { return GetAttribute (name); }
  159. }
  160. public override string this [string localName, string namespaceName]
  161. {
  162. get { return GetAttribute (localName, namespaceName); }
  163. }
  164. public int LineNumber
  165. {
  166. get { return line; }
  167. }
  168. public int LinePosition
  169. {
  170. get { return column; }
  171. }
  172. public override string LocalName
  173. {
  174. get { return localName; }
  175. }
  176. public override string Name
  177. {
  178. get { return name; }
  179. }
  180. [MonoTODO]
  181. public bool Namespaces
  182. {
  183. get { throw new NotImplementedException (); }
  184. set { throw new NotImplementedException (); }
  185. }
  186. public override string NamespaceURI
  187. {
  188. get { return namespaceURI; }
  189. }
  190. public override XmlNameTable NameTable
  191. {
  192. get { return parserContext.NameTable; }
  193. }
  194. public override XmlNodeType NodeType
  195. {
  196. get { return nodeType; }
  197. }
  198. [MonoTODO]
  199. public bool Normalization
  200. {
  201. get { throw new NotImplementedException (); }
  202. set { throw new NotImplementedException (); }
  203. }
  204. public override string Prefix
  205. {
  206. get { return prefix; }
  207. }
  208. [MonoTODO]
  209. public override char QuoteChar
  210. {
  211. get { throw new NotImplementedException (); }
  212. }
  213. public override ReadState ReadState
  214. {
  215. get { return readState; }
  216. }
  217. public override string Value
  218. {
  219. get {
  220. if(NodeType == XmlNodeType.Attribute)
  221. return ResolveAttributeValue(value);
  222. else
  223. return value;
  224. }
  225. }
  226. public WhitespaceHandling WhitespaceHandling
  227. {
  228. get { return whitespaceHandling; }
  229. set { whitespaceHandling = value; }
  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. if (i > attributes.Count)
  257. throw new ArgumentOutOfRangeException ("i is smaller than AttributeCount");
  258. else
  259. return ResolveAttributeValue (attributes [orderedAttributes [i]] as string);
  260. }
  261. public override string GetAttribute (string name)
  262. {
  263. return attributes.ContainsKey (name) ?
  264. ResolveAttributeValue (attributes [name] as string) : String.Empty;
  265. }
  266. public override string GetAttribute (string localName, string namespaceURI)
  267. {
  268. foreach (DictionaryEntry entry in attributes)
  269. {
  270. string thisName = entry.Key as string;
  271. int indexOfColon = thisName.IndexOf (':');
  272. if (indexOfColon != -1) {
  273. string thisLocalName = thisName.Substring (indexOfColon + 1);
  274. if (localName == thisLocalName) {
  275. string thisPrefix = thisName.Substring (0, indexOfColon);
  276. string thisNamespaceURI = LookupNamespace (thisPrefix);
  277. if (namespaceURI == thisNamespaceURI)
  278. return attributes.ContainsKey (thisName) ?
  279. ResolveAttributeValue (attributes [thisName] as string) : String.Empty;
  280. }
  281. } else if (localName == "xmlns" && namespaceURI == "http://www.w3.org/2000/xmlns/" && thisName == "xmlns")
  282. return attributes.ContainsKey (thisName) ?
  283. ResolveAttributeValue (attributes [thisName] as string) : String.Empty;
  284. }
  285. return String.Empty;
  286. }
  287. [MonoTODO]
  288. public TextReader GetRemainder ()
  289. {
  290. throw new NotImplementedException ();
  291. }
  292. [MonoTODO]
  293. bool IXmlLineInfo.HasLineInfo ()
  294. {
  295. return false;
  296. }
  297. public override string LookupNamespace (string prefix)
  298. {
  299. return parserContext.NamespaceManager.LookupNamespace (prefix);
  300. }
  301. public override void MoveToAttribute (int i)
  302. {
  303. MoveToElement ();
  304. if (attributes == null || orderedAttributes.Count < i || i < 0)
  305. throw new ArgumentOutOfRangeException ("attribute index out of range.");
  306. string name = orderedAttributes [i] as string;
  307. string value = attributes [name] as string;
  308. SetProperties (
  309. XmlNodeType.Attribute, // nodeType
  310. name, // name
  311. false, // isEmptyElement
  312. value, // value
  313. false // clearAttributes
  314. );
  315. }
  316. public override bool MoveToAttribute (string name)
  317. {
  318. MoveToElement ();
  319. bool match = false;
  320. if (attributes == null)
  321. return false;
  322. if (orderedAttributesEnumerator == null) {
  323. SaveProperties ();
  324. orderedAttributesEnumerator = orderedAttributes.GetEnumerator ();
  325. }
  326. while (orderedAttributesEnumerator.MoveNext ()) {
  327. if(name == orderedAttributesEnumerator.Current as string) {
  328. match = true;
  329. break;
  330. }
  331. }
  332. if (match) {
  333. string value = attributes [name] as string;
  334. SetProperties (
  335. XmlNodeType.Attribute, // nodeType
  336. name, // name
  337. false, // isEmptyElement
  338. value, // value
  339. false // clearAttributes
  340. );
  341. }
  342. return match;
  343. }
  344. [MonoTODO]
  345. public override bool MoveToAttribute (string localName, string namespaceName)
  346. {
  347. throw new NotImplementedException ();
  348. }
  349. public override bool MoveToElement ()
  350. {
  351. if (orderedAttributesEnumerator != null) {
  352. orderedAttributesEnumerator = null;
  353. RestoreProperties ();
  354. return true;
  355. }
  356. return false;
  357. }
  358. public override bool MoveToFirstAttribute ()
  359. {
  360. MoveToElement ();
  361. return MoveToNextAttribute ();
  362. }
  363. public override bool MoveToNextAttribute ()
  364. {
  365. if (attributes == null)
  366. return false;
  367. if (orderedAttributesEnumerator == null) {
  368. SaveProperties ();
  369. orderedAttributesEnumerator = orderedAttributes.GetEnumerator ();
  370. }
  371. if (orderedAttributesEnumerator.MoveNext ()) {
  372. string name = orderedAttributesEnumerator.Current as string;
  373. string value = attributes [name] as string;
  374. SetProperties (
  375. XmlNodeType.Attribute, // nodeType
  376. name, // name
  377. false, // isEmptyElement
  378. value, // value
  379. false // clearAttributes
  380. );
  381. return true;
  382. }
  383. return false;
  384. }
  385. public override bool Read ()
  386. {
  387. bool more = false;
  388. readState = ReadState.Interactive;
  389. more = ReadContent ();
  390. return more;
  391. }
  392. [MonoTODO("This method should consider entity references")]
  393. public override bool ReadAttributeValue ()
  394. {
  395. // reading attribute value phase now stopped
  396. if(attributeStringCurrentPosition < 0 ||
  397. attributeString.Length < attributeStringCurrentPosition) {
  398. attributeStringCurrentPosition = 0;
  399. attributeString = String.Empty;
  400. return false;
  401. }
  402. // If not started, then initialize attributeString when parsing is at start.
  403. if(attributeStringCurrentPosition == 0)
  404. attributeString = value;
  405. bool returnEntity = false;
  406. value = String.Empty;
  407. int nextPosition = attributeString.IndexOf ('&',
  408. attributeStringCurrentPosition);
  409. // if attribute string starts from '&' then it may be (unparsable) entity reference.
  410. if(nextPosition == 0) {
  411. string parsed = ReadAttributeValueEntityReference ();
  412. if(parsed == null) {
  413. // return entity (It is only this case to return entity reference.)
  414. int endEntityPosition = attributeString.IndexOf (';',
  415. attributeStringCurrentPosition);
  416. SetProperties (XmlNodeType.EntityReference,
  417. attributeString.Substring (attributeStringCurrentPosition + 1,
  418. endEntityPosition - attributeStringCurrentPosition - 1),
  419. false,
  420. String.Empty,
  421. false);
  422. attributeStringCurrentPosition = endEntityPosition + 1;
  423. return true;
  424. }
  425. else
  426. value += parsed;
  427. }
  428. // Other case always set text node.
  429. while(!returnEntity) {
  430. nextPosition = attributeString.IndexOf ('&', attributeStringCurrentPosition);
  431. if(nextPosition < 0) {
  432. // Reached to the end of value string.
  433. value += attributeString.Substring (attributeStringCurrentPosition);
  434. attributeStringCurrentPosition = -1;
  435. break;
  436. } else if(nextPosition == attributeStringCurrentPosition) {
  437. string parsed = ReadAttributeValueEntityReference ();
  438. if(parsed != null)
  439. value += parsed;
  440. else {
  441. // Found that an entity reference starts from this point.
  442. // Then once stop to parse attribute value and then return text.
  443. value += attributeString.Substring (attributeStringCurrentPosition,
  444. nextPosition - attributeStringCurrentPosition);
  445. break;
  446. }
  447. } else {
  448. value += attributeString.Substring (attributeStringCurrentPosition,
  449. nextPosition - attributeStringCurrentPosition);
  450. attributeStringCurrentPosition = nextPosition;
  451. break;
  452. }
  453. }
  454. SetProperties(XmlNodeType.Text,
  455. "#text",
  456. false,
  457. value,
  458. false);
  459. return true;
  460. }
  461. [MonoTODO]
  462. public int ReadBase64 (byte [] buffer, int offset, int length)
  463. {
  464. throw new NotImplementedException ();
  465. }
  466. [MonoTODO]
  467. public int ReadBinHex (byte [] buffer, int offset, int length)
  468. {
  469. throw new NotImplementedException ();
  470. }
  471. [MonoTODO]
  472. public int ReadChars (char [] buffer, int offset, int length)
  473. {
  474. throw new NotImplementedException ();
  475. }
  476. [MonoTODO]
  477. public override string ReadInnerXml ()
  478. {
  479. // Still need a Well Formedness check.
  480. // Will wait for Validating reader ;-)
  481. if (NodeType == XmlNodeType.Attribute) {
  482. return Value;
  483. } else {
  484. saveToXmlBuffer = true;
  485. string startname = this.Name;
  486. string endname = string.Empty;
  487. readState = ReadState.Interactive;
  488. while (startname != endname) {
  489. ReadContent ();
  490. endname = this.Name;
  491. }
  492. xmlBuffer.Replace (currentTag.ToString (), "");
  493. saveToXmlBuffer = false;
  494. string InnerXml = xmlBuffer.ToString ();
  495. xmlBuffer.Length = 0;
  496. return InnerXml;
  497. }
  498. }
  499. [MonoTODO]
  500. public override string ReadOuterXml ()
  501. {
  502. if (NodeType == XmlNodeType.Attribute) {
  503. return Name + "=\"" + Value.Replace ("\"", "'") + "\"";
  504. } else {
  505. saveToXmlBuffer = true;
  506. xmlBuffer.Append (currentTag.ToString ());
  507. string startname = this.Name;
  508. string endname = string.Empty;
  509. readState = ReadState.Interactive;
  510. while (startname != endname) {
  511. ReadContent ();
  512. endname = this.Name;
  513. }
  514. saveToXmlBuffer = false;
  515. string OuterXml = xmlBuffer.ToString ();
  516. xmlBuffer.Length = 0;
  517. return OuterXml;
  518. }
  519. }
  520. [MonoTODO]
  521. public override string ReadString ()
  522. {
  523. throw new NotImplementedException ();
  524. }
  525. [MonoTODO]
  526. public void ResetState ()
  527. {
  528. throw new NotImplementedException ();
  529. }
  530. public override void ResolveEntity ()
  531. {
  532. // XmlTextReaders don't resolve entities.
  533. throw new InvalidOperationException ("XmlTextReader cannot resolve external entities.");
  534. }
  535. #endregion
  536. #region Internals
  537. internal string publicId;
  538. internal string systemId;
  539. internal void SetReaderContext (string url, XmlParserContext context)
  540. {
  541. parserContext = context;
  542. parserContext.BaseURI = url;
  543. Init ();
  544. }
  545. internal void SetReaderFragment(TextReader fragment, XmlNodeType fragType)
  546. {
  547. this.reader = fragment;
  548. can_seek = fragment != null && fragment.Peek () != -1;
  549. /* for future use
  550. switch(fragType)
  551. {
  552. case XmlNodeType.Attribute: // attribute content
  553. parserContext.InputState = XmlParserInputState.AttributeValue;
  554. break;
  555. case XmlNodeType.DocumentFragment: // element content
  556. parserContext.InputState = XmlParserInputState.Content;
  557. break;
  558. case XmlNodeType.Element: // one element
  559. parserContext.InputState = XmlParserInputState.StartTag;
  560. break;
  561. case XmlNodeType.Document: // document content
  562. parserContext.InputState = XmlParserInputState.Start;
  563. break;
  564. default:
  565. throw new InvalidOperationException("setting this xml node type not allowed.");
  566. }
  567. */
  568. }
  569. #endregion
  570. #region Privates
  571. private XmlParserContext parserContext;
  572. private TextReader reader;
  573. private ReadState readState;
  574. private int depth;
  575. private int elementDepth;
  576. private bool depthDown;
  577. private bool popScope;
  578. private XmlNodeType nodeType;
  579. private string name;
  580. private string prefix;
  581. private string localName;
  582. private string namespaceURI;
  583. private bool isEmptyElement;
  584. private string value;
  585. private XmlNodeType saveNodeType;
  586. private string saveName;
  587. private string savePrefix;
  588. private string saveLocalName;
  589. private string saveNamespaceURI;
  590. private bool saveIsEmptyElement;
  591. private Hashtable attributes;
  592. private ArrayList orderedAttributes;
  593. private IEnumerator orderedAttributesEnumerator;
  594. private bool returnEntityReference;
  595. private string entityReferenceName;
  596. private char [] nameBuffer;
  597. private int nameLength;
  598. private int nameCapacity;
  599. private const int initialNameCapacity = 256;
  600. private char [] valueBuffer;
  601. private int valueLength;
  602. private int valueCapacity;
  603. private const int initialValueCapacity = 8192;
  604. private StringBuilder xmlBuffer; // This is for Read(Inner|Outer)Xml
  605. private StringBuilder currentTag; // A buffer for ReadContent for ReadOuterXml
  606. private bool saveToXmlBuffer;
  607. private int line = 1;
  608. private int column = 1;
  609. private bool has_peek;
  610. private bool can_seek;
  611. private int peek_char;
  612. private string attributeString = String.Empty;
  613. private int attributeStringCurrentPosition;
  614. private void Init ()
  615. {
  616. readState = ReadState.Initial;
  617. depth = 0;
  618. depthDown = false;
  619. popScope = false;
  620. nodeType = XmlNodeType.None;
  621. name = String.Empty;
  622. prefix = String.Empty;
  623. localName = string.Empty;
  624. isEmptyElement = false;
  625. value = String.Empty;
  626. attributes = new Hashtable ();
  627. orderedAttributes = new ArrayList ();
  628. orderedAttributesEnumerator = null;
  629. returnEntityReference = false;
  630. entityReferenceName = String.Empty;
  631. nameBuffer = new char [initialNameCapacity];
  632. nameLength = 0;
  633. nameCapacity = initialNameCapacity;
  634. valueBuffer = new char [initialValueCapacity];
  635. valueLength = 0;
  636. valueCapacity = initialValueCapacity;
  637. xmlBuffer = new StringBuilder ();
  638. currentTag = new StringBuilder ();
  639. }
  640. // Use this method rather than setting the properties
  641. // directly so that all the necessary properties can
  642. // be changed in harmony with each other. Maybe the
  643. // fields should be in a seperate class to help enforce
  644. // this.
  645. private void SetProperties (
  646. XmlNodeType nodeType,
  647. string name,
  648. bool isEmptyElement,
  649. string value,
  650. bool clearAttributes)
  651. {
  652. this.nodeType = nodeType;
  653. this.name = name;
  654. this.isEmptyElement = isEmptyElement;
  655. this.value = value;
  656. this.elementDepth = depth;
  657. if (clearAttributes)
  658. ClearAttributes ();
  659. int indexOfColon = name.IndexOf (':');
  660. if (indexOfColon == -1) {
  661. prefix = String.Empty;
  662. localName = name;
  663. } else {
  664. prefix = name.Substring (0, indexOfColon);
  665. localName = name.Substring (indexOfColon + 1);
  666. }
  667. namespaceURI = LookupNamespace (prefix);
  668. }
  669. private void SaveProperties ()
  670. {
  671. saveNodeType = nodeType;
  672. saveName = name;
  673. savePrefix = prefix;
  674. saveLocalName = localName;
  675. saveNamespaceURI = namespaceURI;
  676. saveIsEmptyElement = isEmptyElement;
  677. // An element's value is always String.Empty.
  678. }
  679. private void RestoreProperties ()
  680. {
  681. nodeType = saveNodeType;
  682. name = saveName;
  683. prefix = savePrefix;
  684. localName = saveLocalName;
  685. namespaceURI = saveNamespaceURI;
  686. isEmptyElement = saveIsEmptyElement;
  687. value = String.Empty;
  688. }
  689. private void AddAttribute (string name, string value)
  690. {
  691. attributes.Add (name, value);
  692. orderedAttributes.Add (name);
  693. }
  694. private void ClearAttributes ()
  695. {
  696. if (attributes.Count > 0) {
  697. attributes.Clear ();
  698. orderedAttributes.Clear ();
  699. }
  700. orderedAttributesEnumerator = null;
  701. }
  702. private int PeekChar ()
  703. {
  704. if (can_seek)
  705. return reader.Peek ();
  706. if (has_peek)
  707. return peek_char;
  708. peek_char = reader.Read ();
  709. has_peek = true;
  710. return peek_char;
  711. }
  712. private int ReadChar ()
  713. {
  714. int ch;
  715. if (has_peek) {
  716. ch = peek_char;
  717. has_peek = false;
  718. } else {
  719. ch = reader.Read ();
  720. }
  721. if (ch == '\n') {
  722. line++;
  723. column = 1;
  724. } else {
  725. column++;
  726. }
  727. if (saveToXmlBuffer) {
  728. xmlBuffer.Append ((char) ch);
  729. }
  730. currentTag.Append ((char) ch);
  731. return ch;
  732. }
  733. // This should really keep track of some state so
  734. // that it's not possible to have more than one document
  735. // element or text outside of the document element.
  736. private bool ReadContent ()
  737. {
  738. currentTag.Length = 0;
  739. if (popScope) {
  740. parserContext.NamespaceManager.PopScope ();
  741. popScope = false;
  742. }
  743. if (returnEntityReference) {
  744. SetEntityReferenceProperties ();
  745. } else {
  746. switch (PeekChar ())
  747. {
  748. case '<':
  749. ReadChar ();
  750. ReadTag ();
  751. break;
  752. case '\r':
  753. if (whitespaceHandling == WhitespaceHandling.All ||
  754. whitespaceHandling == WhitespaceHandling.Significant)
  755. return ReadWhitespace ();
  756. ReadChar ();
  757. return ReadContent ();
  758. case '\n':
  759. if (whitespaceHandling == WhitespaceHandling.All ||
  760. whitespaceHandling == WhitespaceHandling.Significant)
  761. return ReadWhitespace ();
  762. ReadChar ();
  763. return ReadContent ();
  764. case ' ':
  765. if (whitespaceHandling == WhitespaceHandling.All ||
  766. whitespaceHandling == WhitespaceHandling.Significant)
  767. return ReadWhitespace ();
  768. SkipWhitespace ();
  769. return ReadContent ();
  770. case -1:
  771. readState = ReadState.EndOfFile;
  772. SetProperties (
  773. XmlNodeType.None, // nodeType
  774. String.Empty, // name
  775. false, // isEmptyElement
  776. String.Empty, // value
  777. true // clearAttributes
  778. );
  779. break;
  780. default:
  781. ReadText (true);
  782. break;
  783. }
  784. }
  785. return this.ReadState != ReadState.EndOfFile;
  786. }
  787. private void SetEntityReferenceProperties ()
  788. {
  789. SetProperties (
  790. XmlNodeType.EntityReference, // nodeType
  791. entityReferenceName, // name
  792. false, // isEmptyElement
  793. String.Empty, // value
  794. true // clearAttributes
  795. );
  796. returnEntityReference = false;
  797. entityReferenceName = String.Empty;
  798. }
  799. // The leading '<' has already been consumed.
  800. private void ReadTag ()
  801. {
  802. switch (PeekChar ())
  803. {
  804. case '/':
  805. ReadChar ();
  806. ReadEndTag ();
  807. break;
  808. case '?':
  809. ReadChar ();
  810. ReadProcessingInstruction ();
  811. break;
  812. case '!':
  813. ReadChar ();
  814. ReadDeclaration ();
  815. break;
  816. default:
  817. ReadStartTag ();
  818. break;
  819. }
  820. }
  821. // The leading '<' has already been consumed.
  822. private void ReadStartTag ()
  823. {
  824. parserContext.NamespaceManager.PushScope ();
  825. string name = ReadName ();
  826. SkipWhitespace ();
  827. bool isEmptyElement = false;
  828. ClearAttributes ();
  829. if (XmlChar.IsFirstNameChar (PeekChar ()))
  830. ReadAttributes ();
  831. if (PeekChar () == '/') {
  832. ReadChar ();
  833. isEmptyElement = true;
  834. depthDown = true;
  835. popScope = true;
  836. }
  837. Expect ('>');
  838. SetProperties (
  839. XmlNodeType.Element, // nodeType
  840. name, // name
  841. isEmptyElement, // isEmptyElement
  842. String.Empty, // value
  843. false // clearAttributes
  844. );
  845. if (!depthDown)
  846. ++depth;
  847. else
  848. depthDown = false;
  849. }
  850. // The reader is positioned on the first character
  851. // of the element's name.
  852. private void ReadEndTag ()
  853. {
  854. string name = ReadName ();
  855. SkipWhitespace ();
  856. Expect ('>');
  857. --depth;
  858. SetProperties (
  859. XmlNodeType.EndElement, // nodeType
  860. name, // name
  861. false, // isEmptyElement
  862. String.Empty, // value
  863. true // clearAttributes
  864. );
  865. popScope = true;
  866. }
  867. private void AppendNameChar (int ch)
  868. {
  869. CheckNameCapacity ();
  870. nameBuffer [nameLength++] = (char)ch;
  871. }
  872. private void CheckNameCapacity ()
  873. {
  874. if (nameLength == nameCapacity) {
  875. nameCapacity = nameCapacity * 2;
  876. char [] oldNameBuffer = nameBuffer;
  877. nameBuffer = new char [nameCapacity];
  878. Array.Copy (oldNameBuffer, nameBuffer, nameLength);
  879. }
  880. }
  881. private string CreateNameString ()
  882. {
  883. return new String (nameBuffer, 0, nameLength);
  884. }
  885. private void AppendValueChar (int ch)
  886. {
  887. CheckValueCapacity ();
  888. valueBuffer [valueLength++] = (char)ch;
  889. }
  890. private void CheckValueCapacity ()
  891. {
  892. if (valueLength == valueCapacity) {
  893. valueCapacity = valueCapacity * 2;
  894. char [] oldValueBuffer = valueBuffer;
  895. valueBuffer = new char [valueCapacity];
  896. Array.Copy (oldValueBuffer, valueBuffer, valueLength);
  897. }
  898. }
  899. private string CreateValueString ()
  900. {
  901. return new String (valueBuffer, 0, valueLength);
  902. }
  903. // The reader is positioned on the first character
  904. // of the text.
  905. private void ReadText (bool cleanValue)
  906. {
  907. if (cleanValue)
  908. valueLength = 0;
  909. int ch = PeekChar ();
  910. while (ch != '<' && ch != -1) {
  911. if (ch == '&') {
  912. ReadChar ();
  913. if (ReadReference (false))
  914. break;
  915. } else
  916. AppendValueChar (ReadChar ());
  917. ch = PeekChar ();
  918. }
  919. if (returnEntityReference && valueLength == 0) {
  920. SetEntityReferenceProperties ();
  921. } else {
  922. SetProperties (
  923. XmlNodeType.Text, // nodeType
  924. String.Empty, // name
  925. false, // isEmptyElement
  926. CreateValueString (), // value
  927. true // clearAttributes
  928. );
  929. }
  930. }
  931. // The leading '&' has already been consumed.
  932. // Returns true if the entity reference isn't a simple
  933. // character reference or one of the predefined entities.
  934. // This allows the ReadText method to break so that the
  935. // next call to Read will return the EntityReference node.
  936. private bool ReadReference (bool ignoreEntityReferences)
  937. {
  938. if (PeekChar () == '#') {
  939. ReadChar ();
  940. ReadCharacterReference ();
  941. } else
  942. ReadEntityReference (ignoreEntityReferences);
  943. return returnEntityReference;
  944. }
  945. private void ReadCharacterReference ()
  946. {
  947. int value = 0;
  948. if (PeekChar () == 'x') {
  949. ReadChar ();
  950. while (PeekChar () != ';' && PeekChar () != -1) {
  951. int ch = ReadChar ();
  952. if (ch >= '0' && ch <= '9')
  953. value = (value << 4) + ch - '0';
  954. else if (ch >= 'A' && ch <= 'F')
  955. value = (value << 4) + ch - 'A' + 10;
  956. else if (ch >= 'a' && ch <= 'f')
  957. value = (value << 4) + ch - 'a' + 10;
  958. else
  959. throw new XmlException (
  960. String.Format (
  961. "invalid hexadecimal digit: {0} (#x{1:X})",
  962. (char)ch,
  963. ch));
  964. }
  965. } else {
  966. while (PeekChar () != ';' && PeekChar () != -1) {
  967. int ch = ReadChar ();
  968. if (ch >= '0' && ch <= '9')
  969. value = value * 10 + ch - '0';
  970. else
  971. throw new XmlException (
  972. String.Format (
  973. "invalid decimal digit: {0} (#x{1:X})",
  974. (char)ch,
  975. ch));
  976. }
  977. }
  978. ReadChar (); // ';'
  979. AppendValueChar (value);
  980. }
  981. private void ReadEntityReference (bool ignoreEntityReferences)
  982. {
  983. nameLength = 0;
  984. int ch = PeekChar ();
  985. while (ch != ';' && ch != -1) {
  986. AppendNameChar (ReadChar ());
  987. ch = PeekChar ();
  988. }
  989. Expect (';');
  990. string name = CreateNameString ();
  991. switch (name)
  992. {
  993. case "lt":
  994. AppendValueChar ('<');
  995. break;
  996. case "gt":
  997. AppendValueChar ('>');
  998. break;
  999. case "amp":
  1000. AppendValueChar ('&');
  1001. break;
  1002. case "apos":
  1003. AppendValueChar ('\'');
  1004. break;
  1005. case "quot":
  1006. AppendValueChar ('"');
  1007. break;
  1008. default:
  1009. if (ignoreEntityReferences) {
  1010. AppendValueChar ('&');
  1011. foreach (char ch2 in name) {
  1012. AppendValueChar (ch2);
  1013. }
  1014. AppendValueChar (';');
  1015. } else {
  1016. returnEntityReference = true;
  1017. entityReferenceName = name;
  1018. }
  1019. break;
  1020. }
  1021. }
  1022. // The reader is positioned on the first character of
  1023. // the attribute name.
  1024. private void ReadAttributes ()
  1025. {
  1026. do {
  1027. string name = ReadName ();
  1028. SkipWhitespace ();
  1029. Expect ('=');
  1030. SkipWhitespace ();
  1031. string value = ReadAttribute ();
  1032. SkipWhitespace ();
  1033. if (name == "xmlns")
  1034. parserContext.NamespaceManager.AddNamespace (String.Empty, value);
  1035. else if (name.StartsWith ("xmlns:"))
  1036. parserContext.NamespaceManager.AddNamespace (name.Substring (6), value);
  1037. AddAttribute (name, value);
  1038. } while (PeekChar () != '/' && PeekChar () != '>' && PeekChar () != -1);
  1039. }
  1040. // The reader is positioned on the quote character.
  1041. private string ReadAttribute ()
  1042. {
  1043. int quoteChar = ReadChar ();
  1044. if (quoteChar != '\'' && quoteChar != '\"')
  1045. throw new XmlException ("an attribute value was not quoted");
  1046. valueLength = 0;
  1047. while (PeekChar () != quoteChar) {
  1048. int ch = ReadChar ();
  1049. switch (ch)
  1050. {
  1051. case '<':
  1052. throw new XmlException ("attribute values cannot contain '<'");
  1053. // expansion of entity now should be done at ResolveAttributeValue() method
  1054. // case '&':
  1055. // ReadReference (true);
  1056. // break;
  1057. case -1:
  1058. throw new XmlException ("unexpected end of file in an attribute value");
  1059. default:
  1060. AppendValueChar (ch);
  1061. break;
  1062. }
  1063. }
  1064. ReadChar (); // quoteChar
  1065. return CreateValueString ();
  1066. }
  1067. // The reader is positioned on the first character
  1068. // of the target.
  1069. //
  1070. // Now it also reads XmlDeclaration, this method name became improper...
  1071. private void ReadProcessingInstruction ()
  1072. {
  1073. string target = ReadName ();
  1074. SkipWhitespace ();
  1075. valueLength = 0;
  1076. while (PeekChar () != -1) {
  1077. int ch = ReadChar ();
  1078. if (ch == '?' && PeekChar () == '>') {
  1079. ReadChar ();
  1080. break;
  1081. }
  1082. AppendValueChar ((char)ch);
  1083. }
  1084. /* for future use
  1085. if(target == "xml") && parserContext.InputState != XmlParserInputState.Start)
  1086. throw new XmlException("Xml declaration is not allowed here.");
  1087. else {
  1088. parserContext.InputState = XmlParserInputState.DTD; //for future use
  1089. }
  1090. */
  1091. SetProperties (
  1092. target == "xml" ?
  1093. XmlNodeType.XmlDeclaration :
  1094. XmlNodeType.ProcessingInstruction, // nodeType
  1095. target, // name
  1096. false, // isEmptyElement
  1097. CreateValueString (), // value
  1098. true // clearAttributes
  1099. );
  1100. }
  1101. // The reader is positioned on the first character after
  1102. // the leading '<!'.
  1103. private void ReadDeclaration ()
  1104. {
  1105. int ch = PeekChar ();
  1106. switch (ch)
  1107. {
  1108. case '-':
  1109. Expect ("--");
  1110. ReadComment ();
  1111. break;
  1112. case '[':
  1113. ReadChar ();
  1114. Expect ("CDATA[");
  1115. ReadCDATA ();
  1116. break;
  1117. case 'D':
  1118. Expect ("DOCTYPE");
  1119. ReadDoctypeDecl ();
  1120. break;
  1121. }
  1122. }
  1123. // The reader is positioned on the first character after
  1124. // the leading '<!--'.
  1125. private void ReadComment ()
  1126. {
  1127. valueLength = 0;
  1128. while (PeekChar () != -1) {
  1129. int ch = ReadChar ();
  1130. if (ch == '-' && PeekChar () == '-') {
  1131. ReadChar ();
  1132. if (PeekChar () != '>')
  1133. throw new XmlException ("comments cannot contain '--'");
  1134. ReadChar ();
  1135. break;
  1136. }
  1137. AppendValueChar ((char)ch);
  1138. }
  1139. SetProperties (
  1140. XmlNodeType.Comment, // nodeType
  1141. String.Empty, // name
  1142. false, // isEmptyElement
  1143. CreateValueString (), // value
  1144. true // clearAttributes
  1145. );
  1146. }
  1147. // The reader is positioned on the first character after
  1148. // the leading '<![CDATA['.
  1149. private void ReadCDATA ()
  1150. {
  1151. valueLength = 0;
  1152. while (PeekChar () != -1) {
  1153. int ch = ReadChar ();
  1154. if (ch == ']' && PeekChar () == ']') {
  1155. ch = ReadChar (); // ']'
  1156. if (PeekChar () == '>') {
  1157. ReadChar (); // '>'
  1158. break;
  1159. } else {
  1160. AppendValueChar (']');
  1161. AppendValueChar (']');
  1162. ch = ReadChar ();
  1163. }
  1164. }
  1165. AppendValueChar ((char)ch);
  1166. }
  1167. SetProperties (
  1168. XmlNodeType.CDATA, // nodeType
  1169. String.Empty, // name
  1170. false, // isEmptyElement
  1171. CreateValueString (), // value
  1172. true // clearAttributes
  1173. );
  1174. }
  1175. // The reader is positioned on the first character after
  1176. // the leading '<!DOCTYPE'.
  1177. private void ReadDoctypeDecl ()
  1178. {
  1179. string doctypeName = null;
  1180. string publicId = String.Empty;
  1181. string systemId = String.Empty;
  1182. SkipWhitespace ();
  1183. doctypeName = ReadName ();
  1184. SkipWhitespace ();
  1185. xmlBuffer.Length = 0;
  1186. switch(PeekChar ())
  1187. {
  1188. case 'S':
  1189. systemId = ReadSystemLiteral (true);
  1190. break;
  1191. case 'P':
  1192. publicId = ReadPubidLiteral ();
  1193. SkipWhitespace ();
  1194. systemId = ReadSystemLiteral (false);
  1195. break;
  1196. }
  1197. SkipWhitespace ();
  1198. if(PeekChar () == '[')
  1199. {
  1200. // read markupdecl etc. or end of decl
  1201. ReadChar ();
  1202. xmlBuffer.Length = 0;
  1203. saveToXmlBuffer = true;
  1204. do {
  1205. ReadDTDInternalSubset ();
  1206. } while(nodeType != XmlNodeType.None);
  1207. xmlBuffer.Remove (xmlBuffer.Length - 1, 1); // cut off ']'
  1208. saveToXmlBuffer = false;
  1209. }
  1210. // end of DOCTYPE decl.
  1211. SkipWhitespace ();
  1212. Expect ('>');
  1213. parserContext.InternalSubset = xmlBuffer.ToString ();
  1214. // set properties for <!DOCTYPE> node
  1215. SetProperties (
  1216. XmlNodeType.DocumentType, // nodeType
  1217. doctypeName, // name
  1218. false, // isEmptyElement
  1219. parserContext.InternalSubset, // value
  1220. true // clearAttributes
  1221. );
  1222. }
  1223. // Read any one of following:
  1224. // elementdecl, AttlistDecl, EntityDecl, NotationDecl,
  1225. // PI, Comment, Parameter Entity, or doctype termination char(']')
  1226. //
  1227. // returns a node of some nodeType or null, setting nodeType.
  1228. // (if None then ']' was found.)
  1229. private void ReadDTDInternalSubset()
  1230. {
  1231. SkipWhitespace ();
  1232. switch(ReadChar ())
  1233. {
  1234. case ']':
  1235. nodeType = XmlNodeType.None;
  1236. break;
  1237. case '%':
  1238. string peName = ReadName ();
  1239. Expect (';');
  1240. nodeType = XmlNodeType.EntityReference; // It's chating a bit;-)
  1241. break;
  1242. case '<':
  1243. switch(ReadChar ())
  1244. {
  1245. case '?':
  1246. ReadProcessingInstruction ();
  1247. break;
  1248. case '!':
  1249. switch(ReadChar ())
  1250. {
  1251. case '-':
  1252. Expect ('-');
  1253. ReadComment ();
  1254. break;
  1255. case 'E':
  1256. switch(ReadChar ())
  1257. {
  1258. case 'N':
  1259. Expect ("TITY");
  1260. ReadEntityDecl ();
  1261. break;
  1262. case 'L':
  1263. Expect ("EMENT");
  1264. ReadElementDecl ();
  1265. break;
  1266. default:
  1267. throw new XmlException ("Syntax Error after '<!E' (ELEMENT or ENTITY must be found)");
  1268. }
  1269. break;
  1270. case 'A':
  1271. Expect ("TTLIST");
  1272. ReadAttListDecl ();
  1273. break;
  1274. case 'N':
  1275. Expect ("OTATION");
  1276. ReadNotationDecl ();
  1277. break;
  1278. default:
  1279. throw new XmlException ("Syntax Error after '<!' characters.");
  1280. }
  1281. break;
  1282. default:
  1283. throw new XmlException ("Syntax Error after '<' character.");
  1284. }
  1285. break;
  1286. default:
  1287. throw new XmlException ("Syntax Error inside doctypedecl markup.");
  1288. }
  1289. }
  1290. // The reader is positioned on the head of the name.
  1291. private void ReadElementDecl()
  1292. {
  1293. while(ReadChar () != '>');
  1294. }
  1295. private void ReadEntityDecl()
  1296. {
  1297. while(ReadChar () != '>');
  1298. }
  1299. private void ReadAttListDecl()
  1300. {
  1301. while(ReadChar () != '>');
  1302. }
  1303. private void ReadNotationDecl()
  1304. {
  1305. while(ReadChar () != '>');
  1306. }
  1307. // The reader is positioned on the first 'S' of "SYSTEM".
  1308. private string ReadSystemLiteral (bool expectSYSTEM)
  1309. {
  1310. if(expectSYSTEM)
  1311. Expect ("SYSTEM");
  1312. SkipWhitespace ();
  1313. int quoteChar = ReadChar (); // apos or quot
  1314. xmlBuffer.Length = 0;
  1315. saveToXmlBuffer = true;
  1316. int c = 0;
  1317. while(c != quoteChar) {
  1318. c = ReadChar ();
  1319. if(c < 0) throw new XmlException ("Unexpected end of stream in ExternalID.");
  1320. }
  1321. saveToXmlBuffer = false;
  1322. xmlBuffer.Remove (xmlBuffer.Length-1, 1); // cut quoteChar
  1323. return xmlBuffer.ToString ();
  1324. }
  1325. private string ReadPubidLiteral()
  1326. {
  1327. Expect ("PUBLIC");
  1328. SkipWhitespace ();
  1329. int quoteChar = ReadChar ();
  1330. xmlBuffer.Length = 0;
  1331. saveToXmlBuffer = true;
  1332. int c = 0;
  1333. while(c != quoteChar)
  1334. {
  1335. c = ReadChar ();
  1336. if(c < 0) throw new XmlException ("Unexpected end of stream in ExternalID.");
  1337. if(c != quoteChar && !XmlChar.IsPubidChar (c))
  1338. throw new XmlException("character '" + (char)c + "' not allowed for PUBLIC ID");
  1339. }
  1340. ReadChar(); // skips quoteChar
  1341. xmlBuffer.Remove (xmlBuffer.Length-1, 1); // cut quoteChar
  1342. saveToXmlBuffer = false;
  1343. return xmlBuffer.ToString ();
  1344. }
  1345. // The reader is positioned on the first character
  1346. // of the name.
  1347. private string ReadName ()
  1348. {
  1349. if (!XmlChar.IsFirstNameChar (PeekChar ()))
  1350. throw new XmlException ("a name did not start with a legal character");
  1351. nameLength = 0;
  1352. AppendNameChar (ReadChar ());
  1353. while (XmlChar.IsNameChar (PeekChar ())) {
  1354. AppendNameChar (ReadChar ());
  1355. }
  1356. return CreateNameString ();
  1357. }
  1358. // Read the next character and compare it against the
  1359. // specified character.
  1360. private void Expect (int expected)
  1361. {
  1362. int ch = ReadChar ();
  1363. if (ch != expected) {
  1364. throw new XmlException (
  1365. String.Format (
  1366. "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
  1367. (char)expected,
  1368. expected,
  1369. (char)ch,
  1370. ch));
  1371. }
  1372. }
  1373. private void Expect (string expected)
  1374. {
  1375. int len = expected.Length;
  1376. for(int i=0; i< len; i++)
  1377. Expect (expected[i]);
  1378. }
  1379. // Does not consume the first non-whitespace character.
  1380. private void SkipWhitespace ()
  1381. {
  1382. //FIXME: Should not skip if whitespaceHandling == WhiteSpaceHandling.None
  1383. while (XmlChar.IsWhitespace (PeekChar ()))
  1384. ReadChar ();
  1385. }
  1386. private bool ReadWhitespace ()
  1387. {
  1388. valueLength = 0;
  1389. int ch = PeekChar ();
  1390. do {
  1391. AppendValueChar (ReadChar ());
  1392. } while ((ch = PeekChar ()) != -1 && XmlChar.IsWhitespace (ch));
  1393. if (ch != -1 && ch != '<')
  1394. ReadText (false);
  1395. else
  1396. SetProperties (XmlNodeType.Whitespace,
  1397. String.Empty,
  1398. false,
  1399. CreateValueString (),
  1400. true);
  1401. return (PeekChar () != -1);
  1402. }
  1403. // read entity reference from attribute string and if parsable then return the value.
  1404. private string ReadAttributeValueEntityReference ()
  1405. {
  1406. int endEntityPosition = attributeString.IndexOf(';',
  1407. attributeStringCurrentPosition);
  1408. string entityName = attributeString.Substring (attributeStringCurrentPosition + 1,
  1409. endEntityPosition - attributeStringCurrentPosition - 1);
  1410. attributeStringCurrentPosition = endEntityPosition + 1;
  1411. if(entityName [0] == '#') {
  1412. char c;
  1413. // character entity
  1414. if(entityName [1] == 'x') {
  1415. // hexadecimal
  1416. c = (char) int.Parse ("0" + entityName.Substring (2),
  1417. System.Globalization.NumberStyles.HexNumber);
  1418. } else {
  1419. // decimal
  1420. c = (char) int.Parse (entityName.Substring (1));
  1421. }
  1422. return c.ToString();
  1423. }
  1424. else {
  1425. switch(entityName)
  1426. {
  1427. case "lt": return "<";
  1428. case "gt": return ">";
  1429. case "amp": return "&";
  1430. case "quot": return "\"";
  1431. case "apos": return "'";
  1432. default: return null;
  1433. }
  1434. }
  1435. }
  1436. private string ResolveAttributeValue (string unresolved)
  1437. {
  1438. if(unresolved == null) return null;
  1439. StringBuilder resolved = new StringBuilder();
  1440. int pos = 0;
  1441. int next = unresolved.IndexOf ('&');
  1442. if(next < 0)
  1443. return unresolved;
  1444. while(next >= 0) {
  1445. if(pos < next)
  1446. resolved.Append (unresolved.Substring (pos, next - pos));// - 1);
  1447. int endPos = unresolved.IndexOf (';', next+1);
  1448. string entityName =
  1449. unresolved.Substring (next + 1, endPos - next - 1);
  1450. if(entityName [0] == '#') {
  1451. char c;
  1452. // character entity
  1453. if(entityName [1] == 'x') {
  1454. // hexadecimal
  1455. c = (char) int.Parse ("0" + entityName.Substring (2),
  1456. System.Globalization.NumberStyles.HexNumber);
  1457. } else {
  1458. // decimal
  1459. c = (char) int.Parse (entityName.Substring (1));
  1460. }
  1461. resolved.Append (c);
  1462. } else {
  1463. switch(entityName) {
  1464. case "lt": resolved.Append ("<"); break;
  1465. case "gt": resolved.Append (">"); break;
  1466. case "amp": resolved.Append ("&"); break;
  1467. case "quot": resolved.Append ("\""); break;
  1468. case "apos": resolved.Append ("'"); break;
  1469. // With respect to "Value", MS document is helpless
  1470. // and the implemention returns inconsistent value
  1471. // (e.g. XML: "&ent; &amp;ent;" ---> Value: "&ent; &ent;".)
  1472. default: resolved.Append ("&" + entityName + ";"); break;
  1473. }
  1474. }
  1475. pos = endPos + 1;
  1476. if(pos > unresolved.Length)
  1477. break;
  1478. next = unresolved.IndexOf('&', pos);
  1479. }
  1480. resolved.Append (unresolved.Substring(pos));
  1481. return resolved.ToString();
  1482. }
  1483. #endregion
  1484. }
  1485. }