XQueryASTCompiler.cs 7.6 KB

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