DTDObjectModel.cs 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. //
  2. // Mono.Xml.DTDObjectModel
  3. //
  4. // Author:
  5. // Atsushi Enomoto ([email protected])
  6. //
  7. // (C)2003 Atsushi Enomoto
  8. //
  9. using System;
  10. using System.Collections;
  11. //using System.Collections.Specialized;
  12. using System.Globalization;
  13. using System.IO;
  14. using System.Text;
  15. using System.Xml;
  16. using System.Xml.Schema;
  17. using Mono.Xml.Schema;
  18. namespace Mono.Xml
  19. {
  20. internal class DTDObjectModel
  21. {
  22. // This specifies the max number of dependent external entities
  23. // per a DTD can consume. A malicious external document server
  24. // might send users' document processing server a large number
  25. // of external entities.
  26. public const int AllowedExternalEntitiesMax = 256;
  27. DTDAutomataFactory factory;
  28. DTDElementAutomata rootAutomata;
  29. DTDEmptyAutomata emptyAutomata;
  30. DTDAnyAutomata anyAutomata;
  31. DTDInvalidAutomata invalidAutomata;
  32. DTDElementDeclarationCollection elementDecls;
  33. DTDAttListDeclarationCollection attListDecls;
  34. DTDParameterEntityDeclarationCollection peDecls;
  35. DTDEntityDeclarationCollection entityDecls;
  36. DTDNotationDeclarationCollection notationDecls;
  37. ArrayList validationErrors;
  38. XmlResolver resolver;
  39. XmlNameTable nameTable;
  40. Hashtable externalResources;
  41. string baseURI;
  42. string name;
  43. string publicId;
  44. string systemId;
  45. string intSubset;
  46. bool intSubsetHasPERef;
  47. bool isStandalone;
  48. int lineNumber;
  49. int linePosition;
  50. public DTDObjectModel (XmlNameTable nameTable)
  51. {
  52. this.nameTable = nameTable;
  53. elementDecls = new DTDElementDeclarationCollection (this);
  54. attListDecls = new DTDAttListDeclarationCollection (this);
  55. entityDecls = new DTDEntityDeclarationCollection (this);
  56. peDecls = new DTDParameterEntityDeclarationCollection (this);
  57. notationDecls = new DTDNotationDeclarationCollection (this);
  58. factory = new DTDAutomataFactory (this);
  59. validationErrors = new ArrayList ();
  60. externalResources = new Hashtable ();
  61. }
  62. public string BaseURI {
  63. get { return baseURI; }
  64. set { baseURI = value; }
  65. }
  66. public bool IsStandalone {
  67. get { return isStandalone; }
  68. set { isStandalone = value; }
  69. }
  70. public string Name {
  71. get { return name; }
  72. set { name = value; }
  73. }
  74. public XmlNameTable NameTable {
  75. get { return nameTable; }
  76. }
  77. public string PublicId {
  78. get { return publicId; }
  79. set { publicId = value; }
  80. }
  81. public string SystemId {
  82. get { return systemId; }
  83. set { systemId = value; }
  84. }
  85. public string InternalSubset {
  86. get { return intSubset; }
  87. set { intSubset = value; }
  88. }
  89. public bool InternalSubsetHasPEReference {
  90. get { return intSubsetHasPERef; }
  91. set { intSubsetHasPERef = value; }
  92. }
  93. public int LineNumber {
  94. get { return lineNumber; }
  95. set { lineNumber = value; }
  96. }
  97. public int LinePosition {
  98. get { return linePosition; }
  99. set { linePosition = value; }
  100. }
  101. public string ResolveEntity (string name)
  102. {
  103. DTDEntityDeclaration decl = EntityDecls [name]
  104. as DTDEntityDeclaration;
  105. if (decl == null) {
  106. AddError (new XmlSchemaException ("Required entity was not found.",
  107. this.LineNumber, this.LinePosition, null, this.BaseURI, null));
  108. return " ";
  109. }
  110. else
  111. return decl.EntityValue;
  112. }
  113. internal XmlResolver Resolver {
  114. get { return resolver; }
  115. }
  116. public XmlResolver XmlResolver {
  117. set { resolver = value; }
  118. }
  119. internal Hashtable ExternalResources {
  120. get { return externalResources; }
  121. }
  122. public DTDAutomataFactory Factory {
  123. get { return factory; }
  124. }
  125. public DTDElementDeclaration RootElement {
  126. get { return ElementDecls [Name]; }
  127. }
  128. public DTDElementDeclarationCollection ElementDecls {
  129. get { return elementDecls; }
  130. }
  131. public DTDAttListDeclarationCollection AttListDecls {
  132. get { return attListDecls; }
  133. }
  134. public DTDEntityDeclarationCollection EntityDecls {
  135. get { return entityDecls; }
  136. }
  137. public DTDParameterEntityDeclarationCollection PEDecls {
  138. get { return peDecls; }
  139. }
  140. public DTDNotationDeclarationCollection NotationDecls {
  141. get { return notationDecls; }
  142. }
  143. public DTDAutomata RootAutomata {
  144. get {
  145. if (rootAutomata == null)
  146. rootAutomata = new DTDElementAutomata (this, this.Name);
  147. return rootAutomata;
  148. }
  149. }
  150. public DTDEmptyAutomata Empty {
  151. get {
  152. if (emptyAutomata == null)
  153. emptyAutomata = new DTDEmptyAutomata (this);
  154. return emptyAutomata;
  155. }
  156. }
  157. public DTDAnyAutomata Any {
  158. get {
  159. if (anyAutomata == null)
  160. anyAutomata = new DTDAnyAutomata (this);
  161. return anyAutomata;
  162. }
  163. }
  164. public DTDInvalidAutomata Invalid {
  165. get {
  166. if (invalidAutomata == null)
  167. invalidAutomata = new DTDInvalidAutomata (this);
  168. return invalidAutomata;
  169. }
  170. }
  171. public XmlSchemaException [] Errors {
  172. get { return validationErrors.ToArray (typeof (XmlSchemaException)) as XmlSchemaException []; }
  173. }
  174. public void AddError (XmlSchemaException ex)
  175. {
  176. validationErrors.Add (ex);
  177. }
  178. }
  179. internal class DTDCollectionBase : DictionaryBase
  180. {
  181. DTDObjectModel root;
  182. protected DTDCollectionBase (DTDObjectModel root)
  183. {
  184. this.root = root;
  185. }
  186. protected DTDObjectModel Root {
  187. get { return root; }
  188. }
  189. public ICollection Keys {
  190. get { return InnerHashtable.Keys; }
  191. }
  192. public ICollection Values {
  193. get { return InnerHashtable.Values; }
  194. }
  195. }
  196. internal class DTDElementDeclarationCollection : DTDCollectionBase
  197. {
  198. public DTDElementDeclarationCollection (DTDObjectModel root) : base (root) {}
  199. public DTDElementDeclaration this [string name] {
  200. get { return Get (name); }
  201. }
  202. public DTDElementDeclaration Get (string name)
  203. {
  204. return InnerHashtable [name] as DTDElementDeclaration;
  205. }
  206. public void Add (string name, DTDElementDeclaration decl)
  207. {
  208. if (InnerHashtable.Contains (name)) {
  209. Root.AddError (new XmlSchemaException (String.Format (
  210. "Element declaration for {0} was already added.",
  211. name), null));
  212. return;
  213. }
  214. decl.SetRoot (Root);
  215. InnerHashtable.Add (name, decl);
  216. }
  217. }
  218. internal class DTDAttListDeclarationCollection : DTDCollectionBase
  219. {
  220. public DTDAttListDeclarationCollection (DTDObjectModel root) : base (root) {}
  221. public DTDAttListDeclaration this [string name] {
  222. get { return InnerHashtable [name] as DTDAttListDeclaration; }
  223. }
  224. public void Add (string name, DTDAttListDeclaration decl)
  225. {
  226. DTDAttListDeclaration existing = this [name];
  227. if (existing != null) {
  228. // It is valid, that is additive declaration.
  229. foreach (DTDAttributeDefinition def in decl.Definitions)
  230. if (decl.Get (def.Name) == null)
  231. existing.Add (def);
  232. } else {
  233. decl.SetRoot (Root);
  234. InnerHashtable.Add (name, decl);
  235. }
  236. }
  237. }
  238. internal class DTDEntityDeclarationCollection : DTDCollectionBase
  239. {
  240. public DTDEntityDeclarationCollection (DTDObjectModel root) : base (root) {}
  241. public DTDEntityDeclaration this [string name] {
  242. get { return InnerHashtable [name] as DTDEntityDeclaration; }
  243. }
  244. public void Add (string name, DTDEntityDeclaration decl)
  245. {
  246. if (InnerHashtable [name] != null)
  247. throw new InvalidOperationException (String.Format (
  248. "Entity declaration for {0} was already added.",
  249. name));
  250. decl.SetRoot (Root);
  251. InnerHashtable.Add (name, decl);
  252. }
  253. }
  254. internal class DTDNotationDeclarationCollection : DTDCollectionBase
  255. {
  256. public DTDNotationDeclarationCollection (DTDObjectModel root) : base (root) {}
  257. public DTDNotationDeclaration this [string name] {
  258. get { return InnerHashtable [name] as DTDNotationDeclaration; }
  259. }
  260. public void Add (string name, DTDNotationDeclaration decl)
  261. {
  262. if (InnerHashtable [name] != null)
  263. throw new InvalidOperationException (String.Format (
  264. "Notation declaration for {0} was already added.",
  265. name));
  266. decl.SetRoot (Root);
  267. InnerHashtable.Add (name, decl);
  268. }
  269. }
  270. // This class contains either ElementName or ChildModels.
  271. internal class DTDContentModel : DTDNode
  272. {
  273. DTDObjectModel root;
  274. DTDAutomata compiledAutomata;
  275. string ownerElementName;
  276. string elementName;
  277. DTDContentOrderType orderType = DTDContentOrderType.None;
  278. DTDContentModelCollection childModels = new DTDContentModelCollection ();
  279. DTDOccurence occurence = DTDOccurence.One;
  280. internal DTDContentModel (DTDObjectModel root, string ownerElementName)
  281. {
  282. this.root = root;
  283. this.ownerElementName = ownerElementName;
  284. }
  285. public DTDContentModelCollection ChildModels {
  286. get { return childModels; }
  287. set { childModels = value; }
  288. }
  289. public DTDElementDeclaration ElementDecl {
  290. get { return root.ElementDecls [ownerElementName]; }
  291. }
  292. public string ElementName {
  293. get { return elementName; }
  294. set { elementName = value; }
  295. }
  296. public DTDOccurence Occurence {
  297. get { return occurence; }
  298. set { occurence = value; }
  299. }
  300. public DTDContentOrderType OrderType {
  301. get { return orderType; }
  302. set { orderType = value; }
  303. }
  304. public DTDAutomata GetAutomata ()
  305. {
  306. if (compiledAutomata == null)
  307. Compile ();
  308. return compiledAutomata;
  309. }
  310. public DTDAutomata Compile ()
  311. {
  312. compiledAutomata = CompileInternal ();
  313. return compiledAutomata;
  314. }
  315. private DTDAutomata CompileInternal ()
  316. {
  317. if (ElementDecl.IsAny)
  318. return root.Any;
  319. if (ElementDecl.IsEmpty)
  320. return root.Empty;
  321. DTDAutomata basis = GetBasicContentAutomata ();
  322. switch (Occurence) {
  323. case DTDOccurence.One:
  324. return basis;
  325. case DTDOccurence.Optional:
  326. return Choice (root.Empty, basis);
  327. case DTDOccurence.OneOrMore:
  328. return new DTDOneOrMoreAutomata (root, basis);
  329. case DTDOccurence.ZeroOrMore:
  330. return Choice (root.Empty, new DTDOneOrMoreAutomata (root, basis));
  331. }
  332. throw new InvalidOperationException ();
  333. }
  334. private DTDAutomata GetBasicContentAutomata ()
  335. {
  336. if (ElementName != null)
  337. return new DTDElementAutomata (root, ElementName);
  338. switch (ChildModels.Count) {
  339. case 0:
  340. return root.Empty;
  341. case 1:
  342. return ChildModels [0].GetAutomata ();
  343. }
  344. DTDAutomata current = null;
  345. int childCount = ChildModels.Count;
  346. switch (OrderType) {
  347. case DTDContentOrderType.Seq:
  348. current = Sequence (
  349. ChildModels [childCount - 2].GetAutomata (),
  350. ChildModels [childCount - 1].GetAutomata ());
  351. for (int i = childCount - 2; i > 0; i--)
  352. current = Sequence (
  353. ChildModels [i - 1].GetAutomata (), current);
  354. return current;
  355. case DTDContentOrderType.Or:
  356. current = Choice (
  357. ChildModels [childCount - 2].GetAutomata (),
  358. ChildModels [childCount - 1].GetAutomata ());
  359. for (int i = childCount - 2; i > 0; i--)
  360. current = Choice (
  361. ChildModels [i - 1].GetAutomata (), current);
  362. return current;
  363. default:
  364. throw new InvalidOperationException ("Invalid pattern specification");
  365. }
  366. }
  367. private DTDAutomata Sequence (DTDAutomata l, DTDAutomata r)
  368. {
  369. return root.Factory.Sequence (l, r);
  370. }
  371. private DTDAutomata Choice (DTDAutomata l, DTDAutomata r)
  372. {
  373. return l.MakeChoice (r);
  374. }
  375. }
  376. internal class DTDContentModelCollection
  377. {
  378. ArrayList contentModel = new ArrayList ();
  379. public DTDContentModelCollection ()
  380. {
  381. }
  382. public DTDContentModel this [int i] {
  383. get { return contentModel [i] as DTDContentModel; }
  384. }
  385. public int Count {
  386. get { return contentModel.Count; }
  387. }
  388. public void Add (DTDContentModel model)
  389. {
  390. contentModel.Add (model);
  391. }
  392. }
  393. internal abstract class DTDNode : IXmlLineInfo
  394. {
  395. DTDObjectModel root;
  396. bool isInternalSubset;
  397. string baseURI;
  398. int lineNumber;
  399. int linePosition;
  400. public virtual string BaseURI {
  401. get { return baseURI; }
  402. set { baseURI = value; }
  403. }
  404. public bool IsInternalSubset {
  405. get { return isInternalSubset; }
  406. set { isInternalSubset = value; }
  407. }
  408. public int LineNumber {
  409. get { return lineNumber; }
  410. set { lineNumber = value; }
  411. }
  412. public int LinePosition {
  413. get { return linePosition; }
  414. set { linePosition = value; }
  415. }
  416. public bool HasLineInfo ()
  417. {
  418. return lineNumber != 0;
  419. }
  420. internal void SetRoot (DTDObjectModel root)
  421. {
  422. this.root = root;
  423. if (baseURI == null)
  424. this.BaseURI = root.BaseURI;
  425. }
  426. protected DTDObjectModel Root {
  427. get { return root; }
  428. }
  429. }
  430. internal class DTDElementDeclaration : DTDNode
  431. {
  432. DTDObjectModel root;
  433. DTDContentModel contentModel;
  434. string name;
  435. bool isEmpty;
  436. bool isAny;
  437. bool isMixedContent;
  438. internal DTDElementDeclaration (DTDObjectModel root)
  439. {
  440. this.root = root;
  441. }
  442. public string Name {
  443. get { return name; }
  444. set { name = value; }
  445. }
  446. public bool IsEmpty {
  447. get { return isEmpty; }
  448. set { isEmpty = value; }
  449. }
  450. public bool IsAny {
  451. get { return isAny; }
  452. set { isAny = value; }
  453. }
  454. public bool IsMixedContent {
  455. get { return isMixedContent; }
  456. set { isMixedContent = value; }
  457. }
  458. public DTDContentModel ContentModel {
  459. get {
  460. if (contentModel == null)
  461. contentModel = new DTDContentModel (root, Name);
  462. return contentModel;
  463. }
  464. }
  465. public DTDAttListDeclaration Attributes {
  466. get {
  467. return Root.AttListDecls [Name];
  468. }
  469. }
  470. }
  471. internal class DTDAttributeDefinition : DTDNode
  472. {
  473. string name;
  474. XmlSchemaDatatype datatype;
  475. ArrayList enumeratedLiterals;
  476. string unresolvedDefault;
  477. ArrayList enumeratedNotations;
  478. DTDAttributeOccurenceType occurenceType = DTDAttributeOccurenceType.None;
  479. string resolvedDefaultValue;
  480. string resolvedNormalizedDefaultValue;
  481. internal DTDAttributeDefinition (DTDObjectModel root)
  482. {
  483. this.SetRoot (root);
  484. }
  485. public string Name {
  486. get { return name; }
  487. set { name =value; }
  488. }
  489. public XmlSchemaDatatype Datatype {
  490. get { return datatype; }
  491. set { datatype = value; }
  492. }
  493. public DTDAttributeOccurenceType OccurenceType {
  494. get { return this.occurenceType; }
  495. set { this.occurenceType = value; }
  496. }
  497. // entity reference inside enumerated values are not allowed,
  498. // but on the other hand, they are allowed inside default value.
  499. // Then I decided to use string ArrayList for enumerated values,
  500. // and unresolved string value for DefaultValue.
  501. public ArrayList EnumeratedAttributeDeclaration {
  502. get {
  503. if (enumeratedLiterals == null)
  504. enumeratedLiterals = new ArrayList ();
  505. return this.enumeratedLiterals;
  506. }
  507. }
  508. public ArrayList EnumeratedNotations {
  509. get {
  510. if (enumeratedNotations == null)
  511. enumeratedNotations = new ArrayList ();
  512. return this.enumeratedNotations;
  513. }
  514. }
  515. public string DefaultValue {
  516. get {
  517. if (resolvedDefaultValue == null)
  518. resolvedDefaultValue = ComputeDefaultValue ();
  519. return resolvedDefaultValue;
  520. }
  521. }
  522. public string NormalizedDefaultValue {
  523. get {
  524. if (resolvedNormalizedDefaultValue == null) {
  525. string s = ComputeDefaultValue ();
  526. try {
  527. object o = Datatype.ParseValue (s, null, null);
  528. resolvedNormalizedDefaultValue =
  529. (o is string []) ?
  530. String.Join (" ", (string []) o) :
  531. o.ToString ();
  532. } catch (Exception) {
  533. // This is for non-error-reporting reader
  534. resolvedNormalizedDefaultValue = Datatype.Normalize (s);
  535. }
  536. }
  537. return resolvedNormalizedDefaultValue;
  538. }
  539. }
  540. public string UnresolvedDefaultValue {
  541. get { return this.unresolvedDefault; }
  542. set { this.unresolvedDefault = value; }
  543. }
  544. public char QuoteChar {
  545. get {
  546. return UnresolvedDefaultValue.Length > 0 ?
  547. this.UnresolvedDefaultValue [0] :
  548. '"';
  549. }
  550. }
  551. internal string ComputeDefaultValue ()
  552. {
  553. if (UnresolvedDefaultValue == null)
  554. return null;
  555. StringBuilder sb = new StringBuilder ();
  556. int pos = 0;
  557. int next = 0;
  558. string value = this.UnresolvedDefaultValue;
  559. while ((next = value.IndexOf ('&', pos)) >= 0) {
  560. int semicolon = value.IndexOf (';', next);
  561. if (value [next + 1] == '#') {
  562. // character reference.
  563. char c = value [next + 2];
  564. NumberStyles style = NumberStyles.Integer;
  565. string spec;
  566. if (c == 'x' || c == 'X') {
  567. spec = value.Substring (next + 3, semicolon - next - 3);
  568. style |= NumberStyles.HexNumber;
  569. }
  570. else
  571. spec = value.Substring (next + 2, semicolon - next - 2);
  572. sb.Append ((char) int.Parse (spec, style));
  573. } else {
  574. sb.Append (value.Substring (pos, next - 1));
  575. string name = value.Substring (next + 1, semicolon - 2);
  576. int predefined = XmlChar.GetPredefinedEntity (name);
  577. if (predefined >= 0)
  578. sb.Append (predefined);
  579. else
  580. sb.Append (Root.ResolveEntity (name));
  581. }
  582. pos = semicolon + 1;
  583. }
  584. sb.Append (value.Substring (pos));
  585. // strip quote chars
  586. string ret = sb.ToString (1, sb.Length - 2);
  587. sb.Length = 0;
  588. return ret;
  589. }
  590. }
  591. internal class DTDAttListDeclaration : DTDNode
  592. {
  593. string name;
  594. Hashtable attributeOrders = new Hashtable ();
  595. ArrayList attributes = new ArrayList ();
  596. internal DTDAttListDeclaration (DTDObjectModel root)
  597. {
  598. SetRoot (root);
  599. }
  600. public string Name {
  601. get { return name; }
  602. set { name = value; }
  603. }
  604. public DTDAttributeDefinition this [int i] {
  605. get { return Get (i); }
  606. }
  607. public DTDAttributeDefinition this [string name] {
  608. get { return Get (name); }
  609. }
  610. public DTDAttributeDefinition Get (int i)
  611. {
  612. return attributes [i] as DTDAttributeDefinition;
  613. }
  614. public DTDAttributeDefinition Get (string name)
  615. {
  616. object o = attributeOrders [name];
  617. if (o != null)
  618. return attributes [(int) o] as DTDAttributeDefinition;
  619. else
  620. return null;
  621. }
  622. public IList Definitions {
  623. get { return attributes; }
  624. }
  625. public void Add (DTDAttributeDefinition def)
  626. {
  627. if (attributeOrders [def.Name] != null)
  628. throw new InvalidOperationException (String.Format (
  629. "Attribute definition for {0} was already added at element {1}.",
  630. def.Name, this.Name));
  631. def.SetRoot (Root);
  632. attributeOrders.Add (def.Name, attributes.Count);
  633. attributes.Add (def);
  634. }
  635. public int Count {
  636. get { return attributeOrders.Count; }
  637. }
  638. }
  639. internal class DTDEntityBase : DTDNode
  640. {
  641. string name;
  642. string publicId;
  643. string systemId;
  644. string literalValue;
  645. string replacementText;
  646. bool isInvalid;
  647. Exception loadException;
  648. bool loadFailed;
  649. protected DTDEntityBase (DTDObjectModel root)
  650. {
  651. SetRoot (root);
  652. }
  653. internal bool IsInvalid {
  654. get { return isInvalid; }
  655. set { isInvalid = value; }
  656. }
  657. public bool LoadFailed {
  658. get { return loadFailed; }
  659. set { loadFailed = value; }
  660. }
  661. public string Name {
  662. get { return name; }
  663. set { name = value; }
  664. }
  665. public string PublicId {
  666. get { return publicId; }
  667. set { publicId = value; }
  668. }
  669. public string SystemId {
  670. get { return systemId; }
  671. set { systemId = value; }
  672. }
  673. public string LiteralEntityValue {
  674. get { return literalValue; }
  675. set { literalValue = value; }
  676. }
  677. public string ReplacementText {
  678. get { return replacementText; }
  679. set { replacementText = value; }
  680. }
  681. public void Resolve (XmlResolver resolver)
  682. {
  683. if (resolver == null || SystemId == null || SystemId.Length == 0) {
  684. LoadFailed = true;
  685. LiteralEntityValue = String.Empty;
  686. return;
  687. }
  688. Uri baseUri = null;
  689. try {
  690. if (BaseURI != null && BaseURI.Length > 0)
  691. baseUri = new Uri (BaseURI);
  692. } catch (UriFormatException) {
  693. }
  694. Uri absUri = resolver.ResolveUri (baseUri, SystemId);
  695. string absPath = absUri.ToString ();
  696. if (Root.ExternalResources.ContainsKey (absPath))
  697. LiteralEntityValue = (string) Root.ExternalResources [absPath];
  698. Stream s = null;
  699. try {
  700. s = resolver.GetEntity (absUri, null, typeof (Stream)) as Stream;
  701. XmlTextReader xtr = new XmlTextReader (s);
  702. // Don't skip Text declaration here. LiteralEntityValue contains it. See spec 4.5
  703. this.BaseURI = absPath;
  704. LiteralEntityValue = xtr.GetRemainder ().ReadToEnd ();
  705. Root.ExternalResources.Add (absPath, LiteralEntityValue);
  706. if (Root.ExternalResources.Count > DTDObjectModel.AllowedExternalEntitiesMax)
  707. throw new InvalidOperationException ("The total amount of external entities exceeded the allowed number.");
  708. } catch (Exception ex) {
  709. loadException = ex;
  710. LiteralEntityValue = String.Empty;
  711. LoadFailed = true;
  712. // throw new XmlException (this, "Cannot resolve external entity. URI is " + absPath + " .");
  713. } finally {
  714. if (s != null)
  715. s.Close ();
  716. }
  717. }
  718. }
  719. internal class DTDEntityDeclaration : DTDEntityBase
  720. {
  721. string entityValue;
  722. string notationName;
  723. ArrayList ReferencingEntities = new ArrayList ();
  724. bool scanned;
  725. bool recursed;
  726. bool hasExternalReference;
  727. internal DTDEntityDeclaration (DTDObjectModel root) : base (root)
  728. {
  729. }
  730. public string NotationName {
  731. get { return notationName; }
  732. set { notationName = value; }
  733. }
  734. public bool HasExternalReference {
  735. get {
  736. if (!scanned)
  737. ScanEntityValue (new ArrayList ());
  738. return hasExternalReference;
  739. }
  740. }
  741. public string EntityValue {
  742. get {
  743. if (this.IsInvalid)
  744. return String.Empty;
  745. if (PublicId == null && SystemId == null && LiteralEntityValue == null)
  746. return String.Empty;
  747. if (entityValue == null) {
  748. if (NotationName != null)
  749. entityValue = "";
  750. else if (SystemId == null || SystemId == String.Empty) {
  751. entityValue = ReplacementText;
  752. if (entityValue == null)
  753. entityValue = String.Empty;
  754. } else {
  755. entityValue = ReplacementText;
  756. }
  757. // Check illegal recursion.
  758. ScanEntityValue (new ArrayList ());
  759. }
  760. return entityValue;
  761. }
  762. }
  763. // It returns whether the entity contains references to external entities.
  764. public void ScanEntityValue (ArrayList refs)
  765. {
  766. // To modify this code, beware nesting between this and EntityValue.
  767. string value = EntityValue;
  768. if (this.SystemId != null)
  769. hasExternalReference = true;
  770. if (recursed)
  771. throw new XmlException ("Entity recursion was found.");
  772. recursed = true;
  773. if (scanned) {
  774. foreach (string referenced in refs)
  775. if (this.ReferencingEntities.Contains (referenced))
  776. throw new XmlException (String.Format (
  777. "Nested entity was found between {0} and {1}",
  778. referenced, Name));
  779. recursed = false;
  780. return;
  781. }
  782. int len = value.Length;
  783. int start = 0;
  784. for (int i=0; i<len; i++) {
  785. switch (value [i]) {
  786. case '&':
  787. start = i+1;
  788. break;
  789. case ';':
  790. if (start == 0)
  791. break;
  792. string name = value.Substring (start, i - start);
  793. if (name.Length == 0)
  794. throw new XmlException (this as IXmlLineInfo, "Entity reference name is missing.");
  795. if (name [0] == '#')
  796. break; // character reference
  797. if (XmlChar.GetPredefinedEntity (name) >= 0)
  798. break; // predefined reference
  799. this.ReferencingEntities.Add (name);
  800. DTDEntityDeclaration decl = Root.EntityDecls [name];
  801. if (decl != null) {
  802. if (decl.SystemId != null)
  803. hasExternalReference = true;
  804. refs.Add (Name);
  805. decl.ScanEntityValue (refs);
  806. foreach (string str in decl.ReferencingEntities)
  807. ReferencingEntities.Add (str);
  808. refs.Remove (Name);
  809. value = value.Remove (start - 1, name.Length + 2);
  810. value = value.Insert (start - 1, decl.EntityValue);
  811. i -= name.Length + 1; // not +2, because of immediate i++ .
  812. len = value.Length;
  813. }
  814. start = 0;
  815. break;
  816. }
  817. }
  818. if (start != 0)
  819. Root.AddError (new XmlSchemaException ("Invalid reference character '&' is specified.",
  820. this.LineNumber, this.LinePosition, null, this.BaseURI, null));
  821. scanned = true;
  822. recursed = false;
  823. }
  824. }
  825. internal class DTDNotationDeclaration : DTDNode
  826. {
  827. string name;
  828. string localName;
  829. string prefix;
  830. string publicId;
  831. string systemId;
  832. public string Name {
  833. get { return name; }
  834. set { name = value; }
  835. }
  836. public string PublicId {
  837. get { return publicId; }
  838. set { publicId = value; }
  839. }
  840. public string SystemId {
  841. get { return systemId; }
  842. set { systemId = value; }
  843. }
  844. public string LocalName {
  845. get { return localName; }
  846. set { localName = value; }
  847. }
  848. public string Prefix {
  849. get { return prefix; }
  850. set { prefix = value; }
  851. }
  852. internal DTDNotationDeclaration (DTDObjectModel root)
  853. {
  854. SetRoot (root);
  855. }
  856. }
  857. internal class DTDParameterEntityDeclarationCollection
  858. {
  859. Hashtable peDecls = new Hashtable ();
  860. DTDObjectModel root;
  861. public DTDParameterEntityDeclarationCollection (DTDObjectModel root)
  862. {
  863. this.root = root;
  864. }
  865. public DTDParameterEntityDeclaration this [string name] {
  866. get { return peDecls [name] as DTDParameterEntityDeclaration; }
  867. }
  868. public void Add (string name, DTDParameterEntityDeclaration decl)
  869. {
  870. // PEDecl can be overriden.
  871. if (peDecls [name] != null)
  872. return;
  873. decl.SetRoot (root);
  874. peDecls.Add (name, decl);
  875. }
  876. public ICollection Keys {
  877. get { return peDecls.Keys; }
  878. }
  879. public ICollection Values {
  880. get { return peDecls.Values; }
  881. }
  882. }
  883. internal class DTDParameterEntityDeclaration : DTDEntityBase
  884. {
  885. internal DTDParameterEntityDeclaration (DTDObjectModel root) : base (root)
  886. {
  887. }
  888. }
  889. internal enum DTDContentOrderType
  890. {
  891. None,
  892. Seq,
  893. Or
  894. }
  895. internal enum DTDAttributeOccurenceType
  896. {
  897. None,
  898. Required,
  899. Optional,
  900. Fixed
  901. }
  902. internal enum DTDOccurence
  903. {
  904. One,
  905. Optional,
  906. ZeroOrMore,
  907. OneOrMore
  908. }
  909. }