XmlTextReader.cs 65 KB

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