pass context to Evaluate* so that we dont have to clone the expression
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl / XslKey.cs
1 //
2 // XslKey.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
7 //      
8 // (C) 2003 Ben Maurer
9 // (C) 2003 Atsushi Enomoto
10 //
11
12 using System;
13 using System.CodeDom;
14 using System.Collections;
15 using System.Collections.Specialized;
16 using System.Xml;
17 using System.Xml.Schema;
18 using System.Xml.XPath;
19 using System.Xml.Xsl;
20
21 using QName = System.Xml.XmlQualifiedName;
22
23 namespace Mono.Xml.Xsl {
24         public class XslKey {
25                 QName name;
26                 XPathExpression usePattern;
27                 XPathExpression matchPattern;
28
29                 public XslKey (Compiler c)
30                 {
31                         this.name = c.ParseQNameAttribute ("name");
32                         
33                         usePattern = c.CompileExpression (c.GetAttribute ("use"));
34                         if (usePattern == null)
35                                 usePattern = c.CompileExpression (".");
36
37                         c.AssertAttribute ("match");
38                         this.matchPattern = c.CompileExpression (c.GetAttribute ("match"));
39                 }
40
41                 public QName Name { get { return name; }}
42                 public XPathExpression UsePattern { get { return usePattern; }}
43                 public XPathExpression MatchPattern { get { return matchPattern; }}
44                 
45                 public bool Matches (XPathNavigator nav, string value)
46                 {
47                         
48                         if (!nav.Matches (MatchPattern)) 
49                                 return false;
50                         Debug.WriteLine ("? " + nav.Name);
51                         switch (UsePattern.ReturnType)
52                         {
53                         case XPathResultType.NodeSet:
54                                 XPathNodeIterator matches = nav.Select (UsePattern);
55                                 while (matches.MoveNext ()) {
56                                         if (matches.Current.Value == value)
57                                                 return true;
58                                 }
59                                 
60                                 return false;
61                         case XPathResultType.Any:
62                                 
63                                 object o = nav.Evaluate (UsePattern);
64                                 if (o is XPathNodeIterator) {
65                                         XPathNodeIterator it = (XPathNodeIterator)o;
66                                         while (it.MoveNext ())
67                                                 if (it.Current.Value == value)
68                                                         return true;
69                                         return false;
70                                 } else {
71                                         return value == XPathFunctions.ToString (o);
72                                 }
73                         default:
74                                 return value == nav.EvaluateString (UsePattern, null, null);
75                         }
76                 }
77         }
78 }