XQueryASTCompiler.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // XQueryASTCompiler.cs - XQuery static context compiler
  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.IO;
  33. using System.Security.Policy;
  34. using System.Xml;
  35. using System.Xml.Query;
  36. using System.Xml.Schema;
  37. using Mono.Xml.XQuery;
  38. using Mono.Xml.XQuery.Parser;
  39. namespace Mono.Xml.XPath2
  40. {
  41. internal class XQueryASTCompiler
  42. {
  43. // Static method
  44. public static XQueryStaticContext Compile (XQueryModule module, XQueryCompileOptions options, Evidence evidence, XQueryCommandImpl commandImpl)
  45. {
  46. if (options == null)
  47. options = new XQueryCompileOptions ();
  48. return new XQueryASTCompiler (module, options, new XQueryCompileContext (), evidence, commandImpl).Compile ();
  49. }
  50. // Constructor
  51. private XQueryASTCompiler (XQueryModule module, XQueryCompileOptions options, XQueryCompileContext compileContext, Evidence evidence, XQueryCommandImpl commandImpl)
  52. {
  53. this.module = module;
  54. this.options = options;
  55. this.compileContext = compileContext;
  56. this.evidence = evidence;
  57. this.commandImpl = commandImpl;
  58. inScopeSchemas = new XmlSchemaSet ();
  59. localVariables = new Hashtable ();
  60. localFunctions = new XQueryFunctionTable ();
  61. }
  62. XQueryModule module;
  63. XQueryCompileOptions options;
  64. XQueryCompileContext compileContext;
  65. IXmlNamespaceResolver nsResolver;
  66. string defaultFunctionNamespace;
  67. // FIXME: Is it OK for an XmlSchema to be in two or more set?
  68. XmlSchemaSet inScopeSchemas;
  69. ArrayList libModuleContexts = new ArrayList ();
  70. Hashtable localVariables;
  71. XQueryFunctionTable localFunctions;
  72. bool preserveWhitespace; // Xml space policy
  73. bool constructionSpace; // construction mode
  74. bool defaultOrdered; // Ordering mode
  75. string baseUri;
  76. Evidence evidence;
  77. XQueryCommandImpl commandImpl;
  78. // methods.
  79. private XQueryStaticContext Compile ()
  80. {
  81. CompileProlog ();
  82. XQueryMainModule main = module as XQueryMainModule;
  83. ExprSequence expr = (main != null) ?
  84. CompileExprSequence (main.QueryBody) : null;
  85. return new XQueryStaticContext (
  86. options,
  87. compileContext,
  88. expr,
  89. inScopeSchemas,
  90. localVariables,
  91. localFunctions,
  92. module.NSResolver,
  93. module.Prolog.DefaultFunctionNamespace,
  94. preserveWhitespace,
  95. constructionSpace,
  96. defaultOrdered,
  97. baseUri,
  98. evidence,
  99. commandImpl);
  100. }
  101. private void CompileProlog ()
  102. {
  103. Prolog p = module.Prolog;
  104. // resolve external modules
  105. // FIXME: check if external queries are allowed by default.
  106. // FIXME: check recursion
  107. XmlUrlResolver res = new XmlUrlResolver ();
  108. foreach (ModuleImport modimp in p.ModuleImports) {
  109. foreach (string uri in modimp.Locations) {
  110. Stream s = res.GetEntity (res.ResolveUri (null, uri), null, typeof (Stream)) as Stream;
  111. XQueryLibraryModule ext = XQueryParser.Parse (new StreamReader (s)) as XQueryLibraryModule;
  112. if (ext == null)
  113. throw new XmlQueryCompileException (String.Format ("External module {0} is resolved as a main module, while it should be a library module."));
  114. XQueryStaticContext sctx = new XQueryASTCompiler (ext, options, compileContext, evidence, commandImpl).Compile ();
  115. libModuleContexts.Add (sctx);
  116. }
  117. }
  118. // resolve and compile in-scope schemas
  119. foreach (SchemaImport xsimp in p.SchemaImports) {
  120. foreach (string uri in xsimp.Locations) {
  121. XmlSchema schema = inScopeSchemas.Add (xsimp.Namespace, uri);
  122. compileContext.InEffectSchemas.Add (schema);
  123. }
  124. }
  125. inScopeSchemas.Compile ();
  126. CheckReferences ();
  127. ResolveVariableReferences ();
  128. // compile FunctionDeclaration into XQueryFunction
  129. foreach (FunctionDeclaration func in p.Functions.Values) {
  130. XQueryFunction cfunc = CompileFunction (func);
  131. localFunctions.Add (cfunc);
  132. }
  133. }
  134. private void CheckReferences ()
  135. {
  136. XQueryMainModule main = module as XQueryMainModule;
  137. if (main != null)
  138. main.QueryBody.CheckReference (this);
  139. foreach (FunctionDeclaration func in module.Prolog.Functions.Values) {
  140. if (!func.External)
  141. func.FunctionBody.CheckReference (this);
  142. CheckSchemaType (func.ReturnType);
  143. foreach (XQueryFunctionArgument param in func.Parameters)
  144. CheckSchemaType (param.Type);
  145. }
  146. }
  147. internal void CheckSchemaType (SequenceType type)
  148. {
  149. if (type == null)
  150. return;
  151. type.ItemType.CheckReference (this);
  152. }
  153. internal void CheckSchemaTypeName (XmlQualifiedName name)
  154. {
  155. XmlSchemaType type = XmlSchemaType.GetBuiltInType (name);
  156. if (type != null)
  157. return;
  158. throw new XmlQueryCompileException (String.Format ("Unresolved schema type name: {0}", name));
  159. }
  160. internal void CheckVariableName (XmlQualifiedName name)
  161. {
  162. // This should not be done, since unresolved QName
  163. // may be still valid in context of XmlArgumentList
  164. // which is supplied at dynamic evaluation phase.
  165. /*
  166. if (module.Prolog.Variables [name] != null)
  167. return;
  168. if (localVariables [name] != null)
  169. return;
  170. foreach (XQueryStaticContext ctx in libModuleContexts)
  171. if (ctx.InScopeVariables [name] != null)
  172. return;
  173. throw new XmlQueryCompileException (String.Format ("Unresolved variable name: {0}", name));
  174. */
  175. }
  176. internal void CheckFunctionName (XmlQualifiedName name)
  177. {
  178. if (XQueryFunction.FindKnownFunction (name) != null)
  179. return;
  180. if (module.Prolog.Functions [name] != null)
  181. return;
  182. foreach (XQueryStaticContext ctx in libModuleContexts)
  183. if (ctx.InScopeFunctions [name] != null)
  184. return;
  185. throw new XmlQueryCompileException (String.Format ("Unresolved function name: {0}", name));
  186. }
  187. private void ResolveVariableReferences ()
  188. {
  189. // TODO
  190. }
  191. internal XmlSchemaType ResolveSchemaType (XmlQualifiedName name)
  192. {
  193. XmlSchemaType type = XmlSchemaType.GetBuiltInType (name);
  194. if (type != null)
  195. return type;
  196. type = inScopeSchemas.GlobalTypes [name] as XmlSchemaType;
  197. if (type != null)
  198. return type;
  199. return null;
  200. }
  201. private XQueryFunction CompileFunction (FunctionDeclaration func)
  202. {
  203. if (func.External)
  204. return XQueryFunction.FromQName (func.Name);
  205. return new XQueryUserFunction (func.Name, func.Parameters.ToArray (), func.FunctionBody.Expr, func.ReturnType);
  206. }
  207. private ExprSequence CompileExprSequence (ExprSequence expr)
  208. {
  209. for (int i = 0; i < expr.Count; i++)
  210. expr [i] = expr [i].Compile (this);
  211. return expr;
  212. }
  213. internal void CheckType (ExprSingle expr, SequenceType type)
  214. {
  215. if (!expr.StaticType.CanConvertTo (type))
  216. throw new XmlQueryCompileException (String.Format ("Cannot convert type from {0} to {1}", expr.StaticType, type));
  217. }
  218. internal XQueryFunction ResolveFunction (XmlQualifiedName name)
  219. {
  220. XQueryFunction func = XQueryFunction.FindKnownFunction (name);
  221. if (func == null)
  222. func = localFunctions [name];
  223. if (func != null)
  224. return func;
  225. else
  226. throw new XmlQueryCompileException ("Could not find specified function.");
  227. }
  228. }
  229. }
  230. #endif