DTDReader.cs 46 KB

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