XmlTextReader.cs 78 KB

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