DTDObjectModel.cs 25 KB

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