DTDAutomata.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // Mono.Xml.DTDAutomata
  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.Text;
  32. using System.Xml;
  33. using System.Xml.Schema;
  34. using Mono.Xml.Schema;
  35. namespace Mono.Xml
  36. {
  37. internal class DTDAutomataFactory
  38. {
  39. public DTDAutomataFactory (DTDObjectModel root)
  40. {
  41. this.root = root;
  42. }
  43. DTDObjectModel root;
  44. Hashtable choiceTable = new Hashtable ();
  45. Hashtable sequenceTable = new Hashtable ();
  46. public DTDChoiceAutomata Choice (DTDAutomata left, DTDAutomata right)
  47. {
  48. Hashtable rightPool = choiceTable [left] as Hashtable;
  49. if (rightPool == null) {
  50. rightPool = new Hashtable ();
  51. choiceTable [left] = rightPool;
  52. }
  53. DTDChoiceAutomata result = rightPool [right] as DTDChoiceAutomata;
  54. if (result == null) {
  55. result = new DTDChoiceAutomata (root, left, right);
  56. rightPool [right] = result;
  57. }
  58. return result;
  59. }
  60. public DTDSequenceAutomata Sequence (DTDAutomata left, DTDAutomata right)
  61. {
  62. Hashtable rightPool = sequenceTable [left] as Hashtable;
  63. if (rightPool == null) {
  64. rightPool = new Hashtable ();
  65. sequenceTable [left] = rightPool;
  66. }
  67. DTDSequenceAutomata result = rightPool [right] as DTDSequenceAutomata;
  68. if (result == null) {
  69. result = new DTDSequenceAutomata (root, left, right);
  70. rightPool [right] = result;
  71. }
  72. return result;
  73. }
  74. }
  75. internal abstract class DTDAutomata
  76. {
  77. public DTDAutomata (DTDObjectModel root)
  78. {
  79. this.root = root;
  80. }
  81. private DTDObjectModel root;
  82. public DTDObjectModel Root {
  83. get { return root; }
  84. }
  85. public DTDAutomata MakeChoice (DTDAutomata other)
  86. {
  87. if (this == Root.Invalid)
  88. return other;
  89. if (other == Root.Invalid)
  90. return this;
  91. if (this == Root.Empty && other == Root.Empty)
  92. return this;
  93. if (this == Root.Any && other == Root.Any)
  94. return this;
  95. else if (other == Root.Empty)
  96. return Root.Factory.Choice (other, this);
  97. else
  98. return Root.Factory.Choice (this, other);
  99. }
  100. public DTDAutomata MakeSequence (DTDAutomata other)
  101. {
  102. if (this == Root.Invalid || other == Root.Invalid)
  103. return Root.Invalid;
  104. if (this == Root.Empty)
  105. return other;
  106. if (other == Root.Empty)
  107. return this;
  108. else
  109. return Root.Factory.Sequence (this, other);
  110. }
  111. public abstract DTDAutomata TryStartElement (string name);
  112. public virtual DTDAutomata TryEndElement ()
  113. {
  114. return Root.Invalid;
  115. }
  116. public virtual bool Emptiable {
  117. get { return false; }
  118. }
  119. }
  120. internal class DTDElementAutomata : DTDAutomata
  121. {
  122. public DTDElementAutomata (DTDObjectModel root, string name)
  123. : base (root)
  124. {
  125. this.name = name;
  126. }
  127. private string name;
  128. public string Name {
  129. get { return name; }
  130. }
  131. public override DTDAutomata TryStartElement (string name)
  132. {
  133. if (name == Name)
  134. return Root.Empty;
  135. else
  136. return Root.Invalid;
  137. }
  138. }
  139. internal class DTDChoiceAutomata : DTDAutomata
  140. {
  141. public DTDChoiceAutomata (DTDObjectModel root,
  142. DTDAutomata left, DTDAutomata right)
  143. : base (root)
  144. {
  145. this.left = left;
  146. this.right = right;
  147. }
  148. private DTDAutomata left;
  149. private DTDAutomata right;
  150. public DTDAutomata Left {
  151. get { return left; }
  152. }
  153. public DTDAutomata Right {
  154. get { return right; }
  155. }
  156. public override DTDAutomata TryStartElement (string name)
  157. {
  158. return left.TryStartElement (name).MakeChoice (
  159. right.TryStartElement (name));
  160. }
  161. public override DTDAutomata TryEndElement ()
  162. {
  163. return left.TryEndElement ().MakeChoice (right.TryEndElement ());
  164. }
  165. bool hasComputedEmptiable;
  166. bool cachedEmptiable;
  167. public override bool Emptiable {
  168. get {
  169. if (!hasComputedEmptiable) {
  170. cachedEmptiable = left.Emptiable ||
  171. right.Emptiable;
  172. hasComputedEmptiable = true;
  173. }
  174. return cachedEmptiable;
  175. }
  176. }
  177. }
  178. internal class DTDSequenceAutomata : DTDAutomata
  179. {
  180. public DTDSequenceAutomata (DTDObjectModel root,
  181. DTDAutomata left, DTDAutomata right)
  182. : base (root)
  183. {
  184. this.left = left;
  185. this.right = right;
  186. }
  187. private DTDAutomata left;
  188. private DTDAutomata right;
  189. public DTDAutomata Left {
  190. get { return left; }
  191. }
  192. public DTDAutomata Right {
  193. get { return right; }
  194. }
  195. public override DTDAutomata TryStartElement (string name)
  196. {
  197. DTDAutomata afterL = left.TryStartElement (name);
  198. DTDAutomata afterR = right.TryStartElement (name);
  199. if (afterL == Root.Invalid)
  200. return (left.Emptiable) ? afterR : afterL;
  201. // else
  202. DTDAutomata whenLeftConsumed = afterL.MakeSequence (right);
  203. if (left.Emptiable)
  204. return afterR.MakeChoice (whenLeftConsumed);
  205. else
  206. return whenLeftConsumed;
  207. }
  208. public override DTDAutomata TryEndElement ()
  209. {
  210. return left.Emptiable ? right : Root.Invalid;
  211. }
  212. bool hasComputedEmptiable;
  213. bool cachedEmptiable;
  214. public override bool Emptiable {
  215. get {
  216. if (!hasComputedEmptiable) {
  217. cachedEmptiable = left.Emptiable &&
  218. right.Emptiable;
  219. hasComputedEmptiable = true;
  220. }
  221. return cachedEmptiable;
  222. }
  223. }
  224. }
  225. internal class DTDOneOrMoreAutomata : DTDAutomata
  226. {
  227. public DTDOneOrMoreAutomata (DTDObjectModel root,
  228. DTDAutomata children)
  229. : base (root)
  230. {
  231. this.children = children;
  232. }
  233. private DTDAutomata children;
  234. public DTDAutomata Children {
  235. get { return children; }
  236. }
  237. public override DTDAutomata TryStartElement (string name)
  238. {
  239. DTDAutomata afterC = children.TryStartElement (name);
  240. if (afterC != Root.Invalid)
  241. return afterC.MakeSequence (
  242. Root.Empty.MakeChoice (this));
  243. else
  244. return Root.Invalid;
  245. }
  246. public override DTDAutomata TryEndElement ()
  247. {
  248. return Emptiable ? children.TryEndElement () : Root.Invalid;
  249. }
  250. }
  251. internal class DTDEmptyAutomata : DTDAutomata
  252. {
  253. public DTDEmptyAutomata (DTDObjectModel root)
  254. : base (root)
  255. {
  256. }
  257. public override DTDAutomata TryEndElement ()
  258. {
  259. return this;
  260. }
  261. public override DTDAutomata TryStartElement (string name)
  262. {
  263. return Root.Invalid;
  264. }
  265. public override bool Emptiable {
  266. get { return true; }
  267. }
  268. }
  269. internal class DTDAnyAutomata : DTDAutomata
  270. {
  271. public DTDAnyAutomata (DTDObjectModel root)
  272. : base (root)
  273. {
  274. }
  275. public override DTDAutomata TryEndElement ()
  276. {
  277. return this;
  278. }
  279. public override DTDAutomata TryStartElement (string name)
  280. {
  281. return this;
  282. }
  283. public override bool Emptiable {
  284. get { return true; }
  285. }
  286. }
  287. internal class DTDInvalidAutomata : DTDAutomata
  288. {
  289. public DTDInvalidAutomata (DTDObjectModel root)
  290. : base (root)
  291. {
  292. }
  293. public override DTDAutomata TryEndElement ()
  294. {
  295. return this;
  296. }
  297. public override DTDAutomata TryStartElement (string name)
  298. {
  299. return this;
  300. }
  301. }
  302. }