XmlTextReader.cs 64 KB

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