remove executable bit from changelogs
[mono.git] / mcs / class / Mono.Xml.Ext / Mono.Xml.XPath2 / XQueryModuleProlog.cs
1 //
2 // XQueryModuleProlog.cs - abstract syntax tree for XQuery
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
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
29 #if NET_2_0
30
31 using System;
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.Xml;
35 using System.Xml.Query;
36 using System.Xml.Schema;
37 using Mono.Xml.XQuery;
38
39 namespace Mono.Xml.XPath2
40 {
41         internal abstract class XQueryModule
42         {
43                 string version;
44                 Prolog prolog;
45                 IXmlNamespaceResolver nsResolver;
46
47                 public string Version {
48                         get { return version; }
49                         set { version = value; }
50                 }
51
52                 public Prolog Prolog {
53                         get { return prolog; }
54                         set { prolog = value; }
55                 }
56
57                 public IXmlNamespaceResolver NSResolver {
58                         get { return nsResolver; }
59                         set { nsResolver = value; }
60                 }
61         }
62
63         internal class XQueryMainModule : XQueryModule
64         {
65                 ExprSequence queryBody;
66
67                 public ExprSequence QueryBody {
68                         get { return queryBody; }
69                         set { queryBody = value; }
70                 }
71         }
72
73         internal class XQueryLibraryModule : XQueryModule
74         {
75                 ModuleDecl moduleDecl;
76
77                 public ModuleDecl ModuleDecl {
78                         get { return moduleDecl; }
79                         set { moduleDecl = value; }
80                 }
81         }
82
83         internal class ModuleDecl
84         {
85                 string prefix;
86                 string ns;
87                 
88                 public string Prefix {
89                         get { return prefix; }
90                         set { prefix = value; }
91                 }
92
93                 public string Namespace {
94                         get { return ns; }
95                         set { ns = value; }
96                 }
97         }
98
99         internal class Prolog
100         {
101                 public Prolog ()
102                 {
103                         namespaceDecls = new StringDictionary ();
104                         schemaImports = new SchemaImportCollection ();
105                         moduleImports = new ModuleImportCollection ();
106                         variables = new XQueryVariableTable ();
107                         functions = new FunctionCollection ();
108                 }
109
110                 string version;
111                 StringDictionary namespaceDecls;
112                 XmlSpace xmlSpaceDecl;
113                 XmlSpace constructorDecl;
114                 string defaultElementNamespace;
115                 string defaultFunctionNamespace;
116                 string defaultCollation;
117                 string baseUri;
118                 bool defaultOrdered; // false by default
119                 SchemaImportCollection schemaImports;
120                 ModuleImportCollection moduleImports;
121                 XQueryVariableTable variables;
122                 XmlSchemaContentProcessing validationType;
123                 FunctionCollection functions;
124
125                 public string Version {
126                         get { return version; }
127                         set { version = value; }
128                 }
129
130                 public StringDictionary NamespaceDecls {
131                         get { return namespaceDecls; }
132                 }
133
134                 public XmlSpace XmlSpace {
135                         get { return xmlSpaceDecl; }
136                         set { xmlSpaceDecl = value; }
137                 }
138
139                 public XmlSpace Constructor {
140                         get { return constructorDecl; }
141                         set { constructorDecl = value; }
142                 }
143
144                 public bool DefaultOrdered {
145                         get { return defaultOrdered; }
146                         set { defaultOrdered = value; }
147                 }
148
149                 public string DefaultElementNamespace {
150                         get { return defaultElementNamespace; }
151                         set { defaultElementNamespace = value; }
152                 }
153
154                 public string DefaultFunctionNamespace {
155                         get { return defaultFunctionNamespace; }
156                         set { defaultFunctionNamespace = value; }
157                 }
158
159                 public string DefaultCollation {
160                         get { return defaultCollation; }
161                         set { defaultCollation = value; }
162                 }
163
164                 public string BaseUri {
165                         get { return baseUri; }
166                         set { baseUri = value; }
167                 }
168
169                 public SchemaImportCollection SchemaImports {
170                         get { return schemaImports; }
171                 }
172
173                 public ModuleImportCollection ModuleImports {
174                         get { return moduleImports; }
175                 }
176
177                 public XQueryVariableTable Variables {
178                         get { return variables; }
179                 }
180
181                 public XmlSchemaContentProcessing ValidationType {
182                         get { return validationType; }
183                         set { validationType = value; }
184                 }
185
186                 public FunctionCollection Functions {
187                         get { return functions; }
188                 }
189
190                 public void Add (object item)
191                 {
192                         if (item is bool)
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;
204                                 switch (c.Type) {
205                                         case PrologContentType.DefaultElementNamespace:
206                                                 DefaultElementNamespace = s;
207                                                 break;
208                                         case PrologContentType.DefaultFunctionNamespace:
209                                                 DefaultFunctionNamespace = s;
210                                                 break;
211                                         case PrologContentType.DefaultCollation:
212                                                 DefaultCollation = s;
213                                                 break;
214                                         case PrologContentType.BaseUri:
215                                                 BaseUri = s;
216                                                 break;
217                                         default:
218                                                 throw new XmlQueryCompileException ("Invalid XQuery prolog content was found.");
219                                 }
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;
226                                 Variables.Add (var);
227                         } else if (item is XmlSchemaContentProcessing) {
228                                 ValidationType = (XmlSchemaContentProcessing) item;
229                         } else if (item is FunctionDeclaration) {
230                                 Functions.Add (item as FunctionDeclaration);
231                         } else
232                                 throw new XmlQueryCompileException ("Invalid XQuery prolog content item was found.");
233                 }
234         }
235
236         class XmlSpaceDecl
237         {
238                 public XmlSpace Value;
239
240                 public XmlSpaceDecl (XmlSpace value)
241                 {
242                         Value = value;
243                 }
244         }
245
246         class ConstructionDecl
247         {
248                 public XmlSpace Value;
249
250                 public ConstructionDecl (XmlSpace value)
251                 {
252                         Value = value;
253                 }
254         }
255
256         public class ModuleImportCollection : CollectionBase
257         {
258                 public void Add (ModuleImport import)
259                 {
260                         List.Add (import);
261                 }
262         }
263
264         public class SchemaImportCollection : CollectionBase
265         {
266                 public void Add (SchemaImport import)
267                 {
268                         List.Add (import);
269                 }
270         }
271
272         public enum PrologContentType {
273                 DefaultElementNamespace,
274                 DefaultFunctionNamespace,
275                 DefaultCollation,
276                 BaseUri
277         }
278
279         public class SimplePrologContent
280         {
281                 public SimplePrologContent (PrologContentType type, string literalValue)
282                 {
283                         this.type = type;
284                         this.literalValue = literalValue;
285                 }
286
287                 PrologContentType type;
288                 string literalValue;
289
290                 public PrologContentType Type {
291                         get { return type; }
292                         set { type = value; }
293                 }
294
295                 public string LiteralValue {
296                         get { return literalValue; }
297                         set { literalValue = value; }
298                 }
299         }
300
301         public abstract class AbstractImport
302         {
303                 public AbstractImport (string prefix, string ns, ICollection locations)
304                 {
305                         this.prefix = prefix;
306                         this.ns = ns;
307                         this.locations = locations;
308                         if (locations == null)
309                                 this.locations = new ArrayList (); // empty list
310                 }
311
312                 string prefix, ns;
313                 ICollection locations;
314
315                 public string Prefix {
316                         get { return prefix; }
317                         set { prefix = value; }
318                 }
319
320                 public string Namespace {
321                         get { return ns; }
322                         set { ns = value; }
323                 }
324
325                 public ICollection Locations {
326                         get { return locations; }
327                         set { locations = value != null ? value : new ArrayList (); }
328                 }
329         }
330
331         public class SchemaImport : AbstractImport
332         {
333                 public SchemaImport (string prefix, string ns, ICollection schemaLocations)
334                         : base (prefix == "default element namespace" ? String.Empty : prefix, ns, schemaLocations)
335                 {
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;
341                 }
342
343                 bool useDefaultElementNamespace;
344
345                 public bool UseDefaultElementNamespace {
346                         get { return useDefaultElementNamespace; }
347                         set { useDefaultElementNamespace = value; }
348                 }
349         }
350
351         public class ModuleImport : AbstractImport
352         {
353                 public ModuleImport (string prefix, string ns, ICollection moduleLocations)
354                         : base (prefix, ns, moduleLocations)
355                 {
356                 }
357         }
358
359         public class XQueryVariableTable : DictionaryBase
360         {
361                 public void Add (XQueryVariable decl)
362                 {
363                         Dictionary.Add (decl.Name, decl);
364                 }
365
366                 public ICollection Keys {
367                         get { return Dictionary.Keys; }
368                 }
369
370                 public ICollection Values {
371                         get { return Dictionary.Values; }
372                 }
373
374                 public XQueryVariable this [XmlQualifiedName name] {
375                         get { return Dictionary [name] as XQueryVariable; }
376                 }
377         }
378
379         public class XQueryVariable
380         {
381                 public XQueryVariable (XmlQualifiedName name, SequenceType type, ExprSequence varBody)
382                 {
383                         this.name = name;
384                         this.type = type;
385                         this.varBody = varBody; // might be null (just declaration).
386                 }
387
388                 XmlQualifiedName name;
389                 SequenceType type;
390                 ExprSequence varBody;
391
392                 public XmlQualifiedName Name {
393                         get { return name; }
394                 }
395
396                 public SequenceType VariableType {
397                         get { return type; }
398                 }
399
400                 public bool External {
401                         get { return varBody == null; }
402                 }
403
404                 public ExprSequence VariableBody {
405                         get { return varBody; }
406                 }
407         }
408
409         internal class FunctionCollection : DictionaryBase
410         {
411                 public void Add (FunctionDeclaration decl)
412                 {
413                         Dictionary.Add (decl.Name, decl);
414                 }
415
416                 public ICollection Keys {
417                         get { return Dictionary.Keys; }
418                 }
419
420                 public ICollection Values {
421                         get { return Dictionary.Values; }
422                 }
423
424                 public FunctionDeclaration this [XmlQualifiedName name] {
425                         get { return Dictionary [name] as FunctionDeclaration; }
426                 }
427         }
428
429         internal class FunctionDeclaration
430         {
431                 public FunctionDeclaration (XmlQualifiedName name,
432                         XQueryFunctionArgumentList parameters,
433                         SequenceType type,
434                         EnclosedExpr expr)
435                 {
436                         this.name = name;
437                         this.parameters = parameters;
438                         this.returnType = type;
439                         this.funcBody = expr;
440                 }
441
442                 XmlQualifiedName name;
443                 SequenceType returnType;
444                 XQueryFunctionArgumentList parameters;
445                 EnclosedExpr funcBody;
446
447                 public XmlQualifiedName Name {
448                         get { return name; }
449                 }
450
451                 public SequenceType ReturnType {
452                         get { return returnType; }
453                 }
454
455                 public bool External {
456                         get { return funcBody == null; }
457                 }
458
459                 public XQueryFunctionArgumentList Parameters {
460                         get { return parameters; }
461                 }
462
463                 public EnclosedExpr FunctionBody {
464                         get { return funcBody; }
465                 }
466         }
467
468         public class XQueryFunctionArgumentList : CollectionBase
469         {
470                 public void Add (XQueryFunctionArgument p)
471                 {
472                         List.Add (p);
473                 }
474
475                 public void Insert (int pos, XQueryFunctionArgument p)
476                 {
477                         List.Insert (pos, p);
478                 }
479
480                 public XQueryFunctionArgument this [int i] {
481                         get { return (XQueryFunctionArgument) List [i]; }
482                 }
483
484                 public XQueryFunctionArgument [] ToArray ()
485                 {
486                         XQueryFunctionArgument [] arr = new XQueryFunctionArgument [List.Count];
487                         List.CopyTo (arr, 0);
488                         return arr;
489                 }
490         }
491
492         public abstract class PragmaMUExtensionBase
493         {
494                 XmlQualifiedName name;
495                 string text;
496
497                 protected PragmaMUExtensionBase (XmlQualifiedName name, string text)
498                 {
499                         this.name = name;
500                         this.text = text;
501                 }
502
503                 public XmlQualifiedName Name {
504                         get { return name; }
505                 }
506
507                 public string Text {
508                         get { return text; }
509                 }
510         }
511
512         public class Pragma : PragmaMUExtensionBase
513         {
514                 public Pragma (XmlQualifiedName name, string text)
515                         : base (name, text)
516                 {
517                 }
518         }
519
520         public class MUExtension : PragmaMUExtensionBase
521         {
522                 public MUExtension (XmlQualifiedName name, string text)
523                         : base (name, text)
524                 {
525                 }
526         }
527 }
528
529 #endif