2 // XQueryModuleProlog.cs - abstract syntax tree for XQuery
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.Collections.Specialized;
35 using System.Xml.Query;
36 using System.Xml.Schema;
37 using Mono.Xml.XQuery;
39 namespace Mono.Xml.XPath2
41 internal abstract class XQueryModule
45 IXmlNamespaceResolver nsResolver;
47 public string Version {
48 get { return version; }
49 set { version = value; }
52 public Prolog Prolog {
53 get { return prolog; }
54 set { prolog = value; }
57 public IXmlNamespaceResolver NSResolver {
58 get { return nsResolver; }
59 set { nsResolver = value; }
63 internal class XQueryMainModule : XQueryModule
65 ExprSequence queryBody;
67 public ExprSequence QueryBody {
68 get { return queryBody; }
69 set { queryBody = value; }
73 internal class XQueryLibraryModule : XQueryModule
75 ModuleDecl moduleDecl;
77 public ModuleDecl ModuleDecl {
78 get { return moduleDecl; }
79 set { moduleDecl = value; }
83 internal class ModuleDecl
88 public string Prefix {
89 get { return prefix; }
90 set { prefix = value; }
93 public string Namespace {
103 namespaceDecls = new StringDictionary ();
104 schemaImports = new SchemaImportCollection ();
105 moduleImports = new ModuleImportCollection ();
106 variables = new XQueryVariableTable ();
107 functions = new FunctionCollection ();
111 StringDictionary namespaceDecls;
112 XmlSpace xmlSpaceDecl;
113 XmlSpace constructorDecl;
114 string defaultElementNamespace;
115 string defaultFunctionNamespace;
116 string defaultCollation;
118 bool defaultOrdered; // false by default
119 SchemaImportCollection schemaImports;
120 ModuleImportCollection moduleImports;
121 XQueryVariableTable variables;
122 XmlSchemaContentProcessing validationType;
123 FunctionCollection functions;
125 public string Version {
126 get { return version; }
127 set { version = value; }
130 public StringDictionary NamespaceDecls {
131 get { return namespaceDecls; }
134 public XmlSpace XmlSpace {
135 get { return xmlSpaceDecl; }
136 set { xmlSpaceDecl = value; }
139 public XmlSpace Constructor {
140 get { return constructorDecl; }
141 set { constructorDecl = value; }
144 public bool DefaultOrdered {
145 get { return defaultOrdered; }
146 set { defaultOrdered = value; }
149 public string DefaultElementNamespace {
150 get { return defaultElementNamespace; }
151 set { defaultElementNamespace = value; }
154 public string DefaultFunctionNamespace {
155 get { return defaultFunctionNamespace; }
156 set { defaultFunctionNamespace = value; }
159 public string DefaultCollation {
160 get { return defaultCollation; }
161 set { defaultCollation = value; }
164 public string BaseUri {
165 get { return baseUri; }
166 set { baseUri = value; }
169 public SchemaImportCollection SchemaImports {
170 get { return schemaImports; }
173 public ModuleImportCollection ModuleImports {
174 get { return moduleImports; }
177 public XQueryVariableTable Variables {
178 get { return variables; }
181 public XmlSchemaContentProcessing ValidationType {
182 get { return validationType; }
183 set { validationType = value; }
186 public FunctionCollection Functions {
187 get { return functions; }
190 public void Add (object item)
193 DefaultOrdered = (bool) item;
194 else if (item is XmlQualifiedName) {
195 XmlQualifiedName q = (XmlQualifiedName) item;
196 NamespaceDecls.Add (q.Name, q.Namespace);
197 } else if (item is XmlSpaceDecl) {
198 XmlSpace = ((XmlSpaceDecl) item).Value;
199 } else if (item is ConstructionDecl) {
200 Constructor = ((ConstructionDecl) item).Value;
201 } else if (item is SimplePrologContent) {
202 SimplePrologContent c = (SimplePrologContent) item;
203 string s = c.LiteralValue;
205 case PrologContentType.DefaultElementNamespace:
206 DefaultElementNamespace = s;
208 case PrologContentType.DefaultFunctionNamespace:
209 DefaultFunctionNamespace = s;
211 case PrologContentType.DefaultCollation:
212 DefaultCollation = s;
214 case PrologContentType.BaseUri:
218 throw new XmlQueryCompileException ("Invalid XQuery prolog content was found.");
220 } else if (item is SchemaImport) {
221 SchemaImports.Add (item as SchemaImport);
222 } else if (item is ModuleImport) {
223 ModuleImports.Add (item as ModuleImport);
224 } else if (item is XQueryVariable) {
225 XQueryVariable var = item as XQueryVariable;
227 } else if (item is XmlSchemaContentProcessing) {
228 ValidationType = (XmlSchemaContentProcessing) item;
229 } else if (item is FunctionDeclaration) {
230 Functions.Add (item as FunctionDeclaration);
232 throw new XmlQueryCompileException ("Invalid XQuery prolog content item was found.");
238 public XmlSpace Value;
240 public XmlSpaceDecl (XmlSpace value)
246 class ConstructionDecl
248 public XmlSpace Value;
250 public ConstructionDecl (XmlSpace value)
256 public class ModuleImportCollection : CollectionBase
258 public void Add (ModuleImport import)
264 public class SchemaImportCollection : CollectionBase
266 public void Add (SchemaImport import)
272 public enum PrologContentType {
273 DefaultElementNamespace,
274 DefaultFunctionNamespace,
279 public class SimplePrologContent
281 public SimplePrologContent (PrologContentType type, string literalValue)
284 this.literalValue = literalValue;
287 PrologContentType type;
290 public PrologContentType Type {
292 set { type = value; }
295 public string LiteralValue {
296 get { return literalValue; }
297 set { literalValue = value; }
301 public abstract class AbstractImport
303 public AbstractImport (string prefix, string ns, ICollection locations)
305 this.prefix = prefix;
307 this.locations = locations;
308 if (locations == null)
309 this.locations = new ArrayList (); // empty list
313 ICollection locations;
315 public string Prefix {
316 get { return prefix; }
317 set { prefix = value; }
320 public string Namespace {
325 public ICollection Locations {
326 get { return locations; }
327 set { locations = value != null ? value : new ArrayList (); }
331 public class SchemaImport : AbstractImport
333 public SchemaImport (string prefix, string ns, ICollection schemaLocations)
334 : base (prefix == "default element namespace" ? String.Empty : prefix, ns, schemaLocations)
336 // Prefix might 1) String.Empty for non-specified prefix,
337 // 2) "default element namespace" that is as is
338 // specified in xquery.
339 if (prefix == "default element namespace")
340 useDefaultElementNamespace = true;
343 bool useDefaultElementNamespace;
345 public bool UseDefaultElementNamespace {
346 get { return useDefaultElementNamespace; }
347 set { useDefaultElementNamespace = value; }
351 public class ModuleImport : AbstractImport
353 public ModuleImport (string prefix, string ns, ICollection moduleLocations)
354 : base (prefix, ns, moduleLocations)
359 public class XQueryVariableTable : DictionaryBase
361 public void Add (XQueryVariable decl)
363 Dictionary.Add (decl.Name, decl);
366 public ICollection Keys {
367 get { return Dictionary.Keys; }
370 public ICollection Values {
371 get { return Dictionary.Values; }
374 public XQueryVariable this [XmlQualifiedName name] {
375 get { return Dictionary [name] as XQueryVariable; }
379 public class XQueryVariable
381 public XQueryVariable (XmlQualifiedName name, SequenceType type, ExprSequence varBody)
385 this.varBody = varBody; // might be null (just declaration).
388 XmlQualifiedName name;
390 ExprSequence varBody;
392 public XmlQualifiedName Name {
396 public SequenceType VariableType {
400 public bool External {
401 get { return varBody == null; }
404 public ExprSequence VariableBody {
405 get { return varBody; }
409 internal class FunctionCollection : DictionaryBase
411 public void Add (FunctionDeclaration decl)
413 Dictionary.Add (decl.Name, decl);
416 public ICollection Keys {
417 get { return Dictionary.Keys; }
420 public ICollection Values {
421 get { return Dictionary.Values; }
424 public FunctionDeclaration this [XmlQualifiedName name] {
425 get { return Dictionary [name] as FunctionDeclaration; }
429 internal class FunctionDeclaration
431 public FunctionDeclaration (XmlQualifiedName name,
432 XQueryFunctionArgumentList parameters,
437 this.parameters = parameters;
438 this.returnType = type;
439 this.funcBody = expr;
442 XmlQualifiedName name;
443 SequenceType returnType;
444 XQueryFunctionArgumentList parameters;
445 EnclosedExpr funcBody;
447 public XmlQualifiedName Name {
451 public SequenceType ReturnType {
452 get { return returnType; }
455 public bool External {
456 get { return funcBody == null; }
459 public XQueryFunctionArgumentList Parameters {
460 get { return parameters; }
463 public EnclosedExpr FunctionBody {
464 get { return funcBody; }
468 public class XQueryFunctionArgumentList : CollectionBase
470 public void Add (XQueryFunctionArgument p)
475 public void Insert (int pos, XQueryFunctionArgument p)
477 List.Insert (pos, p);
480 public XQueryFunctionArgument this [int i] {
481 get { return (XQueryFunctionArgument) List [i]; }
484 public XQueryFunctionArgument [] ToArray ()
486 XQueryFunctionArgument [] arr = new XQueryFunctionArgument [List.Count];
487 List.CopyTo (arr, 0);
492 public abstract class PragmaMUExtensionBase
494 XmlQualifiedName name;
497 protected PragmaMUExtensionBase (XmlQualifiedName name, string text)
503 public XmlQualifiedName Name {
512 public class Pragma : PragmaMUExtensionBase
514 public Pragma (XmlQualifiedName name, string text)
520 public class MUExtension : PragmaMUExtensionBase
522 public MUExtension (XmlQualifiedName name, string text)