XmlTextReader.cs 38 KB

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