DTDObjectModel.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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 = new ArrayList ();
  477. string unresolvedDefault;
  478. ArrayList enumeratedNotations = new ArrayList ();
  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 { return this.enumeratedLiterals; }
  504. }
  505. public ArrayList EnumeratedNotations {
  506. get { return this.enumeratedNotations; }
  507. }
  508. public string DefaultValue {
  509. get {
  510. if (resolvedDefaultValue == null)
  511. resolvedDefaultValue = ComputeDefaultValue ();
  512. return resolvedDefaultValue;
  513. }
  514. }
  515. public string NormalizedDefaultValue {
  516. get {
  517. if (resolvedNormalizedDefaultValue == null) {
  518. string s = ComputeDefaultValue ();
  519. try {
  520. object o = Datatype.ParseValue (s, null, null);
  521. resolvedNormalizedDefaultValue =
  522. (o is string []) ?
  523. String.Join (" ", (string []) o) :
  524. o.ToString ();
  525. } catch (Exception) {
  526. // This is for non-error-reporting reader
  527. resolvedNormalizedDefaultValue = Datatype.Normalize (s);
  528. }
  529. }
  530. return resolvedNormalizedDefaultValue;
  531. }
  532. }
  533. public string UnresolvedDefaultValue {
  534. get { return this.unresolvedDefault; }
  535. set { this.unresolvedDefault = value; }
  536. }
  537. public char QuoteChar {
  538. get {
  539. return UnresolvedDefaultValue.Length > 0 ?
  540. this.UnresolvedDefaultValue [0] :
  541. '"';
  542. }
  543. }
  544. internal string ComputeDefaultValue ()
  545. {
  546. if (UnresolvedDefaultValue == null)
  547. return null;
  548. StringBuilder sb = new StringBuilder ();
  549. int pos = 0;
  550. int next = 0;
  551. string value = this.UnresolvedDefaultValue;
  552. while ((next = value.IndexOf ('&', pos)) >= 0) {
  553. int semicolon = value.IndexOf (';', next);
  554. if (value [next + 1] == '#') {
  555. // character reference.
  556. char c = value [next + 2];
  557. NumberStyles style = NumberStyles.Integer;
  558. string spec;
  559. if (c == 'x' || c == 'X') {
  560. spec = value.Substring (next + 3, semicolon - next - 3);
  561. style |= NumberStyles.HexNumber;
  562. }
  563. else
  564. spec = value.Substring (next + 2, semicolon - next - 2);
  565. sb.Append ((char) int.Parse (spec, style));
  566. } else {
  567. sb.Append (value.Substring (pos, next - 1));
  568. string name = value.Substring (next + 1, semicolon - 2);
  569. int predefined = XmlChar.GetPredefinedEntity (name);
  570. if (predefined >= 0)
  571. sb.Append (predefined);
  572. else
  573. sb.Append (Root.ResolveEntity (name));
  574. }
  575. pos = semicolon + 1;
  576. }
  577. sb.Append (value.Substring (pos));
  578. // strip quote chars
  579. string ret = sb.ToString (1, sb.Length - 2);
  580. sb.Length = 0;
  581. return ret;
  582. }
  583. }
  584. internal class DTDAttListDeclaration : DTDNode
  585. {
  586. string name;
  587. Hashtable attributeOrders = new Hashtable ();
  588. ArrayList attributes = new ArrayList ();
  589. internal DTDAttListDeclaration (DTDObjectModel root)
  590. {
  591. SetRoot (root);
  592. }
  593. public string Name {
  594. get { return name; }
  595. set { name = value; }
  596. }
  597. public DTDAttributeDefinition this [int i] {
  598. get { return Get (i); }
  599. }
  600. public DTDAttributeDefinition this [string name] {
  601. get { return Get (name); }
  602. }
  603. public DTDAttributeDefinition Get (int i)
  604. {
  605. return attributes [i] as DTDAttributeDefinition;
  606. }
  607. public DTDAttributeDefinition Get (string name)
  608. {
  609. object o = attributeOrders [name];
  610. if (o != null)
  611. return attributes [(int) o] as DTDAttributeDefinition;
  612. else
  613. return null;
  614. }
  615. public IList Definitions {
  616. get { return attributes; }
  617. }
  618. public void Add (DTDAttributeDefinition def)
  619. {
  620. if (attributeOrders [def.Name] != null)
  621. throw new InvalidOperationException (String.Format (
  622. "Attribute definition for {0} was already added at element {1}.",
  623. def.Name, this.Name));
  624. def.SetRoot (Root);
  625. attributeOrders.Add (def.Name, attributes.Count);
  626. attributes.Add (def);
  627. }
  628. public int Count {
  629. get { return attributeOrders.Count; }
  630. }
  631. }
  632. internal class DTDEntityBase : DTDNode
  633. {
  634. string name;
  635. string publicId;
  636. string systemId;
  637. string literalValue;
  638. string replacementText;
  639. bool isInvalid;
  640. Exception loadException;
  641. bool loadFailed;
  642. protected DTDEntityBase (DTDObjectModel root)
  643. {
  644. SetRoot (root);
  645. }
  646. internal bool IsInvalid {
  647. get { return isInvalid; }
  648. set { isInvalid = value; }
  649. }
  650. public bool LoadFailed {
  651. get { return loadFailed; }
  652. set { loadFailed = value; }
  653. }
  654. public string Name {
  655. get { return name; }
  656. set { name = value; }
  657. }
  658. public string PublicId {
  659. get { return publicId; }
  660. set { publicId = value; }
  661. }
  662. public string SystemId {
  663. get { return systemId; }
  664. set { systemId = value; }
  665. }
  666. public string LiteralEntityValue {
  667. get { return literalValue; }
  668. set { literalValue = value; }
  669. }
  670. public string ReplacementText {
  671. get { return replacementText; }
  672. set { replacementText = value; }
  673. }
  674. public void Resolve (XmlResolver resolver)
  675. {
  676. if (resolver == null || SystemId == null || SystemId.Length == 0) {
  677. LoadFailed = true;
  678. LiteralEntityValue = String.Empty;
  679. return;
  680. }
  681. Uri baseUri = null;
  682. try {
  683. if (BaseURI != null && BaseURI.Length > 0)
  684. baseUri = new Uri (BaseURI);
  685. } catch (UriFormatException) {
  686. }
  687. Uri absUri = resolver.ResolveUri (baseUri, SystemId);
  688. string absPath = absUri.ToString ();
  689. if (Root.ExternalResources.ContainsKey (absPath))
  690. LiteralEntityValue = (string) Root.ExternalResources [absPath];
  691. try {
  692. Stream s = resolver.GetEntity (absUri, null, typeof (Stream)) as Stream;
  693. XmlTextReader xtr = new XmlTextReader (s);
  694. // Don't skip Text declaration here. LiteralEntityValue contains it. See spec 4.5
  695. this.BaseURI = absPath;
  696. LiteralEntityValue = xtr.GetRemainder ().ReadToEnd ();
  697. Root.ExternalResources.Add (absPath, LiteralEntityValue);
  698. if (Root.ExternalResources.Count > DTDObjectModel.AllowedExternalEntitiesMax)
  699. throw new InvalidOperationException ("The total amount of external entities exceeded the allowed number.");
  700. } catch (Exception ex) {
  701. loadException = ex;
  702. LiteralEntityValue = String.Empty;
  703. LoadFailed = true;
  704. // throw new XmlException (this, "Cannot resolve external entity. URI is " + absPath + " .");
  705. }
  706. }
  707. }
  708. internal class DTDEntityDeclaration : DTDEntityBase
  709. {
  710. string entityValue;
  711. string notationName;
  712. ArrayList ReferencingEntities = new ArrayList ();
  713. bool scanned;
  714. bool recursed;
  715. bool hasExternalReference;
  716. internal DTDEntityDeclaration (DTDObjectModel root) : base (root)
  717. {
  718. }
  719. public string NotationName {
  720. get { return notationName; }
  721. set { notationName = value; }
  722. }
  723. public bool HasExternalReference {
  724. get {
  725. if (!scanned)
  726. ScanEntityValue (new ArrayList ());
  727. return hasExternalReference;
  728. }
  729. }
  730. public string EntityValue {
  731. get {
  732. if (this.IsInvalid)
  733. return String.Empty;
  734. if (PublicId == null && SystemId == null && LiteralEntityValue == null)
  735. return String.Empty;
  736. if (entityValue == null) {
  737. if (NotationName != null)
  738. entityValue = "";
  739. else if (SystemId == null || SystemId == String.Empty) {
  740. entityValue = ReplacementText;
  741. if (entityValue == null)
  742. entityValue = String.Empty;
  743. } else {
  744. entityValue = ReplacementText;
  745. }
  746. // Check illegal recursion.
  747. ScanEntityValue (new ArrayList ());
  748. }
  749. return entityValue;
  750. }
  751. }
  752. // It returns whether the entity contains references to external entities.
  753. public void ScanEntityValue (ArrayList refs)
  754. {
  755. // To modify this code, beware nesting between this and EntityValue.
  756. string value = EntityValue;
  757. if (this.SystemId != null)
  758. hasExternalReference = true;
  759. if (recursed)
  760. throw new XmlException ("Entity recursion was found.");
  761. recursed = true;
  762. if (scanned) {
  763. foreach (string referenced in refs)
  764. if (this.ReferencingEntities.Contains (referenced))
  765. throw new XmlException (String.Format (
  766. "Nested entity was found between {0} and {1}",
  767. referenced, Name));
  768. recursed = false;
  769. return;
  770. }
  771. int len = value.Length;
  772. int start = 0;
  773. for (int i=0; i<len; i++) {
  774. switch (value [i]) {
  775. case '&':
  776. start = i+1;
  777. break;
  778. case ';':
  779. if (start == 0)
  780. break;
  781. string name = value.Substring (start, i - start);
  782. if (name.Length == 0)
  783. throw new XmlException (this as IXmlLineInfo, "Entity reference name is missing.");
  784. if (name [0] == '#')
  785. break; // character reference
  786. if (XmlChar.GetPredefinedEntity (name) >= 0)
  787. break; // predefined reference
  788. this.ReferencingEntities.Add (name);
  789. DTDEntityDeclaration decl = Root.EntityDecls [name];
  790. if (decl != null) {
  791. if (decl.SystemId != null)
  792. hasExternalReference = true;
  793. refs.Add (Name);
  794. decl.ScanEntityValue (refs);
  795. foreach (string str in decl.ReferencingEntities)
  796. ReferencingEntities.Add (str);
  797. refs.Remove (Name);
  798. value = value.Remove (start - 1, name.Length + 2);
  799. value = value.Insert (start - 1, decl.EntityValue);
  800. i -= name.Length + 1; // not +2, because of immediate i++ .
  801. len = value.Length;
  802. }
  803. start = 0;
  804. break;
  805. }
  806. }
  807. if (start != 0)
  808. Root.AddError (new XmlSchemaException ("Invalid reference character '&' is specified.",
  809. this.LineNumber, this.LinePosition, null, this.BaseURI, null));
  810. scanned = true;
  811. recursed = false;
  812. }
  813. }
  814. internal class DTDNotationDeclaration : DTDNode
  815. {
  816. string name;
  817. string localName;
  818. string prefix;
  819. string publicId;
  820. string systemId;
  821. public string Name {
  822. get { return name; }
  823. set { name = value; }
  824. }
  825. public string PublicId {
  826. get { return publicId; }
  827. set { publicId = value; }
  828. }
  829. public string SystemId {
  830. get { return systemId; }
  831. set { systemId = value; }
  832. }
  833. public string LocalName {
  834. get { return localName; }
  835. set { localName = value; }
  836. }
  837. public string Prefix {
  838. get { return prefix; }
  839. set { prefix = value; }
  840. }
  841. internal DTDNotationDeclaration (DTDObjectModel root)
  842. {
  843. SetRoot (root);
  844. }
  845. }
  846. internal class DTDParameterEntityDeclarationCollection
  847. {
  848. Hashtable peDecls = new Hashtable ();
  849. DTDObjectModel root;
  850. public DTDParameterEntityDeclarationCollection (DTDObjectModel root)
  851. {
  852. this.root = root;
  853. }
  854. public DTDParameterEntityDeclaration this [string name] {
  855. get { return peDecls [name] as DTDParameterEntityDeclaration; }
  856. }
  857. public void Add (string name, DTDParameterEntityDeclaration decl)
  858. {
  859. // PEDecl can be overriden.
  860. if (peDecls [name] != null)
  861. return;
  862. decl.SetRoot (root);
  863. peDecls.Add (name, decl);
  864. }
  865. public ICollection Keys {
  866. get { return peDecls.Keys; }
  867. }
  868. public ICollection Values {
  869. get { return peDecls.Values; }
  870. }
  871. }
  872. internal class DTDParameterEntityDeclaration : DTDEntityBase
  873. {
  874. internal DTDParameterEntityDeclaration (DTDObjectModel root) : base (root)
  875. {
  876. }
  877. }
  878. public enum DTDContentOrderType
  879. {
  880. None,
  881. Seq,
  882. Or
  883. }
  884. public enum DTDAttributeOccurenceType
  885. {
  886. None,
  887. Required,
  888. Optional,
  889. Fixed
  890. }
  891. public enum DTDOccurence
  892. {
  893. One,
  894. Optional,
  895. ZeroOrMore,
  896. OneOrMore
  897. }
  898. }