Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XQueryASTCompiler.cs « Mono.Xml.XPath2 « Mono.Xml.Ext « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2d18e9e676c44186dafaa4841dec4ded2285d90e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//
// XQueryASTCompiler.cs - XQuery static context compiler
//
// Author:
//	Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#if NET_2_0

using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Security.Policy;
using System.Xml;
using System.Xml.Query;
using System.Xml.Schema;
using Mono.Xml.XQuery;
using Mono.Xml.XQuery.Parser;
using Mono.Xml;

namespace Mono.Xml.XPath2
{
	internal class XQueryASTCompiler
	{
		// Static method

		public static XQueryStaticContext Compile (XQueryModule module, XQueryCompileOptions options, Evidence evidence, XQueryCommandImpl commandImpl)
		{
			if (options == null)
				options = new XQueryCompileOptions ();
			return new XQueryASTCompiler (module, options, new XQueryCompileContext (), evidence, commandImpl).Compile ();
		}

		// Constructor

		private XQueryASTCompiler (XQueryModule module, XQueryCompileOptions options, XQueryCompileContext compileContext, Evidence evidence, XQueryCommandImpl commandImpl)
		{
			this.module = module;
			this.options = options;
			this.compileContext = compileContext;
			this.evidence = evidence;
			this.commandImpl = commandImpl;

			inScopeSchemas = new XmlSchemaSet ();
			localVariables = new Hashtable ();
			localFunctions = new XQueryFunctionTable ();
		}

		XQueryModule module;
		XQueryCompileOptions options;
		XQueryCompileContext compileContext;

		IXmlNamespaceResolver nsResolver;
		string defaultFunctionNamespace;

		// FIXME: Is it OK for an XmlSchema to be in two or more set?
		XmlSchemaSet inScopeSchemas;
		ArrayList libModuleContexts = new ArrayList ();

		Hashtable localVariables;
		XQueryFunctionTable localFunctions;

		bool preserveWhitespace; // Xml space policy
		bool constructionSpace; // construction mode
		bool defaultOrdered; // Ordering mode
		string baseUri;
		Evidence evidence;
		XQueryCommandImpl commandImpl;

		// methods.

		private XQueryStaticContext Compile ()
		{
			CompileProlog ();

			XQueryMainModule main = module as XQueryMainModule;
			ExprSequence expr = (main != null) ?
				CompileExprSequence (main.QueryBody) : null;

			return new XQueryStaticContext (
				options,
				compileContext,
				expr,
				inScopeSchemas,
				localVariables,
				localFunctions,
				module.NSResolver,
				module.Prolog.DefaultFunctionNamespace,
				preserveWhitespace,
				constructionSpace,
				defaultOrdered,
				baseUri,
				evidence,
				commandImpl);
		}

		private void CompileProlog ()
		{
			Prolog p = module.Prolog;

			// resolve external modules
			// FIXME: check if external queries are allowed by default.
			// FIXME: check recursion
			XmlUrlResolver res = new XmlUrlResolver ();
			foreach (ModuleImport modimp in p.ModuleImports) {
				foreach (string uri in modimp.Locations) {
					Stream s = res.GetEntity (res.ResolveUri (null, uri), null, typeof (Stream)) as Stream;
					XQueryLibraryModule ext = XQueryParser.Parse (new StreamReader (s)) as XQueryLibraryModule;
					if (ext == null)
						throw new XmlQueryCompileException (String.Format ("External module {0} is resolved as a main module, while it should be a library module."));
					XQueryStaticContext sctx = new XQueryASTCompiler (ext, options, compileContext, evidence, commandImpl).Compile ();
					libModuleContexts.Add (sctx);
				}
			}

			// resolve and compile in-scope schemas
			foreach (SchemaImport xsimp in p.SchemaImports) {
				foreach (string uri in xsimp.Locations) {
					XmlSchema schema = inScopeSchemas.Add (xsimp.Namespace, uri);
					compileContext.InEffectSchemas.Add (schema);
				}
			}
			inScopeSchemas.Compile ();

			CheckReferences ();

			ResolveVariableReferences ();

			// compile FunctionDeclaration into XQueryFunction
			foreach (FunctionDeclaration func in p.Functions.Values) {
				XQueryFunction cfunc = CompileFunction (func);
				localFunctions.Add (cfunc);
			}
		}

		private void CheckReferences ()
		{
			XQueryMainModule main = module as XQueryMainModule;
			if (main != null)
				main.QueryBody.CheckReference (this);
			foreach (FunctionDeclaration func in module.Prolog.Functions.Values) {
				if (!func.External)
					func.FunctionBody.CheckReference (this);
				CheckSchemaType (func.ReturnType);
				foreach (XQueryFunctionArgument param in func.Parameters)
					CheckSchemaType (param.Type);
			}
		}

		internal void CheckSchemaType (SequenceType type)
		{
			if (type == null)
				return;
			type.ItemType.CheckReference (this);
		}

		internal void CheckSchemaTypeName (XmlQualifiedName name)
		{
			XmlSchemaType type = InternalPool.GetBuiltInType (name);
			if (type != null)
				return;
			throw new XmlQueryCompileException (String.Format ("Unresolved schema type name: {0}", name));
		}

		internal void CheckVariableName (XmlQualifiedName name)
		{
			// This should not be done, since unresolved QName
			// may be still valid in context of XmlArgumentList
			// which is supplied at dynamic evaluation phase.
			/*
			if (module.Prolog.Variables [name] != null)
				return;
			if (localVariables [name] != null)
				return;
			foreach (XQueryStaticContext ctx in libModuleContexts)
				if (ctx.InScopeVariables [name] != null)
					return;
			throw new XmlQueryCompileException (String.Format ("Unresolved variable name: {0}", name));
			*/
		}

		internal void CheckFunctionName (XmlQualifiedName name)
		{
			if (XQueryFunction.FindKnownFunction (name) != null)
				return;
			if (module.Prolog.Functions [name] != null)
				return;
			foreach (XQueryStaticContext ctx in libModuleContexts)
				if (ctx.InScopeFunctions [name] != null)
					return;
			throw new XmlQueryCompileException (String.Format ("Unresolved function name: {0}", name));
		}

		private void ResolveVariableReferences ()
		{
			// TODO
		}

		internal XmlSchemaType ResolveSchemaType (XmlQualifiedName name)
		{
			XmlSchemaType type = InternalPool.GetBuiltInType (name);
			if (type != null)
				return type;
			type = inScopeSchemas.GlobalTypes [name] as XmlSchemaType;
			if (type != null)
				return type;
			return null;
		}

		private XQueryFunction CompileFunction (FunctionDeclaration func)
		{
			if (func.External)
				return XQueryFunction.FromQName (func.Name);
			return new XQueryUserFunction (func.Name, func.Parameters.ToArray (), func.FunctionBody.Expr, func.ReturnType);
		}

		private ExprSequence CompileExprSequence (ExprSequence expr)
		{
			for (int i = 0; i < expr.Count; i++)
				expr [i] = expr [i].Compile (this);
			return expr;
		}

		internal void CheckType (ExprSingle expr, SequenceType type)
		{
			if (!expr.StaticType.CanConvertTo (type))
				throw new XmlQueryCompileException (String.Format ("Cannot convert type from {0} to {1}", expr.StaticType, type));
		}

		internal XQueryFunction ResolveFunction (XmlQualifiedName name)
		{
			XQueryFunction func = XQueryFunction.FindKnownFunction (name);
			if (func == null)
				func = localFunctions [name];

			if (func != null)
				return func;
			else
				throw new XmlQueryCompileException ("Could not find specified function.");
		}
	}
}
#endif