XmlTextReader.cs 68 KB

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