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

XQueryDefaultFunctionCall.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: 7eb4e280563f2266ef3a41242f069cdb8ede2ab7 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//
// XQueryDefaultFunctionCall.cs
//
// 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.
//

//
// XQuery 1.0 and XPath 2.0 Functions implementation as XPathItemExpression.
// See XQuery 1.0 and XPath 2.0 Functions and Operators.
//
#if NET_2_0
using System;
using System.Collections;
using System.Reflection;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Query;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace Mono.Xml.XPath2
{
	internal abstract class DefaultFunctionCall : FunctionCallExprBase
	{
		static Hashtable qnameTable = new Hashtable ();
		static XmlQualifiedName GetName (string name)
		{
			XmlQualifiedName qname = qnameTable [name] as XmlQualifiedName;
			if (qname == null) {
				qname = new XmlQualifiedName (name, XQueryFunction.Namespace);
				qnameTable.Add (name, qname);
			}
			return qname;
		}

		public DefaultFunctionCall (XQueryStaticContext ctx, string name, int minArgs, int maxArgs, SequenceType type, ExprSequence args)
			: base (GetName (name), args)
		{
			this.type = type;
			this.minArgs = minArgs;
			this.maxArgs = maxArgs;
		}

		SequenceType type;
		int minArgs;
		int maxArgs;

		public override int MinArgs { get { return minArgs; } }
		public override int MaxArgs { get { return maxArgs; } }

		public override SequenceType StaticType {
			get { return type; }
		}
	}

	// Accessors

	// 2.1 fn:node-name ($arg as node ()?) as xs:QName?
	internal class FnNodeNameCall : DefaultFunctionCall
	{
		public FnNodeNameCall (XQueryStaticContext ctx, ExprSequence args)
			: base (ctx, "node-name", 1, 1, SequenceType.Create (XmlSchemaSimpleType.XsQName, Occurence.Optional), args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence iter)
		{
			XPathSequence res = Args [0].Evaluate (iter);
			if (!res.MoveNext ())
				return new XPathEmptySequence (iter.Context);
			// FIXME: what happens if context item is not a node.
			XPathNavigator nav = res.Current as XPathNavigator;
			if (nav == null || nav.LocalName == String.Empty)
				return new XPathEmptySequence (iter.Context);
			return new SingleItemIterator (new XPathAtomicValue (new XmlQualifiedName (nav.LocalName, nav.NamespaceURI), XmlSchemaSimpleType.XsQName), iter);
		}
	}

	// 2.2 fn:nilled ($arg as node()) as xs:boolean?
	internal class FnNilledCall : DefaultFunctionCall
	{
		public FnNilledCall (XQueryStaticContext ctx, XPathItemExpression [] args)
			: base (ctx, "nilled", 1, 1, SequenceType.Create (XmlSchemaSimpleType.XsBoolean, Occurence.One), args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence iter)
		{
			XPathSequence res = Args [0].Evaluate (iter);
			if (!res.MoveNext ())
				return new XPathEmptySequence (iter.Context);
			XPathNavigator nav = res.Current as XPathNavigator;
			IXmlSchemaInfo info = nav.NodeType == XPathNodeType.Element ? nav.SchemaInfo : null;
			if (info != null)
				return new SingleItemIterator (new XPathAtomicValue (info.IsNil, null), iter);
			else
				return new XPathEmptySequence (iter.Context);
		}
	}

	// 2.3 fn:string ($arg as item()?) as xs:string
	internal class FnStringCall : DefaultFunctionCall
	{
		public FnStringCall (XQueryStaticContext ctx, XPathItemExpression [] args)
			: base (ctx, "string", 0, 1, SequenceType.Create (XmlSchemaSimpleType.XsString, Occurence.Optional), args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence iter)
		{
			XPathItem item = null;
			if (Args.Length == 0)
				item = iter.Context.CurrentItem;
			else {
				XPathSequence res = Args [0].Evaluate (iter);
				if (!res.MoveNext ())
					return new XPathEmptySequence (iter.Context);
				item = res.Current;
			}
			return new SingleItemIterator (new XPathAtomicValue (Core (item), null), iter);
		}

		private string Core (XPathItem item)
		{
			XPathNavigator nav = item as XPathNavigator;
			
			return nav != null ? nav.Value : XQueryConvert.ItemToString (item);
		}
	}

	// 2.4 fn:data ($arg as item()*) as xdt:anyAtomicType*
	internal class FnDataCall : DefaultFunctionCall
	{
		public FnDataCall (XQueryStaticContext ctx, XPathItemExpression [] args)
			: base (ctx, "data", 1, 1, SequenceType.Create (XmlSchemaComplexType.AnyType, Occurence.ZeroOrMore), args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence iter)
		{
			return new AtomizingIterator (Args [0].Evaluate (iter));
		}
	}

	// 2.5 fn:base-uri ($arg as node()?) as xs:anyURI?
	internal class FnBaseUriCall : DefaultFunctionCall
	{
		public FnBaseUriCall (XQueryStaticContext ctx, XPathItemExpression [] args)
			: base (ctx, "base-uri", 1, 1, SequenceType.Create (XmlSchemaSimpleType.XsAnyUri, Occurence.Optional), args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence iter)
		{
			XPathSequence res = Args [0].Evaluate (iter);
			if (res.MoveNext ())
				return new XPathEmptySequence (iter.Context);
			XPathNavigator nav = res.Current as XPathNavigator;
			if (nav == null)
				return new XPathEmptySequence (iter.Context);
			else
				return new SingleItemIterator (new XPathAtomicValue (nav.BaseURI, XmlSchemaSimpleType.XsString), iter);
		}
	}

	// 2.6 fn:document-uri ($arg as node()?) as xs:anyURI?
	internal class FnDocumentUriCall : DefaultFunctionCall
	{
		public FnDocumentUriCall (XQueryStaticContext ctx, XPathItemExpression [] args)
			: base (ctx, "document-uri", 1, 1, SequenceType.Create (XmlSchemaSimpleType.XsAnyUri, Occurence.Optional), args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence iter)
		{
			XPathSequence res = Args [0].Evaluate (iter);
			if (res.MoveNext ())
				return new XPathEmptySequence (iter.Context);
			XPathNavigator nav = res.Current as XPathNavigator;
			if (nav == null)
				return new XPathEmptySequence (iter.Context);
			nav = nav.Clone ();
			nav.MoveToRoot ();
			return new SingleItemIterator (new XPathAtomicValue (nav.BaseURI, null), iter);
		}
	}

	// 3 fn:error ()
	//   fn:error ($error as xs:QName)
	//   fn:error ($error as xs:QName, $description as xs:string)
	//   fn:error ($error as xs:QName, $description as xs:string, $error-object as item()*)
	internal class FnErrorCall : DefaultFunctionCall
	{
		public FnErrorCall (XQueryStaticContext ctx, XPathItemExpression [] args)
			// FIXME: return type is actually none
			: base (ctx, "error", 0, 3, SequenceType.AnyType, args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence iter)
		{
			// error name
			XPathSequence errorNameIter = Args.Length > 0 ? Args [0].Evaluate (iter) : null;
			XmlQualifiedName errorType = XmlQualifiedName.Empty;
			if (errorNameIter != null && errorNameIter.MoveNext ())
				errorType = XQueryConvert.ItemToQName (errorNameIter.Current);

			// description
			string description = Args.Length > 1 ? Args [1].EvaluateAsString (iter) : String.Empty;

			// error-object
			XPathSequence errorObjIter = Args.Length > 2 ? Args [2].Evaluate (iter) : null;

			// FIXME: add error-object information
			throw new XmlQueryException (errorType + description);
		}
	}

	// 4 trace ($value as item()*, $label as xs:string) as item()*
	internal class FnTraceCall : DefaultFunctionCall
	{
		public FnTraceCall (XQueryStaticContext ctx, XPathItemExpression [] args)
			: base (ctx, "trace", 2, 2, SequenceType.Create (XmlSchemaComplexType.AnyType, Occurence.ZeroOrMore), args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence  iter)
		{
			return new TracingIterator (Args [0].Evaluate (iter), Args [1].EvaluateAsString (iter));
		}
	}

	// 5 constructor functions
	internal class AtomicConstructorCall : DefaultFunctionCall
	{
		// FIXME: use IXmlNamespaceResolver.LookupPrefix() in ctx
		public AtomicConstructorCall (XQueryStaticContext ctx, SequenceType type, XPathItemExpression [] args)
			: base (ctx, type.SchemaType.QualifiedName.Name, 1, 1, type, args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence iter)
		{
			return new SingleItemIterator (XQueryConvert.ItemToItem (Atomize (Args [0].Evaluate (iter)), null), iter);
		}
	}

	// 6 functions on numerics (operators are not defined here)

	// 6.4.1 fn:abs ($arg as numeric?) as numeric?
	internal class FnAbsCall : DefaultFunctionCall
	{
		public FnAbsCall (XQueryStaticContext ctx, XPathItemExpression [] args)
			: base (ctx, "abs", 1, 1, args [0].StaticType, args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence iter)
		{
			XPathSequence arg = Args [0].Evaluate (iter);
			if (!arg.MoveNext ())
				return new XPathEmptySequence (iter.Context);
			XPathAtomicValue a = null;
			// FIXME: use schema type IsDerivedFrom()
			switch (Type.GetTypeCode (arg.Current.ValueType)) {
			case TypeCode.Int64:
				return new SingleItemIterator (new XPathAtomicValue (System.Math.Abs (arg.Current.ValueAsInt64), arg.Current.XmlType), iter);
			case TypeCode.Int32:
				return new SingleItemIterator (new XPathAtomicValue (System.Math.Abs (arg.Current.ValueAsInt32), arg.Current.XmlType), iter);
			case TypeCode.Double:
				return new SingleItemIterator (new XPathAtomicValue (System.Math.Abs (arg.Current.ValueAsDouble), arg.Current.XmlType), iter);
			case TypeCode.Decimal:
				return new SingleItemIterator (new XPathAtomicValue (System.Math.Abs (arg.Current.ValueAsDecimal), arg.Current.XmlType), iter);
			case TypeCode.Single:
				return new SingleItemIterator (new XPathAtomicValue (System.Math.Abs (arg.Current.ValueAsSingle), arg.Current.XmlType), iter);
			}
			return new XPathEmptySequence (iter.Context);
		}
	}

	// 6.4.2 fn:ceiling ($arg as numeric?) as numeric?
	internal class FnCeilingCall : DefaultFunctionCall
	{
		public FnCeilingCall (XQueryStaticContext ctx, XPathItemExpression [] args)
			: base (ctx, "ceiling", 1, 1, args [0].StaticType, args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence iter)
		{
			throw new NotImplementedException ();
		}
	}

	// 6.4.3 fn:floor ($arg as numeric?) as numeric?
	internal class FnFloorCall : DefaultFunctionCall
	{
		public FnFloorCall (XQueryStaticContext ctx, XPathItemExpression [] args)
			: base (ctx, "floor", 1, 1, args [0].StaticType, args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence iter)
		{
			throw new NotImplementedException ();
		}
	}

	// 6.4.4 fn:round ($arg as numeric?) as numeric?
	internal class FnRoundCall : DefaultFunctionCall
	{
		public FnRoundCall (XQueryStaticContext ctx, XPathItemExpression [] args)
			: base (ctx, "round", 1, 1, args [0].StaticType, args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence iter)
		{
			throw new NotImplementedException ();
		}
	}

	// 6.4.5 fn:round-half-to-even ($arg as numeric?) as numeric?
	internal class FnRoundHalfToEvenCall : DefaultFunctionCall
	{
		public FnRoundHalfToEvenCall (XQueryStaticContext ctx, XPathItemExpression [] args)
			: base (ctx, "round-half-to-even", 1, 2, args [0].StaticType, args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence iter)
		{
			throw new NotImplementedException ();
		}
	}

	// 7.2.1 fn:codepoints-to-string ($arg as xs:integer*) as xs:string
	internal class FnCodepointsToStringCall : DefaultFunctionCall
	{
		public FnCodepointsToStringCall (XQueryStaticContext ctx, XPathItemExpression [] args)
			: base (ctx, "codepoints-to-string", 1, 1, SequenceType.IntegerList, args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence iter)
		{
			throw new NotImplementedException ();
		}
	}

	internal class FnStringCallToCodepointsCall : DefaultFunctionCall
	{
		public FnStringCallToCodepointsCall (XQueryStaticContext ctx, XPathItemExpression [] args)
			: base (ctx, "string-to-codepoints", 1, 1, SequenceType.Create (XmlSchemaSimpleType.XsString, Occurence.Optional), args)
		{
		}

		public override XPathSequence Evaluate (XPathSequence iter)
		{
			throw new NotImplementedException ();
		}
	}
}
#endif