DTDObjectModel.cs 27 KB

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