Merge pull request #357 from Suremaker/master
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl / XsltCompiledContext.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 Novell Inc.
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
32 using System;
33 using System.Collections;
34 using System.Collections.Specialized;
35 using System.Xml;
36 using System.Xml.Schema;
37 using System.Xml.XPath;
38 using System.Xml.Xsl;
39 using System.Text;
40 using System.IO;
41 using Mono.Xml.Xsl.Operations;
42 using System.Reflection;
43
44 using BF = System.Reflection.BindingFlags;
45 using QName = System.Xml.XmlQualifiedName;
46
47
48 namespace Mono.Xml.Xsl 
49 {
50         internal class XsltCompiledContext : XsltContext 
51         {
52                 class XsltContextInfo
53                 {
54                         public bool IsCData;
55                         public bool PreserveWhitespace = true;
56                         public string ElementPrefix;
57                         public string ElementNamespace;
58
59                         public void Clear ()
60                         {
61                                 IsCData = false;
62                                 PreserveWhitespace = true;
63                                 ElementPrefix = ElementNamespace = null;
64                         }
65                 }
66
67                 Hashtable keyNameCache = new Hashtable ();
68                 Hashtable keyIndexTables = new Hashtable ();
69                 Hashtable patternNavCaches = new Hashtable ();
70
71                 XslTransformProcessor p;
72                 XsltContextInfo [] scopes;
73                 int scopeAt = 0;
74                 
75
76                 public XslTransformProcessor Processor { get { return p; }}
77                         
78                 public XsltCompiledContext (XslTransformProcessor p) : base (new NameTable ())
79                 {
80                         this.p = p;
81                         scopes = new XsltContextInfo [10];
82                         for (int i = 0; i < 10; i++)
83                                 scopes [i] = new XsltContextInfo ();
84                 }
85
86                 public override string DefaultNamespace { get { return String.Empty; }}
87
88                 public XPathNavigator GetNavCache (Mono.Xml.XPath.Pattern p, XPathNavigator node)
89                 {
90                         XPathNavigator nav =
91                                 patternNavCaches [p] as XPathNavigator;
92                         if (nav == null || !nav.MoveTo (node)) {
93                                 nav = node.Clone ();
94                                 patternNavCaches [p] = nav;
95                         }
96                         return nav;
97                 }
98
99                 public object EvaluateKey (IStaticXsltContext staticContext,
100                         BaseIterator iter,
101                         Expression nameExpr, Expression valueExpr)
102                 {
103                         QName name = GetKeyName (staticContext, iter, nameExpr);
104                         KeyIndexTable table = GetIndexTable (name);
105                         return table.Evaluate (iter, valueExpr);
106                 }
107
108                 public bool MatchesKey (XPathNavigator nav,
109                         IStaticXsltContext staticContext,
110                         string name, string value)
111                 {
112                         QName qname = XslNameUtil.FromString (name, staticContext);
113                         KeyIndexTable table = GetIndexTable (qname);
114                         return table.Matches (nav, value, this);
115                 }
116
117                 private QName GetKeyName (IStaticXsltContext staticContext,
118                         BaseIterator iter, Expression nameExpr)
119                 {
120                         QName name = null;
121                         if (nameExpr.HasStaticValue) {
122                                 name = (QName) keyNameCache [nameExpr];
123                                 if (name == null) {
124                                         name = XslNameUtil.FromString (
125                                                 nameExpr.EvaluateString (iter),
126                                                 staticContext);
127                                         keyNameCache [nameExpr] = name;
128                                 }
129                         }
130                         else
131                                 name = XslNameUtil.FromString (
132                                         nameExpr.EvaluateString (iter), this);
133                         return name;
134                 }
135
136                 private KeyIndexTable GetIndexTable (QName name)
137                 {
138                         KeyIndexTable table =
139                                 keyIndexTables [name] as KeyIndexTable;
140                         if (table == null) {
141                                 table = new KeyIndexTable (this, p.CompiledStyle.ResolveKey (name));
142                                 keyIndexTables [name] = table;
143                         }
144                         return table;
145                 }
146
147                 public override string LookupNamespace (string prefix)
148                 {
149                         throw new InvalidOperationException ("we should never get here");
150                 }
151                 
152                 internal override IXsltContextFunction ResolveFunction (XmlQualifiedName name, XPathResultType [] argTypes)
153                 {
154                         string ns = name.Namespace;
155
156                         if (ns == null) return null;
157
158                         object extension = null;
159                         
160                         if (p.Arguments != null)
161                                 extension = p.Arguments.GetExtensionObject (ns);
162                         
163                         bool isScript = false;
164                         if (extension == null) {
165                                 extension = p.ScriptManager.GetExtensionObject (ns);
166                                 if (extension == null)
167                                         return null;
168
169                                 isScript = true;
170                         }
171                         
172                         
173                         MethodInfo method = FindBestMethod (extension.GetType (), name.Name, argTypes, isScript);
174                         
175                         if (method != null) 
176                                 return new XsltExtensionFunction (extension, method, p.CurrentNode);
177                         return null;
178                 }
179                 
180                 MethodInfo FindBestMethod (Type t, string name, XPathResultType [] argTypes, bool isScript)
181                 {
182                         int free, length;
183                         
184                         MethodInfo [] mi = t.GetMethods ((isScript ? BF.Public | BF.NonPublic : BF.Public) | BF.Instance | BF.Static);
185                         if (mi.Length == 0)
186                                 return null;
187                         
188                         if (argTypes == null)
189                                 return mi [0]; // if we dont have info on the arg types, nothing we can do
190
191
192                         free = 0;
193                         // filter on name + num args
194                         int numArgs = argTypes.Length;
195                         for (int i = 0; i < mi.Length; i ++) {
196                                 if (mi [i].Name == name && mi [i].GetParameters ().Length == numArgs) 
197                                         mi [free++] = mi [i];
198                         }
199                         length = free;
200                         
201                         // No method
202                         if (length == 0)
203                                 return null;
204                         
205                         // Thats it!
206                         if (length == 1)
207                                 return mi [0];
208                         
209                         free = 0;
210                         for (int i = 0; i < length; i ++) {
211                                 bool match = true;
212                                 ParameterInfo [] pi = mi [i].GetParameters ();
213                                 
214                                 for (int par = 0; par < pi.Length; par++) {
215                                         XPathResultType required = argTypes [par];
216                                         if (required == XPathResultType.Any)
217                                                 continue; // dunno what it is
218                                         
219                                         XPathResultType actual = XPFuncImpl.GetXPathType (pi [par].ParameterType, p.CurrentNode);
220                                         if (actual != required && actual != XPathResultType.Any) {
221                                                 match = false;
222                                                 break;
223                                         }
224                                         
225                                         if (actual == XPathResultType.Any) {
226                                                 // try to get a stronger gind
227                                                 if (required != XPathResultType.NodeSet && !(pi [par].ParameterType == typeof (object)))
228                                                 {
229                                                         match = false;
230                                                         break;
231                                                 }
232                                         }
233                                 }
234                                 if (match) return mi [i]; // TODO look for exact match
235                         }
236                         return null;
237                 }
238                         
239                 public override IXsltContextVariable ResolveVariable (string prefix, string name)
240                 {
241                         throw new InvalidOperationException ("shouldn't get here");
242                 }
243                 
244                 public override IXsltContextFunction ResolveFunction (string prefix, string name, XPathResultType [] ArgTypes)
245                 {
246                         throw new InvalidOperationException ("XsltCompiledContext exception: shouldn't get here.");
247                 }
248                 
249                 internal override System.Xml.Xsl.IXsltContextVariable ResolveVariable(QName q)
250                 {
251                         return p.CompiledStyle.ResolveVariable (q);
252                 }
253
254                 public override int CompareDocument (string baseUri, string nextBaseUri) 
255                 {
256                         // it is implementation specific
257                         return baseUri.GetHashCode ().CompareTo (nextBaseUri.GetHashCode ());
258                 }
259
260                 public override bool PreserveWhitespace (XPathNavigator nav) 
261                 {
262                         return p.CompiledStyle.Style.GetPreserveWhitespace (nav);
263                 }
264
265                 public override bool Whitespace { get { return WhitespaceHandling; } }
266
267                 // Below are mimicking XmlNamespaceManager ;-)
268                 public bool IsCData {
269                         get { return scopes [scopeAt].IsCData; }
270                         set { scopes [scopeAt].IsCData = value; }
271                 }
272                 public bool WhitespaceHandling {
273                         get { return scopes [scopeAt].PreserveWhitespace; }
274                         set { scopes [scopeAt].PreserveWhitespace = value; }
275                 }
276                 public string ElementPrefix {
277                         get { return scopes [scopeAt].ElementPrefix; }
278                         set { scopes [scopeAt].ElementPrefix = value; }
279                 }
280                 public string ElementNamespace {
281                         get { return scopes [scopeAt].ElementNamespace; }
282                         set { scopes [scopeAt].ElementNamespace = value; }
283                 }
284                 
285                 // precondition scopeAt == scopes.Length
286                 void ExtendScope ()
287                 {
288                         XsltContextInfo [] old = scopes;
289                         scopes = new XsltContextInfo [scopeAt * 2 + 1];
290                         if (scopeAt > 0)
291                                 Array.Copy (old, 0, scopes, 0, scopeAt);
292                 }
293
294                 public override bool PopScope ()
295                 {
296                         base.PopScope ();
297
298                         if (scopeAt == -1)
299                                 return false;
300                         scopeAt--;
301                         return true;
302                 }
303
304                 public override void PushScope ()
305                 {
306                         base.PushScope ();
307
308                         scopeAt++;
309                         if (scopeAt == scopes.Length)
310                                 ExtendScope ();
311                         if (scopes [scopeAt] == null)
312                                 scopes [scopeAt] = new XsltContextInfo ();
313                         else
314                                 scopes [scopeAt].Clear ();
315                 }
316         }
317 }