DTDObjectModel.cs 24 KB

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