2
0

XQueryModuleProlog.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. //
  2. // XQueryModuleProlog.cs - abstract syntax tree for XQuery
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Specialized;
  32. using System.Xml;
  33. using System.Xml.Query;
  34. using System.Xml.Schema;
  35. using Mono.Xml.XQuery;
  36. namespace Mono.Xml.XPath2
  37. {
  38. internal abstract class XQueryModule
  39. {
  40. string version;
  41. Prolog prolog;
  42. IXmlNamespaceResolver nsResolver;
  43. public string Version {
  44. get { return version; }
  45. set { version = value; }
  46. }
  47. public Prolog Prolog {
  48. get { return prolog; }
  49. set { prolog = value; }
  50. }
  51. public IXmlNamespaceResolver NSResolver {
  52. get { return nsResolver; }
  53. set { nsResolver = value; }
  54. }
  55. }
  56. internal class XQueryMainModule : XQueryModule
  57. {
  58. ExprSequence queryBody;
  59. public ExprSequence QueryBody {
  60. get { return queryBody; }
  61. set { queryBody = value; }
  62. }
  63. }
  64. internal class XQueryLibraryModule : XQueryModule
  65. {
  66. ModuleDecl moduleDecl;
  67. public ModuleDecl ModuleDecl {
  68. get { return moduleDecl; }
  69. set { moduleDecl = value; }
  70. }
  71. }
  72. internal class ModuleDecl
  73. {
  74. string prefix;
  75. string ns;
  76. public string Prefix {
  77. get { return prefix; }
  78. set { prefix = value; }
  79. }
  80. public string Namespace {
  81. get { return ns; }
  82. set { ns = value; }
  83. }
  84. }
  85. internal class Prolog
  86. {
  87. public Prolog ()
  88. {
  89. namespaceDecls = new StringDictionary ();
  90. schemaImports = new SchemaImportCollection ();
  91. moduleImports = new ModuleImportCollection ();
  92. variables = new XQueryVariableTable ();
  93. functions = new FunctionCollection ();
  94. }
  95. string version;
  96. StringDictionary namespaceDecls;
  97. XmlSpace xmlSpaceDecl;
  98. XmlSpace constructorDecl;
  99. string defaultElementNamespace;
  100. string defaultFunctionNamespace;
  101. string defaultCollation;
  102. string baseUri;
  103. bool defaultOrdered; // false by default
  104. SchemaImportCollection schemaImports;
  105. ModuleImportCollection moduleImports;
  106. XQueryVariableTable variables;
  107. XmlSchemaContentProcessing validationType;
  108. FunctionCollection functions;
  109. public string Version {
  110. get { return version; }
  111. set { version = value; }
  112. }
  113. public StringDictionary NamespaceDecls {
  114. get { return namespaceDecls; }
  115. }
  116. public XmlSpace XmlSpace {
  117. get { return xmlSpaceDecl; }
  118. set { xmlSpaceDecl = value; }
  119. }
  120. public XmlSpace Constructor {
  121. get { return constructorDecl; }
  122. set { constructorDecl = value; }
  123. }
  124. public bool DefaultOrdered {
  125. get { return defaultOrdered; }
  126. set { defaultOrdered = value; }
  127. }
  128. public string DefaultElementNamespace {
  129. get { return defaultElementNamespace; }
  130. set { defaultElementNamespace = value; }
  131. }
  132. public string DefaultFunctionNamespace {
  133. get { return defaultFunctionNamespace; }
  134. set { defaultFunctionNamespace = value; }
  135. }
  136. public string DefaultCollation {
  137. get { return defaultCollation; }
  138. set { defaultCollation = value; }
  139. }
  140. public string BaseUri {
  141. get { return baseUri; }
  142. set { baseUri = value; }
  143. }
  144. public SchemaImportCollection SchemaImports {
  145. get { return schemaImports; }
  146. }
  147. public ModuleImportCollection ModuleImports {
  148. get { return moduleImports; }
  149. }
  150. public XQueryVariableTable Variables {
  151. get { return variables; }
  152. }
  153. public XmlSchemaContentProcessing ValidationType {
  154. get { return validationType; }
  155. set { validationType = value; }
  156. }
  157. public FunctionCollection Functions {
  158. get { return functions; }
  159. }
  160. public void Add (object item)
  161. {
  162. if (item is bool)
  163. DefaultOrdered = (bool) item;
  164. else if (item is XmlQualifiedName) {
  165. XmlQualifiedName q = (XmlQualifiedName) item;
  166. NamespaceDecls.Add (q.Name, q.Namespace);
  167. } else if (item is XmlSpaceDecl) {
  168. XmlSpace = ((XmlSpaceDecl) item).Value;
  169. } else if (item is ConstructionDecl) {
  170. Constructor = ((ConstructionDecl) item).Value;
  171. } else if (item is SimplePrologContent) {
  172. SimplePrologContent c = (SimplePrologContent) item;
  173. string s = c.LiteralValue;
  174. switch (c.Type) {
  175. case PrologContentType.DefaultElementNamespace:
  176. DefaultElementNamespace = s;
  177. break;
  178. case PrologContentType.DefaultFunctionNamespace:
  179. DefaultFunctionNamespace = s;
  180. break;
  181. case PrologContentType.DefaultCollation:
  182. DefaultCollation = s;
  183. break;
  184. case PrologContentType.BaseUri:
  185. BaseUri = s;
  186. break;
  187. default:
  188. throw new XmlQueryCompileException ("Invalid XQuery prolog content was found.");
  189. }
  190. } else if (item is SchemaImport) {
  191. SchemaImports.Add (item as SchemaImport);
  192. } else if (item is ModuleImport) {
  193. ModuleImports.Add (item as ModuleImport);
  194. } else if (item is XQueryVariable) {
  195. XQueryVariable var = item as XQueryVariable;
  196. Variables.Add (var);
  197. } else if (item is XmlSchemaContentProcessing) {
  198. ValidationType = (XmlSchemaContentProcessing) item;
  199. } else if (item is FunctionDeclaration) {
  200. Functions.Add (item as FunctionDeclaration);
  201. } else
  202. throw new XmlQueryCompileException ("Invalid XQuery prolog content item was found.");
  203. }
  204. }
  205. class XmlSpaceDecl
  206. {
  207. public XmlSpace Value;
  208. public XmlSpaceDecl (XmlSpace value)
  209. {
  210. Value = value;
  211. }
  212. }
  213. class ConstructionDecl
  214. {
  215. public XmlSpace Value;
  216. public ConstructionDecl (XmlSpace value)
  217. {
  218. Value = value;
  219. }
  220. }
  221. public class ModuleImportCollection : CollectionBase
  222. {
  223. public void Add (ModuleImport import)
  224. {
  225. List.Add (import);
  226. }
  227. }
  228. public class SchemaImportCollection : CollectionBase
  229. {
  230. public void Add (SchemaImport import)
  231. {
  232. List.Add (import);
  233. }
  234. }
  235. public enum PrologContentType {
  236. DefaultElementNamespace,
  237. DefaultFunctionNamespace,
  238. DefaultCollation,
  239. BaseUri
  240. }
  241. public class SimplePrologContent
  242. {
  243. public SimplePrologContent (PrologContentType type, string literalValue)
  244. {
  245. this.type = type;
  246. this.literalValue = literalValue;
  247. }
  248. PrologContentType type;
  249. string literalValue;
  250. public PrologContentType Type {
  251. get { return type; }
  252. set { type = value; }
  253. }
  254. public string LiteralValue {
  255. get { return literalValue; }
  256. set { literalValue = value; }
  257. }
  258. }
  259. public abstract class AbstractImport
  260. {
  261. public AbstractImport (string prefix, string ns, ICollection locations)
  262. {
  263. this.prefix = prefix;
  264. this.ns = ns;
  265. this.locations = locations;
  266. if (locations == null)
  267. this.locations = new ArrayList (); // empty list
  268. }
  269. string prefix, ns;
  270. ICollection locations;
  271. public string Prefix {
  272. get { return prefix; }
  273. set { prefix = value; }
  274. }
  275. public string Namespace {
  276. get { return ns; }
  277. set { ns = value; }
  278. }
  279. public ICollection Locations {
  280. get { return locations; }
  281. set { locations = value != null ? value : new ArrayList (); }
  282. }
  283. }
  284. public class SchemaImport : AbstractImport
  285. {
  286. public SchemaImport (string prefix, string ns, ICollection schemaLocations)
  287. : base (prefix == "default element namespace" ? String.Empty : prefix, ns, schemaLocations)
  288. {
  289. // Prefix might 1) String.Empty for non-specified prefix,
  290. // 2) "default element namespace" that is as is
  291. // specified in xquery.
  292. if (prefix == "default element namespace")
  293. useDefaultElementNamespace = true;
  294. }
  295. bool useDefaultElementNamespace;
  296. public bool UseDefaultElementNamespace {
  297. get { return useDefaultElementNamespace; }
  298. set { useDefaultElementNamespace = value; }
  299. }
  300. }
  301. public class ModuleImport : AbstractImport
  302. {
  303. public ModuleImport (string prefix, string ns, ICollection moduleLocations)
  304. : base (prefix, ns, moduleLocations)
  305. {
  306. }
  307. }
  308. public class XQueryVariableTable : DictionaryBase
  309. {
  310. public void Add (XQueryVariable decl)
  311. {
  312. Dictionary.Add (decl.Name, decl);
  313. }
  314. public ICollection Keys {
  315. get { return Dictionary.Keys; }
  316. }
  317. public ICollection Values {
  318. get { return Dictionary.Values; }
  319. }
  320. public XQueryVariable this [XmlQualifiedName name] {
  321. get { return Dictionary [name] as XQueryVariable; }
  322. }
  323. }
  324. public class XQueryVariable
  325. {
  326. public XQueryVariable (XmlQualifiedName name, SequenceType type, ExprSequence varBody)
  327. {
  328. this.name = name;
  329. this.type = type;
  330. this.varBody = varBody; // might be null (just declaration).
  331. }
  332. XmlQualifiedName name;
  333. SequenceType type;
  334. ExprSequence varBody;
  335. public XmlQualifiedName Name {
  336. get { return name; }
  337. }
  338. public SequenceType VariableType {
  339. get { return type; }
  340. }
  341. public bool External {
  342. get { return varBody == null; }
  343. }
  344. public ExprSequence VariableBody {
  345. get { return varBody; }
  346. }
  347. }
  348. internal class FunctionCollection : DictionaryBase
  349. {
  350. public void Add (FunctionDeclaration decl)
  351. {
  352. Dictionary.Add (decl.Name, decl);
  353. }
  354. public ICollection Keys {
  355. get { return Dictionary.Keys; }
  356. }
  357. public ICollection Values {
  358. get { return Dictionary.Values; }
  359. }
  360. public FunctionDeclaration this [XmlQualifiedName name] {
  361. get { return Dictionary [name] as FunctionDeclaration; }
  362. }
  363. }
  364. internal class FunctionDeclaration
  365. {
  366. public FunctionDeclaration (XmlQualifiedName name,
  367. XQueryFunctionArgumentList parameters,
  368. SequenceType type,
  369. EnclosedExpr expr)
  370. {
  371. this.name = name;
  372. this.parameters = parameters;
  373. this.returnType = type;
  374. this.funcBody = expr;
  375. }
  376. XmlQualifiedName name;
  377. SequenceType returnType;
  378. XQueryFunctionArgumentList parameters;
  379. EnclosedExpr funcBody;
  380. public XmlQualifiedName Name {
  381. get { return name; }
  382. }
  383. public SequenceType ReturnType {
  384. get { return returnType; }
  385. }
  386. public bool External {
  387. get { return funcBody == null; }
  388. }
  389. public XQueryFunctionArgumentList Parameters {
  390. get { return parameters; }
  391. }
  392. public EnclosedExpr FunctionBody {
  393. get { return funcBody; }
  394. }
  395. }
  396. public class XQueryFunctionArgumentList : CollectionBase
  397. {
  398. public void Add (XQueryFunctionArgument p)
  399. {
  400. List.Add (p);
  401. }
  402. public void Insert (int pos, XQueryFunctionArgument p)
  403. {
  404. List.Insert (pos, p);
  405. }
  406. public XQueryFunctionArgument this [int i] {
  407. get { return (XQueryFunctionArgument) List [i]; }
  408. }
  409. public XQueryFunctionArgument [] ToArray ()
  410. {
  411. XQueryFunctionArgument [] arr = new XQueryFunctionArgument [List.Count];
  412. List.CopyTo (arr, 0);
  413. return arr;
  414. }
  415. }
  416. public abstract class PragmaMUExtensionBase
  417. {
  418. XmlQualifiedName name;
  419. string text;
  420. protected PragmaMUExtensionBase (XmlQualifiedName name, string text)
  421. {
  422. this.name = name;
  423. this.text = text;
  424. }
  425. public XmlQualifiedName Name {
  426. get { return name; }
  427. }
  428. public string Text {
  429. get { return text; }
  430. }
  431. }
  432. public class Pragma : PragmaMUExtensionBase
  433. {
  434. public Pragma (XmlQualifiedName name, string text)
  435. : base (name, text)
  436. {
  437. }
  438. }
  439. public class MUExtension : PragmaMUExtensionBase
  440. {
  441. public MUExtension (XmlQualifiedName name, string text)
  442. : base (name, text)
  443. {
  444. }
  445. }
  446. }
  447. #endif