gluezilla/src:
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl / XslFunctions.cs
1 //
2 // XsltCompiledContext.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Atsushi Enomoto (atsushi@ximian.com)
7 // (C) 2003 Ben Maurer
8 // (C) 2004 Atsushi Enomoto
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 using System;
32 using System.Collections;
33 using System.Globalization;
34 using System.Reflection;
35 using System.Text;
36 using System.Xml;
37 using System.Xml.XPath;
38 using System.Xml.Xsl;
39 using Mono.Xml.Xsl;
40
41 using QName = System.Xml.XmlQualifiedName;
42
43 namespace Mono.Xml.Xsl
44 {
45         internal abstract class XPFuncImpl : IXsltContextFunction 
46         {
47                 int minargs, maxargs;
48                 XPathResultType returnType;
49                 XPathResultType [] argTypes;
50
51                 public XPFuncImpl () {}
52                 public XPFuncImpl (int minArgs, int maxArgs, XPathResultType returnType, XPathResultType[] argTypes)
53                 {
54                         this.Init(minArgs, maxArgs, returnType, argTypes);
55                 }
56                 
57                 protected void Init (int minArgs, int maxArgs, XPathResultType returnType, XPathResultType[] argTypes)
58                 {
59                         this.minargs    = minArgs;
60                         this.maxargs    = maxArgs;
61                         this.returnType = returnType;
62                         this.argTypes   = argTypes;
63                 }
64
65                 public int Minargs { get { return this.minargs; }}
66                 public int Maxargs { get { return this.maxargs; }}
67                 public XPathResultType ReturnType { get { return this.returnType; }}
68                 public XPathResultType [] ArgTypes { get { return this.argTypes; }}
69                 public object Invoke (XsltContext xsltContext, object [] args, XPathNavigator docContext)
70                 {
71                         return Invoke ((XsltCompiledContext)xsltContext, args, docContext);
72                 }
73                 
74                 public abstract object Invoke (XsltCompiledContext xsltContext, object [] args, XPathNavigator docContext);
75                 
76                 public static XPathResultType GetXPathType (Type type, XPathNavigator node) {
77                         switch (Type.GetTypeCode(type)) {
78                         case TypeCode.String:
79                                 return XPathResultType.String;
80                         case TypeCode.Boolean:
81                                 return XPathResultType.Boolean;
82                         case TypeCode.Object:
83                                 if (typeof (XPathNavigator).IsAssignableFrom (type) || typeof (IXPathNavigable).IsAssignableFrom (type))
84                                         return XPathResultType.Navigator;
85                                 
86                                 if (typeof (XPathNodeIterator).IsAssignableFrom (type))
87                                         return XPathResultType.NodeSet;
88                                 
89                                 return XPathResultType.Any;
90                         case TypeCode.DateTime :
91                                 throw new XsltException ("Invalid type DateTime was specified.", null, node);
92                         default: // Numeric
93                                 return XPathResultType.Number;
94                         } 
95                 }
96         }
97         
98         class XsltExtensionFunction : XPFuncImpl 
99         {
100                 private object extension;
101                 private MethodInfo method;
102                 private TypeCode [] typeCodes;
103
104                 public XsltExtensionFunction (object extension, MethodInfo method, XPathNavigator currentNode)
105                 {
106                         this.extension = extension;
107                         this.method = method;
108
109                         ParameterInfo [] parameters = method.GetParameters ();
110                         int minArgs = parameters.Length;
111                         int maxArgs = parameters.Length;
112                         
113                         this.typeCodes = new TypeCode [parameters.Length];
114                         XPathResultType[] argTypes = new XPathResultType [parameters.Length];
115                         
116                         bool canBeOpt = true;
117                         for (int i = parameters.Length - 1; 0 <= i; i--) { // optionals at the end
118                                 typeCodes [i] = Type.GetTypeCode (parameters [i].ParameterType);
119                                 argTypes [i] = GetXPathType (parameters [i].ParameterType, currentNode);
120                                 if (canBeOpt) {
121                                         if (parameters[i].IsOptional)
122                                                 minArgs --;
123                                         else
124                                                 canBeOpt = false;
125                                 }
126                         }
127                         base.Init (minArgs, maxArgs, GetXPathType (method.ReturnType, currentNode), argTypes);
128                 }
129
130                 public override object Invoke (XsltCompiledContext xsltContext, object [] args, XPathNavigator docContext)
131                 {
132                         try {
133                                 ParameterInfo [] pis = method.GetParameters ();
134                                 object [] castedArgs = new object [pis.Length];
135                                 for (int i = 0; i < args.Length; i++) {
136                                         Type t = pis [i].ParameterType;
137                                         switch (t.FullName) {
138                                         case "System.Int16":
139                                         case "System.UInt16":
140                                         case "System.Int32":
141                                         case "System.UInt32":
142                                         case "System.Int64":
143                                         case "System.UInt64":
144                                         case "System.Single":
145                                         case "System.Decimal":
146                                                 castedArgs [i] = Convert.ChangeType (args [i], t);
147                                                 break;
148                                         default:
149                                                 castedArgs [i] = args [i];
150                                                 break;
151                                         }
152                                 }
153
154                                 object result = null;
155                                 switch (method.ReturnType.FullName) {
156                                 case "System.Int16":
157                                 case "System.UInt16":
158                                 case "System.Int32":
159                                 case "System.UInt32":
160                                 case "System.Int64":
161                                 case "System.UInt64":
162                                 case "System.Single":
163                                 case "System.Decimal":
164                                         result = Convert.ChangeType (method.Invoke (extension, castedArgs), typeof (double));
165                                         break;
166                                 default:
167                                         result = method.Invoke(extension, castedArgs);
168                                         break;
169                                 }
170                                 IXPathNavigable navigable = result as IXPathNavigable;
171                                 if (navigable != null)
172                                         return navigable.CreateNavigator ();
173
174                                 return result;
175                         } catch (Exception ex) {
176                                 throw new XsltException ("Custom function reported an error.", ex);
177 //                              Debug.WriteLine ("****** INCORRECT RESOLUTION **********");
178                         }
179                 }
180         }
181         
182         class XsltCurrent : XPathFunction 
183         {
184                 public XsltCurrent (FunctionArguments args) : base (args)
185                 {
186                         if (args != null)
187                                 throw new XPathException ("current takes 0 args");
188                 }
189                 
190                 public override XPathResultType ReturnType { get { return XPathResultType.NodeSet; }}
191
192                 public override object Evaluate (BaseIterator iter)
193                 {
194                         return new SelfIterator ((iter.NamespaceManager as XsltCompiledContext).Processor.CurrentNode, null);
195                 }
196
197                 internal override bool Peer {
198                         get { return false; }
199                 }
200
201                 public override string ToString ()
202                 {
203                         return "current()";
204                 }
205         }
206         
207         class XsltDocument : XPathFunction 
208         {
209                 Expression arg0, arg1;
210                 XPathNavigator doc;
211                 
212                 public XsltDocument (FunctionArguments args, Compiler c) : base (args)
213                 {
214                         if (args == null || (args.Tail != null && args.Tail.Tail != null))
215                                 throw new XPathException ("document takes one or two args");
216                         
217                         arg0 = args.Arg;
218                         if (args.Tail != null)
219                                 arg1 = args.Tail.Arg;
220                         doc = c.Input.Clone ();
221                 }
222                 public override XPathResultType ReturnType { get { return XPathResultType.NodeSet; }}
223
224                 internal override bool Peer {
225                         get { return arg0.Peer && (arg1 != null ? arg1.Peer : true); }
226                 }
227
228                 public override object Evaluate (BaseIterator iter)
229                 {
230                         string baseUri = null;
231                         if (arg1 != null) {
232                                 XPathNodeIterator it = arg1.EvaluateNodeSet (iter);
233                                 if (it.MoveNext())
234                                         baseUri = it.Current.BaseURI;
235                                 else
236                                         baseUri = VoidBaseUriFlag;
237                         }
238
239                         object o = arg0.Evaluate (iter);
240                         if (o is XPathNodeIterator)
241                                 return GetDocument ((iter.NamespaceManager as XsltCompiledContext), (XPathNodeIterator)o, baseUri);
242                         else
243                                 return GetDocument ((iter.NamespaceManager as XsltCompiledContext), o is IFormattable ? ((IFormattable) o).ToString (null, CultureInfo.InvariantCulture) : o.ToString (), baseUri);
244                 }
245                 
246                 static string VoidBaseUriFlag = "&^)(*&%*^$&$VOID!BASE!URI!";
247                 
248                 Uri Resolve (string thisUri, string baseUri, XslTransformProcessor p)
249                 {
250 //                      Debug.WriteLine ("THIS: " + thisUri);
251 //                      Debug.WriteLine ("BASE: " + baseUri);
252                         XmlResolver r = p.Resolver;
253                         if (r == null)
254                                 return null;
255                         Uri uriBase = null;
256                         if (! object.ReferenceEquals (baseUri, VoidBaseUriFlag) && baseUri != String.Empty)
257                                 uriBase = r.ResolveUri (null, baseUri);
258                                 
259                         return r.ResolveUri (uriBase, thisUri);
260                 }
261                 
262                 XPathNodeIterator GetDocument (XsltCompiledContext xsltContext, XPathNodeIterator itr, string baseUri)
263                 {
264                         ArrayList list = new ArrayList ();
265                         try {
266                                 Hashtable got = new Hashtable ();
267                         
268                                 while (itr.MoveNext()) {
269                                         Uri uri = Resolve (itr.Current.Value, baseUri != null ? baseUri : /*itr.Current.BaseURI*/doc.BaseURI, xsltContext.Processor);
270                                         if (!got.ContainsKey (uri)) {
271                                                 got.Add (uri, null);
272                                                 if (uri != null && uri.ToString () == "") {
273                                                         XPathNavigator n = doc.Clone ();
274                                                         n.MoveToRoot ();
275                                                         list.Add (n);
276                                                 } else
277                                                         list.Add (xsltContext.Processor.GetDocument (uri));
278                                         }
279                                 }
280                         } catch (Exception) {
281                                 // Error recovery.
282                                 // See http://www.w3.org/TR/xslt#document and
283                                 // bug #75663.
284                                 list.Clear ();
285                         }
286                         return new ListIterator (list, xsltContext);
287                 }
288         
289                 XPathNodeIterator GetDocument (XsltCompiledContext xsltContext, string arg0, string baseUri)
290                 {
291                         try {
292                                 Uri uri = Resolve (arg0, baseUri != null ? baseUri : doc.BaseURI, xsltContext.Processor);
293                                 XPathNavigator n;
294                                 if (uri != null && uri.ToString () == "") {
295                                         n = doc.Clone ();
296                                         n.MoveToRoot ();
297                                 } else
298                                         n = xsltContext.Processor.GetDocument (uri);
299                         
300                                 return new SelfIterator (n, xsltContext);
301                         } catch (Exception) {
302                                 return new ListIterator (new ArrayList (), xsltContext);
303                         }
304                 }
305
306                 public override string ToString ()
307                 {
308                         return String.Concat ("document(",
309                                 arg0.ToString (),
310                                 arg1 != null ? "," : String.Empty,
311                                 arg1 != null ? arg1.ToString () : String.Empty,
312                                 ")");
313                 }
314         }
315         
316         class XsltElementAvailable : XPathFunction 
317         {
318                 Expression arg0;
319                 IStaticXsltContext ctx;
320                 
321                 public XsltElementAvailable (FunctionArguments args, IStaticXsltContext ctx) : base (args)
322                 {
323                         if (args == null || args.Tail != null)
324                                 throw new XPathException ("element-available takes 1 arg");
325                         
326                         arg0 = args.Arg;
327                         this.ctx = ctx;
328                 }
329                 
330                 public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
331
332                 internal override bool Peer {
333                         get { return arg0.Peer; }
334                 }
335
336                 public override object Evaluate (BaseIterator iter)
337                 {
338                         QName name = XslNameUtil.FromString (arg0.EvaluateString (iter), ctx);
339
340                         return (
341                                 (name.Namespace == Compiler.XsltNamespace) &&
342                                 (
343                                         //
344                                         // A list of all the instructions (does not include top-level-elements)
345                                         //
346                                         name.Name == "apply-imports" ||
347                                         name.Name == "apply-templates" ||
348                                         name.Name == "call-template" ||
349                                         name.Name == "choose" ||
350                                         name.Name == "comment" ||
351                                         name.Name == "copy" ||
352                                         name.Name == "copy-of" ||
353                                         name.Name == "element" ||
354                                         name.Name == "fallback" ||
355                                         name.Name == "for-each" ||
356                                         name.Name == "message" ||
357                                         name.Name == "number" ||
358                                         name.Name == "processing-instruction" ||
359                                         name.Name == "text" ||
360                                         name.Name == "value-of" ||
361                                         name.Name == "variable"
362                                 )
363                         );
364                 }
365         }
366
367         class XsltFormatNumber : XPathFunction 
368         {
369                 Expression arg0, arg1, arg2;
370                 IStaticXsltContext ctx;
371                 
372                 public XsltFormatNumber (FunctionArguments args, IStaticXsltContext ctx) : base (args)
373                 {
374                         if (args == null || args.Tail == null || (args.Tail.Tail != null && args.Tail.Tail.Tail != null))
375                                 throw new XPathException ("format-number takes 2 or 3 args");
376                         
377                         arg0 = args.Arg;
378                         arg1 = args.Tail.Arg;
379                         if (args.Tail.Tail != null) {
380                                 arg2= args.Tail.Tail.Arg;
381                                 this.ctx = ctx;
382                         }
383                 }
384                 public override XPathResultType ReturnType { get { return XPathResultType.String; }}
385
386                 internal override bool Peer {
387                         get { return arg0.Peer && arg1.Peer && (arg2 != null ? arg2.Peer : true); }
388                 }
389                 
390                 public override object Evaluate (BaseIterator iter)
391                 {
392                         double d = arg0.EvaluateNumber (iter);
393                         string s = arg1.EvaluateString (iter);
394                         QName nm = QName.Empty;
395                         
396                         if (arg2 != null)
397                                 nm = XslNameUtil.FromString (arg2.EvaluateString (iter), ctx);
398                         
399                         try {
400                                 return (iter.NamespaceManager as XsltCompiledContext).Processor.CompiledStyle
401                                 .LookupDecimalFormat (nm).FormatNumber (d, s);
402                         } catch (ArgumentException ex) {
403                                 throw new XsltException (ex.Message, ex, iter.Current);
404                         }
405                 }
406         }
407         
408         class XsltFunctionAvailable : XPathFunction 
409         {
410                 Expression arg0;
411                 IStaticXsltContext ctx;
412                 
413                 public XsltFunctionAvailable (FunctionArguments args, IStaticXsltContext ctx) : base (args)
414                 {
415                         if (args == null || args.Tail != null)
416                                 throw new XPathException ("element-available takes 1 arg");
417                         
418                         arg0 = args.Arg;
419                         this.ctx = ctx;
420                 }
421                 
422                 public override XPathResultType ReturnType { get { return XPathResultType.Boolean; }}
423
424                 internal override bool Peer {
425                         get { return arg0.Peer; }
426                 }
427                 
428                 public override object Evaluate (BaseIterator iter)
429                 {
430                         
431                         string name = arg0.EvaluateString (iter);
432                         int colon = name.IndexOf (':');
433                         // extension function
434                         if (colon > 0)
435                                 return (iter.NamespaceManager as XsltCompiledContext).ResolveFunction (
436                                         XslNameUtil.FromString (name, ctx),
437                                         null) != null;
438                         
439                         return (
440                                 //
441                                 // XPath
442                                 //
443                                 name == "boolean" ||
444                                 name == "ceiling" ||
445                                 name == "concat" ||
446                                 name == "contains" ||
447                                 name == "count" ||
448                                 name == "false" ||
449                                 name == "floor" ||
450                                 name == "id"||
451                                 name == "lang" ||
452                                 name == "last" ||
453                                 name == "local-name" ||
454                                 name == "name" ||
455                                 name == "namespace-uri" ||
456                                 name == "normalize-space" ||
457                                 name == "not" ||
458                                 name == "number" ||
459                                 name == "position" ||
460                                 name == "round" ||
461                                 name == "starts-with" ||
462                                 name == "string" ||
463                                 name == "string-length" ||
464                                 name == "substring" ||
465                                 name == "substring-after" ||
466                                 name == "substring-before" ||
467                                 name == "sum" ||
468                                 name == "translate" ||
469                                 name == "true" ||
470                                 // XSLT
471                                 name == "document" ||
472                                 name == "format-number" ||
473                                 name == "function-available" ||
474                                 name == "generate-id" ||
475                                 name == "key" ||
476                                 name == "current" ||
477                                 name == "unparsed-entity-uri" ||
478                                 name == "element-available" ||
479                                 name == "system-property"
480                         );
481                 }
482         } 
483
484         class XsltGenerateId : XPathFunction 
485         {
486                 //FIXME: generate short string, not the huge thing it makes now
487                 Expression arg0;
488                 public XsltGenerateId (FunctionArguments args) : base (args)
489                 {
490                         if (args != null) {
491                                 if (args.Tail != null)
492                                         throw new XPathException ("generate-id takes 1 or no args");
493                                 arg0 = args.Arg;
494                         }
495                 }
496                 
497                 public override XPathResultType ReturnType { get { return XPathResultType.String; }}
498
499                 internal override bool Peer {
500                         get { return arg0.Peer; }
501                 }
502
503                 public override object Evaluate (BaseIterator iter)
504                 {
505                         XPathNavigator n;
506                         if (arg0 != null) {
507                                 XPathNodeIterator itr = arg0.EvaluateNodeSet (iter);
508                                 if (itr.MoveNext ())
509                                         n = itr.Current.Clone ();
510                                 else
511                                         return string.Empty; // empty nodeset == empty string
512                         } else
513                                 n = iter.Current.Clone ();
514                         
515                         StringBuilder sb = new StringBuilder ("Mono"); // Ensure begins with alpha
516                         sb.Append (XmlConvert.EncodeLocalName (n.BaseURI));
517                         sb.Replace ('_', 'm'); // remove underscores from EncodeLocalName
518                         sb.Append (n.NodeType);
519                         sb.Append ('m');
520
521                         do {
522                                 sb.Append (IndexInParent (n));
523                                 sb.Append ('m');
524                         } while (n.MoveToParent ());
525                         
526                         return sb.ToString ();
527                 }
528                 
529                 int IndexInParent (XPathNavigator nav)
530                 {
531                         int n = 0;
532                         while (nav.MoveToPrevious ())
533                                 n++;
534                         
535                         return n;
536                 }
537         } 
538         
539         class XsltKey : XPathFunction 
540         {
541                 Expression arg0, arg1;
542                 IStaticXsltContext staticContext;
543                 
544                 public XsltKey (FunctionArguments args, IStaticXsltContext ctx) : base (args)
545                 {
546                         staticContext = ctx;
547                         if (args == null || args.Tail == null)
548                                 throw new XPathException ("key takes 2 args");
549                         arg0 = args.Arg;
550                         arg1 = args.Tail.Arg;
551                 }
552                 public Expression KeyName { get { return arg0; } }
553                 public Expression Field { get { return arg1; } }
554                 public override XPathResultType ReturnType { get { return XPathResultType.NodeSet; }}
555
556                 internal override bool Peer {
557                         get { return arg0.Peer && arg1.Peer; }
558                 }
559
560                 public bool PatternMatches (XPathNavigator nav, XsltContext nsmgr)
561                 {
562                         XsltCompiledContext ctx = nsmgr as XsltCompiledContext;
563                         // for key pattern, it must contain literal value
564                         return ctx.MatchesKey (nav, staticContext,
565                                 arg0.StaticValueAsString,
566                                 arg1.StaticValueAsString);
567                 }
568
569                 public override object Evaluate (BaseIterator iter)
570                 {
571                         XsltCompiledContext ctx = iter.NamespaceManager
572                                 as XsltCompiledContext;
573                         return ctx.EvaluateKey (staticContext, iter, arg0, arg1);
574                 }
575         }
576         
577         class XsltSystemProperty : XPathFunction 
578         {
579                 Expression arg0;
580                 IStaticXsltContext ctx;
581                 
582                 public XsltSystemProperty (FunctionArguments args, IStaticXsltContext ctx) : base (args)
583                 {
584                         if (args == null || args.Tail != null)
585                                 throw new XPathException ("system-property takes 1 arg");
586                         
587                         arg0 = args.Arg;
588                         this.ctx = ctx;
589                 }
590                 
591                 public override XPathResultType ReturnType { get { return XPathResultType.String; }}
592
593                 internal override bool Peer {
594                         get { return arg0.Peer; }
595                 }
596
597                 public override object Evaluate (BaseIterator iter)
598                 {
599                         QName name = XslNameUtil.FromString (arg0.EvaluateString (iter), ctx);
600                         
601                         if (name.Namespace == Compiler.XsltNamespace) {
602                                 switch (name.Name) {
603                                         case "version": return "1.0";
604                                         case "vendor": return "Mono";
605                                         case "vendor-url": return "http://www.go-mono.com/";
606                                 }
607                         }
608                         
609                         return "";
610                 }
611         } 
612
613         class XsltUnparsedEntityUri : XPathFunction 
614         {
615                 Expression arg0;
616                 
617                 public XsltUnparsedEntityUri (FunctionArguments args) : base (args)
618                 {
619                         if (args == null || args.Tail != null)
620                                 throw new XPathException ("unparsed-entity-uri takes 1 arg");
621                         
622                         arg0 = args.Arg;
623                 }
624                 
625                 public override XPathResultType ReturnType { get { return XPathResultType.String; }}
626
627                 internal override bool Peer {
628                         get { return arg0.Peer; }
629                 }
630
631                 public override object Evaluate (BaseIterator iter)
632                 {
633                         IHasXmlNode xn = iter.Current as IHasXmlNode;
634                         if (xn == null)
635                                 return String.Empty;
636                         XmlNode n = xn.GetNode ();
637                         if (n.OwnerDocument == null)
638                                 return String.Empty;
639                         XmlDocumentType doctype = n.OwnerDocument.DocumentType;
640                         if (doctype == null)
641                                 return String.Empty;
642                         XmlEntity ent = doctype.Entities.GetNamedItem (arg0.EvaluateString (iter)) as XmlEntity;
643                         if (ent == null)
644                                 return String.Empty;
645                         return ent.SystemId != null ? ent.SystemId : String.Empty;
646                 }
647         }
648
649         class MSXslNodeSet : XPathFunction
650         {
651                 Expression arg0;
652
653                 public MSXslNodeSet (FunctionArguments args) : base (args)
654                 {
655                         if (args == null || args.Tail != null)
656                                 throw new XPathException ("element-available takes 1 arg");
657                         
658                         arg0 = args.Arg;
659                 }
660
661                 public override XPathResultType ReturnType {
662                         get {
663                                 return XPathResultType.NodeSet;
664                         }
665                 }
666
667                 internal override bool Peer {
668                         get { return arg0.Peer; }
669                 }
670
671                 public override object Evaluate (BaseIterator iter)
672                 {
673                         XsltCompiledContext ctx = iter.NamespaceManager as XsltCompiledContext;
674                         XPathNavigator loc = iter.Current != null ? iter.Current.Clone () : null;
675                         XPathNavigator nav = arg0.EvaluateAs (iter, XPathResultType.Navigator) as XPathNavigator;
676                         if (nav == null) {
677                                 if (loc != null)
678                                         return new XsltException ("Cannot convert the XPath argument to a result tree fragment.", null, loc);
679                                 else
680                                         return new XsltException ("Cannot convert the XPath argument to a result tree fragment.", null);
681                         }
682                         ArrayList al = new ArrayList ();
683                         al.Add (nav);
684                         return new ListIterator (al, ctx);
685                 }
686         }
687 }