XmlTextReader.cs 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708
  1. //
  2. // System.Xml.XmlTextReader
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. // Adam Treat ([email protected])
  7. // Atsushi Enomoto ([email protected])
  8. //
  9. // (C) 2001, 2002 Jason Diamond http://injektilo.org/
  10. //
  11. // FIXME:
  12. //
  13. // I haven't checked whether DTD parser runs correct.
  14. //
  15. // More strict well-formedness checking should be done.
  16. //
  17. // NameTables aren't being used completely yet.
  18. //
  19. // Some thought needs to be given to performance. There's too many
  20. // strings being allocated.
  21. //
  22. // Some of the MoveTo methods haven't been implemented yet.
  23. //
  24. // xml:space, xml:lang aren't being tracked.
  25. //
  26. using System;
  27. using System.Collections;
  28. using System.IO;
  29. using System.Text;
  30. using Mono.Xml;
  31. using Mono.Xml.Native;
  32. namespace System.Xml
  33. {
  34. public class XmlTextReader : XmlReader, IXmlLineInfo
  35. {
  36. WhitespaceHandling whitespaceHandling = WhitespaceHandling.All;
  37. #region Constructors
  38. protected XmlTextReader ()
  39. {
  40. }
  41. public XmlTextReader (Stream input)
  42. : this (new XmlStreamReader (input))
  43. {
  44. }
  45. public XmlTextReader (string url)
  46. : this(url, new NameTable ())
  47. {
  48. }
  49. public XmlTextReader (TextReader input)
  50. : this (input, new NameTable ())
  51. {
  52. }
  53. protected XmlTextReader (XmlNameTable nt)
  54. : this (String.Empty, null, XmlNodeType.None, null)
  55. {
  56. }
  57. public XmlTextReader (Stream input, XmlNameTable nt)
  58. : this(new XmlStreamReader (input), nt)
  59. {
  60. }
  61. public XmlTextReader (string url, Stream input)
  62. : this (url, new XmlStreamReader (input))
  63. {
  64. }
  65. public XmlTextReader (string url, TextReader input)
  66. : this (url, input, new NameTable ())
  67. {
  68. }
  69. [MonoTODO("Non-filename-url must be supported. Waiting for WebClient")]
  70. public XmlTextReader (string url, XmlNameTable nt)
  71. : this (url, new XmlStreamReader (url), nt)
  72. {
  73. }
  74. public XmlTextReader (TextReader input, XmlNameTable nt)
  75. : this (String.Empty, input, nt)
  76. {
  77. }
  78. public XmlTextReader (Stream xmlFragment, XmlNodeType fragType, XmlParserContext context)
  79. : this (context.BaseURI, new XmlStreamReader (xmlFragment), fragType, context)
  80. {
  81. }
  82. public XmlTextReader (string url, Stream input, XmlNameTable nt)
  83. : this (url, new XmlStreamReader (input), nt)
  84. {
  85. }
  86. public XmlTextReader (string url, TextReader input, XmlNameTable nt)
  87. : this (url, input, XmlNodeType.Document, null)
  88. {
  89. }
  90. [MonoTODO("TODO as same as private XmlTextReader(TextReader, XmlNodeType, XmlParserContext)")]
  91. public XmlTextReader (string xmlFragment, XmlNodeType fragType, XmlParserContext context)
  92. : this (context != null ? context.BaseURI : String.Empty,
  93. new StringReader (xmlFragment),
  94. fragType,
  95. context)
  96. {
  97. }
  98. // TODO still remains as described at head of this file,
  99. // but it might not be TODO of the constructors...
  100. XmlTextReader (string url, TextReader fragment, XmlNodeType fragType, XmlParserContext context)
  101. {
  102. this.Initialize (url, context, fragment, fragType);
  103. }
  104. #endregion
  105. #region Properties
  106. public override int AttributeCount
  107. {
  108. get { return attributes.Count; }
  109. }
  110. public override string BaseURI
  111. {
  112. get { return parserContext.BaseURI; }
  113. }
  114. public override int Depth
  115. {
  116. get {
  117. return elementDepth;
  118. }
  119. }
  120. public Encoding Encoding
  121. {
  122. get { return parserContext.Encoding; }
  123. }
  124. public override bool EOF
  125. {
  126. get
  127. {
  128. return
  129. readState == ReadState.EndOfFile ||
  130. readState == ReadState.Closed;
  131. }
  132. }
  133. public override bool HasValue
  134. {
  135. get { return value != String.Empty; }
  136. }
  137. public override bool IsDefault
  138. {
  139. get
  140. {
  141. // XmlTextReader does not expand default attributes.
  142. return false;
  143. }
  144. }
  145. public override bool IsEmptyElement
  146. {
  147. get { return isEmptyElement; }
  148. }
  149. public override string this [int i]
  150. {
  151. get { return GetAttribute (i); }
  152. }
  153. public override string this [string name]
  154. {
  155. get { return GetAttribute (name); }
  156. }
  157. public override string this [string localName, string namespaceName]
  158. {
  159. get { return GetAttribute (localName, namespaceName); }
  160. }
  161. public int LineNumber
  162. {
  163. get { return currentInput.LineNumber; }
  164. }
  165. public int LinePosition
  166. {
  167. get { return currentInput.LinePosition; }
  168. }
  169. public override string LocalName
  170. {
  171. get { return localName; }
  172. }
  173. public override string Name
  174. {
  175. get { return name; }
  176. }
  177. public bool Namespaces
  178. {
  179. get { return namespaces; }
  180. set {
  181. if (readState != ReadState.Initial)
  182. throw new InvalidOperationException ("Namespaces have to be set before reading.");
  183. namespaces = value;
  184. }
  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. public XmlResolver XmlResolver
  242. {
  243. set { resolver = value; }
  244. }
  245. [MonoTODO]
  246. public override XmlSpace XmlSpace
  247. {
  248. get { throw new NotImplementedException (); }
  249. }
  250. #endregion
  251. #region Methods
  252. public override void Close ()
  253. {
  254. readState = ReadState.Closed;
  255. foreach (XmlParserInput input in parserInputStack.ToArray ())
  256. input.Close ();
  257. this.currentInput.Close ();
  258. }
  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. private int GetIndexOfQualifiedAttribute (string localName, string namespaceURI)
  272. {
  273. for(int i = 0; i < orderedAttributes.Count; i++)
  274. {
  275. string thisName = (string) orderedAttributes [i];
  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 i;
  284. }
  285. } else if (localName == "xmlns" && namespaceURI == "http://www.w3.org/2000/xmlns/" && thisName == "xmlns")
  286. return i;
  287. }
  288. return -1;
  289. }
  290. public override string GetAttribute (string localName, string namespaceURI)
  291. {
  292. int idx = this.GetIndexOfQualifiedAttribute (localName, namespaceURI);
  293. if (idx < 0)
  294. return String.Empty;
  295. return UnescapeAttributeValue (attributes [orderedAttributes [idx]] as string);
  296. }
  297. [MonoTODO]
  298. public TextReader GetRemainder ()
  299. {
  300. throw new NotImplementedException ();
  301. }
  302. bool IXmlLineInfo.HasLineInfo ()
  303. {
  304. return true;
  305. }
  306. public override string LookupNamespace (string prefix)
  307. {
  308. return parserContext.NamespaceManager.LookupNamespace (prefix);
  309. }
  310. public override void MoveToAttribute (int i)
  311. {
  312. MoveToElement ();
  313. if (attributes == null || orderedAttributes.Count < i || i < 0)
  314. throw new ArgumentOutOfRangeException ("attribute index out of range.");
  315. string name = orderedAttributes [i] as string;
  316. string value = attributes [name] as string;
  317. SetProperties (
  318. XmlNodeType.Attribute, // nodeType
  319. name, // name
  320. false, // isEmptyElement
  321. value, // value
  322. false // clearAttributes
  323. );
  324. attributeValuePos = 0;
  325. }
  326. public override bool MoveToAttribute (string name)
  327. {
  328. MoveToElement ();
  329. bool match = false;
  330. if (attributes == null)
  331. return false;
  332. if (orderedAttributesEnumerator == null) {
  333. SaveProperties ();
  334. orderedAttributesEnumerator = orderedAttributes.GetEnumerator ();
  335. }
  336. while (orderedAttributesEnumerator.MoveNext ()) {
  337. if(name == orderedAttributesEnumerator.Current as string) {
  338. match = true;
  339. break;
  340. }
  341. }
  342. if (match) {
  343. string value = attributes [name] as string;
  344. SetProperties (
  345. XmlNodeType.Attribute, // nodeType
  346. name, // name
  347. false, // isEmptyElement
  348. value, // value
  349. false // clearAttributes
  350. );
  351. attributeValuePos = 0;
  352. }
  353. return match;
  354. }
  355. public override bool MoveToAttribute (string localName, string namespaceName)
  356. {
  357. MoveToElement ();
  358. if (attributes == null)
  359. return false;
  360. int idx = GetIndexOfQualifiedAttribute (localName, namespaceName);
  361. if (idx < 0)
  362. return false;
  363. MoveToAttribute (idx);
  364. return true;
  365. }
  366. public override bool MoveToElement ()
  367. {
  368. if (orderedAttributesEnumerator != null) {
  369. orderedAttributesEnumerator = null;
  370. if (isPropertySaved)
  371. RestoreProperties ();
  372. return true;
  373. }
  374. return false;
  375. }
  376. public override bool MoveToFirstAttribute ()
  377. {
  378. MoveToElement ();
  379. return MoveToNextAttribute ();
  380. }
  381. public override bool MoveToNextAttribute ()
  382. {
  383. if (attributes == null)
  384. return false;
  385. if (orderedAttributesEnumerator == null) {
  386. SaveProperties ();
  387. orderedAttributesEnumerator = orderedAttributes.GetEnumerator ();
  388. }
  389. if (orderedAttributesEnumerator.MoveNext ()) {
  390. string name = orderedAttributesEnumerator.Current as string;
  391. string value = attributes [name] as string;
  392. SetProperties (
  393. XmlNodeType.Attribute, // nodeType
  394. name, // name
  395. false, // isEmptyElement
  396. value, // value
  397. false // clearAttributes
  398. );
  399. attributeValuePos = 0;
  400. return true;
  401. }
  402. return false;
  403. }
  404. public override bool Read ()
  405. {
  406. bool more = false;
  407. isPropertySaved = false;
  408. readState = ReadState.Interactive;
  409. // It was moved from end of ReadStartTag ().
  410. if (depthUp)
  411. ++depth;
  412. depthUp = false;
  413. more = ReadContent ();
  414. return more;
  415. }
  416. public override bool ReadAttributeValue ()
  417. {
  418. // 'attributeString' holds real string value (without their
  419. // quotation characters).
  420. //
  421. // 'attributeValuePos' holds current position
  422. // of 'attributeString' while iterating ReadAttribute().
  423. // It may be:
  424. // -1 if ReadAttributeValue() has already finished.
  425. // 0 if ReadAttributeValue() ready to start reading.
  426. // >0 if ReadAttributeValue() already got 1 or more values
  427. //
  428. // local 'refPosition' holds the position on the
  429. // attributeString which may be used next time.
  430. if (attributeValuePos < 0) {
  431. // SetProperties (XmlNodeType.None,
  432. // String.Empty,
  433. // false,
  434. // String.Empty,
  435. // false);
  436. return false;
  437. }
  438. // If not started, then initialize attributeString when parsing is at start.
  439. if (attributeValuePos == 0)
  440. attributeString =
  441. value.Substring (1, value.Length - 2);
  442. returnEntityReference = false;
  443. value = String.Empty;
  444. int refPosition;
  445. int loop = 0;
  446. do {
  447. refPosition = attributeString.IndexOf ('&', attributeValuePos);
  448. if (refPosition < 0) {
  449. // Reached to the end of value string.
  450. value += attributeString.Substring (attributeValuePos);
  451. attributeValuePos = -1;
  452. break;
  453. } else if (refPosition == attributeValuePos) {
  454. string parsed = ReadAttributeValueReference ();
  455. if (parsed != null)
  456. value += parsed;
  457. else {
  458. // Found that an entity reference starts from this point.
  459. // reset position to after '&'.
  460. attributeValuePos = refPosition;
  461. if (value.Length <= 0) {
  462. int endNamePos = attributeString.IndexOf (";", attributeValuePos);
  463. value = attributeString.Substring (attributeValuePos+1, endNamePos - attributeValuePos - 1);
  464. attributeValuePos += value.Length + 2;
  465. returnEntityReference = true;
  466. }
  467. break;
  468. }
  469. } else {
  470. value += attributeString.Substring (attributeValuePos,
  471. refPosition - attributeValuePos);
  472. attributeValuePos = refPosition;
  473. continue;
  474. }
  475. } while (++loop > 0);
  476. if (returnEntityReference)
  477. SetProperties (XmlNodeType.EntityReference,
  478. value,
  479. false,
  480. String.Empty,
  481. false);
  482. else
  483. SetProperties (XmlNodeType.Text,
  484. "#text",
  485. false,
  486. value,
  487. false);
  488. return true;
  489. }
  490. [MonoTODO]
  491. public int ReadBase64 (byte [] buffer, int offset, int length)
  492. {
  493. throw new NotImplementedException ();
  494. }
  495. [MonoTODO]
  496. public int ReadBinHex (byte [] buffer, int offset, int length)
  497. {
  498. throw new NotImplementedException ();
  499. }
  500. [MonoTODO]
  501. public int ReadChars (char [] buffer, int offset, int length)
  502. {
  503. throw new NotImplementedException ();
  504. }
  505. public override string ReadInnerXml ()
  506. {
  507. if (readState != ReadState.Interactive)
  508. return String.Empty;
  509. switch (NodeType) {
  510. case XmlNodeType.Attribute:
  511. return value.Substring (1, value.Length - 2);
  512. case XmlNodeType.Element:
  513. if (IsEmptyElement)
  514. return String.Empty;
  515. int startDepth = depth;
  516. if (innerXmlBuilder == null)
  517. innerXmlBuilder = new StringBuilder ();
  518. innerXmlBuilder.Length = 0;
  519. bool loop = true;
  520. do {
  521. Read ();
  522. if (NodeType ==XmlNodeType.None)
  523. throw new XmlException ("unexpected end of xml.");
  524. else if (NodeType == XmlNodeType.EndElement && depth == startDepth) {
  525. loop = false;
  526. Read ();
  527. }
  528. else
  529. innerXmlBuilder.Append (currentTag);
  530. } while (loop);
  531. string xml = innerXmlBuilder.ToString ();
  532. innerXmlBuilder.Length = 0;
  533. return xml;
  534. case XmlNodeType.None:
  535. // MS document is incorrect. Seems not to progress.
  536. return String.Empty;
  537. default:
  538. Read ();
  539. return String.Empty;
  540. }
  541. }
  542. public override string ReadOuterXml ()
  543. {
  544. if (readState != ReadState.Interactive)
  545. return String.Empty;
  546. switch (NodeType) {
  547. case XmlNodeType.Attribute:
  548. // strictly incompatible with MS... (it holds spaces attribute between name, value and "=" char (very trivial).
  549. return String.Format ("{0}={1}{2}{1}", Name, QuoteChar, ReadInnerXml ());
  550. case XmlNodeType.Element:
  551. bool isEmpty = IsEmptyElement;
  552. string startTag = currentTag.ToString ();
  553. string name = Name;
  554. if (NodeType == XmlNodeType.Element && !isEmpty)
  555. return String.Format ("{0}{1}</{2}>", startTag, ReadInnerXml (), name);
  556. else
  557. return currentTag.ToString ();
  558. case XmlNodeType.None:
  559. // MS document is incorrect. Seems not to progress.
  560. return String.Empty;
  561. default:
  562. Read ();
  563. return String.Empty;
  564. }
  565. }
  566. public override string ReadString ()
  567. {
  568. return ReadStringInternal ();
  569. }
  570. [MonoTODO]
  571. public void ResetState ()
  572. {
  573. throw new NotImplementedException ();
  574. }
  575. public override void ResolveEntity ()
  576. {
  577. // XmlTextReaders don't resolve entities.
  578. throw new InvalidOperationException ("XmlTextReader cannot resolve external entities.");
  579. }
  580. #endregion
  581. #region Internals
  582. // Parsed DTD Objects
  583. internal DTDObjectModel currentSubset;
  584. internal void Initialize (string url, XmlParserContext context, TextReader fragment, XmlNodeType fragType)
  585. {
  586. parserContext = context;
  587. if (context == null) {
  588. XmlNameTable nt = new NameTable ();
  589. parserContext = new XmlParserContext (nt,
  590. new XmlNamespaceManager (nt),
  591. String.Empty,
  592. XmlSpace.None);
  593. }
  594. if (url != null && url != String.Empty) {
  595. string path = Path.GetFullPath (".");
  596. UriBuilder ub = new UriBuilder (path);
  597. ub.Scheme = "file";
  598. parserContext.BaseURI = new Uri (ub.Uri, url).ToString ();
  599. }
  600. Init ();
  601. switch (fragType) {
  602. case XmlNodeType.Attribute:
  603. value = fragment.ReadToEnd ();
  604. break;
  605. case XmlNodeType.Element:
  606. allowMultipleRoot = true;
  607. break;
  608. case XmlNodeType.Document:
  609. break;
  610. default:
  611. throw new XmlException (String.Format ("NodeType {0} is not allowed to create XmlTextReader.", fragType));
  612. }
  613. this.currentInput = new XmlParserInput (fragment, url);
  614. StreamReader sr = fragment as StreamReader;
  615. }
  616. #endregion
  617. #region Privates
  618. private XmlParserContext parserContext;
  619. private XmlParserInput currentInput;
  620. private Stack parserInputStack = new Stack ();
  621. private ReadState readState;
  622. private int depth;
  623. private int elementDepth;
  624. private bool depthUp;
  625. private bool popScope;
  626. private Stack elementStack;
  627. private Stack baseURIStack;
  628. private bool haveEnteredDocument;
  629. private bool allowMultipleRoot = false;
  630. private XmlNodeType nodeType;
  631. private string name;
  632. private string prefix;
  633. private string localName;
  634. private string namespaceURI;
  635. private bool isEmptyElement;
  636. private string value;
  637. private bool isPropertySaved;
  638. private XmlNodeType saveNodeType;
  639. private string saveName;
  640. private string savePrefix;
  641. private string saveLocalName;
  642. private string saveNamespaceURI;
  643. private bool saveIsEmptyElement;
  644. private Hashtable attributes;
  645. private ArrayList orderedAttributes;
  646. private IEnumerator orderedAttributesEnumerator;
  647. private bool returnEntityReference;
  648. private string entityReferenceName;
  649. private char [] nameBuffer;
  650. private int nameLength;
  651. private int nameCapacity;
  652. private const int initialNameCapacity = 256;
  653. private char [] valueBuffer;
  654. private int valueLength;
  655. private int valueCapacity;
  656. private const int initialValueCapacity = 8192;
  657. // A buffer for ReadContent for ReadOuterXml
  658. private StringBuilder currentTag {
  659. get {
  660. return currentInput.CurrentMarkup;
  661. }
  662. }
  663. private string attributeString = String.Empty;
  664. private int attributeValuePos;
  665. // This should be only referenced(used) by ReadInnerXml(). Kind of flyweight pattern.
  666. private StringBuilder innerXmlBuilder;
  667. // Parameter entity placeholder
  668. private Hashtable parameterEntities = new Hashtable ();
  669. int dtdIncludeSect;
  670. private XmlResolver resolver = new XmlUrlResolver ();
  671. private bool namespaces = true;
  672. private XmlException ReaderError (string message)
  673. {
  674. return new XmlException (message, LineNumber, LinePosition);
  675. }
  676. private void Init ()
  677. {
  678. readState = ReadState.Initial;
  679. depth = 0;
  680. depthUp = false;
  681. popScope = false;
  682. elementStack = new Stack();
  683. baseURIStack = new Stack();
  684. haveEnteredDocument = false;
  685. nodeType = XmlNodeType.None;
  686. name = String.Empty;
  687. prefix = String.Empty;
  688. localName = string.Empty;
  689. isEmptyElement = false;
  690. value = String.Empty;
  691. attributes = new Hashtable ();
  692. orderedAttributes = new ArrayList ();
  693. orderedAttributesEnumerator = null;
  694. returnEntityReference = false;
  695. entityReferenceName = String.Empty;
  696. nameBuffer = new char [initialNameCapacity];
  697. nameLength = 0;
  698. nameCapacity = initialNameCapacity;
  699. valueBuffer = new char [initialValueCapacity];
  700. valueLength = 0;
  701. valueCapacity = initialValueCapacity;
  702. }
  703. // Use this method rather than setting the properties
  704. // directly so that all the necessary properties can
  705. // be changed in harmony with each other. Maybe the
  706. // fields should be in a seperate class to help enforce
  707. // this.
  708. private void SetProperties (
  709. XmlNodeType nodeType,
  710. string name,
  711. bool isEmptyElement,
  712. string value,
  713. bool clearAttributes)
  714. {
  715. this.nodeType = nodeType;
  716. this.name = name;
  717. this.isEmptyElement = isEmptyElement;
  718. this.value = value;
  719. this.elementDepth = depth;
  720. if (clearAttributes)
  721. ClearAttributes ();
  722. if (namespaces) {
  723. int indexOfColon = name.IndexOf (':');
  724. if (indexOfColon == -1) {
  725. prefix = String.Empty;
  726. localName = name;
  727. } else {
  728. prefix = name.Substring (0, indexOfColon);
  729. localName = name.Substring (indexOfColon + 1);
  730. }
  731. } else {
  732. prefix = String.Empty;
  733. localName = name;
  734. }
  735. switch (nodeType) {
  736. case XmlNodeType.Attribute:
  737. case XmlNodeType.Element:
  738. case XmlNodeType.EndElement:
  739. namespaceURI = LookupNamespace (prefix);
  740. if (localName == "xmlns" && prefix == "")
  741. namespaceURI = "http://www.w3.org/2000/xmlns/";
  742. break;
  743. default:
  744. namespaceURI = "";
  745. break;
  746. }
  747. }
  748. private void SaveProperties ()
  749. {
  750. // If already saved, then return.
  751. if (isPropertySaved)
  752. return;
  753. saveNodeType = nodeType;
  754. saveName = name;
  755. savePrefix = prefix;
  756. saveLocalName = localName;
  757. saveNamespaceURI = namespaceURI;
  758. saveIsEmptyElement = isEmptyElement;
  759. // An element's value is always String.Empty.
  760. isPropertySaved = true;
  761. }
  762. private void RestoreProperties ()
  763. {
  764. nodeType = saveNodeType;
  765. name = saveName;
  766. prefix = savePrefix;
  767. localName = saveLocalName;
  768. namespaceURI = saveNamespaceURI;
  769. isEmptyElement = saveIsEmptyElement;
  770. value = String.Empty;
  771. isPropertySaved = false;
  772. }
  773. private void AddAttribute (string name, string value)
  774. {
  775. attributes.Add (name, value);
  776. orderedAttributes.Add (name);
  777. }
  778. private void ClearAttributes ()
  779. {
  780. if (attributes.Count > 0) {
  781. attributes.Clear ();
  782. orderedAttributes.Clear ();
  783. }
  784. orderedAttributesEnumerator = null;
  785. }
  786. private int PeekChar ()
  787. {
  788. return currentInput.PeekChar ();
  789. }
  790. private int ReadChar ()
  791. {
  792. return currentInput.ReadChar ();
  793. }
  794. // This should really keep track of some state so
  795. // that it's not possible to have more than one document
  796. // element or text outside of the document element.
  797. private bool ReadContent ()
  798. {
  799. currentTag.Length = 0;
  800. if (popScope) {
  801. parserContext.NamespaceManager.PopScope ();
  802. popScope = false;
  803. }
  804. if (returnEntityReference) {
  805. SetEntityReferenceProperties ();
  806. } else {
  807. switch (PeekChar ()) {
  808. case '<':
  809. ReadChar ();
  810. ReadTag ();
  811. break;
  812. case '\r': goto case ' ';
  813. case '\n': goto case ' ';
  814. case '\t': goto case ' ';
  815. case ' ':
  816. if (whitespaceHandling == WhitespaceHandling.All ||
  817. whitespaceHandling == WhitespaceHandling.Significant)
  818. return ReadWhitespace ();
  819. SkipWhitespace ();
  820. return ReadContent ();
  821. case -1:
  822. if (depth > 0)
  823. throw new XmlException ("unexpected end of file. Current depth is " + depth);
  824. readState = ReadState.EndOfFile;
  825. SetProperties (
  826. XmlNodeType.None, // nodeType
  827. String.Empty, // name
  828. false, // isEmptyElement
  829. String.Empty, // value
  830. true // clearAttributes
  831. );
  832. break;
  833. default:
  834. ReadText (true);
  835. break;
  836. }
  837. }
  838. return this.ReadState != ReadState.EndOfFile;
  839. }
  840. private void SetEntityReferenceProperties ()
  841. {
  842. SetProperties (
  843. XmlNodeType.EntityReference, // nodeType
  844. entityReferenceName, // name
  845. false, // isEmptyElement
  846. String.Empty, // value
  847. true // clearAttributes
  848. );
  849. returnEntityReference = false;
  850. entityReferenceName = String.Empty;
  851. }
  852. // The leading '<' has already been consumed.
  853. private void ReadTag ()
  854. {
  855. switch (PeekChar ())
  856. {
  857. case '/':
  858. ReadChar ();
  859. ReadEndTag ();
  860. break;
  861. case '?':
  862. ReadChar ();
  863. ReadProcessingInstruction ();
  864. break;
  865. case '!':
  866. ReadChar ();
  867. ReadDeclaration ();
  868. break;
  869. default:
  870. ReadStartTag ();
  871. break;
  872. }
  873. }
  874. // The leading '<' has already been consumed.
  875. private void ReadStartTag ()
  876. {
  877. parserContext.NamespaceManager.PushScope ();
  878. string name = ReadName ();
  879. if (haveEnteredDocument && elementStack.Count == 0 && !allowMultipleRoot)
  880. throw ReaderError("document has terminated, cannot open new element");
  881. haveEnteredDocument = true;
  882. bool isEmptyElement = false;
  883. ClearAttributes ();
  884. SkipWhitespace ();
  885. if (XmlConstructs.IsNameStart (PeekChar ()))
  886. ReadAttributes (false);
  887. if (PeekChar () == '/') {
  888. ReadChar ();
  889. isEmptyElement = true;
  890. popScope = true;
  891. }
  892. else {
  893. depthUp = true;
  894. elementStack.Push (name);
  895. baseURIStack.Push (attributes ["xml:base"] != null ?
  896. attributes ["xml:base"] : BaseURI);
  897. }
  898. Expect ('>');
  899. SetProperties (
  900. XmlNodeType.Element, // nodeType
  901. name, // name
  902. isEmptyElement, // isEmptyElement
  903. String.Empty, // value
  904. false // clearAttributes
  905. );
  906. }
  907. // The reader is positioned on the first character
  908. // of the element's name.
  909. private void ReadEndTag ()
  910. {
  911. string name = ReadName ();
  912. if (elementStack.Count == 0)
  913. throw ReaderError("closing element without matching opening element");
  914. string expected = (string)elementStack.Pop();
  915. if (expected != name)
  916. throw ReaderError(String.Format ("unmatched closing element: expected {0} but found {1}", expected, name));
  917. baseURIStack.Pop ();
  918. SkipWhitespace ();
  919. Expect ('>');
  920. --depth;
  921. SetProperties (
  922. XmlNodeType.EndElement, // nodeType
  923. name, // name
  924. false, // isEmptyElement
  925. String.Empty, // value
  926. true // clearAttributes
  927. );
  928. popScope = true;
  929. }
  930. private void AppendNameChar (int ch)
  931. {
  932. CheckNameCapacity ();
  933. nameBuffer [nameLength++] = (char)ch;
  934. }
  935. private void CheckNameCapacity ()
  936. {
  937. if (nameLength == nameCapacity) {
  938. nameCapacity = nameCapacity * 2;
  939. char [] oldNameBuffer = nameBuffer;
  940. nameBuffer = new char [nameCapacity];
  941. Array.Copy (oldNameBuffer, nameBuffer, nameLength);
  942. }
  943. }
  944. private string CreateNameString ()
  945. {
  946. return parserContext.NameTable.Add (nameBuffer, 0, nameLength);
  947. }
  948. private void AppendValueChar (int ch)
  949. {
  950. CheckValueCapacity ();
  951. valueBuffer [valueLength++] = (char)ch;
  952. }
  953. private void CheckValueCapacity ()
  954. {
  955. if (valueLength == valueCapacity) {
  956. valueCapacity = valueCapacity * 2;
  957. char [] oldValueBuffer = valueBuffer;
  958. valueBuffer = new char [valueCapacity];
  959. Array.Copy (oldValueBuffer, valueBuffer, valueLength);
  960. }
  961. }
  962. private string CreateValueString ()
  963. {
  964. return new String (valueBuffer, 0, valueLength);
  965. }
  966. // The reader is positioned on the first character
  967. // of the text.
  968. private void ReadText (bool cleanValue)
  969. {
  970. if (cleanValue)
  971. valueLength = 0;
  972. int ch = PeekChar ();
  973. while (ch != '<' && ch != -1) {
  974. if (ch == '&') {
  975. ReadChar ();
  976. if (ReadReference (false))
  977. break;
  978. } else
  979. AppendValueChar (ReadChar ());
  980. ch = PeekChar ();
  981. }
  982. if (returnEntityReference && valueLength == 0) {
  983. SetEntityReferenceProperties ();
  984. } else {
  985. SetProperties (
  986. XmlNodeType.Text, // nodeType
  987. String.Empty, // name
  988. false, // isEmptyElement
  989. CreateValueString (), // value
  990. true // clearAttributes
  991. );
  992. }
  993. }
  994. // The leading '&' has already been consumed.
  995. // Returns true if the entity reference isn't a simple
  996. // character reference or one of the predefined entities.
  997. // This allows the ReadText method to break so that the
  998. // next call to Read will return the EntityReference node.
  999. private bool ReadReference (bool ignoreEntityReferences)
  1000. {
  1001. if (PeekChar () == '#') {
  1002. ReadChar ();
  1003. ReadCharacterReference ();
  1004. } else
  1005. ReadEntityReference (ignoreEntityReferences);
  1006. return returnEntityReference;
  1007. }
  1008. private void ReadCharacterReference ()
  1009. {
  1010. int value = 0;
  1011. if (PeekChar () == 'x') {
  1012. ReadChar ();
  1013. while (PeekChar () != ';' && PeekChar () != -1) {
  1014. int ch = ReadChar ();
  1015. if (ch >= '0' && ch <= '9')
  1016. value = (value << 4) + ch - '0';
  1017. else if (ch >= 'A' && ch <= 'F')
  1018. value = (value << 4) + ch - 'A' + 10;
  1019. else if (ch >= 'a' && ch <= 'f')
  1020. value = (value << 4) + ch - 'a' + 10;
  1021. else
  1022. throw ReaderError (
  1023. String.Format (
  1024. "invalid hexadecimal digit: {0} (#x{1:X})",
  1025. (char)ch,
  1026. ch));
  1027. }
  1028. } else {
  1029. while (PeekChar () != ';' && PeekChar () != -1) {
  1030. int ch = ReadChar ();
  1031. if (ch >= '0' && ch <= '9')
  1032. value = value * 10 + ch - '0';
  1033. else
  1034. throw ReaderError (
  1035. String.Format (
  1036. "invalid decimal digit: {0} (#x{1:X})",
  1037. (char)ch,
  1038. ch));
  1039. }
  1040. }
  1041. ReadChar (); // ';'
  1042. AppendValueChar (value);
  1043. }
  1044. private void ReadEntityReference (bool ignoreEntityReferences)
  1045. {
  1046. nameLength = 0;
  1047. int ch = PeekChar ();
  1048. while (ch != ';' && ch != -1) {
  1049. AppendNameChar (ReadChar ());
  1050. ch = PeekChar ();
  1051. }
  1052. Expect (';');
  1053. string name = CreateNameString ();
  1054. switch (name)
  1055. {
  1056. case "lt":
  1057. AppendValueChar ('<');
  1058. break;
  1059. case "gt":
  1060. AppendValueChar ('>');
  1061. break;
  1062. case "amp":
  1063. AppendValueChar ('&');
  1064. break;
  1065. case "apos":
  1066. AppendValueChar ('\'');
  1067. break;
  1068. case "quot":
  1069. AppendValueChar ('"');
  1070. break;
  1071. default:
  1072. if (ignoreEntityReferences) {
  1073. AppendValueChar ('&');
  1074. foreach (char ch2 in name) {
  1075. AppendValueChar (ch2);
  1076. }
  1077. AppendValueChar (';');
  1078. } else {
  1079. returnEntityReference = true;
  1080. entityReferenceName = name;
  1081. }
  1082. break;
  1083. }
  1084. }
  1085. // The reader is positioned on the first character of
  1086. // the attribute name.
  1087. private void ReadAttributes (bool allowPIEnd)
  1088. {
  1089. int peekChar = -1;
  1090. bool requireWhitespace = false;
  1091. do {
  1092. if (!SkipWhitespace () && requireWhitespace)
  1093. throw new XmlException ("Unexpected token. Name is required here.");
  1094. string name = ReadName ();
  1095. SkipWhitespace ();
  1096. Expect ('=');
  1097. SkipWhitespace ();
  1098. string value = ReadAttribute ();
  1099. if (name == "xmlns")
  1100. parserContext.NamespaceManager.AddNamespace (String.Empty, UnescapeAttributeValue (value));
  1101. else if (name.StartsWith ("xmlns:"))
  1102. parserContext.NamespaceManager.AddNamespace (name.Substring (6), UnescapeAttributeValue (value));
  1103. AddAttribute (name, value);
  1104. if (XmlConstructs.IsSpace (PeekChar ()))
  1105. SkipWhitespace ();
  1106. else
  1107. requireWhitespace = true;
  1108. peekChar = PeekChar ();
  1109. if (peekChar == '?' && allowPIEnd)
  1110. break;
  1111. } while (peekChar != '/' && peekChar != '>' && peekChar != -1);
  1112. }
  1113. // The reader is positioned on the quote character.
  1114. // *Keeps quote char* to value to get_QuoteChar() correctly.
  1115. private string ReadAttribute ()
  1116. {
  1117. valueLength = 0;
  1118. int quoteChar = ReadChar ();
  1119. if (quoteChar != '\'' && quoteChar != '\"')
  1120. throw ReaderError ("an attribute value was not quoted");
  1121. AppendValueChar (quoteChar);
  1122. while (PeekChar () != quoteChar) {
  1123. int ch = ReadChar ();
  1124. switch (ch)
  1125. {
  1126. case '<':
  1127. throw ReaderError ("attribute values cannot contain '<'");
  1128. case -1:
  1129. throw ReaderError ("unexpected end of file in an attribute value");
  1130. default:
  1131. AppendValueChar (ch);
  1132. break;
  1133. }
  1134. }
  1135. ReadChar (); // quoteChar
  1136. AppendValueChar (quoteChar);
  1137. return CreateValueString ();
  1138. }
  1139. // The reader is positioned on the first character
  1140. // of the target.
  1141. //
  1142. // It may be xml declaration or processing instruction.
  1143. private void ReadProcessingInstruction ()
  1144. {
  1145. string target = ReadName ();
  1146. if (target == "xml") {
  1147. ReadXmlDeclaration ();
  1148. return;
  1149. }
  1150. SkipWhitespace ();
  1151. valueLength = 0;
  1152. while (PeekChar () != -1) {
  1153. int ch = ReadChar ();
  1154. if (ch == '?' && PeekChar () == '>') {
  1155. ReadChar ();
  1156. break;
  1157. }
  1158. AppendValueChar ((char)ch);
  1159. }
  1160. SetProperties (
  1161. XmlNodeType.ProcessingInstruction, // nodeType
  1162. target, // name
  1163. false, // isEmptyElement
  1164. CreateValueString (), // value
  1165. true // clearAttributes
  1166. );
  1167. }
  1168. // The reader is positioned after "<?xml "
  1169. private void ReadXmlDeclaration ()
  1170. {
  1171. ClearAttributes ();
  1172. ReadAttributes (true); // They must have "version."
  1173. Expect ("?>");
  1174. SetProperties (
  1175. XmlNodeType.XmlDeclaration, // nodeType
  1176. "xml", // name
  1177. false, // isEmptyElement
  1178. currentInput.CurrentMarkup.ToString (6, currentInput.CurrentMarkup.Length - 6), // value
  1179. false // clearAttributes
  1180. );
  1181. }
  1182. // The reader is positioned on the first character after
  1183. // the leading '<!'.
  1184. private void ReadDeclaration ()
  1185. {
  1186. int ch = PeekChar ();
  1187. switch (ch)
  1188. {
  1189. case '-':
  1190. Expect ("--");
  1191. ReadComment ();
  1192. break;
  1193. case '[':
  1194. ReadChar ();
  1195. Expect ("CDATA[");
  1196. ReadCDATA ();
  1197. break;
  1198. case 'D':
  1199. Expect ("DOCTYPE");
  1200. ReadDoctypeDecl ();
  1201. break;
  1202. }
  1203. }
  1204. // The reader is positioned on the first character after
  1205. // the leading '<!--'.
  1206. private void ReadComment ()
  1207. {
  1208. valueLength = 0;
  1209. while (PeekChar () != -1) {
  1210. int ch = ReadChar ();
  1211. if (ch == '-' && PeekChar () == '-') {
  1212. ReadChar ();
  1213. if (PeekChar () != '>')
  1214. throw ReaderError ("comments cannot contain '--'");
  1215. ReadChar ();
  1216. break;
  1217. }
  1218. AppendValueChar ((char)ch);
  1219. }
  1220. SetProperties (
  1221. XmlNodeType.Comment, // nodeType
  1222. String.Empty, // name
  1223. false, // isEmptyElement
  1224. CreateValueString (), // value
  1225. true // clearAttributes
  1226. );
  1227. }
  1228. // The reader is positioned on the first character after
  1229. // the leading '<![CDATA['.
  1230. private void ReadCDATA ()
  1231. {
  1232. valueLength = 0;
  1233. bool skip = false;
  1234. int ch = 0;
  1235. while (PeekChar () != -1) {
  1236. if (!skip)
  1237. ch = ReadChar ();
  1238. skip = false;
  1239. if (ch == ']' && PeekChar () == ']') {
  1240. ch = ReadChar (); // ']'
  1241. if (PeekChar () == '>') {
  1242. ReadChar (); // '>'
  1243. break;
  1244. } else {
  1245. skip = true;
  1246. // AppendValueChar (']');
  1247. // AppendValueChar (']');
  1248. // ch = ReadChar ();
  1249. }
  1250. }
  1251. AppendValueChar ((char)ch);
  1252. }
  1253. SetProperties (
  1254. XmlNodeType.CDATA, // nodeType
  1255. String.Empty, // name
  1256. false, // isEmptyElement
  1257. CreateValueString (), // value
  1258. true // clearAttributes
  1259. );
  1260. }
  1261. // The reader is positioned on the first character after
  1262. // the leading '<!DOCTYPE'.
  1263. private void ReadDoctypeDecl ()
  1264. {
  1265. string doctypeName = null;
  1266. string publicId = String.Empty;
  1267. string systemId = String.Empty;
  1268. int intSubsetStartLine = 0;
  1269. int intSubsetStartColumn = 0;
  1270. SkipWhitespace ();
  1271. doctypeName = ReadName ();
  1272. SkipWhitespace ();
  1273. switch(PeekChar ())
  1274. {
  1275. case 'S':
  1276. systemId = ReadSystemLiteral (true);
  1277. break;
  1278. case 'P':
  1279. publicId = ReadPubidLiteral ();
  1280. SkipWhitespace ();
  1281. systemId = ReadSystemLiteral (false);
  1282. break;
  1283. }
  1284. SkipWhitespace ();
  1285. if(PeekChar () == '[')
  1286. {
  1287. // read markupdecl etc. or end of decl
  1288. ReadChar ();
  1289. intSubsetStartLine = this.LineNumber;
  1290. intSubsetStartColumn = this.LinePosition;
  1291. int startPos = currentTag.Length;
  1292. // do {
  1293. ReadInternalSubset ();
  1294. // } while (nodeType != XmlNodeType.None);
  1295. int endPos = currentTag.Length - 1;
  1296. parserContext.InternalSubset = currentTag.ToString (startPos, endPos - startPos);
  1297. }
  1298. // end of DOCTYPE decl.
  1299. SkipWhitespace ();
  1300. Expect ('>');
  1301. // now compile DTD
  1302. currentSubset = new DTDObjectModel (); // merges both internal and external subsets in the meantime,
  1303. int originalParserDepth = parserInputStack.Count;
  1304. if (intSubsetStartLine > 0) {
  1305. XmlParserInput original = currentInput;
  1306. currentInput = new XmlParserInput (new StringReader (parserContext.InternalSubset), BaseURI, intSubsetStartLine, intSubsetStartColumn);
  1307. do {
  1308. CompileDTDSubset ();
  1309. if (PeekChar () == -1 && parserInputStack.Count > 0)
  1310. popParserInput ();
  1311. } while (nodeType != XmlNodeType.None || parserInputStack.Count > originalParserDepth);
  1312. if (dtdIncludeSect != 0)
  1313. this.ReaderError ("INCLUDE section is not ended correctly.");
  1314. currentInput = original;
  1315. }
  1316. if (systemId != String.Empty && resolver != null) {
  1317. pushParserInput (systemId);
  1318. do {
  1319. this.CompileDTDSubset ();
  1320. if (PeekChar () == -1 && parserInputStack.Count > 1)
  1321. popParserInput ();
  1322. } while (nodeType != XmlNodeType.None || parserInputStack.Count > originalParserDepth + 1);
  1323. popParserInput ();
  1324. }
  1325. // set properties for <!DOCTYPE> node
  1326. SetProperties (
  1327. XmlNodeType.DocumentType, // nodeType
  1328. doctypeName, // name
  1329. false, // isEmptyElement
  1330. parserContext.InternalSubset, // value
  1331. true // clearAttributes
  1332. );
  1333. }
  1334. private void pushParserInput (string url)
  1335. {
  1336. string absPath = null;
  1337. #if NetworkEnabled
  1338. try {
  1339. Uri baseUrl = new Uri (BaseURI);
  1340. absPath = resolver.ResolveUri (baseUrl, url).ToString ();
  1341. } catch (UriFormatException) {
  1342. if (Path.IsPathRooted (url))
  1343. absPath = url;
  1344. else if (BaseURI != String.Empty)
  1345. absPath = new FileInfo (BaseURI).DirectoryName + Path.DirectorySeparatorChar + url;
  1346. else
  1347. absPath = url;
  1348. }
  1349. #else
  1350. if (Path.IsPathRooted (url))
  1351. absPath = url;
  1352. else if (BaseURI != String.Empty)
  1353. absPath = new FileInfo (BaseURI).DirectoryName + Path.DirectorySeparatorChar + url;
  1354. else
  1355. absPath = url;
  1356. #endif
  1357. foreach (XmlParserInput i in parserInputStack.ToArray ()) {
  1358. if (i.BaseURI == url)
  1359. this.ReaderError ("Nested inclusion is not allowed: " + url);
  1360. }
  1361. parserInputStack.Push (currentInput);
  1362. currentInput = new XmlParserInput (new XmlStreamReader (absPath, false), absPath);
  1363. baseURIStack.Push (BaseURI);
  1364. parserContext.BaseURI = absPath;
  1365. }
  1366. private void popParserInput ()
  1367. {
  1368. currentInput = parserInputStack.Pop () as XmlParserInput;
  1369. parserContext.BaseURI = this.baseURIStack.Pop () as string;
  1370. }
  1371. private enum DtdInputState
  1372. {
  1373. Free = 1,
  1374. ElementDecl,
  1375. AttlistDecl,
  1376. EntityDecl,
  1377. NotationDecl,
  1378. PI,
  1379. Comment,
  1380. InsideSingleQuoted,
  1381. InsideDoubleQuoted,
  1382. }
  1383. private class DtdInputStateStack
  1384. {
  1385. Stack intern = new Stack ();
  1386. public DtdInputStateStack ()
  1387. {
  1388. Push (DtdInputState.Free);
  1389. }
  1390. public DtdInputState Peek ()
  1391. {
  1392. return (DtdInputState) intern.Peek ();
  1393. }
  1394. public DtdInputState Pop ()
  1395. {
  1396. return (DtdInputState) intern.Pop ();
  1397. }
  1398. public void Push (DtdInputState val)
  1399. {
  1400. intern.Push (val);
  1401. }
  1402. }
  1403. DtdInputStateStack stateStack = new DtdInputStateStack ();
  1404. DtdInputState State {
  1405. get { return stateStack.Peek (); }
  1406. }
  1407. // Simply read but not generate any result.
  1408. private void ReadInternalSubset ()
  1409. {
  1410. bool continueParse = true;
  1411. while (continueParse) {
  1412. switch (ReadChar ()) {
  1413. case ']':
  1414. switch (State) {
  1415. case DtdInputState.Free:
  1416. continueParse = false;
  1417. break;
  1418. case DtdInputState.InsideDoubleQuoted:
  1419. continue;
  1420. case DtdInputState.InsideSingleQuoted:
  1421. continue;
  1422. default:
  1423. throw ReaderError ("unexpected end of file at DTD.");
  1424. }
  1425. break;
  1426. case -1:
  1427. throw ReaderError ("unexpected end of file at DTD.");
  1428. case '<':
  1429. if (State == DtdInputState.InsideDoubleQuoted ||
  1430. State == DtdInputState.InsideSingleQuoted)
  1431. continue; // well-formed
  1432. switch (ReadChar ()) {
  1433. case '?':
  1434. stateStack.Push (DtdInputState.PI);
  1435. break;
  1436. case '!':
  1437. switch (ReadChar ()) {
  1438. case 'E':
  1439. switch (ReadChar ()) {
  1440. case 'L':
  1441. Expect ("EMENT");
  1442. stateStack.Push (DtdInputState.ElementDecl);
  1443. break;
  1444. case 'N':
  1445. Expect ("TITY");
  1446. stateStack.Push (DtdInputState.EntityDecl);
  1447. break;
  1448. default:
  1449. throw ReaderError ("unexpected token '<!E'.");
  1450. }
  1451. break;
  1452. case 'A':
  1453. Expect ("TTLIST");
  1454. stateStack.Push (DtdInputState.AttlistDecl);
  1455. break;
  1456. case 'N':
  1457. Expect ("OTATION");
  1458. stateStack.Push (DtdInputState.NotationDecl);
  1459. break;
  1460. case '-':
  1461. Expect ("-");
  1462. stateStack.Push (DtdInputState.Comment);
  1463. break;
  1464. }
  1465. break;
  1466. default:
  1467. throw ReaderError ("unexpected '>'.");
  1468. }
  1469. break;
  1470. case '\'':
  1471. if (State == DtdInputState.InsideSingleQuoted)
  1472. stateStack.Pop ();
  1473. else if (State != DtdInputState.InsideDoubleQuoted && State != DtdInputState.Comment)
  1474. stateStack.Push (DtdInputState.InsideSingleQuoted);
  1475. break;
  1476. case '"':
  1477. if (State == DtdInputState.InsideDoubleQuoted)
  1478. stateStack.Pop ();
  1479. else if (State != DtdInputState.InsideSingleQuoted && State != DtdInputState.Comment)
  1480. stateStack.Push (DtdInputState.InsideDoubleQuoted);
  1481. break;
  1482. case '>':
  1483. switch (State) {
  1484. case DtdInputState.ElementDecl:
  1485. goto case DtdInputState.NotationDecl;
  1486. case DtdInputState.AttlistDecl:
  1487. goto case DtdInputState.NotationDecl;
  1488. case DtdInputState.EntityDecl:
  1489. goto case DtdInputState.NotationDecl;
  1490. case DtdInputState.NotationDecl:
  1491. stateStack.Pop ();
  1492. break;
  1493. case DtdInputState.InsideDoubleQuoted:
  1494. continue;
  1495. case DtdInputState.InsideSingleQuoted:
  1496. continue; // well-formed
  1497. case DtdInputState.Comment:
  1498. continue;
  1499. default:
  1500. throw ReaderError ("unexpected token '>'");
  1501. }
  1502. break;
  1503. case '?':
  1504. if (State == DtdInputState.PI) {
  1505. if (ReadChar () == '>')
  1506. stateStack.Pop ();
  1507. }
  1508. break;
  1509. case '-':
  1510. if (State == DtdInputState.Comment) {
  1511. if (PeekChar () == '-') {
  1512. ReadChar ();
  1513. Expect ('>');
  1514. stateStack.Pop ();
  1515. }
  1516. }
  1517. break;
  1518. case '%':
  1519. if (State != DtdInputState.Free && State != DtdInputState.EntityDecl && State != DtdInputState.Comment && State != DtdInputState.InsideDoubleQuoted && State != DtdInputState.InsideSingleQuoted)
  1520. throw ReaderError ("Parameter Entity Reference cannot appear as a part of markupdecl (see XML spec 2.8).");
  1521. break;
  1522. }
  1523. }
  1524. }
  1525. // Read any one of following:
  1526. // elementdecl, AttlistDecl, EntityDecl, NotationDecl,
  1527. // PI, Comment, Parameter Entity, or doctype termination char(']')
  1528. //
  1529. // returns a node of some nodeType or null, setting nodeType.
  1530. // (if None then ']' was found.)
  1531. private void CompileDTDSubset()
  1532. {
  1533. SkipWhitespace ();
  1534. switch(PeekChar ())
  1535. {
  1536. case -1:
  1537. nodeType = XmlNodeType.None;
  1538. break;
  1539. case '%':
  1540. TryExpandPERef ();
  1541. break;
  1542. case '<':
  1543. ReadChar ();
  1544. switch(ReadChar ())
  1545. {
  1546. case '?':
  1547. // Only read, no store.
  1548. ReadProcessingInstruction ();
  1549. break;
  1550. case '!':
  1551. CompileDeclaration ();
  1552. break;
  1553. default:
  1554. throw ReaderError ("Syntax Error after '<' character.");
  1555. }
  1556. break;
  1557. case ']':
  1558. // End of inclusion
  1559. Expect ("]]>");
  1560. dtdIncludeSect--;
  1561. SkipWhitespace ();
  1562. break;
  1563. default:
  1564. throw ReaderError (String.Format ("Syntax Error inside doctypedecl markup : {0}({1})", PeekChar (), (char) PeekChar ()));
  1565. }
  1566. }
  1567. private void CompileDeclaration ()
  1568. {
  1569. nodeType = XmlNodeType.DocumentType; // Hack!!
  1570. switch(ReadChar ())
  1571. {
  1572. case '-':
  1573. Expect ('-');
  1574. // Only read, no store.
  1575. ReadComment ();
  1576. break;
  1577. case 'E':
  1578. switch(ReadChar ())
  1579. {
  1580. case 'N':
  1581. Expect ("TITY");
  1582. SkipWhitespace ();
  1583. LOOPBACK:
  1584. if (PeekChar () == '%') {
  1585. ReadChar ();
  1586. if (!XmlConstructs.IsSpace (PeekChar ())) {
  1587. ExpandPERef ();
  1588. goto LOOPBACK;
  1589. // throw ReaderError ("expected whitespace between '%' and name.");
  1590. } else {
  1591. SkipWhitespace ();
  1592. TryExpandPERef ();
  1593. if (XmlConstructs.IsName (PeekChar ()))
  1594. ReadParameterEntityDecl ();
  1595. else
  1596. throw ReaderError ("expected name character");
  1597. }
  1598. break;
  1599. }
  1600. DTDEntityDeclaration ent = ReadEntityDecl ();
  1601. if (currentSubset.EntityDecls [ent.Name] == null)
  1602. currentSubset.EntityDecls.Add (ent.Name, ent);
  1603. break;
  1604. case 'L':
  1605. Expect ("EMENT");
  1606. DTDElementDeclaration el = ReadElementDecl ();
  1607. currentSubset.ElementDecls.Add (el.Name, el);
  1608. break;
  1609. default:
  1610. throw ReaderError ("Syntax Error after '<!E' (ELEMENT or ENTITY must be found)");
  1611. }
  1612. break;
  1613. case 'A':
  1614. Expect ("TTLIST");
  1615. DTDAttListDeclaration atl = ReadAttListDecl ();
  1616. if (currentSubset.AttListDecls.ContainsKey (atl.Name))
  1617. currentSubset.AttListDecls.Add (atl.Name, atl);
  1618. break;
  1619. case 'N':
  1620. Expect ("OTATION");
  1621. DTDNotationDeclaration not = ReadNotationDecl ();
  1622. currentSubset.NotationDecls.Add (not.Name, not);
  1623. break;
  1624. case '[':
  1625. // conditional sections
  1626. SkipWhitespace ();
  1627. TryExpandPERef ();
  1628. SkipWhitespace ();
  1629. Expect ('I');
  1630. switch (ReadChar ()) {
  1631. case 'N':
  1632. Expect ("CLUDE");
  1633. SkipWhitespace ();
  1634. Expect ('[');
  1635. dtdIncludeSect++;
  1636. break;
  1637. case 'G':
  1638. Expect ("NORE");
  1639. ReadIgnoreSect ();
  1640. break;
  1641. }
  1642. break;
  1643. default:
  1644. throw ReaderError ("Syntax Error after '<!' characters.");
  1645. }
  1646. }
  1647. private void ReadIgnoreSect ()
  1648. {
  1649. bool skip = false;
  1650. SkipWhitespace ();
  1651. Expect ('[');
  1652. int dtdIgnoreSect = 1;
  1653. while (dtdIgnoreSect > 0) {
  1654. switch (skip ? PeekChar () : ReadChar ()) {
  1655. case -1:
  1656. throw ReaderError ("Unexpected IGNORE section end.");
  1657. case '<':
  1658. if (ReadChar () == '!' && ReadChar () == '[')
  1659. dtdIgnoreSect++;
  1660. break;
  1661. case ']':
  1662. if (ReadChar () == ']') {
  1663. if (ReadChar () == '>')
  1664. dtdIgnoreSect--;
  1665. else
  1666. skip = true;
  1667. }
  1668. break;
  1669. }
  1670. skip = false;
  1671. }
  1672. }
  1673. // The reader is positioned on the head of the name.
  1674. private DTDElementDeclaration ReadElementDecl ()
  1675. {
  1676. DTDElementDeclaration decl = new DTDElementDeclaration ();
  1677. SkipWhitespace ();
  1678. TryExpandPERef ();
  1679. decl.Name = ReadName ();
  1680. SkipWhitespace ();
  1681. TryExpandPERef ();
  1682. ReadContentSpec (decl);
  1683. SkipWhitespace ();
  1684. Expect ('>');
  1685. return decl;
  1686. }
  1687. // read 'children'(BNF) of contentspec
  1688. private void ReadContentSpec (DTDElementDeclaration decl)
  1689. {
  1690. switch(PeekChar ())
  1691. {
  1692. case 'E':
  1693. decl.IsEmpty = true;
  1694. Expect ("EMPTY");
  1695. break;
  1696. case 'A':
  1697. decl.IsAny = true;
  1698. Expect ("ANY");
  1699. break;
  1700. case '(':
  1701. DTDContentModel model = decl.ContentModel;
  1702. ReadChar ();
  1703. SkipWhitespace ();
  1704. TryExpandPERef ();
  1705. if(PeekChar () == '#') {
  1706. // Mixed Contents
  1707. decl.IsMixedContent = true;
  1708. Expect ("#PCDATA");
  1709. SkipWhitespace ();
  1710. TryExpandPERef ();
  1711. SkipWhitespace ();
  1712. while(PeekChar () != ')') {
  1713. Expect('|');
  1714. SkipWhitespace ();
  1715. TryExpandPERef ();
  1716. SkipWhitespace ();
  1717. model.ChildModels.Add (ReadName ());
  1718. SkipWhitespace ();
  1719. TryExpandPERef ();
  1720. }
  1721. Expect (')');
  1722. if(PeekChar () == '*')
  1723. ReadChar (); // ZeroOrMore
  1724. } else {
  1725. // Non-Mixed Contents
  1726. model.ChildModels.Add (ReadCP ());
  1727. SkipWhitespace ();
  1728. do { // copied from ReadCP() ...;-)
  1729. TryExpandPERef ();
  1730. SkipWhitespace ();
  1731. if(PeekChar ()=='|') {
  1732. // CPType=Or
  1733. model.OrderType = DTDContentOrderType.Or;
  1734. ReadChar ();
  1735. SkipWhitespace ();
  1736. model.ChildModels.Add (ReadCP ());
  1737. SkipWhitespace ();
  1738. }
  1739. else if(PeekChar () == ',')
  1740. {
  1741. // CPType=Seq
  1742. model.OrderType = DTDContentOrderType.Seq;
  1743. ReadChar ();
  1744. SkipWhitespace ();
  1745. model.ChildModels.Add (ReadCP ());
  1746. SkipWhitespace ();
  1747. }
  1748. else
  1749. break;
  1750. }
  1751. while(true);
  1752. Expect (')');
  1753. switch(PeekChar ())
  1754. {
  1755. case '?':
  1756. model.MinOccurs = 0;
  1757. ReadChar ();
  1758. break;
  1759. case '*':
  1760. model.MinOccurs = 0;
  1761. model.MaxOccurs = decimal.MaxValue;
  1762. ReadChar ();
  1763. break;
  1764. case '+':
  1765. model.MaxOccurs = decimal.MaxValue;
  1766. ReadChar ();
  1767. break;
  1768. }
  1769. SkipWhitespace ();
  1770. }
  1771. SkipWhitespace ();
  1772. break;
  1773. }
  1774. }
  1775. // Read 'cp' (BNF) of contentdecl (BNF)
  1776. private DTDContentModel ReadCP ()
  1777. {
  1778. DTDContentModel model = new DTDContentModel ();
  1779. TryExpandPERef ();
  1780. if(PeekChar () == '(') {
  1781. ReadChar ();
  1782. SkipWhitespace ();
  1783. model.ChildModels.Add (ReadCP ());
  1784. SkipWhitespace ();
  1785. do {
  1786. TryExpandPERef ();
  1787. SkipWhitespace ();
  1788. if(PeekChar ()=='|') {
  1789. // CPType=Or
  1790. model.OrderType = DTDContentOrderType.Or;
  1791. ReadChar ();
  1792. SkipWhitespace ();
  1793. model.ChildModels.Add (ReadCP ());
  1794. SkipWhitespace ();
  1795. }
  1796. else if(PeekChar () == ',') {
  1797. // CPType=Seq
  1798. model.OrderType = DTDContentOrderType.Seq;
  1799. ReadChar ();
  1800. SkipWhitespace ();
  1801. model.ChildModels.Add (ReadCP ());
  1802. SkipWhitespace ();
  1803. }
  1804. else
  1805. break;
  1806. }
  1807. while(true);
  1808. SkipWhitespace ();
  1809. Expect (')');
  1810. }
  1811. else {
  1812. TryExpandPERef ();
  1813. model.ElementName = ReadName ();
  1814. }
  1815. switch(PeekChar ()) {
  1816. case '?':
  1817. model.MinOccurs = 0;
  1818. ReadChar ();
  1819. break;
  1820. case '*':
  1821. model.MinOccurs = 0;
  1822. model.MaxOccurs = decimal.MaxValue;
  1823. ReadChar ();
  1824. break;
  1825. case '+':
  1826. model.MaxOccurs = decimal.MaxValue;
  1827. ReadChar ();
  1828. break;
  1829. }
  1830. return model;
  1831. }
  1832. // The reader is positioned on the first name char.
  1833. private void ReadParameterEntityDecl ()
  1834. {
  1835. DTDParameterEntityDeclaration decl =
  1836. new DTDParameterEntityDeclaration();
  1837. decl.BaseURI = BaseURI;
  1838. decl.Name = ReadName ();
  1839. SkipWhitespace ();
  1840. if (PeekChar () == 'S' || PeekChar () == 'P') {
  1841. // throw new NotImplementedException ("External parameter entity reference is not implemented yet.");
  1842. // read publicId/systemId
  1843. ReadExternalID ();
  1844. decl.PublicId = attributes ["PUBLIC"] as string;
  1845. decl.SystemId = attributes ["SYSTEM"] as string;
  1846. SkipWhitespace ();
  1847. }
  1848. else {
  1849. TryExpandPERef ();
  1850. int quoteChar = ReadChar ();
  1851. int start = currentTag.Length;
  1852. while (true) {
  1853. SkipWhitespace ();
  1854. int c = PeekChar ();
  1855. if ((int) c == -1)
  1856. throw new XmlException ("unexpected end of stream in entity value definition.");
  1857. switch (c) {
  1858. case '"':
  1859. ReadChar ();
  1860. if (quoteChar == '"') goto SKIP;
  1861. break;
  1862. case '\'':
  1863. ReadChar ();
  1864. if (quoteChar == '\'') goto SKIP;
  1865. break;
  1866. case '%':
  1867. ImportAsPERef ();
  1868. break;
  1869. default:
  1870. ReadChar ();
  1871. break;
  1872. }
  1873. }
  1874. SKIP:
  1875. decl.Value = currentTag.ToString (start, currentTag.Length - start - 1);
  1876. }
  1877. SkipWhitespace ();
  1878. Expect ('>');
  1879. if (parameterEntities [decl.Name] == null) {
  1880. parameterEntities.Add (decl.Name, decl);
  1881. }
  1882. }
  1883. // reader is positioned on '%'
  1884. private void ImportAsPERef ()
  1885. {
  1886. StringBuilder sb = null;
  1887. int peRefStart = currentTag.Length;
  1888. string appendStr = "";
  1889. ReadChar ();
  1890. string peName = ReadName ();
  1891. Expect (';');
  1892. DTDParameterEntityDeclaration peDecl =
  1893. this.parameterEntities [peName] as DTDParameterEntityDeclaration;
  1894. if (peDecl == null)
  1895. throw ReaderError ("Parameter entity " + peName + " not found.");
  1896. if (peDecl.SystemId != null) {
  1897. pushParserInput (peDecl.SystemId);
  1898. if (sb == null)
  1899. sb = new StringBuilder ();
  1900. else
  1901. sb.Length = 0;
  1902. while (PeekChar () != -1)
  1903. sb.Append (ReadChar ());
  1904. popParserInput ();
  1905. appendStr = sb.ToString ();
  1906. } else {
  1907. appendStr = peDecl.Value;
  1908. }
  1909. currentTag.Remove (peRefStart,
  1910. currentTag.Length - peRefStart);
  1911. currentTag.Append (Dereference (appendStr));
  1912. }
  1913. // The reader is positioned on the head of the name.
  1914. private DTDEntityDeclaration ReadEntityDecl ()
  1915. {
  1916. DTDEntityDeclaration decl = new DTDEntityDeclaration ();
  1917. decl.Name = ReadName ();
  1918. SkipWhitespace ();
  1919. TryExpandPERef ();
  1920. SkipWhitespace ();
  1921. if (PeekChar () == 'S' || PeekChar () == 'P') {
  1922. // external entity
  1923. ReadExternalID ();
  1924. decl.PublicId = attributes ["PUBLIC"] as string;
  1925. decl.SystemId = attributes ["SYSTEM"] as string;
  1926. SkipWhitespace ();
  1927. if (PeekChar () == 'N')
  1928. {
  1929. // NDataDecl
  1930. Expect ("NDATA");
  1931. SkipWhitespace ();
  1932. decl.NotationName = ReadName (); // ndata_name
  1933. }
  1934. }
  1935. else {
  1936. // general entity
  1937. decl.EntityValue = ReadEntityValueDecl ();
  1938. }
  1939. SkipWhitespace ();
  1940. Expect ('>');
  1941. return decl;
  1942. }
  1943. private string ReadEntityValueDecl ()
  1944. {
  1945. SkipWhitespace ();
  1946. // quotation char will be finally removed on unescaping
  1947. int quoteChar = ReadChar ();
  1948. int start = currentTag.Length;
  1949. if (quoteChar != '\'' && quoteChar != '"')
  1950. throw new XmlException ("quotation char was expected.");
  1951. while (PeekChar () != quoteChar) {
  1952. switch (PeekChar ()) {
  1953. case '%':
  1954. this.ImportAsPERef ();
  1955. continue;
  1956. case '&':
  1957. ReadChar ();
  1958. // Expect ('#');
  1959. // ReadCharacterReference ();
  1960. ReadReference (true);
  1961. break;
  1962. case -1:
  1963. throw new XmlException ("unexpected end of stream.");
  1964. default:
  1965. ReadChar ();
  1966. break;
  1967. }
  1968. }
  1969. string value = Dereference (currentTag.ToString (start, currentTag.Length - start));
  1970. Expect (quoteChar);
  1971. return value;
  1972. }
  1973. private DTDAttListDeclaration ReadAttListDecl ()
  1974. {
  1975. SkipWhitespace ();
  1976. TryExpandPERef ();
  1977. string name = ReadName (); // target element name
  1978. DTDAttListDeclaration decl =
  1979. currentSubset.AttListDecls [name] as DTDAttListDeclaration;
  1980. if (decl == null)
  1981. decl = new DTDAttListDeclaration ();
  1982. decl.Name = name;
  1983. SkipWhitespace ();
  1984. TryExpandPERef ();
  1985. SkipWhitespace ();
  1986. while (XmlConstructs.IsName ((char) PeekChar ())) {
  1987. DTDAttributeDefinition def = ReadAttributeDefinition ();
  1988. if (decl.AttributeDefinitions [def.Name] == null)
  1989. decl.AttributeDefinitions.Add (def.Name, def);
  1990. SkipWhitespace ();
  1991. TryExpandPERef ();
  1992. SkipWhitespace ();
  1993. }
  1994. SkipWhitespace ();
  1995. Expect ('>');
  1996. return decl;
  1997. }
  1998. private DTDAttributeDefinition ReadAttributeDefinition ()
  1999. {
  2000. DTDAttributeDefinition def = new DTDAttributeDefinition ();
  2001. // attr_name
  2002. TryExpandPERef ();
  2003. def.Name = ReadName ();
  2004. SkipWhitespace ();
  2005. // attr_value
  2006. TryExpandPERef ();
  2007. switch(PeekChar ()) {
  2008. case 'C': // CDATA
  2009. Expect ("CDATA");
  2010. def.AttributeType = DTDAttributeType.CData;
  2011. break;
  2012. case 'I': // ID, IDREF, IDREFS
  2013. Expect ("ID");
  2014. if(PeekChar () == 'R') {
  2015. Expect ("REF");
  2016. if(PeekChar () == 'S') {
  2017. // IDREFS
  2018. ReadChar ();
  2019. def.AttributeType = DTDAttributeType.IdRefs;
  2020. }
  2021. else // IDREF
  2022. def.AttributeType = DTDAttributeType.IdRef;
  2023. }
  2024. else // ID
  2025. def.AttributeType = DTDAttributeType.Id;
  2026. break;
  2027. case 'E': // ENTITY, ENTITIES
  2028. Expect ("ENTIT");
  2029. switch(ReadChar ()) {
  2030. case 'Y': // ENTITY
  2031. def.AttributeType = DTDAttributeType.Entity;
  2032. break;
  2033. case 'I': // ENTITIES
  2034. Expect ("ES");
  2035. def.AttributeType = DTDAttributeType.Entities;
  2036. break;
  2037. }
  2038. break;
  2039. case 'N': // NMTOKEN, NMTOKENS, NOTATION
  2040. ReadChar ();
  2041. switch(PeekChar ()) {
  2042. case 'M':
  2043. Expect ("MTOKEN");
  2044. if(PeekChar ()=='S') { // NMTOKENS
  2045. ReadChar ();
  2046. def.AttributeType = DTDAttributeType.NmTokens;
  2047. }
  2048. else // NMTOKEN
  2049. def.AttributeType = DTDAttributeType.NmToken;
  2050. break;
  2051. case 'O':
  2052. Expect ("OTATION");
  2053. def.AttributeType = DTDAttributeType.Notation;
  2054. SkipWhitespace ();
  2055. Expect ('(');
  2056. SkipWhitespace ();
  2057. def.EnumeratedNotations.Add (ReadName ()); // notation name
  2058. SkipWhitespace ();
  2059. while(PeekChar () == '|') {
  2060. ReadChar ();
  2061. SkipWhitespace ();
  2062. def.EnumeratedNotations.Add (ReadName ()); // notation name
  2063. SkipWhitespace ();
  2064. }
  2065. Expect (')');
  2066. break;
  2067. default:
  2068. throw new XmlException ("attribute declaration syntax error.");
  2069. }
  2070. break;
  2071. default: // Enumerated Values
  2072. TryExpandPERef ();
  2073. Expect ('(');
  2074. SkipWhitespace ();
  2075. def.EnumeratedAttributeDeclaration.Add (ReadNmToken ()); // enum value
  2076. SkipWhitespace ();
  2077. while(PeekChar () == '|') {
  2078. ReadChar ();
  2079. SkipWhitespace ();
  2080. def.EnumeratedAttributeDeclaration.Add (ReadNmToken ()); // enum value
  2081. SkipWhitespace ();
  2082. }
  2083. Expect (')');
  2084. break;
  2085. }
  2086. SkipWhitespace ();
  2087. TryExpandPERef ();
  2088. // def_value
  2089. if(PeekChar () == '#')
  2090. {
  2091. ReadChar ();
  2092. switch(PeekChar ())
  2093. {
  2094. case 'R':
  2095. Expect ("REQUIRED");
  2096. def.OccurenceType = DTDAttributeOccurenceType.Required;
  2097. break;
  2098. case 'I':
  2099. Expect ("IMPLIED");
  2100. def.OccurenceType = DTDAttributeOccurenceType.Optional;
  2101. break;
  2102. case 'F':
  2103. Expect ("FIXED");
  2104. def.OccurenceType = DTDAttributeOccurenceType.Fixed;
  2105. SkipWhitespace ();
  2106. def.UnresolvedDefaultValue = ReadAttribute ();
  2107. break;
  2108. }
  2109. } else {
  2110. // one of the enumerated value
  2111. if (PeekChar () == -1) {
  2112. popParserInput ();
  2113. }
  2114. SkipWhitespace ();
  2115. def.UnresolvedDefaultValue = ReadAttribute ();
  2116. }
  2117. return def;
  2118. }
  2119. private DTDNotationDeclaration ReadNotationDecl()
  2120. {
  2121. DTDNotationDeclaration decl = new DTDNotationDeclaration ();
  2122. SkipWhitespace ();
  2123. decl.Name = ReadName (); // notation name
  2124. if (namespaces) { // copy from SetProperties ;-)
  2125. int indexOfColon = decl.Name.IndexOf (':');
  2126. if (indexOfColon == -1) {
  2127. decl.Prefix = String.Empty;
  2128. decl.LocalName = decl.Name;
  2129. } else {
  2130. decl.Prefix = decl.Name.Substring (0, indexOfColon);
  2131. decl.LocalName = decl.Name.Substring (indexOfColon + 1);
  2132. }
  2133. } else {
  2134. decl.Prefix = String.Empty;
  2135. decl.LocalName = decl.Name;
  2136. }
  2137. SkipWhitespace ();
  2138. if(PeekChar () == 'P') {
  2139. decl.PublicId = ReadPubidLiteral ();
  2140. SkipWhitespace ();
  2141. if (PeekChar () == '\'' || PeekChar () == '"') {
  2142. decl.SystemId = ReadSystemLiteral (false);
  2143. SkipWhitespace ();
  2144. }
  2145. } else if(PeekChar () == 'S') {
  2146. decl.SystemId = ReadSystemLiteral (true);
  2147. SkipWhitespace ();
  2148. }
  2149. if(decl.PublicId == null && decl.SystemId == null)
  2150. throw new XmlException ("public or system declaration required for \"NOTATION\" declaration.");
  2151. Expect ('>');
  2152. return decl;
  2153. }
  2154. private void TryExpandPERef ()
  2155. {
  2156. if (PeekChar () == '%') {
  2157. ReadChar ();
  2158. if (!XmlConstructs.IsName (PeekChar ()))
  2159. return;
  2160. ExpandPERef ();
  2161. }
  2162. }
  2163. // reader is positioned on the first letter of the name.
  2164. private void ExpandPERef ()
  2165. {
  2166. ExpandPERef (true);
  2167. }
  2168. private void ExpandPERef (bool attachSpace)
  2169. {
  2170. string peName = ReadName ();
  2171. Expect (";");
  2172. ExpandNamedPERef (peName, attachSpace);
  2173. }
  2174. private void ExpandNamedPERef (string peName, bool attachSpace)
  2175. {
  2176. DTDParameterEntityDeclaration decl =
  2177. parameterEntities [peName] as DTDParameterEntityDeclaration;
  2178. if (decl == null)
  2179. throw new XmlException ("undeclared parameter entity: '" + peName + "'");
  2180. if (decl.SystemId != null) {
  2181. pushParserInput (decl.SystemId);
  2182. }
  2183. // add buffer
  2184. else
  2185. currentInput.InsertParameterEntityBuffer (attachSpace ? " " + Dereference (decl.Value) + " " : decl.Value);
  2186. SkipWhitespace (); // is it ok?
  2187. // while (PeekChar () == '%')
  2188. // TryExpandPERef (); // recursive
  2189. }
  2190. private void ReadExternalID() {
  2191. switch(PeekChar ()) {
  2192. case 'S':
  2193. attributes ["PUBLIC"] = null;
  2194. attributes ["SYSTEM"] = ReadSystemLiteral (true);
  2195. break;
  2196. case 'P':
  2197. attributes ["PUBLIC"] = ReadPubidLiteral ();
  2198. SkipWhitespace ();
  2199. attributes ["SYSTEM"] = ReadSystemLiteral (false);
  2200. break;
  2201. }
  2202. }
  2203. // The reader is positioned on the first 'S' of "SYSTEM".
  2204. private string ReadSystemLiteral (bool expectSYSTEM)
  2205. {
  2206. if(expectSYSTEM)
  2207. Expect ("SYSTEM");
  2208. SkipWhitespace ();
  2209. int quoteChar = ReadChar (); // apos or quot
  2210. int startPos = currentTag.Length;
  2211. int c = 0;
  2212. while(c != quoteChar) {
  2213. c = ReadChar ();
  2214. if(c < 0) throw ReaderError ("Unexpected end of stream in ExternalID.");
  2215. }
  2216. return currentTag.ToString (startPos, currentTag.Length - 1 - startPos);
  2217. }
  2218. private string ReadPubidLiteral()
  2219. {
  2220. Expect ("PUBLIC");
  2221. SkipWhitespace ();
  2222. int quoteChar = ReadChar ();
  2223. int startPos = currentTag.Length;
  2224. int c = 0;
  2225. while(c != quoteChar)
  2226. {
  2227. c = ReadChar ();
  2228. if(c < 0) throw ReaderError ("Unexpected end of stream in ExternalID.");
  2229. if(c != quoteChar && !XmlConstructs.IsPubid (c))
  2230. throw ReaderError("character '" + (char)c + "' not allowed for PUBLIC ID");
  2231. }
  2232. return currentTag.ToString (startPos, currentTag.Length - 1 - startPos);
  2233. }
  2234. // The reader is positioned on the first character
  2235. // of the name.
  2236. internal string ReadName ()
  2237. {
  2238. return ReadNameOrNmToken(false);
  2239. }
  2240. // The reader is positioned on the first character
  2241. // of the name.
  2242. private string ReadNmToken ()
  2243. {
  2244. return ReadNameOrNmToken(true);
  2245. }
  2246. private string ReadNameOrNmToken(bool isNameToken)
  2247. {
  2248. int ch = PeekChar ();
  2249. if(isNameToken) {
  2250. if (!XmlConstructs.IsName ((char) ch))
  2251. throw ReaderError (String.Format ("a name did not start with a legal character {0} ({1})", ch, (char)ch));
  2252. }
  2253. else {
  2254. if (!XmlConstructs.IsNameStart ((char) PeekChar ()))
  2255. throw ReaderError (String.Format ("a name did not start with a legal character {0} ({1})", ch, (char)ch));
  2256. }
  2257. nameLength = 0;
  2258. AppendNameChar (ReadChar ());
  2259. while (XmlConstructs.IsName (PeekChar ())) {
  2260. AppendNameChar (ReadChar ());
  2261. }
  2262. return CreateNameString ();
  2263. }
  2264. // Read the next character and compare it against the
  2265. // specified character.
  2266. private void Expect (int expected)
  2267. {
  2268. int ch = ReadChar ();
  2269. if (ch != expected) {
  2270. throw ReaderError (
  2271. String.Format (
  2272. "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
  2273. (char)expected,
  2274. expected,
  2275. (char)ch,
  2276. ch));
  2277. }
  2278. }
  2279. private void Expect (string expected)
  2280. {
  2281. int len = expected.Length;
  2282. for(int i=0; i< len; i++)
  2283. Expect (expected[i]);
  2284. }
  2285. // Does not consume the first non-whitespace character.
  2286. private bool SkipWhitespace ()
  2287. {
  2288. //FIXME: Should not skip if whitespaceHandling == WhiteSpaceHandling.None
  2289. bool skipped = XmlConstructs.IsSpace (PeekChar ());
  2290. while (XmlConstructs.IsSpace (PeekChar ()))
  2291. ReadChar ();
  2292. return skipped;
  2293. }
  2294. private bool ReadWhitespace ()
  2295. {
  2296. valueLength = 0;
  2297. int ch = PeekChar ();
  2298. do {
  2299. AppendValueChar (ReadChar ());
  2300. } while ((ch = PeekChar ()) != -1 && XmlConstructs.IsSpace (ch));
  2301. if (ch != -1 && ch != '<')
  2302. ReadText (false);
  2303. else
  2304. SetProperties (XmlNodeType.Whitespace,
  2305. String.Empty,
  2306. false,
  2307. CreateValueString (),
  2308. true);
  2309. return (PeekChar () != -1);
  2310. }
  2311. // read entity reference from attribute string and if parsable then return the value.
  2312. private string ReadAttributeValueReference ()
  2313. {
  2314. int endEntityPosition = attributeString.IndexOf(';',
  2315. attributeValuePos);
  2316. string entityName = attributeString.Substring (attributeValuePos + 1,
  2317. endEntityPosition - attributeValuePos - 1);
  2318. attributeValuePos = endEntityPosition + 1;
  2319. if(entityName [0] == '#') {
  2320. char c;
  2321. // character entity
  2322. if(entityName [1] == 'x') {
  2323. // hexadecimal
  2324. c = (char) int.Parse ("0" + entityName.Substring (2),
  2325. System.Globalization.NumberStyles.HexNumber);
  2326. } else {
  2327. // decimal
  2328. c = (char) int.Parse (entityName.Substring (1));
  2329. }
  2330. return c.ToString();
  2331. }
  2332. else {
  2333. switch(entityName)
  2334. {
  2335. case "lt": return "<";
  2336. case "gt": return ">";
  2337. case "amp": return "&";
  2338. case "quot": return "\"";
  2339. case "apos": return "'";
  2340. default: return null;
  2341. }
  2342. }
  2343. }
  2344. private string UnescapeAttributeValue (string unresolved)
  2345. {
  2346. if(unresolved == null) return null;
  2347. // trim start/end edge of quotation character.
  2348. return Dereference (unresolved.Substring (1, unresolved.Length - 2));
  2349. }
  2350. private string Dereference (string unresolved)
  2351. {
  2352. StringBuilder resolved = new StringBuilder();
  2353. int pos = 0;
  2354. int next = unresolved.IndexOf ('&');
  2355. if(next < 0)
  2356. return unresolved;
  2357. while(next >= 0) {
  2358. if(pos < next)
  2359. resolved.Append (unresolved.Substring (pos, next - pos));// - 1);
  2360. int endPos = unresolved.IndexOf (';', next+1);
  2361. string entityName =
  2362. unresolved.Substring (next + 1, endPos - next - 1);
  2363. if(entityName [0] == '#') {
  2364. char c;
  2365. // character entity
  2366. if(entityName [1] == 'x') {
  2367. // hexadecimal
  2368. c = (char) int.Parse ("0" + entityName.Substring (2),
  2369. System.Globalization.NumberStyles.HexNumber);
  2370. } else {
  2371. // decimal
  2372. c = (char) int.Parse (entityName.Substring (1));
  2373. }
  2374. resolved.Append (c);
  2375. } else {
  2376. switch(entityName) {
  2377. case "lt": resolved.Append ("<"); break;
  2378. case "gt": resolved.Append (">"); break;
  2379. case "amp": resolved.Append ("&"); break;
  2380. case "quot": resolved.Append ("\""); break;
  2381. case "apos": resolved.Append ("'"); break;
  2382. // With respect to "Value", MS document is helpless
  2383. // and the implemention returns inconsistent value
  2384. // (e.g. XML: "&ent; &amp;ent;" ---> Value: "&ent; &ent;".)
  2385. default: resolved.Append ("&" + entityName + ";"); break;
  2386. }
  2387. }
  2388. pos = endPos + 1;
  2389. if(pos > unresolved.Length)
  2390. break;
  2391. next = unresolved.IndexOf('&', pos);
  2392. }
  2393. resolved.Append (unresolved.Substring(pos));
  2394. return resolved.ToString();
  2395. }
  2396. #endregion
  2397. }
  2398. }