XmlTextReader.cs 39 KB

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