XQueryModuleProlog.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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.XPath2;
  36. namespace Mono.Xml.XQuery.SyntaxTree
  37. {
  38. public abstract class XQueryModule
  39. {
  40. string version;
  41. Prolog prolog;
  42. public string Version {
  43. get { return version; }
  44. set { version = value; }
  45. }
  46. public Prolog Prolog {
  47. get { return prolog; }
  48. set { prolog = value; }
  49. }
  50. }
  51. public class XQueryMainModule : XQueryModule
  52. {
  53. ExprSequence queryBody;
  54. public ExprSequence QueryBody {
  55. get { return queryBody; }
  56. set { queryBody = value; }
  57. }
  58. }
  59. public class XQueryLibraryModule : XQueryModule
  60. {
  61. ModuleDecl moduleDecl;
  62. public ModuleDecl ModuleDecl {
  63. get { return moduleDecl; }
  64. set { moduleDecl = value; }
  65. }
  66. }
  67. public class ModuleDecl
  68. {
  69. string prefix;
  70. string ns;
  71. public string Prefix {
  72. get { return prefix; }
  73. set { prefix = value; }
  74. }
  75. public string Namespace {
  76. get { return ns; }
  77. set { ns = value; }
  78. }
  79. }
  80. public class Prolog
  81. {
  82. public Prolog ()
  83. {
  84. namespaceDecls = new StringDictionary ();
  85. schemaImports = new SchemaImportCollection ();
  86. moduleImports = new ModuleImportCollection ();
  87. variables = new VariableDeclarationCollection ();
  88. functions = new FunctionCollection ();
  89. }
  90. string version;
  91. StringDictionary namespaceDecls;
  92. XmlSpace xmlSpaceDecl;
  93. XmlSpace constructorDecl;
  94. string defaultElementNamespace;
  95. string defaultFunctionNamespace;
  96. string defaultCollation;
  97. string baseUri;
  98. bool defaultOrdered; // false by default
  99. SchemaImportCollection schemaImports;
  100. ModuleImportCollection moduleImports;
  101. VariableDeclarationCollection variables;
  102. XmlSchemaContentProcessing validationType;
  103. FunctionCollection functions;
  104. public string Version {
  105. get { return version; }
  106. set { version = value; }
  107. }
  108. public StringDictionary NamespaceDecls {
  109. get { return namespaceDecls; }
  110. }
  111. public XmlSpace XmlSpace {
  112. get { return xmlSpaceDecl; }
  113. set { xmlSpaceDecl = value; }
  114. }
  115. public XmlSpace Constructor {
  116. get { return constructorDecl; }
  117. set { constructorDecl = value; }
  118. }
  119. public bool DefaultOrdered {
  120. get { return defaultOrdered; }
  121. set { defaultOrdered = value; }
  122. }
  123. public string DefaultElementNamespace {
  124. get { return defaultElementNamespace; }
  125. set { defaultElementNamespace = value; }
  126. }
  127. public string DefaultFunctionNamespace {
  128. get { return defaultFunctionNamespace; }
  129. set { defaultFunctionNamespace = value; }
  130. }
  131. public string DefaultCollation {
  132. get { return defaultCollation; }
  133. set { defaultCollation = value; }
  134. }
  135. public string BaseUri {
  136. get { return baseUri; }
  137. set { baseUri = value; }
  138. }
  139. public SchemaImportCollection SchemaImports {
  140. get { return schemaImports; }
  141. }
  142. public ModuleImportCollection ModuleImports {
  143. get { return moduleImports; }
  144. }
  145. public VariableDeclarationCollection Variables {
  146. get { return variables; }
  147. }
  148. public XmlSchemaContentProcessing ValidationType {
  149. get { return validationType; }
  150. set { validationType = value; }
  151. }
  152. public FunctionCollection Functions {
  153. get { return functions; }
  154. }
  155. public void Add (object item)
  156. {
  157. if (item is bool)
  158. DefaultOrdered = (bool) item;
  159. else if (item is XmlQualifiedName) {
  160. XmlQualifiedName q = (XmlQualifiedName) item;
  161. NamespaceDecls.Add (q.Name, q.Namespace);
  162. } else if (item is XmlSpaceDecl) {
  163. XmlSpace = ((XmlSpaceDecl) item).Value;
  164. } else if (item is ConstructionDecl) {
  165. Constructor = ((ConstructionDecl) item).Value;
  166. } else if (item is SimplePrologContent) {
  167. SimplePrologContent c = (SimplePrologContent) item;
  168. string s = c.LiteralValue;
  169. switch (c.Type) {
  170. case PrologContentType.DefaultElementNamespace:
  171. DefaultElementNamespace = s;
  172. break;
  173. case PrologContentType.DefaultFunctionNamespace:
  174. DefaultFunctionNamespace = s;
  175. break;
  176. case PrologContentType.DefaultCollation:
  177. DefaultCollation = s;
  178. break;
  179. case PrologContentType.BaseUri:
  180. BaseUri = s;
  181. break;
  182. default:
  183. throw new XmlQueryCompileException ("Invalid XQuery prolog content was found.");
  184. }
  185. } else if (item is SchemaImport) {
  186. SchemaImports.Add (item as SchemaImport);
  187. } else if (item is ModuleImport) {
  188. ModuleImports.Add (item as ModuleImport);
  189. } else if (item is VariableDeclaration) {
  190. Variables.Add (item as VariableDeclaration);
  191. } else if (item is XmlSchemaContentProcessing) {
  192. ValidationType = (XmlSchemaContentProcessing) item;
  193. } else if (item is FunctionDeclaration) {
  194. Functions.Add (item as FunctionDeclaration);
  195. } else
  196. throw new XmlQueryCompileException ("Invalid XQuery prolog content item was found.");
  197. }
  198. }
  199. class XmlSpaceDecl
  200. {
  201. public XmlSpace Value;
  202. public XmlSpaceDecl (XmlSpace value)
  203. {
  204. Value = value;
  205. }
  206. }
  207. class ConstructionDecl
  208. {
  209. public XmlSpace Value;
  210. public ConstructionDecl (XmlSpace value)
  211. {
  212. Value = value;
  213. }
  214. }
  215. public class ModuleImportCollection : CollectionBase
  216. {
  217. public void Add (ModuleImport import)
  218. {
  219. List.Add (import);
  220. }
  221. }
  222. public class SchemaImportCollection : CollectionBase
  223. {
  224. public void Add (SchemaImport import)
  225. {
  226. List.Add (import);
  227. }
  228. }
  229. public enum PrologContentType {
  230. DefaultElementNamespace,
  231. DefaultFunctionNamespace,
  232. DefaultCollation,
  233. BaseUri
  234. }
  235. public class SimplePrologContent
  236. {
  237. public SimplePrologContent (PrologContentType type, string literalValue)
  238. {
  239. this.type = type;
  240. this.literalValue = literalValue;
  241. }
  242. PrologContentType type;
  243. string literalValue;
  244. public PrologContentType Type {
  245. get { return type; }
  246. set { type = value; }
  247. }
  248. public string LiteralValue {
  249. get { return literalValue; }
  250. set { literalValue = value; }
  251. }
  252. }
  253. public abstract class AbstractImport
  254. {
  255. public AbstractImport (string prefix, string ns, ICollection locations)
  256. {
  257. this.prefix = prefix;
  258. this.ns = ns;
  259. this.locations = locations;
  260. }
  261. string prefix, ns;
  262. ICollection locations;
  263. public string Prefix {
  264. get { return prefix; }
  265. set { prefix = value; }
  266. }
  267. public string Namespace {
  268. get { return ns; }
  269. set { ns = value; }
  270. }
  271. public ICollection Locations {
  272. get { return locations; }
  273. set { locations = value; }
  274. }
  275. }
  276. public class SchemaImport : AbstractImport
  277. {
  278. public SchemaImport (string prefix, string ns, ICollection schemaLocations)
  279. : base (prefix == "default element namespace" ? String.Empty : prefix, ns, schemaLocations)
  280. {
  281. // Prefix might 1) String.Empty for non-specified prefix,
  282. // 2) "default element namespace" that is as is specified
  283. // in xquery.
  284. if (prefix == "default element namespace")
  285. useDefaultElementNamespace = true;
  286. }
  287. bool useDefaultElementNamespace;
  288. public bool UseDefaultElementNamespace {
  289. get { return useDefaultElementNamespace; }
  290. set { useDefaultElementNamespace = value; }
  291. }
  292. }
  293. public class ModuleImport : AbstractImport
  294. {
  295. public ModuleImport (string prefix, string ns, ICollection moduleLocations)
  296. : base (prefix, ns, moduleLocations)
  297. {
  298. }
  299. }
  300. public class VariableDeclarationCollection : CollectionBase
  301. {
  302. public void Add (VariableDeclaration decl)
  303. {
  304. List.Add (decl);
  305. }
  306. }
  307. public class VariableDeclaration
  308. {
  309. public VariableDeclaration (XmlQualifiedName name, SequenceType type, ExprSequence varBody)
  310. {
  311. this.name = name;
  312. this.type = type;
  313. this.varBody = varBody; // might be null (just declaration).
  314. }
  315. XmlQualifiedName name;
  316. SequenceType type;
  317. ExprSequence varBody;
  318. public XmlQualifiedName Name {
  319. get { return name; }
  320. }
  321. public SequenceType VariableType {
  322. get { return type; }
  323. }
  324. public bool External {
  325. get { return varBody == null; }
  326. }
  327. public ExprSequence VariableBody {
  328. get { return varBody; }
  329. }
  330. }
  331. public class FunctionCollection : CollectionBase
  332. {
  333. public void Add (FunctionDeclaration decl)
  334. {
  335. List.Add (decl);
  336. }
  337. }
  338. public class FunctionDeclaration
  339. {
  340. public FunctionDeclaration (XmlQualifiedName name,
  341. FunctionParamList parameters,
  342. SequenceType type,
  343. EnclosedExpr expr)
  344. {
  345. this.name = name;
  346. this.parameters = parameters;
  347. this.returnType = type;
  348. this.funcBody = expr;
  349. }
  350. XmlQualifiedName name;
  351. SequenceType returnType;
  352. FunctionParamList parameters;
  353. EnclosedExpr funcBody;
  354. public XmlQualifiedName Name {
  355. get { return name; }
  356. }
  357. public SequenceType ReturnType {
  358. get { return returnType; }
  359. }
  360. public bool External {
  361. get { return funcBody == null; }
  362. }
  363. public FunctionParamList Parameters {
  364. get { return parameters; }
  365. }
  366. public EnclosedExpr FunctionBody {
  367. get { return funcBody; }
  368. }
  369. }
  370. public class FunctionParamList
  371. {
  372. ArrayList list = new ArrayList ();
  373. public void Add (FunctionParam p)
  374. {
  375. list.Add (p);
  376. }
  377. public void Insert (int pos, FunctionParam p)
  378. {
  379. list.Insert (pos, p);
  380. }
  381. }
  382. public class FunctionParam
  383. {
  384. XmlQualifiedName name;
  385. SequenceType type;
  386. public FunctionParam (XmlQualifiedName name, SequenceType type)
  387. {
  388. this.name = name;
  389. this.type = type;
  390. }
  391. }
  392. public abstract class PragmaMUExtensionBase
  393. {
  394. XmlQualifiedName name;
  395. string text;
  396. protected PragmaMUExtensionBase (XmlQualifiedName name, string text)
  397. {
  398. this.name = name;
  399. this.text = text;
  400. }
  401. public XmlQualifiedName Name {
  402. get { return name; }
  403. }
  404. public string Text {
  405. get { return text; }
  406. }
  407. }
  408. public class Pragma : PragmaMUExtensionBase
  409. {
  410. public Pragma (XmlQualifiedName name, string text)
  411. : base (name, text)
  412. {
  413. }
  414. }
  415. public class MUExtension : PragmaMUExtensionBase
  416. {
  417. public MUExtension (XmlQualifiedName name, string text)
  418. : base (name, text)
  419. {
  420. }
  421. }
  422. }
  423. #endif