DTDObjectModel.cs 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. // 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. public 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. public 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. public 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. public 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. public 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. public 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. public 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. public 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. public 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. public 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. public 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. public 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. public class DTDEntityDeclaration : DTDEntityBase
  709. {
  710. string entityValue;
  711. string notationName;
  712. StringCollection ReferencingEntities = new StringCollection ();
  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 StringCollection ());
  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. // FIXME: Isn't it an error??
  741. entityValue = ReplacementText;
  742. // entityValue = LiteralEntityValue;
  743. if (entityValue == null)
  744. entityValue = String.Empty;
  745. } else {
  746. entityValue = ReplacementText;
  747. }
  748. // Check illegal recursion.
  749. ScanEntityValue (new StringCollection ());
  750. }
  751. return entityValue;
  752. }
  753. }
  754. // It returns whether the entity contains references to external entities.
  755. public void ScanEntityValue (StringCollection refs)
  756. {
  757. // To modify this code, beware nesting between this and EntityValue.
  758. string value = EntityValue;
  759. if (this.SystemId != null)
  760. hasExternalReference = true;
  761. if (recursed)
  762. throw new XmlException ("Entity recursion was found.");
  763. recursed = true;
  764. if (scanned) {
  765. foreach (string referenced in refs)
  766. if (this.ReferencingEntities.Contains (referenced))
  767. throw new XmlException (String.Format (
  768. "Nested entity was found between {0} and {1}",
  769. referenced, Name));
  770. recursed = false;
  771. return;
  772. }
  773. int len = value.Length;
  774. int start = 0;
  775. for (int i=0; i<len; i++) {
  776. switch (value [i]) {
  777. case '&':
  778. start = i+1;
  779. break;
  780. case ';':
  781. if (start == 0)
  782. break;
  783. string name = value.Substring (start, i - start);
  784. if (name.Length == 0)
  785. throw new XmlException (this as IXmlLineInfo, "Entity reference name is missing.");
  786. if (name [0] == '#')
  787. break; // character reference
  788. // FIXME: Should be checked, but how to handle entity for ENTITY attribute?
  789. // if (!XmlChar.IsName (name))
  790. // throw new XmlException (this as IXmlLineInfo, "Invalid entity reference name.");
  791. if (XmlChar.GetPredefinedEntity (name) >= 0)
  792. break; // predefined reference
  793. this.ReferencingEntities.Add (name);
  794. DTDEntityDeclaration decl = Root.EntityDecls [name];
  795. if (decl != null) {
  796. if (decl.SystemId != null)
  797. hasExternalReference = true;
  798. refs.Add (Name);
  799. decl.ScanEntityValue (refs);
  800. foreach (string str in decl.ReferencingEntities)
  801. ReferencingEntities.Add (str);
  802. refs.Remove (Name);
  803. value = value.Remove (start - 1, name.Length + 2);
  804. value = value.Insert (start - 1, decl.EntityValue);
  805. i -= name.Length + 1; // not +2, because of immediate i++ .
  806. len = value.Length;
  807. }
  808. start = 0;
  809. break;
  810. }
  811. }
  812. if (start != 0)
  813. Root.AddError (new XmlSchemaException ("Invalid reference character '&' is specified.",
  814. this.LineNumber, this.LinePosition, null, this.BaseURI, null));
  815. scanned = true;
  816. recursed = false;
  817. }
  818. }
  819. public class DTDNotationDeclaration : DTDNode
  820. {
  821. string name;
  822. string localName;
  823. string prefix;
  824. string publicId;
  825. string systemId;
  826. public string Name {
  827. get { return name; }
  828. set { name = value; }
  829. }
  830. public string PublicId {
  831. get { return publicId; }
  832. set { publicId = value; }
  833. }
  834. public string SystemId {
  835. get { return systemId; }
  836. set { systemId = value; }
  837. }
  838. public string LocalName {
  839. get { return localName; }
  840. set { localName = value; }
  841. }
  842. public string Prefix {
  843. get { return prefix; }
  844. set { prefix = value; }
  845. }
  846. internal DTDNotationDeclaration (DTDObjectModel root)
  847. {
  848. SetRoot (root);
  849. }
  850. }
  851. public class DTDParameterEntityDeclarationCollection
  852. {
  853. Hashtable peDecls = new Hashtable ();
  854. DTDObjectModel root;
  855. public DTDParameterEntityDeclarationCollection (DTDObjectModel root)
  856. {
  857. this.root = root;
  858. }
  859. public DTDParameterEntityDeclaration this [string name] {
  860. get { return peDecls [name] as DTDParameterEntityDeclaration; }
  861. }
  862. public void Add (string name, DTDParameterEntityDeclaration decl)
  863. {
  864. // PEDecl can be overriden.
  865. if (peDecls [name] != null)
  866. return;
  867. decl.SetRoot (root);
  868. peDecls.Add (name, decl);
  869. }
  870. public ICollection Keys {
  871. get { return peDecls.Keys; }
  872. }
  873. public ICollection Values {
  874. get { return peDecls.Values; }
  875. }
  876. }
  877. public class DTDParameterEntityDeclaration : DTDEntityBase
  878. {
  879. internal DTDParameterEntityDeclaration (DTDObjectModel root) : base (root)
  880. {
  881. }
  882. }
  883. public enum DTDContentOrderType
  884. {
  885. None,
  886. Seq,
  887. Or
  888. }
  889. public enum DTDAttributeOccurenceType
  890. {
  891. None,
  892. Required,
  893. Optional,
  894. Fixed
  895. }
  896. public enum DTDOccurence
  897. {
  898. One,
  899. Optional,
  900. ZeroOrMore,
  901. OneOrMore
  902. }
  903. }