DTDReader.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. //
  2. // System.Xml.DTDReader
  3. //
  4. // Author:
  5. // Atsushi Enomoto ([email protected])
  6. //
  7. // (C)2003 Atsushi Enomoto
  8. // (C)2004 Novell Inc.
  9. //
  10. // FIXME:
  11. // When a parameter entity contains cp section, it should be closed
  12. // within that declaration.
  13. //
  14. // Resolution to external entities from different BaseURI fails (it is
  15. // the same as MS.NET 1.1, but should be fixed in the future).
  16. //
  17. using System;
  18. using System.Collections;
  19. using System.Globalization;
  20. using System.IO;
  21. using System.Text;
  22. using Mono.Xml;
  23. using System.Xml.Schema;
  24. using Mono.Xml.Native;
  25. namespace System.Xml
  26. {
  27. internal class DTDReader : IXmlLineInfo
  28. {
  29. private XmlParserInput currentInput;
  30. private Stack parserInputStack;
  31. private string entityReferenceName;
  32. private char [] nameBuffer;
  33. private int nameLength;
  34. private int nameCapacity;
  35. private const int initialNameCapacity = 256;
  36. private StringBuilder valueBuffer;
  37. private int currentLinkedNodeLineNumber;
  38. private int currentLinkedNodeLinePosition;
  39. // Parameter entity placeholder
  40. private int dtdIncludeSect;
  41. private bool normalization;
  42. private bool processingInternalSubset;
  43. string cachedPublicId;
  44. string cachedSystemId;
  45. DTDObjectModel DTD;
  46. #if DTD_HANDLE_EVENTS
  47. public event ValidationEventHandler ValidationEventHandler;
  48. #endif
  49. // .ctor()
  50. public DTDReader (DTDObjectModel dtd,
  51. int startLineNumber,
  52. int startLinePosition)
  53. {
  54. this.DTD = dtd;
  55. currentLinkedNodeLineNumber = startLineNumber;
  56. currentLinkedNodeLinePosition = startLinePosition;
  57. Init ();
  58. }
  59. // Properties
  60. public string BaseURI {
  61. get { return currentInput.BaseURI; }
  62. }
  63. public bool Normalization {
  64. get { return normalization; }
  65. set { normalization = value; }
  66. }
  67. public int LineNumber {
  68. get { return currentInput.LineNumber; }
  69. }
  70. public int LinePosition {
  71. get { return currentInput.LinePosition; }
  72. }
  73. public bool HasLineInfo ()
  74. {
  75. return true;
  76. }
  77. // Methods
  78. private void Init ()
  79. {
  80. parserInputStack = new Stack ();
  81. entityReferenceName = String.Empty;
  82. nameBuffer = new char [initialNameCapacity];
  83. nameLength = 0;
  84. nameCapacity = initialNameCapacity;
  85. valueBuffer = new StringBuilder (512);
  86. }
  87. internal DTDObjectModel GenerateDTDObjectModel ()
  88. {
  89. // now compile DTD
  90. int originalParserDepth = parserInputStack.Count;
  91. bool more;
  92. if (DTD.InternalSubset != null && DTD.InternalSubset.Length > 0) {
  93. this.processingInternalSubset = true;
  94. XmlParserInput original = currentInput;
  95. currentInput = new XmlParserInput (
  96. new StringReader (DTD.InternalSubset),
  97. DTD.BaseURI,
  98. currentLinkedNodeLineNumber,
  99. currentLinkedNodeLinePosition);
  100. currentInput.InitialState = false;
  101. do {
  102. more = ProcessDTDSubset ();
  103. if (PeekChar () == -1 && parserInputStack.Count > 0)
  104. PopParserInput ();
  105. } while (more || parserInputStack.Count > originalParserDepth);
  106. if (dtdIncludeSect != 0)
  107. throw new XmlException (this as IXmlLineInfo,"INCLUDE section is not ended correctly.");
  108. currentInput = original;
  109. this.processingInternalSubset = false;
  110. }
  111. if (DTD.SystemId != null && DTD.SystemId != String.Empty && DTD.Resolver != null) {
  112. PushParserInput (DTD.SystemId);
  113. do {
  114. more = ProcessDTDSubset ();
  115. if (PeekChar () == -1 && parserInputStack.Count > 1)
  116. PopParserInput ();
  117. } while (more || parserInputStack.Count > originalParserDepth + 1);
  118. if (dtdIncludeSect != 0)
  119. throw new XmlException (this as IXmlLineInfo,"INCLUDE section is not ended correctly.");
  120. PopParserInput ();
  121. }
  122. ArrayList sc = new ArrayList ();
  123. // Entity recursion check.
  124. foreach (DTDEntityDeclaration ent in DTD.EntityDecls.Values) {
  125. if (ent.NotationName != null) {
  126. ent.ScanEntityValue (sc);
  127. sc.Clear ();
  128. }
  129. }
  130. // release unnecessary memory usage
  131. DTD.ExternalResources.Clear ();
  132. return DTD;
  133. }
  134. // Read any one of following:
  135. // elementdecl, AttlistDecl, EntityDecl, NotationDecl,
  136. // PI, Comment, Parameter Entity, or doctype termination char(']')
  137. //
  138. // Returns true if it may have any more contents, or false if not.
  139. private bool ProcessDTDSubset ()
  140. {
  141. SkipWhitespace ();
  142. int c2 = ReadChar ();
  143. switch(c2)
  144. {
  145. case -1:
  146. return false;
  147. case '%':
  148. // It affects on entity references' well-formedness
  149. if (this.processingInternalSubset)
  150. DTD.InternalSubsetHasPEReference = true;
  151. string peName = ReadName ();
  152. Expect (';');
  153. string peValue = GetPEValue (peName);
  154. if (peValue == String.Empty)
  155. break;
  156. currentInput.InsertParameterEntityBuffer (peValue);
  157. // int currentLine = currentInput.LineNumber;
  158. // int currentColumn = currentInput.LinePosition;
  159. while (currentInput.HasPEBuffer)
  160. ProcessDTDSubset ();
  161. SkipWhitespace ();
  162. // FIXME: Implement correct nest-level check.
  163. // Don't depend on lineinfo (might not be supplied)
  164. // if (currentInput.LineNumber != currentLine ||
  165. // currentInput.LinePosition != currentColumn)
  166. // throw new XmlException (this as IXmlLineInfo,
  167. // "Incorrectly nested parameter entity.");
  168. break;
  169. case '<':
  170. int c = ReadChar ();
  171. switch(c)
  172. {
  173. case '?':
  174. // Only read, no store.
  175. ReadProcessingInstruction ();
  176. break;
  177. case '!':
  178. CompileDeclaration ();
  179. break;
  180. case -1:
  181. throw new XmlException (this as IXmlLineInfo, "Unexpected end of stream.");
  182. default:
  183. throw new XmlException (this as IXmlLineInfo, "Syntax Error after '<' character: " + (char) c);
  184. }
  185. break;
  186. case ']':
  187. if (dtdIncludeSect == 0)
  188. throw new XmlException (this as IXmlLineInfo, "Unbalanced end of INCLUDE/IGNORE section.");
  189. // End of inclusion
  190. Expect ("]>");
  191. dtdIncludeSect--;
  192. SkipWhitespace ();
  193. break;
  194. default:
  195. throw new XmlException (this as IXmlLineInfo,String.Format ("Syntax Error inside doctypedecl markup : {0}({1})", c2, (char) c2));
  196. }
  197. currentInput.InitialState = false;
  198. return true;
  199. }
  200. private void CompileDeclaration ()
  201. {
  202. switch(ReadChar ())
  203. {
  204. case '-':
  205. Expect ('-');
  206. // Only read, no store.
  207. ReadComment ();
  208. break;
  209. case 'E':
  210. switch(ReadChar ())
  211. {
  212. case 'N':
  213. Expect ("TITY");
  214. if (!SkipWhitespace ())
  215. throw new XmlException (this as IXmlLineInfo,
  216. "Whitespace is required after '<!ENTITY' in DTD entity declaration.");
  217. LOOPBACK:
  218. if (PeekChar () == '%') {
  219. ReadChar ();
  220. if (!SkipWhitespace ()) {
  221. ExpandPERef ();
  222. goto LOOPBACK;
  223. } else {
  224. TryExpandPERef ();
  225. SkipWhitespace ();
  226. if (XmlChar.IsNameChar (PeekChar ()))
  227. ReadParameterEntityDecl ();
  228. else
  229. throw new XmlException (this as IXmlLineInfo,"expected name character");
  230. }
  231. break;
  232. }
  233. DTDEntityDeclaration ent = ReadEntityDecl ();
  234. if (DTD.EntityDecls [ent.Name] == null)
  235. DTD.EntityDecls.Add (ent.Name, ent);
  236. break;
  237. case 'L':
  238. Expect ("EMENT");
  239. DTDElementDeclaration el = ReadElementDecl ();
  240. DTD.ElementDecls.Add (el.Name, el);
  241. break;
  242. default:
  243. throw new XmlException (this as IXmlLineInfo,"Syntax Error after '<!E' (ELEMENT or ENTITY must be found)");
  244. }
  245. break;
  246. case 'A':
  247. Expect ("TTLIST");
  248. DTDAttListDeclaration atl = ReadAttListDecl ();
  249. DTD.AttListDecls.Add (atl.Name, atl);
  250. break;
  251. case 'N':
  252. Expect ("OTATION");
  253. DTDNotationDeclaration not = ReadNotationDecl ();
  254. DTD.NotationDecls.Add (not.Name, not);
  255. break;
  256. case '[':
  257. // conditional sections
  258. SkipWhitespace ();
  259. TryExpandPERef ();
  260. ExpectAfterWhitespace ('I');
  261. switch (ReadChar ()) {
  262. case 'N':
  263. Expect ("CLUDE");
  264. ExpectAfterWhitespace ('[');
  265. dtdIncludeSect++;
  266. break;
  267. case 'G':
  268. Expect ("NORE");
  269. ReadIgnoreSect ();
  270. break;
  271. }
  272. break;
  273. default:
  274. throw new XmlException (this as IXmlLineInfo,"Syntax Error after '<!' characters.");
  275. }
  276. }
  277. private void ReadIgnoreSect ()
  278. {
  279. ExpectAfterWhitespace ('[');
  280. int dtdIgnoreSect = 1;
  281. while (dtdIgnoreSect > 0) {
  282. switch (ReadChar ()) {
  283. case -1:
  284. throw new XmlException (this as IXmlLineInfo,"Unexpected IGNORE section end.");
  285. case '<':
  286. if (PeekChar () != '!')
  287. break;
  288. ReadChar ();
  289. if (PeekChar () != '[')
  290. break;
  291. ReadChar ();
  292. dtdIgnoreSect++;
  293. break;
  294. case ']':
  295. if (PeekChar () != ']')
  296. break;
  297. ReadChar ();
  298. if (PeekChar () != '>')
  299. break;
  300. ReadChar ();
  301. dtdIgnoreSect--;
  302. break;
  303. }
  304. }
  305. if (dtdIgnoreSect != 0)
  306. throw new XmlException (this as IXmlLineInfo,"IGNORE section is not ended correctly.");
  307. }
  308. // The reader is positioned on the head of the name.
  309. private DTDElementDeclaration ReadElementDecl ()
  310. {
  311. DTDElementDeclaration decl = new DTDElementDeclaration (DTD);
  312. decl.IsInternalSubset = this.processingInternalSubset;
  313. if (!SkipWhitespace ())
  314. throw new XmlException (this as IXmlLineInfo,
  315. "Whitespace is required between '<!ELEMENT' and name in DTD element declaration.");
  316. TryExpandPERef ();
  317. SkipWhitespace ();
  318. decl.Name = ReadName ();
  319. if (!SkipWhitespace ())
  320. throw new XmlException (this as IXmlLineInfo,
  321. "Whitespace is required between name and content in DTD element declaration.");
  322. TryExpandPERef ();
  323. ReadContentSpec (decl);
  324. SkipWhitespace ();
  325. // This expanding is only allowed as a non-validating parser.
  326. TryExpandPERef ();
  327. ExpectAfterWhitespace ('>');
  328. return decl;
  329. }
  330. // read 'children'(BNF) of contentspec
  331. private void ReadContentSpec (DTDElementDeclaration decl)
  332. {
  333. TryExpandPERef ();
  334. SkipWhitespace ();
  335. switch(ReadChar ())
  336. {
  337. case 'E':
  338. decl.IsEmpty = true;
  339. Expect ("MPTY");
  340. break;
  341. case 'A':
  342. decl.IsAny = true;
  343. Expect ("NY");
  344. break;
  345. case '(':
  346. DTDContentModel model = decl.ContentModel;
  347. SkipWhitespace ();
  348. TryExpandPERef ();
  349. SkipWhitespace ();
  350. if(PeekChar () == '#') {
  351. // Mixed Contents. "#PCDATA" must appear first.
  352. decl.IsMixedContent = true;
  353. model.Occurence = DTDOccurence.ZeroOrMore;
  354. model.OrderType = DTDContentOrderType.Or;
  355. Expect ("#PCDATA");
  356. SkipWhitespace ();
  357. TryExpandPERef ();
  358. SkipWhitespace ();
  359. while(PeekChar () != ')') {
  360. SkipWhitespace ();
  361. if (PeekChar () == '%') {
  362. TryExpandPERef ();
  363. SkipWhitespace ();
  364. continue;
  365. }
  366. Expect('|');
  367. SkipWhitespace ();
  368. TryExpandPERef ();
  369. SkipWhitespace ();
  370. DTDContentModel elem = new DTDContentModel (DTD, decl.Name);
  371. // elem.LineNumber = currentInput.LineNumber;
  372. // elem.LinePosition = currentInput.LinePosition;
  373. elem.ElementName = ReadName ();
  374. this.AddContentModel (model.ChildModels, elem);
  375. SkipWhitespace ();
  376. TryExpandPERef ();
  377. SkipWhitespace ();
  378. }
  379. Expect (')');
  380. if (model.ChildModels.Count > 0)
  381. Expect ('*');
  382. else if (PeekChar () == '*')
  383. Expect ('*');
  384. } else {
  385. // Non-Mixed Contents
  386. model.ChildModels.Add (ReadCP (decl));
  387. SkipWhitespace ();
  388. do { // copied from ReadCP() ...;-)
  389. if (PeekChar () == '%') {
  390. TryExpandPERef ();
  391. SkipWhitespace ();
  392. continue;
  393. }
  394. if(PeekChar ()=='|') {
  395. // CPType=Or
  396. if (model.OrderType == DTDContentOrderType.Seq)
  397. throw new XmlException (this as IXmlLineInfo,
  398. "Inconsistent choice markup in sequence cp.");
  399. model.OrderType = DTDContentOrderType.Or;
  400. ReadChar ();
  401. SkipWhitespace ();
  402. AddContentModel (model.ChildModels, ReadCP (decl));
  403. SkipWhitespace ();
  404. }
  405. else if(PeekChar () == ',')
  406. {
  407. // CPType=Seq
  408. if (model.OrderType == DTDContentOrderType.Or)
  409. throw new XmlException (this as IXmlLineInfo,
  410. "Inconsistent sequence markup in choice cp.");
  411. model.OrderType = DTDContentOrderType.Seq;
  412. ReadChar ();
  413. SkipWhitespace ();
  414. model.ChildModels.Add (ReadCP (decl));
  415. SkipWhitespace ();
  416. }
  417. else
  418. break;
  419. }
  420. while(true);
  421. Expect (')');
  422. switch(PeekChar ())
  423. {
  424. case '?':
  425. model.Occurence = DTDOccurence.Optional;
  426. ReadChar ();
  427. break;
  428. case '*':
  429. model.Occurence = DTDOccurence.ZeroOrMore;
  430. ReadChar ();
  431. break;
  432. case '+':
  433. model.Occurence = DTDOccurence.OneOrMore;
  434. ReadChar ();
  435. break;
  436. }
  437. SkipWhitespace ();
  438. }
  439. SkipWhitespace ();
  440. break;
  441. default:
  442. throw new XmlException (this as IXmlLineInfo, "ContentSpec is missing.");
  443. }
  444. }
  445. // Read 'cp' (BNF) of contentdecl (BNF)
  446. private DTDContentModel ReadCP (DTDElementDeclaration elem)
  447. {
  448. DTDContentModel model = null;
  449. TryExpandPERef ();
  450. SkipWhitespace ();
  451. if(PeekChar () == '(') {
  452. model = new DTDContentModel (DTD, elem.Name);
  453. ReadChar ();
  454. SkipWhitespace ();
  455. model.ChildModels.Add (ReadCP (elem));
  456. SkipWhitespace ();
  457. do {
  458. if (PeekChar () == '%') {
  459. TryExpandPERef ();
  460. SkipWhitespace ();
  461. continue;
  462. }
  463. if(PeekChar ()=='|') {
  464. // CPType=Or
  465. if (model.OrderType == DTDContentOrderType.Seq)
  466. throw new XmlException (this as IXmlLineInfo,
  467. "Inconsistent choice markup in sequence cp.");
  468. model.OrderType = DTDContentOrderType.Or;
  469. ReadChar ();
  470. SkipWhitespace ();
  471. AddContentModel (model.ChildModels, ReadCP (elem));
  472. SkipWhitespace ();
  473. }
  474. else if(PeekChar () == ',') {
  475. // CPType=Seq
  476. if (model.OrderType == DTDContentOrderType.Or)
  477. throw new XmlException (this as IXmlLineInfo,
  478. "Inconsistent sequence markup in choice cp.");
  479. model.OrderType = DTDContentOrderType.Seq;
  480. ReadChar ();
  481. SkipWhitespace ();
  482. model.ChildModels.Add (ReadCP (elem));
  483. SkipWhitespace ();
  484. }
  485. else
  486. break;
  487. }
  488. while(true);
  489. ExpectAfterWhitespace (')');
  490. }
  491. else {
  492. TryExpandPERef ();
  493. model = new DTDContentModel (DTD, elem.Name);
  494. SkipWhitespace ();
  495. model.ElementName = ReadName ();
  496. }
  497. switch(PeekChar ()) {
  498. case '?':
  499. model.Occurence = DTDOccurence.Optional;
  500. ReadChar ();
  501. break;
  502. case '*':
  503. model.Occurence = DTDOccurence.ZeroOrMore;
  504. ReadChar ();
  505. break;
  506. case '+':
  507. model.Occurence = DTDOccurence.OneOrMore;
  508. ReadChar ();
  509. break;
  510. }
  511. return model;
  512. }
  513. private void AddContentModel (DTDContentModelCollection cmc, DTDContentModel cm)
  514. {
  515. if (cm.ElementName != null) {
  516. for (int i = 0; i < cmc.Count; i++) {
  517. if (cmc [i].ElementName == cm.ElementName) {
  518. HandleError (new XmlSchemaException ("Element content must be unique inside mixed content model.",
  519. this.LineNumber,
  520. this.LinePosition,
  521. null,
  522. this.BaseURI,
  523. null));
  524. return;
  525. }
  526. }
  527. }
  528. cmc.Add (cm);
  529. }
  530. // The reader is positioned on the first name char.
  531. private void ReadParameterEntityDecl ()
  532. {
  533. DTDParameterEntityDeclaration decl =
  534. new DTDParameterEntityDeclaration (DTD);
  535. decl.BaseURI = BaseURI;
  536. decl.Name = ReadName ();
  537. if (!SkipWhitespace ())
  538. throw new XmlException (this as IXmlLineInfo,
  539. "Whitespace is required after name in DTD parameter entity declaration.");
  540. if (PeekChar () == 'S' || PeekChar () == 'P') {
  541. // read publicId/systemId
  542. ReadExternalID ();
  543. decl.PublicId = cachedPublicId;
  544. decl.SystemId = cachedSystemId;
  545. SkipWhitespace ();
  546. decl.Resolve (this.DTD.Resolver);
  547. ResolveExternalEntityReplacementText (decl);
  548. } else {
  549. TryExpandPERef ();
  550. int quoteChar = ReadChar ();
  551. if (quoteChar != '\'' && quoteChar != '"')
  552. throw new XmlException ("quotation char was expected.");
  553. ClearValueBuffer ();
  554. bool loop = true;
  555. while (loop) {
  556. int c = ReadChar ();
  557. switch (c) {
  558. case -1:
  559. throw new XmlException ("unexpected end of stream in entity value definition.");
  560. case '"':
  561. if (quoteChar == '"')
  562. loop = false;
  563. else
  564. AppendValueChar ('"');
  565. break;
  566. case '\'':
  567. if (quoteChar == '\'')
  568. loop = false;
  569. else
  570. AppendValueChar ('\'');
  571. break;
  572. default:
  573. if (XmlChar.IsInvalid (c))
  574. throw new XmlException (this as IXmlLineInfo, "Invalid character was used to define parameter entity.");
  575. AppendValueChar (c);
  576. break;
  577. }
  578. }
  579. decl.LiteralEntityValue = CreateValueString ();
  580. ClearValueBuffer ();
  581. ResolveInternalEntityReplacementText (decl);
  582. }
  583. ExpectAfterWhitespace ('>');
  584. if (DTD.PEDecls [decl.Name] == null) {
  585. DTD.PEDecls.Add (decl.Name, decl);
  586. }
  587. }
  588. private void ResolveExternalEntityReplacementText (DTDEntityBase decl)
  589. {
  590. if (decl.LiteralEntityValue.StartsWith ("<?xml")) {
  591. XmlTextReader xtr = new XmlTextReader (decl.LiteralEntityValue, XmlNodeType.Element, null);
  592. xtr.SkipTextDeclaration ();
  593. if (decl is DTDEntityDeclaration) {
  594. // GE - also checked as valid contents
  595. StringBuilder sb = new StringBuilder ();
  596. xtr.Normalization = this.Normalization;
  597. xtr.Read ();
  598. while (!xtr.EOF)
  599. sb.Append (xtr.ReadOuterXml ());
  600. decl.ReplacementText = sb.ToString ();
  601. }
  602. else
  603. // PE
  604. decl.ReplacementText = xtr.GetRemainder ().ReadToEnd ();
  605. }
  606. else
  607. decl.ReplacementText = decl.LiteralEntityValue;
  608. }
  609. private void ResolveInternalEntityReplacementText (DTDEntityBase decl)
  610. {
  611. string value = decl.LiteralEntityValue;
  612. int len = value.Length;
  613. ClearValueBuffer ();
  614. for (int i = 0; i < len; i++) {
  615. int ch = value [i];
  616. int end = 0;
  617. string name;
  618. switch (ch) {
  619. case '&':
  620. i++;
  621. end = value.IndexOf (';', i);
  622. if (end < i + 1)
  623. throw new XmlException (decl, "Invalid reference markup.");
  624. // expand charref
  625. if (value [i] == '#') {
  626. i++;
  627. ch = GetCharacterReference (decl, value, ref i, end);
  628. if (XmlChar.IsInvalid (ch))
  629. throw new XmlException (this as IXmlLineInfo, "Invalid character was used to define parameter entity.");
  630. } else {
  631. name = value.Substring (i, end - i);
  632. // don't expand "general" entity.
  633. AppendValueChar ('&');
  634. valueBuffer.Append (name);
  635. AppendValueChar (';');
  636. i = end;
  637. break;
  638. }
  639. if (XmlChar.IsInvalid (ch))
  640. throw new XmlException (decl, "Invalid character was found in the entity declaration.");
  641. AppendValueChar (ch);
  642. break;
  643. case '%':
  644. i++;
  645. end = value.IndexOf (';', i);
  646. if (end < i + 1)
  647. throw new XmlException (decl, "Invalid reference markup.");
  648. name = value.Substring (i, end - i);
  649. valueBuffer.Append (GetPEValue (name));
  650. i = end;
  651. break;
  652. default:
  653. AppendValueChar (ch);
  654. break;
  655. }
  656. }
  657. decl.ReplacementText = CreateValueString ();
  658. if (decl is DTDEntityDeclaration) {
  659. // GE - also checked as valid contents
  660. XmlTextReader xtr = new XmlTextReader (decl.ReplacementText, XmlNodeType.Element, null);
  661. StringBuilder sb = new StringBuilder ();
  662. xtr.Normalization = this.Normalization;
  663. xtr.Read ();
  664. while (!xtr.EOF)
  665. sb.Append (xtr.ReadOuterXml ());
  666. decl.ReplacementText = sb.ToString ();
  667. }
  668. ClearValueBuffer ();
  669. }
  670. private int GetCharacterReference (IXmlLineInfo li, string value, ref int index, int end)
  671. {
  672. int ret = 0;
  673. if (value [index] == 'x') {
  674. try {
  675. ret = int.Parse (value.Substring (index + 1, end - index - 1), NumberStyles.HexNumber);
  676. } catch (FormatException) {
  677. throw new XmlException (li, "Invalid number for a character reference.");
  678. }
  679. } else {
  680. try {
  681. ret = int.Parse (value.Substring (index, end - index));
  682. } catch (FormatException) {
  683. throw new XmlException (li, "Invalid number for a character reference.");
  684. }
  685. }
  686. index = end;
  687. return ret;
  688. }
  689. private string GetPEValue (string peName)
  690. {
  691. DTDParameterEntityDeclaration peDecl =
  692. DTD.PEDecls [peName] as DTDParameterEntityDeclaration;
  693. if (peDecl != null) {
  694. if (peDecl.IsInternalSubset)
  695. throw new XmlException (this as IXmlLineInfo, "Parameter entity is not allowed in internal subset entity '" + peName + "'");
  696. return peDecl.ReplacementText;
  697. }
  698. // See XML 1.0 section 4.1 for both WFC and VC.
  699. if ((DTD.SystemId == null && !DTD.InternalSubsetHasPEReference) || DTD.IsStandalone)
  700. throw new XmlException (this as IXmlLineInfo,
  701. "Parameter entity " + peName + " not found.");
  702. HandleError (new XmlSchemaException (
  703. "Parameter entity " + peName + " not found.", null));
  704. return "";
  705. }
  706. private void TryExpandPERef ()
  707. {
  708. if (PeekChar () == '%') {
  709. if (this.processingInternalSubset)
  710. throw new XmlException (this as IXmlLineInfo, "Parameter entity reference is not allowed inside internal subset.");
  711. ExpandPERef ();
  712. }
  713. }
  714. // reader is positioned on '%'
  715. private void ExpandPERef ()
  716. {
  717. ReadChar ();
  718. string peName = ReadName ();
  719. Expect (';');
  720. DTDParameterEntityDeclaration peDecl =
  721. DTD.PEDecls [peName] as DTDParameterEntityDeclaration;
  722. if (peDecl == null) {
  723. HandleError (new XmlSchemaException ("Parameter entity " + peName + " not found.", null));
  724. return; // do nothing
  725. }
  726. currentInput.InsertParameterEntityBuffer (" " + peDecl.ReplacementText + " ");
  727. }
  728. // The reader is positioned on the head of the name.
  729. private DTDEntityDeclaration ReadEntityDecl ()
  730. {
  731. DTDEntityDeclaration decl = new DTDEntityDeclaration (DTD);
  732. decl.IsInternalSubset = this.processingInternalSubset;
  733. TryExpandPERef ();
  734. SkipWhitespace ();
  735. decl.Name = ReadName ();
  736. if (!SkipWhitespace ())
  737. throw new XmlException (this as IXmlLineInfo,
  738. "Whitespace is required between name and content in DTD entity declaration.");
  739. TryExpandPERef ();
  740. SkipWhitespace ();
  741. if (PeekChar () == 'S' || PeekChar () == 'P') {
  742. // external entity
  743. ReadExternalID ();
  744. decl.PublicId = cachedPublicId;
  745. decl.SystemId = cachedSystemId;
  746. if (SkipWhitespace ()) {
  747. if (PeekChar () == 'N') {
  748. // NDataDecl
  749. Expect ("NDATA");
  750. if (!SkipWhitespace ())
  751. throw new XmlException (this as IXmlLineInfo,
  752. "Whitespace is required after NDATA.");
  753. decl.NotationName = ReadName (); // ndata_name
  754. }
  755. }
  756. if (decl.NotationName == null) {
  757. decl.Resolve (this.DTD.Resolver);
  758. ResolveExternalEntityReplacementText (decl);
  759. } else {
  760. // Unparsed entity.
  761. decl.LiteralEntityValue = String.Empty;
  762. decl.ReplacementText = String.Empty;
  763. }
  764. }
  765. else {
  766. // literal entity
  767. ReadEntityValueDecl (decl);
  768. ResolveInternalEntityReplacementText (decl);
  769. }
  770. SkipWhitespace ();
  771. // This expanding is only allowed as a non-validating parser.
  772. TryExpandPERef ();
  773. ExpectAfterWhitespace ('>');
  774. return decl;
  775. }
  776. private void ReadEntityValueDecl (DTDEntityDeclaration decl)
  777. {
  778. SkipWhitespace ();
  779. // quotation char will be finally removed on unescaping
  780. int quoteChar = ReadChar ();
  781. if (quoteChar != '\'' && quoteChar != '"')
  782. throw new XmlException ("quotation char was expected.");
  783. ClearValueBuffer ();
  784. while (PeekChar () != quoteChar) {
  785. int ch = ReadChar ();
  786. switch (ch) {
  787. case '%':
  788. string name = ReadName ();
  789. Expect (';');
  790. if (decl.IsInternalSubset)
  791. throw new XmlException (this as IXmlLineInfo,
  792. "Parameter entity is not allowed in internal subset entity '" + name + "'");
  793. valueBuffer.Append (GetPEValue (name));
  794. break;
  795. case -1:
  796. throw new XmlException ("unexpected end of stream.");
  797. default:
  798. if (this.normalization && XmlChar.IsInvalid (ch))
  799. throw new XmlException (this as IXmlLineInfo, "Invalid character was found in the entity declaration.");
  800. AppendValueChar (ch);
  801. break;
  802. }
  803. }
  804. // string value = Dereference (CreateValueString (), false);
  805. string value = CreateValueString ();
  806. ClearValueBuffer ();
  807. Expect (quoteChar);
  808. decl.LiteralEntityValue = value;
  809. }
  810. private DTDAttListDeclaration ReadAttListDecl ()
  811. {
  812. TryExpandPERef ();
  813. if (!SkipWhitespace ())
  814. throw new XmlException (this as IXmlLineInfo,
  815. "Whitespace is required between ATTLIST and name in DTD attlist declaration.");
  816. TryExpandPERef ();
  817. SkipWhitespace ();
  818. string name = ReadName (); // target element name
  819. DTDAttListDeclaration decl =
  820. DTD.AttListDecls [name] as DTDAttListDeclaration;
  821. if (decl == null)
  822. decl = new DTDAttListDeclaration (DTD);
  823. decl.IsInternalSubset = this.processingInternalSubset;
  824. decl.Name = name;
  825. if (!SkipWhitespace ())
  826. if (PeekChar () != '>')
  827. throw new XmlException (this as IXmlLineInfo,
  828. "Whitespace is required between name and content in non-empty DTD attlist declaration.");
  829. TryExpandPERef ();
  830. SkipWhitespace ();
  831. while (XmlChar.IsNameChar (PeekChar ())) {
  832. DTDAttributeDefinition def = ReadAttributeDefinition ();
  833. // There must not be two or more ID attributes.
  834. if (def.Datatype.TokenizedType == XmlTokenizedType.ID) {
  835. for (int i = 0; i < decl.Definitions.Count; i++) {
  836. DTDAttributeDefinition d = decl [i];
  837. if (d.Datatype.TokenizedType == XmlTokenizedType.ID) {
  838. HandleError (new XmlSchemaException ("AttList declaration must not contain two or more ID attributes.",
  839. def.LineNumber, def.LinePosition, null, def.BaseURI, null));
  840. break;
  841. }
  842. }
  843. }
  844. if (decl [def.Name] == null)
  845. decl.Add (def);
  846. SkipWhitespace ();
  847. TryExpandPERef ();
  848. SkipWhitespace ();
  849. }
  850. SkipWhitespace ();
  851. // This expanding is only allowed as a non-validating parser.
  852. TryExpandPERef ();
  853. ExpectAfterWhitespace ('>');
  854. return decl;
  855. }
  856. private DTDAttributeDefinition ReadAttributeDefinition ()
  857. {
  858. DTDAttributeDefinition def = new DTDAttributeDefinition (DTD);
  859. def.IsInternalSubset = this.processingInternalSubset;
  860. // attr_name
  861. TryExpandPERef ();
  862. SkipWhitespace ();
  863. def.Name = ReadName ();
  864. if (!SkipWhitespace ())
  865. throw new XmlException (this as IXmlLineInfo,
  866. "Whitespace is required between name and content in DTD attribute definition.");
  867. // attr_value
  868. TryExpandPERef ();
  869. SkipWhitespace ();
  870. switch(PeekChar ()) {
  871. case 'C': // CDATA
  872. Expect ("CDATA");
  873. def.Datatype = XmlSchemaDatatype.FromName ("normalizedString");
  874. break;
  875. case 'I': // ID, IDREF, IDREFS
  876. Expect ("ID");
  877. if(PeekChar () == 'R') {
  878. Expect ("REF");
  879. if(PeekChar () == 'S') {
  880. // IDREFS
  881. ReadChar ();
  882. def.Datatype = XmlSchemaDatatype.FromName ("IDREFS");
  883. }
  884. else // IDREF
  885. def.Datatype = XmlSchemaDatatype.FromName ("IDREF");
  886. }
  887. else // ID
  888. def.Datatype = XmlSchemaDatatype.FromName ("ID");
  889. break;
  890. case 'E': // ENTITY, ENTITIES
  891. Expect ("ENTIT");
  892. switch(ReadChar ()) {
  893. case 'Y': // ENTITY
  894. def.Datatype = XmlSchemaDatatype.FromName ("ENTITY");
  895. break;
  896. case 'I': // ENTITIES
  897. Expect ("ES");
  898. def.Datatype = XmlSchemaDatatype.FromName ("ENTITIES");
  899. break;
  900. }
  901. break;
  902. case 'N': // NMTOKEN, NMTOKENS, NOTATION
  903. ReadChar ();
  904. switch(PeekChar ()) {
  905. case 'M':
  906. Expect ("MTOKEN");
  907. if(PeekChar ()=='S') { // NMTOKENS
  908. ReadChar ();
  909. def.Datatype = XmlSchemaDatatype.FromName ("NMTOKENS");
  910. }
  911. else // NMTOKEN
  912. def.Datatype = XmlSchemaDatatype.FromName ("NMTOKEN");
  913. break;
  914. case 'O':
  915. Expect ("OTATION");
  916. def.Datatype = XmlSchemaDatatype.FromName ("NOTATION");
  917. if (!SkipWhitespace ())
  918. throw new XmlException (this as IXmlLineInfo,
  919. "Whitespace is required between name and content in DTD attribute definition.");
  920. Expect ('(');
  921. SkipWhitespace ();
  922. def.EnumeratedNotations.Add (ReadName ()); // notation name
  923. SkipWhitespace ();
  924. while(PeekChar () == '|') {
  925. ReadChar ();
  926. SkipWhitespace ();
  927. def.EnumeratedNotations.Add (ReadName ()); // notation name
  928. SkipWhitespace ();
  929. }
  930. Expect (')');
  931. break;
  932. default:
  933. throw new XmlException ("attribute declaration syntax error.");
  934. }
  935. break;
  936. default: // Enumerated Values
  937. def.Datatype = XmlSchemaDatatype.FromName ("NMTOKEN");
  938. TryExpandPERef ();
  939. ExpectAfterWhitespace ('(');
  940. SkipWhitespace ();
  941. def.EnumeratedAttributeDeclaration.Add (
  942. def.Datatype.Normalize (ReadNmToken ())); // enum value
  943. SkipWhitespace ();
  944. while(PeekChar () == '|') {
  945. ReadChar ();
  946. SkipWhitespace ();
  947. def.EnumeratedAttributeDeclaration.Add (
  948. def.Datatype.Normalize (ReadNmToken ())); // enum value
  949. SkipWhitespace ();
  950. }
  951. Expect (')');
  952. break;
  953. }
  954. TryExpandPERef ();
  955. if (!SkipWhitespace ())
  956. throw new XmlException (this as IXmlLineInfo,
  957. "Whitespace is required between type and occurence in DTD attribute definition.");
  958. // def_value
  959. ReadAttributeDefaultValue (def);
  960. return def;
  961. }
  962. private void ReadAttributeDefaultValue (DTDAttributeDefinition def)
  963. {
  964. if(PeekChar () == '#')
  965. {
  966. ReadChar ();
  967. switch(PeekChar ())
  968. {
  969. case 'R':
  970. Expect ("REQUIRED");
  971. def.OccurenceType = DTDAttributeOccurenceType.Required;
  972. break;
  973. case 'I':
  974. Expect ("IMPLIED");
  975. def.OccurenceType = DTDAttributeOccurenceType.Optional;
  976. break;
  977. case 'F':
  978. Expect ("FIXED");
  979. def.OccurenceType = DTDAttributeOccurenceType.Fixed;
  980. if (!SkipWhitespace ())
  981. throw new XmlException (this as IXmlLineInfo,
  982. "Whitespace is required between FIXED and actual value in DTD attribute definition.");
  983. def.UnresolvedDefaultValue = ReadDefaultAttribute ();
  984. break;
  985. }
  986. } else {
  987. // one of the enumerated value
  988. SkipWhitespace ();
  989. TryExpandPERef ();
  990. SkipWhitespace ();
  991. def.UnresolvedDefaultValue = ReadDefaultAttribute ();
  992. }
  993. // VC: If default value exists, it should be valid.
  994. if (def.DefaultValue != null) {
  995. string normalized = def.Datatype.Normalize (def.DefaultValue);
  996. bool breakup = false;
  997. object parsed = null;
  998. // enumeration validity
  999. if (def.EnumeratedAttributeDeclaration.Count > 0) {
  1000. if (!def.EnumeratedAttributeDeclaration.Contains (normalized)) {
  1001. HandleError (new XmlSchemaException ("Default value is not one of the enumerated values.",
  1002. def.LineNumber, def.LinePosition, null, def.BaseURI, null));
  1003. breakup = true;
  1004. }
  1005. }
  1006. if (def.EnumeratedNotations.Count > 0) {
  1007. if (!def.EnumeratedNotations.Contains (normalized)) {
  1008. HandleError (new XmlSchemaException ("Default value is not one of the enumerated notation values.",
  1009. def.LineNumber, def.LinePosition, null, def.BaseURI, null));
  1010. breakup = true;
  1011. }
  1012. }
  1013. // type based validity
  1014. if (!breakup) {
  1015. try {
  1016. parsed = def.Datatype.ParseValue (normalized, DTD.NameTable, null);
  1017. } catch (Exception ex) { // FIXME: (wishlist) bad catch ;-(
  1018. HandleError (new XmlSchemaException ("Invalid default value for ENTITY type.",
  1019. def.LineNumber, def.LinePosition, null, def.BaseURI, ex));
  1020. breakup = true;
  1021. }
  1022. }
  1023. if (!breakup) {
  1024. switch (def.Datatype.TokenizedType) {
  1025. case XmlTokenizedType.ENTITY:
  1026. if (DTD.EntityDecls [normalized] == null)
  1027. HandleError (new XmlSchemaException ("Specified entity declaration used by default attribute value was not found.",
  1028. def.LineNumber, def.LinePosition, null, def.BaseURI, null));
  1029. break;
  1030. case XmlTokenizedType.ENTITIES:
  1031. string [] entities = parsed as string [];
  1032. for (int i = 0; i < entities.Length; i++) {
  1033. string entity = entities [i];
  1034. if (DTD.EntityDecls [entity] == null)
  1035. HandleError (new XmlSchemaException ("Specified entity declaration used by default attribute value was not found.",
  1036. def.LineNumber, def.LinePosition, null, def.BaseURI, null));
  1037. }
  1038. break;
  1039. }
  1040. }
  1041. }
  1042. // Extra ID attribute validity check.
  1043. if (def.Datatype != null && def.Datatype.TokenizedType == XmlTokenizedType.ID)
  1044. if (def.UnresolvedDefaultValue != null)
  1045. HandleError (new XmlSchemaException ("ID attribute must not have fixed value constraint.",
  1046. def.LineNumber, def.LinePosition, null, def.BaseURI, null));
  1047. }
  1048. private DTDNotationDeclaration ReadNotationDecl()
  1049. {
  1050. DTDNotationDeclaration decl = new DTDNotationDeclaration (DTD);
  1051. if (!SkipWhitespace ())
  1052. throw new XmlException (this as IXmlLineInfo,
  1053. "Whitespace is required between NOTATION and name in DTD notation declaration.");
  1054. TryExpandPERef ();
  1055. SkipWhitespace ();
  1056. decl.Name = ReadName (); // notation name
  1057. /*
  1058. if (namespaces) { // copy from SetProperties ;-)
  1059. int indexOfColon = decl.Name.IndexOf (':');
  1060. if (indexOfColon == -1) {
  1061. decl.Prefix = String.Empty;
  1062. decl.LocalName = decl.Name;
  1063. } else {
  1064. decl.Prefix = decl.Name.Substring (0, indexOfColon);
  1065. decl.LocalName = decl.Name.Substring (indexOfColon + 1);
  1066. }
  1067. } else {
  1068. */
  1069. decl.Prefix = String.Empty;
  1070. decl.LocalName = decl.Name;
  1071. // }
  1072. SkipWhitespace ();
  1073. if(PeekChar () == 'P') {
  1074. decl.PublicId = ReadPubidLiteral ();
  1075. bool wsSkipped = SkipWhitespace ();
  1076. if (PeekChar () == '\'' || PeekChar () == '"') {
  1077. if (!wsSkipped)
  1078. throw new XmlException (this as IXmlLineInfo,
  1079. "Whitespace is required between public id and system id.");
  1080. decl.SystemId = ReadSystemLiteral (false);
  1081. SkipWhitespace ();
  1082. }
  1083. } else if(PeekChar () == 'S') {
  1084. decl.SystemId = ReadSystemLiteral (true);
  1085. SkipWhitespace ();
  1086. }
  1087. if(decl.PublicId == null && decl.SystemId == null)
  1088. throw new XmlException ("public or system declaration required for \"NOTATION\" declaration.");
  1089. // This expanding is only allowed as a non-validating parser.
  1090. TryExpandPERef ();
  1091. ExpectAfterWhitespace ('>');
  1092. return decl;
  1093. }
  1094. private void ReadExternalID () {
  1095. switch (PeekChar ()) {
  1096. case 'S':
  1097. cachedSystemId = ReadSystemLiteral (true);
  1098. break;
  1099. case 'P':
  1100. cachedPublicId = ReadPubidLiteral ();
  1101. if (!SkipWhitespace ())
  1102. throw new XmlException (this as IXmlLineInfo,
  1103. "Whitespace is required between PUBLIC id and SYSTEM id.");
  1104. cachedSystemId = ReadSystemLiteral (false);
  1105. break;
  1106. }
  1107. }
  1108. // The reader is positioned on the first 'S' of "SYSTEM".
  1109. private string ReadSystemLiteral (bool expectSYSTEM)
  1110. {
  1111. if(expectSYSTEM) {
  1112. Expect ("SYSTEM");
  1113. if (!SkipWhitespace ())
  1114. throw new XmlException (this as IXmlLineInfo,
  1115. "Whitespace is required after 'SYSTEM'.");
  1116. }
  1117. else
  1118. SkipWhitespace ();
  1119. int quoteChar = ReadChar (); // apos or quot
  1120. int c = 0;
  1121. ClearValueBuffer ();
  1122. while (c != quoteChar) {
  1123. c = ReadChar ();
  1124. if (c < 0)
  1125. throw new XmlException (this as IXmlLineInfo,"Unexpected end of stream in ExternalID.");
  1126. if (c != quoteChar)
  1127. AppendValueChar (c);
  1128. }
  1129. return CreateValueString (); //currentTag.ToString (startPos, currentTag.Length - 1 - startPos);
  1130. }
  1131. private string ReadPubidLiteral()
  1132. {
  1133. Expect ("PUBLIC");
  1134. if (!SkipWhitespace ())
  1135. throw new XmlException (this as IXmlLineInfo,
  1136. "Whitespace is required after 'PUBLIC'.");
  1137. int quoteChar = ReadChar ();
  1138. int c = 0;
  1139. ClearValueBuffer ();
  1140. while(c != quoteChar)
  1141. {
  1142. c = ReadChar ();
  1143. if(c < 0) throw new XmlException (this as IXmlLineInfo,"Unexpected end of stream in ExternalID.");
  1144. if(c != quoteChar && !XmlChar.IsPubidChar (c))
  1145. throw new XmlException (this as IXmlLineInfo,"character '" + (char) c + "' not allowed for PUBLIC ID");
  1146. if (c != quoteChar)
  1147. AppendValueChar (c);
  1148. }
  1149. return CreateValueString (); //currentTag.ToString (startPos, currentTag.Length - 1 - startPos);
  1150. }
  1151. // The reader is positioned on the first character
  1152. // of the name.
  1153. internal string ReadName ()
  1154. {
  1155. return ReadNameOrNmToken(false);
  1156. }
  1157. // The reader is positioned on the first character
  1158. // of the name.
  1159. private string ReadNmToken ()
  1160. {
  1161. return ReadNameOrNmToken(true);
  1162. }
  1163. private string ReadNameOrNmToken(bool isNameToken)
  1164. {
  1165. int ch = PeekChar ();
  1166. if(isNameToken) {
  1167. if (!XmlChar.IsNameChar (ch))
  1168. throw new XmlException (this as IXmlLineInfo,String.Format ("a nmtoken did not start with a legal character {0} ({1})", ch, (char) ch));
  1169. }
  1170. else {
  1171. if (!XmlChar.IsFirstNameChar (ch))
  1172. throw new XmlException (this as IXmlLineInfo,String.Format ("a name did not start with a legal character {0} ({1})", ch, (char) ch));
  1173. }
  1174. nameLength = 0;
  1175. AppendNameChar (ReadChar ());
  1176. while (XmlChar.IsNameChar (PeekChar ())) {
  1177. AppendNameChar (ReadChar ());
  1178. }
  1179. return CreateNameString ();
  1180. }
  1181. // Read the next character and compare it against the
  1182. // specified character.
  1183. private void Expect (int expected)
  1184. {
  1185. int ch = ReadChar ();
  1186. if (ch != expected) {
  1187. throw new XmlException (this as IXmlLineInfo,
  1188. String.Format (
  1189. "expected '{0}' ({1:X}) but found '{2}' ({3:X})",
  1190. (char) expected,
  1191. expected,
  1192. (char) ch,
  1193. ch));
  1194. }
  1195. }
  1196. private void Expect (string expected)
  1197. {
  1198. int len = expected.Length;
  1199. for (int i=0; i< len; i++)
  1200. Expect (expected [i]);
  1201. }
  1202. private void ExpectAfterWhitespace (char c)
  1203. {
  1204. while (true) {
  1205. int i = ReadChar ();
  1206. if (XmlChar.IsWhitespace (i))
  1207. continue;
  1208. if (c != i)
  1209. throw new XmlException (String.Join (String.Empty, new string [] {"Expected ", c.ToString (), ", but found " + (char) i, "[", i.ToString (), "]"}));
  1210. break;
  1211. }
  1212. }
  1213. // Does not consume the first non-whitespace character.
  1214. private bool SkipWhitespace ()
  1215. {
  1216. bool skipped = XmlChar.IsWhitespace (PeekChar ());
  1217. while (XmlChar.IsWhitespace (PeekChar ()))
  1218. ReadChar ();
  1219. return skipped;
  1220. }
  1221. private int PeekChar ()
  1222. {
  1223. return currentInput.PeekChar ();
  1224. }
  1225. private int ReadChar ()
  1226. {
  1227. return currentInput.ReadChar ();
  1228. }
  1229. private string ExpandSurrogateChar (int ch)
  1230. {
  1231. if (ch < Char.MaxValue)
  1232. return ((char) ch).ToString ();
  1233. else {
  1234. char [] tmp = new char [] {(char) (ch / 0x10000 + 0xD800 - 1), (char) (ch % 0x10000 + 0xDC00)};
  1235. return new string (tmp);
  1236. }
  1237. }
  1238. // The reader is positioned on the first character after
  1239. // the leading '<!--'.
  1240. private void ReadComment ()
  1241. {
  1242. currentInput.InitialState = false;
  1243. while (PeekChar () != -1) {
  1244. int ch = ReadChar ();
  1245. if (ch == '-' && PeekChar () == '-') {
  1246. ReadChar ();
  1247. if (PeekChar () != '>')
  1248. throw new XmlException (this as IXmlLineInfo,"comments cannot contain '--'");
  1249. ReadChar ();
  1250. break;
  1251. }
  1252. if (XmlChar.IsInvalid (ch))
  1253. throw new XmlException (this as IXmlLineInfo,
  1254. "Not allowed character was found.");
  1255. }
  1256. }
  1257. // The reader is positioned on the first character
  1258. // of the target.
  1259. //
  1260. // It may be xml declaration or processing instruction.
  1261. private void ReadProcessingInstruction ()
  1262. {
  1263. string target = ReadName ();
  1264. if (target == "xml") {
  1265. ReadTextDeclaration ();
  1266. return;
  1267. } else if (target.ToLower () == "xml")
  1268. throw new XmlException (this as IXmlLineInfo,
  1269. "Not allowed processing instruction name which starts with 'X', 'M', 'L' was found.");
  1270. currentInput.InitialState = false;
  1271. if (!SkipWhitespace ())
  1272. if (PeekChar () != '?')
  1273. throw new XmlException (this as IXmlLineInfo,
  1274. "Invalid processing instruction name was found.");
  1275. while (PeekChar () != -1) {
  1276. int ch = ReadChar ();
  1277. if (ch == '?' && PeekChar () == '>') {
  1278. ReadChar ();
  1279. break;
  1280. }
  1281. }
  1282. }
  1283. // The reader is positioned after "<?xml "
  1284. private void ReadTextDeclaration ()
  1285. {
  1286. if (!currentInput.InitialState)
  1287. throw new XmlException (this as IXmlLineInfo,
  1288. "Text declaration cannot appear in this state.");
  1289. currentInput.InitialState = false;
  1290. SkipWhitespace ();
  1291. // version decl
  1292. if (PeekChar () == 'v') {
  1293. Expect ("version");
  1294. ExpectAfterWhitespace ('=');
  1295. SkipWhitespace ();
  1296. int quoteChar = ReadChar ();
  1297. char [] expect1_0 = new char [3];
  1298. int versionLength = 0;
  1299. switch (quoteChar) {
  1300. case '\'':
  1301. case '"':
  1302. while (PeekChar () != quoteChar) {
  1303. if (PeekChar () == -1)
  1304. throw new XmlException (this as IXmlLineInfo,
  1305. "Invalid version declaration inside text declaration.");
  1306. else if (versionLength == 3)
  1307. throw new XmlException (this as IXmlLineInfo,
  1308. "Invalid version number inside text declaration.");
  1309. else {
  1310. expect1_0 [versionLength] = (char) ReadChar ();
  1311. versionLength++;
  1312. if (versionLength == 3 && new String (expect1_0) != "1.0")
  1313. throw new XmlException (this as IXmlLineInfo,
  1314. "Invalid version number inside text declaration.");
  1315. }
  1316. }
  1317. ReadChar ();
  1318. SkipWhitespace ();
  1319. break;
  1320. default:
  1321. throw new XmlException (this as IXmlLineInfo,
  1322. "Invalid version declaration inside text declaration.");
  1323. }
  1324. }
  1325. if (PeekChar () == 'e') {
  1326. Expect ("encoding");
  1327. ExpectAfterWhitespace ('=');
  1328. SkipWhitespace ();
  1329. int quoteChar = ReadChar ();
  1330. switch (quoteChar) {
  1331. case '\'':
  1332. case '"':
  1333. while (PeekChar () != quoteChar)
  1334. if (ReadChar () == -1)
  1335. throw new XmlException (this as IXmlLineInfo,
  1336. "Invalid encoding declaration inside text declaration.");
  1337. ReadChar ();
  1338. SkipWhitespace ();
  1339. break;
  1340. default:
  1341. throw new XmlException (this as IXmlLineInfo,
  1342. "Invalid encoding declaration inside text declaration.");
  1343. }
  1344. // Encoding value should be checked inside XmlInputStream.
  1345. }
  1346. else
  1347. throw new XmlException (this as IXmlLineInfo,
  1348. "Encoding declaration is mandatory in text declaration.");
  1349. Expect ("?>");
  1350. }
  1351. // Note that now this method behaves differently from
  1352. // XmlTextReader's one. It calles AppendValueChar() internally.
  1353. private int ReadCharacterReference ()
  1354. {
  1355. int value = 0;
  1356. if (PeekChar () == 'x') {
  1357. ReadChar ();
  1358. while (PeekChar () != ';' && PeekChar () != -1) {
  1359. int ch = ReadChar ();
  1360. if (ch >= '0' && ch <= '9')
  1361. value = (value << 4) + ch - '0';
  1362. else if (ch >= 'A' && ch <= 'F')
  1363. value = (value << 4) + ch - 'A' + 10;
  1364. else if (ch >= 'a' && ch <= 'f')
  1365. value = (value << 4) + ch - 'a' + 10;
  1366. else
  1367. throw new XmlException (this as IXmlLineInfo,
  1368. String.Format (
  1369. "invalid hexadecimal digit: {0} (#x{1:X})",
  1370. (char) ch,
  1371. ch));
  1372. }
  1373. } else {
  1374. while (PeekChar () != ';' && PeekChar () != -1) {
  1375. int ch = ReadChar ();
  1376. if (ch >= '0' && ch <= '9')
  1377. value = value * 10 + ch - '0';
  1378. else
  1379. throw new XmlException (this as IXmlLineInfo,
  1380. String.Format (
  1381. "invalid decimal digit: {0} (#x{1:X})",
  1382. (char) ch,
  1383. ch));
  1384. }
  1385. }
  1386. ReadChar (); // ';'
  1387. // There is no way to save surrogate pairs...
  1388. if (XmlChar.IsInvalid (value))
  1389. throw new XmlException (this as IXmlLineInfo,
  1390. "Referenced character was not allowed in XML.");
  1391. AppendValueChar (value);
  1392. return value;
  1393. }
  1394. private void AppendNameChar (int ch)
  1395. {
  1396. CheckNameCapacity ();
  1397. if (ch < Char.MaxValue)
  1398. nameBuffer [nameLength++] = (char) ch;
  1399. else {
  1400. nameBuffer [nameLength++] = (char) (ch / 0x10000 + 0xD800 - 1);
  1401. CheckNameCapacity ();
  1402. nameBuffer [nameLength++] = (char) (ch % 0x10000 + 0xDC00);
  1403. }
  1404. }
  1405. private void CheckNameCapacity ()
  1406. {
  1407. if (nameLength == nameCapacity) {
  1408. nameCapacity = nameCapacity * 2;
  1409. char [] oldNameBuffer = nameBuffer;
  1410. nameBuffer = new char [nameCapacity];
  1411. Array.Copy (oldNameBuffer, nameBuffer, nameLength);
  1412. }
  1413. }
  1414. private string CreateNameString ()
  1415. {
  1416. return DTD.NameTable.Add (nameBuffer, 0, nameLength);
  1417. }
  1418. private void AppendValueChar (int ch)
  1419. {
  1420. if (ch < Char.MaxValue)
  1421. valueBuffer.Append ((char) ch);
  1422. else
  1423. valueBuffer.Append (ExpandSurrogateChar (ch));
  1424. }
  1425. private string CreateValueString ()
  1426. {
  1427. return valueBuffer.ToString ();
  1428. }
  1429. private void ClearValueBuffer ()
  1430. {
  1431. valueBuffer.Length = 0;
  1432. }
  1433. // The reader is positioned on the quote character.
  1434. // *Keeps quote char* to value to get_QuoteChar() correctly.
  1435. private string ReadDefaultAttribute ()
  1436. {
  1437. ClearValueBuffer ();
  1438. int quoteChar = ReadChar ();
  1439. if (quoteChar != '\'' && quoteChar != '\"')
  1440. throw new XmlException (this as IXmlLineInfo,"an attribute value was not quoted");
  1441. AppendValueChar (quoteChar);
  1442. while (PeekChar () != quoteChar) {
  1443. int ch = ReadChar ();
  1444. switch (ch)
  1445. {
  1446. case '<':
  1447. throw new XmlException (this as IXmlLineInfo,"attribute values cannot contain '<'");
  1448. case -1:
  1449. throw new XmlException (this as IXmlLineInfo,"unexpected end of file in an attribute value");
  1450. case '&':
  1451. AppendValueChar (ch);
  1452. if (PeekChar () == '#')
  1453. break;
  1454. // Check XML 1.0 section 3.1 WFC.
  1455. string entName = ReadName ();
  1456. Expect (';');
  1457. if (XmlChar.GetPredefinedEntity (entName) < 0) {
  1458. DTDEntityDeclaration entDecl =
  1459. DTD == null ? null : DTD.EntityDecls [entName];
  1460. if (entDecl == null || entDecl.SystemId != null)
  1461. // WFC: Entity Declared (see 4.1)
  1462. if (DTD.IsStandalone || (DTD.SystemId == null && !DTD.InternalSubsetHasPEReference))
  1463. throw new XmlException (this as IXmlLineInfo,
  1464. "Reference to external entities is not allowed in attribute value.");
  1465. }
  1466. valueBuffer.Append (entName);
  1467. AppendValueChar (';');
  1468. break;
  1469. default:
  1470. AppendValueChar (ch);
  1471. break;
  1472. }
  1473. }
  1474. ReadChar (); // quoteChar
  1475. AppendValueChar (quoteChar);
  1476. return CreateValueString ();
  1477. }
  1478. private void PushParserInput (string url)
  1479. {
  1480. Uri baseUri = null;
  1481. try {
  1482. if (DTD.BaseURI != null && DTD.BaseURI.Length > 0)
  1483. baseUri = new Uri (DTD.BaseURI);
  1484. } catch (UriFormatException) {
  1485. }
  1486. Uri absUri = DTD.Resolver.ResolveUri (baseUri, url);
  1487. string absPath = absUri.ToString ();
  1488. foreach (XmlParserInput i in parserInputStack.ToArray ()) {
  1489. if (i.BaseURI == absPath)
  1490. throw new XmlException (this as IXmlLineInfo, "Nested inclusion is not allowed: " + url);
  1491. }
  1492. parserInputStack.Push (currentInput);
  1493. try {
  1494. Stream s = DTD.Resolver.GetEntity (absUri, null, typeof (Stream)) as Stream;
  1495. currentInput = new XmlParserInput (new XmlStreamReader (s), absPath);
  1496. } catch (Exception ex) { // FIXME: (wishlist) Bad exception catch ;-(
  1497. int line = currentInput == null ? 0 : currentInput.LineNumber;
  1498. int col = currentInput == null ? 0 : currentInput.LinePosition;
  1499. string bu = (currentInput == null) ? String.Empty : currentInput.BaseURI;
  1500. HandleError (new XmlSchemaException ("Specified external entity not found. Target URL is " + url + " .",
  1501. line, col, null, bu, ex));
  1502. currentInput = new XmlParserInput (new StringReader (String.Empty), absPath);
  1503. }
  1504. }
  1505. private void PopParserInput ()
  1506. {
  1507. currentInput = parserInputStack.Pop () as XmlParserInput;
  1508. }
  1509. private void HandleError (XmlSchemaException ex)
  1510. {
  1511. #if DTD_HANDLE_EVENTS
  1512. if (this.ValidationEventHandler != null)
  1513. ValidationEventHandler (this, new ValidationEventArgs (ex, ex.Message, XmlSeverityType.Error));
  1514. #else
  1515. DTD.AddError (ex);
  1516. #endif
  1517. }
  1518. }
  1519. }