DTDObjectModel.cs 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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 DTDElementDeclarationCollection
  171. {
  172. Hashtable elementDecls = new Hashtable ();
  173. DTDObjectModel root;
  174. public DTDElementDeclarationCollection (DTDObjectModel root)
  175. {
  176. this.root = root;
  177. }
  178. public DTDElementDeclaration this [string name] {
  179. get { return elementDecls [name] as DTDElementDeclaration; }
  180. }
  181. public void Add (string name, DTDElementDeclaration decl)
  182. {
  183. if (elementDecls [name] != null) {
  184. this.root.AddError (new XmlSchemaException (String.Format (
  185. "Element declaration for {0} was already added.",
  186. name), null));
  187. return;
  188. }
  189. decl.SetRoot (root);
  190. elementDecls.Add (name, decl);
  191. }
  192. public ICollection Keys {
  193. get { return elementDecls.Keys; }
  194. }
  195. public ICollection Values {
  196. get { return elementDecls.Values; }
  197. }
  198. }
  199. public class DTDAttListDeclarationCollection
  200. {
  201. Hashtable attListDecls = new Hashtable ();
  202. DTDObjectModel root;
  203. public DTDAttListDeclarationCollection (DTDObjectModel root)
  204. {
  205. this.root = root;
  206. }
  207. public DTDAttListDeclaration this [string name] {
  208. get { return attListDecls [name] as DTDAttListDeclaration; }
  209. }
  210. public void Add (string name, DTDAttListDeclaration decl)
  211. {
  212. DTDAttListDeclaration existing = this [name];
  213. if (existing != null) {
  214. // It should be valid and
  215. // has effect of additive declaration.
  216. foreach (DTDAttributeDefinition def in decl.Definitions)
  217. if (decl.Get (def.Name) == null)
  218. existing.Add (def);
  219. } else {
  220. decl.SetRoot (root);
  221. attListDecls.Add (name, decl);
  222. }
  223. }
  224. public ICollection Keys {
  225. get { return attListDecls.Keys; }
  226. }
  227. public ICollection Values {
  228. get { return attListDecls.Values; }
  229. }
  230. }
  231. public class DTDEntityDeclarationCollection
  232. {
  233. Hashtable entityDecls = new Hashtable ();
  234. DTDObjectModel root;
  235. public DTDEntityDeclarationCollection (DTDObjectModel root)
  236. {
  237. this.root = root;
  238. }
  239. public DTDEntityDeclaration this [string name] {
  240. get { return entityDecls [name] as DTDEntityDeclaration; }
  241. }
  242. public void Add (string name, DTDEntityDeclaration decl)
  243. {
  244. if (entityDecls [name] != null)
  245. throw new InvalidOperationException (String.Format (
  246. "Entity declaration for {0} was already added.",
  247. name));
  248. decl.SetRoot (root);
  249. entityDecls.Add (name, decl);
  250. }
  251. public ICollection Keys {
  252. get { return entityDecls.Keys; }
  253. }
  254. public ICollection Values {
  255. get { return entityDecls.Values; }
  256. }
  257. }
  258. public class DTDNotationDeclarationCollection
  259. {
  260. Hashtable notationDecls = new Hashtable ();
  261. DTDObjectModel root;
  262. public DTDNotationDeclarationCollection (DTDObjectModel root)
  263. {
  264. this.root = root;
  265. }
  266. public DTDNotationDeclaration this [string name] {
  267. get { return notationDecls [name] as DTDNotationDeclaration; }
  268. }
  269. public void Add (string name, DTDNotationDeclaration decl)
  270. {
  271. if (notationDecls [name] != null)
  272. throw new InvalidOperationException (String.Format (
  273. "Notation declaration for {0} was already added.",
  274. name));
  275. decl.SetRoot (root);
  276. notationDecls.Add (name, decl);
  277. }
  278. public ICollection Keys {
  279. get { return notationDecls.Keys; }
  280. }
  281. public ICollection Values {
  282. get { return notationDecls.Values; }
  283. }
  284. }
  285. // This class contains either ElementName or ChildModels.
  286. public class DTDContentModel : DTDNode
  287. {
  288. DTDObjectModel root;
  289. DTDAutomata compiledAutomata;
  290. string ownerElementName;
  291. string elementName;
  292. DTDContentOrderType orderType = DTDContentOrderType.None;
  293. DTDContentModelCollection childModels = new DTDContentModelCollection ();
  294. DTDOccurence occurence = DTDOccurence.One;
  295. internal DTDContentModel (DTDObjectModel root, string ownerElementName)
  296. {
  297. this.root = root;
  298. this.ownerElementName = ownerElementName;
  299. }
  300. public DTDContentModelCollection ChildModels {
  301. get { return childModels; }
  302. set { childModels = value; }
  303. }
  304. public DTDElementDeclaration ElementDecl {
  305. get { return root.ElementDecls [ownerElementName]; }
  306. }
  307. public string ElementName {
  308. get { return elementName; }
  309. set { elementName = value; }
  310. }
  311. public DTDOccurence Occurence {
  312. get { return occurence; }
  313. set { occurence = value; }
  314. }
  315. public DTDContentOrderType OrderType {
  316. get { return orderType; }
  317. set { orderType = value; }
  318. }
  319. public DTDAutomata GetAutomata ()
  320. {
  321. if (compiledAutomata == null)
  322. Compile ();
  323. return compiledAutomata;
  324. }
  325. public DTDAutomata Compile ()
  326. {
  327. compiledAutomata = CompileInternal ();
  328. return compiledAutomata;
  329. }
  330. private DTDAutomata CompileInternal ()
  331. {
  332. if (ElementDecl.IsAny)
  333. return root.Any;
  334. if (ElementDecl.IsEmpty)
  335. return root.Empty;
  336. DTDAutomata basis = GetBasicContentAutomata ();
  337. switch (Occurence) {
  338. case DTDOccurence.One:
  339. return basis;
  340. case DTDOccurence.Optional:
  341. return Choice (root.Empty, basis);
  342. case DTDOccurence.OneOrMore:
  343. return new DTDOneOrMoreAutomata (root, basis);
  344. case DTDOccurence.ZeroOrMore:
  345. return Choice (root.Empty, new DTDOneOrMoreAutomata (root, basis));
  346. }
  347. throw new InvalidOperationException ();
  348. }
  349. private DTDAutomata GetBasicContentAutomata ()
  350. {
  351. if (ElementName != null)
  352. return new DTDElementAutomata (root, ElementName);
  353. switch (ChildModels.Count) {
  354. case 0:
  355. return root.Empty;
  356. case 1:
  357. return ChildModels [0].GetAutomata ();
  358. }
  359. DTDAutomata current = null;
  360. int childCount = ChildModels.Count;
  361. switch (OrderType) {
  362. case DTDContentOrderType.Seq:
  363. current = Sequence (
  364. ChildModels [childCount - 2].GetAutomata (),
  365. ChildModels [childCount - 1].GetAutomata ());
  366. for (int i = childCount - 2; i > 0; i--)
  367. current = Sequence (
  368. ChildModels [i - 1].GetAutomata (), current);
  369. return current;
  370. case DTDContentOrderType.Or:
  371. current = Choice (
  372. ChildModels [childCount - 2].GetAutomata (),
  373. ChildModels [childCount - 1].GetAutomata ());
  374. for (int i = childCount - 2; i > 0; i--)
  375. current = Choice (
  376. ChildModels [i - 1].GetAutomata (), current);
  377. return current;
  378. default:
  379. throw new InvalidOperationException ("Invalid pattern specification");
  380. }
  381. }
  382. private DTDAutomata Sequence (DTDAutomata l, DTDAutomata r)
  383. {
  384. return root.Factory.Sequence (l, r);
  385. }
  386. private DTDAutomata Choice (DTDAutomata l, DTDAutomata r)
  387. {
  388. return l.MakeChoice (r);
  389. }
  390. }
  391. public class DTDContentModelCollection
  392. {
  393. ArrayList contentModel = new ArrayList ();
  394. public DTDContentModelCollection ()
  395. {
  396. }
  397. public DTDContentModel this [int i] {
  398. get { return contentModel [i] as DTDContentModel; }
  399. }
  400. public int Count {
  401. get { return contentModel.Count; }
  402. }
  403. public void Add (DTDContentModel model)
  404. {
  405. contentModel.Add (model);
  406. }
  407. }
  408. public abstract class DTDNode : IXmlLineInfo
  409. {
  410. DTDObjectModel root;
  411. bool isInternalSubset;
  412. string baseURI;
  413. int lineNumber;
  414. int linePosition;
  415. public virtual string BaseURI {
  416. get { return baseURI; }
  417. set { baseURI = value; }
  418. }
  419. public bool IsInternalSubset {
  420. get { return isInternalSubset; }
  421. set { isInternalSubset = value; }
  422. }
  423. public int LineNumber {
  424. get { return lineNumber; }
  425. set { lineNumber = value; }
  426. }
  427. public int LinePosition {
  428. get { return linePosition; }
  429. set { linePosition = value; }
  430. }
  431. public bool HasLineInfo ()
  432. {
  433. return lineNumber != 0;
  434. }
  435. internal void SetRoot (DTDObjectModel root)
  436. {
  437. this.root = root;
  438. if (baseURI == null)
  439. this.BaseURI = root.BaseURI;
  440. }
  441. protected DTDObjectModel Root {
  442. get { return root; }
  443. }
  444. }
  445. public class DTDElementDeclaration : DTDNode
  446. {
  447. DTDObjectModel root;
  448. DTDContentModel contentModel;
  449. string name;
  450. bool isEmpty;
  451. bool isAny;
  452. bool isMixedContent;
  453. internal DTDElementDeclaration (DTDObjectModel root)
  454. {
  455. this.root = root;
  456. }
  457. public string Name {
  458. get { return name; }
  459. set { name = value; }
  460. }
  461. public bool IsEmpty {
  462. get { return isEmpty; }
  463. set { isEmpty = value; }
  464. }
  465. public bool IsAny {
  466. get { return isAny; }
  467. set { isAny = value; }
  468. }
  469. public bool IsMixedContent {
  470. get { return isMixedContent; }
  471. set { isMixedContent = value; }
  472. }
  473. public DTDContentModel ContentModel {
  474. get {
  475. if (contentModel == null)
  476. contentModel = new DTDContentModel (root, Name);
  477. return contentModel;
  478. }
  479. }
  480. public DTDAttListDeclaration Attributes {
  481. get {
  482. return Root.AttListDecls [Name];
  483. }
  484. }
  485. }
  486. public class DTDAttributeDefinition : DTDNode
  487. {
  488. string name;
  489. XmlSchemaDatatype datatype;
  490. ArrayList enumeratedLiterals = new ArrayList ();
  491. string unresolvedDefault;
  492. ArrayList enumeratedNotations = new ArrayList ();
  493. DTDAttributeOccurenceType occurenceType = DTDAttributeOccurenceType.None;
  494. string resolvedDefaultValue;
  495. string resolvedNormalizedDefaultValue;
  496. internal DTDAttributeDefinition (DTDObjectModel root)
  497. {
  498. this.SetRoot (root);
  499. }
  500. public string Name {
  501. get { return name; }
  502. set { name =value; }
  503. }
  504. public XmlSchemaDatatype Datatype {
  505. get { return datatype; }
  506. set { datatype = value; }
  507. }
  508. public DTDAttributeOccurenceType OccurenceType {
  509. get { return this.occurenceType; }
  510. set { this.occurenceType = value; }
  511. }
  512. // entity reference inside enumerated values are not allowed,
  513. // but on the other hand, they are allowed inside default value.
  514. // Then I decided to use string ArrayList for enumerated values,
  515. // and unresolved string value for DefaultValue.
  516. public ArrayList EnumeratedAttributeDeclaration {
  517. get { return this.enumeratedLiterals; }
  518. }
  519. public ArrayList EnumeratedNotations {
  520. get { return this.enumeratedNotations; }
  521. }
  522. public string DefaultValue {
  523. get {
  524. if (resolvedDefaultValue == null)
  525. resolvedDefaultValue = ComputeDefaultValue ();
  526. return resolvedDefaultValue;
  527. }
  528. }
  529. public string NormalizedDefaultValue {
  530. get {
  531. if (resolvedNormalizedDefaultValue == null) {
  532. object o = Datatype.ParseValue (ComputeDefaultValue (), null, null);
  533. resolvedNormalizedDefaultValue =
  534. (o is string []) ?
  535. String.Join (" ", (string []) o) :
  536. o.ToString ();
  537. }
  538. return resolvedNormalizedDefaultValue;
  539. }
  540. }
  541. public string UnresolvedDefaultValue {
  542. get { return this.unresolvedDefault; }
  543. set { this.unresolvedDefault = value; }
  544. }
  545. public char QuoteChar {
  546. get {
  547. return UnresolvedDefaultValue.Length > 0 ?
  548. this.UnresolvedDefaultValue [0] :
  549. '"';
  550. }
  551. }
  552. private string ComputeDefaultValue ()
  553. {
  554. if (UnresolvedDefaultValue == null)
  555. return null;
  556. StringBuilder sb = new StringBuilder ();
  557. int pos = 0;
  558. int next = 0;
  559. string value = this.UnresolvedDefaultValue;
  560. while ((next = value.IndexOf ('&', pos)) >= 0) {
  561. int semicolon = value.IndexOf (';', next);
  562. if (value [next + 1] == '#') {
  563. // character reference.
  564. char c = value [next + 2];
  565. NumberStyles style = NumberStyles.Integer;
  566. string spec;
  567. if (c == 'x' || c == 'X') {
  568. spec = value.Substring (next + 3, semicolon - next - 3);
  569. style |= NumberStyles.HexNumber;
  570. }
  571. else
  572. spec = value.Substring (next + 2, semicolon - next - 2);
  573. sb.Append ((char) int.Parse (spec, style));
  574. } else {
  575. sb.Append (value.Substring (pos, next - 1));
  576. string name = value.Substring (next + 1, semicolon - 2);
  577. char predefined = XmlChar.GetPredefinedEntity (name);
  578. if (predefined != 0)
  579. sb.Append (predefined);
  580. else
  581. sb.Append (Root.ResolveEntity (name));
  582. }
  583. pos = semicolon + 1;
  584. }
  585. sb.Append (value.Substring (pos));
  586. // strip quote chars
  587. string ret = sb.ToString (1, sb.Length - 2);
  588. sb.Length = 0;
  589. return ret;
  590. }
  591. }
  592. public class DTDAttListDeclaration : DTDNode
  593. {
  594. string name;
  595. Hashtable attributeOrders = new Hashtable ();
  596. ArrayList attributes = new ArrayList ();
  597. internal DTDAttListDeclaration (DTDObjectModel root)
  598. {
  599. SetRoot (root);
  600. }
  601. public string Name {
  602. get { return name; }
  603. set { name = value; }
  604. }
  605. public DTDAttributeDefinition this [int i] {
  606. get { return Get (i); }
  607. }
  608. public DTDAttributeDefinition this [string name] {
  609. get { return Get (name); }
  610. }
  611. public DTDAttributeDefinition Get (int i)
  612. {
  613. return attributes [i] as DTDAttributeDefinition;
  614. }
  615. public DTDAttributeDefinition Get (string name)
  616. {
  617. object o = attributeOrders [name];
  618. if (o != null)
  619. return attributes [(int) o] as DTDAttributeDefinition;
  620. else
  621. return null;
  622. }
  623. public ICollection Definitions {
  624. get { return attributes; }
  625. }
  626. public void Add (DTDAttributeDefinition def)
  627. {
  628. if (attributeOrders [def.Name] != null)
  629. throw new InvalidOperationException (String.Format (
  630. "Attribute definition for {0} was already added at element {1}.",
  631. def.Name, this.Name));
  632. def.SetRoot (Root);
  633. attributeOrders.Add (def.Name, attributes.Count);
  634. attributes.Add (def);
  635. }
  636. public int Count {
  637. get { return attributeOrders.Count; }
  638. }
  639. }
  640. public class DTDEntityBase : DTDNode
  641. {
  642. string name;
  643. string publicId;
  644. string systemId;
  645. string literalValue;
  646. bool isInvalid;
  647. internal bool IsInvalid {
  648. get { return isInvalid; }
  649. set { isInvalid = value; }
  650. }
  651. public string Name {
  652. get { return name; }
  653. set { name = value; }
  654. }
  655. public string PublicId {
  656. get { return publicId; }
  657. set { publicId = value; }
  658. }
  659. public string SystemId {
  660. get { return systemId; }
  661. set { systemId = value; }
  662. }
  663. public string LiteralEntityValue {
  664. get { return literalValue; }
  665. set { literalValue = value; }
  666. }
  667. }
  668. public class DTDEntityDeclaration : DTDEntityBase
  669. {
  670. string entityValue;
  671. string notationName;
  672. StringCollection ReferencingEntities = new StringCollection ();
  673. bool scanned;
  674. bool recursed;
  675. bool hasExternalReference;
  676. internal DTDEntityDeclaration (DTDObjectModel root)
  677. {
  678. this.SetRoot (root);
  679. }
  680. public string NotationName {
  681. get { return notationName; }
  682. set { notationName = value; }
  683. }
  684. public bool HasExternalReference {
  685. get {
  686. if (!scanned)
  687. ScanEntityValue (new StringCollection ());
  688. return hasExternalReference;
  689. }
  690. }
  691. public string EntityValue {
  692. get {
  693. if (this.IsInvalid)
  694. return String.Empty;
  695. if (PublicId == null && SystemId == null && LiteralEntityValue == null)
  696. return String.Empty;
  697. if (entityValue == null) {
  698. if (NotationName != null)
  699. entityValue = "";
  700. else if (SystemId == null || SystemId == String.Empty) {
  701. // FIXME: Isn't it an error??
  702. entityValue = LiteralEntityValue;
  703. if (entityValue == null)
  704. entityValue = String.Empty;
  705. } else {
  706. entityValue = ResolveExternalEntity (Root.Resolver);
  707. }
  708. // Check illegal recursion.
  709. ScanEntityValue (new StringCollection ());
  710. }
  711. return entityValue;
  712. }
  713. }
  714. // It returns whether the entity contains references to external entities.
  715. public void ScanEntityValue (StringCollection refs)
  716. {
  717. // To modify this code, beware nesting between this and EntityValue.
  718. string value = EntityValue;
  719. if (this.SystemId != null)
  720. hasExternalReference = true;
  721. if (recursed)
  722. throw new XmlException ("Entity recursion was found.");
  723. recursed = true;
  724. if (scanned) {
  725. foreach (string referenced in refs)
  726. if (this.ReferencingEntities.Contains (referenced))
  727. throw new XmlException (String.Format (
  728. "Nested entity was found between {0} and {1}",
  729. referenced, Name));
  730. recursed = false;
  731. return;
  732. }
  733. int len = value.Length;
  734. int start = 0;
  735. for (int i=0; i<len; i++) {
  736. switch (value [i]) {
  737. case '&':
  738. start = i+1;
  739. break;
  740. case ';':
  741. if (start == 0)
  742. break;
  743. string name = value.Substring (start, i - start);
  744. if (name.Length == 0)
  745. throw new XmlException (this as IXmlLineInfo, "Entity reference name is missing.");
  746. if (name [0] == '#')
  747. break; // character reference
  748. // FIXME: Should be checked, but how to handle entity for ENTITY attribute?
  749. // if (!XmlChar.IsName (name))
  750. // throw new XmlException (this as IXmlLineInfo, "Invalid entity reference name.");
  751. if (XmlChar.GetPredefinedEntity (name) != 0)
  752. break; // predefined reference
  753. this.ReferencingEntities.Add (name);
  754. DTDEntityDeclaration decl = Root.EntityDecls [name];
  755. if (decl != null) {
  756. if (decl.SystemId != null)
  757. hasExternalReference = true;
  758. refs.Add (Name);
  759. decl.ScanEntityValue (refs);
  760. foreach (string str in decl.ReferencingEntities)
  761. ReferencingEntities.Add (str);
  762. refs.Remove (Name);
  763. value = value.Remove (start - 1, name.Length + 2);
  764. value = value.Insert (start - 1, decl.EntityValue);
  765. i -= name.Length + 1; // not +2, because of immediate i++ .
  766. len = value.Length;
  767. }
  768. start = 0;
  769. break;
  770. }
  771. }
  772. if (start != 0)
  773. Root.AddError (new XmlSchemaException ("Invalid reference character '&' is specified.",
  774. this.LineNumber, this.LinePosition, null, this.BaseURI, null));
  775. scanned = true;
  776. entityValue = value;
  777. recursed = false;
  778. }
  779. private string ResolveExternalEntity (XmlResolver resolver)
  780. {
  781. if (resolver == null)
  782. return String.Empty;
  783. string baseUri = this.BaseURI;
  784. if (baseUri == "")
  785. baseUri = null;
  786. Uri uri = resolver.ResolveUri (
  787. baseUri != null ? new Uri (baseUri) : null, SystemId);
  788. Stream stream = null;
  789. try {
  790. stream = resolver.GetEntity (uri, null, typeof (Stream)) as Stream;
  791. } catch (Exception ex) { // FIXME: (wishlist) bad catch ;-(
  792. Root.AddError (new XmlSchemaException ("Cannot resolve external entity " + uri.ToString () + " .",
  793. this.LineNumber, this.LinePosition, null, this.BaseURI, ex));
  794. }
  795. if (stream == null)
  796. return String.Empty;
  797. XmlTextReader extEntReader = new XmlTextReader (uri.ToString (), stream, this.Root.NameTable);
  798. extEntReader.SkipTextDeclaration ();
  799. TextReader reader = extEntReader.GetRemainder ();
  800. extEntReader.Close ();
  801. StringBuilder sb = new StringBuilder ();
  802. bool checkTextDecl = true;
  803. while (reader.Peek () != -1) {
  804. sb.Append ((char) reader.Read ());
  805. if (checkTextDecl && sb.Length == 6) {
  806. if (sb.ToString () == "<?xml ") {
  807. // Skip Text declaration.
  808. sb.Length = 0;
  809. StringBuilder textdecl = new StringBuilder ();
  810. while (reader.Peek () != '>' && reader.Peek () != -1)
  811. textdecl.Append ((char) reader.Read ());
  812. if (textdecl.ToString ().IndexOf ("encoding") < 0)
  813. throw new XmlException ("Text declaration must have encoding specification: " + BaseURI);
  814. if (textdecl.ToString ().IndexOf ("standalone") >= 0)
  815. throw new XmlException ("Text declaration cannot have standalone declaration: " + BaseURI);
  816. }
  817. checkTextDecl = false;
  818. }
  819. }
  820. return sb.ToString ();
  821. }
  822. }
  823. public class DTDNotationDeclaration : DTDNode
  824. {
  825. string name;
  826. string localName;
  827. string prefix;
  828. string publicId;
  829. string systemId;
  830. public string Name {
  831. get { return name; }
  832. set { name = value; }
  833. }
  834. public string PublicId {
  835. get { return publicId; }
  836. set { publicId = value; }
  837. }
  838. public string SystemId {
  839. get { return systemId; }
  840. set { systemId = value; }
  841. }
  842. public string LocalName {
  843. get { return localName; }
  844. set { localName = value; }
  845. }
  846. public string Prefix {
  847. get { return prefix; }
  848. set { prefix = value; }
  849. }
  850. internal DTDNotationDeclaration (DTDObjectModel root)
  851. {
  852. SetRoot (root);
  853. }
  854. }
  855. public class DTDParameterEntityDeclarationCollection
  856. {
  857. Hashtable peDecls = new Hashtable ();
  858. DTDObjectModel root;
  859. public DTDParameterEntityDeclarationCollection (DTDObjectModel root)
  860. {
  861. this.root = root;
  862. }
  863. public DTDParameterEntityDeclaration this [string name] {
  864. get { return peDecls [name] as DTDParameterEntityDeclaration; }
  865. }
  866. public void Add (string name, DTDParameterEntityDeclaration decl)
  867. {
  868. if (peDecls [name] != null) {
  869. // this.root.AddError (new XmlSchemaException (String.Format (
  870. // "Parameter entity declaration for {0} was already added.",
  871. // name), null));
  872. return;
  873. }
  874. decl.SetRoot (root);
  875. peDecls.Add (name, decl);
  876. }
  877. public ICollection Keys {
  878. get { return peDecls.Keys; }
  879. }
  880. public ICollection Values {
  881. get { return peDecls.Values; }
  882. }
  883. }
  884. public class DTDParameterEntityDeclaration : DTDEntityBase
  885. {
  886. string resolvedValue;
  887. Exception loadException;
  888. bool loadFailed;
  889. public bool LoadFailed {
  890. get { return loadFailed; }
  891. set { loadFailed = value; }
  892. }
  893. public string Value {
  894. get {
  895. if (LiteralEntityValue != null)
  896. return LiteralEntityValue;
  897. if (resolvedValue == null)
  898. throw new InvalidOperationException ();
  899. return resolvedValue;
  900. }
  901. }
  902. public void Resolve (XmlResolver resolver)
  903. {
  904. if (resolver == null) {
  905. resolvedValue = String.Empty;
  906. LoadFailed = true;
  907. return;
  908. }
  909. Uri baseUri = null;
  910. try {
  911. if (BaseURI != null && BaseURI.Length > 0)
  912. baseUri = new Uri (BaseURI);
  913. } catch (UriFormatException) {
  914. }
  915. Uri absUri = resolver.ResolveUri (baseUri, SystemId);
  916. string absPath = absUri.ToString ();
  917. try {
  918. Stream s = resolver.GetEntity (absUri, null, typeof (Stream)) as Stream;
  919. XmlTextReader xtr = new XmlTextReader (s);
  920. xtr.SkipTextDeclaration ();
  921. resolvedValue = xtr.GetRemainder ().ReadToEnd ();
  922. } catch (IOException ex) {
  923. loadException = ex;
  924. resolvedValue = String.Empty;
  925. LoadFailed = true;
  926. }
  927. }
  928. }
  929. public enum DTDContentOrderType
  930. {
  931. None,
  932. Seq,
  933. Or
  934. }
  935. public enum DTDAttributeOccurenceType
  936. {
  937. None,
  938. Required,
  939. Optional,
  940. Fixed
  941. }
  942. public enum DTDOccurence
  943. {
  944. One,
  945. Optional,
  946. ZeroOrMore,
  947. OneOrMore
  948. }
  949. }