XmlTextReader.cs 65 KB

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