XmlTextReader.cs 33 KB

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