XmlTextReader.cs 65 KB

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