2005-03-03 Atsushi Enomoto <atsushi@ximian.com>
[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                 XslTransformProcessor p;
53                         
54                 public XslTransformProcessor Processor { get { return p; }}
55                         
56                 public XsltCompiledContext (XslTransformProcessor p) : base (new NameTable ())
57                 {
58                         this.p = p;
59                 }
60
61                 public override string DefaultNamespace { get { return String.Empty; }}
62
63
64                 public override string LookupNamespace (string prefix)
65                 {
66                         throw new InvalidOperationException ("we should never get here");
67                 }
68                 
69                 internal override IXsltContextFunction ResolveFunction (XmlQualifiedName name, XPathResultType [] argTypes)
70                 {
71                         string ns = name.Namespace;
72
73                         if (ns == null) return null;
74
75                         object extension = null;
76                         
77                         if (p.Arguments != null)
78                                 extension = p.Arguments.GetExtensionObject (ns);
79                         
80                         bool isScript = false;
81                         if (extension == null) {
82                                 extension = p.ScriptManager.GetExtensionObject (ns);
83                                 if (extension == null)
84                                         return null;
85
86                                 isScript = true;
87                         }
88                         
89                         
90                         MethodInfo method = FindBestMethod (extension.GetType (), name.Name, argTypes, isScript);
91                         
92                         if (method != null) 
93                                 return new XsltExtensionFunction (extension, method, p.CurrentNode);
94                         return null;
95                 }
96                 
97                 MethodInfo FindBestMethod (Type t, string name, XPathResultType [] argTypes, bool isScript)
98                 {
99                         int free, length;
100                         
101                         MethodInfo [] mi = t.GetMethods ((isScript ? BF.Public | BF.NonPublic : BF.Public) | BF.Instance | BF.Static);
102                         if (mi.Length == 0)
103                                 return null;
104                         
105                         if (argTypes == null)
106                                 return mi [0]; // if we dont have info on the arg types, nothing we can do
107
108
109                         free = 0;
110                         // filter on name + num args
111                         int numArgs = argTypes.Length;
112                         for (int i = 0; i < mi.Length; i ++) {
113                                 if (mi [i].Name == name && mi [i].GetParameters ().Length == numArgs) 
114                                         mi [free++] = mi [i];
115                         }
116                         length = free;
117                         
118                         // No method
119                         if (length == 0)
120                                 return null;
121                         
122                         // Thats it!
123                         if (length == 1)
124                                 return mi [0];
125                         
126                         free = 0;
127                         for (int i = 0; i < length; i ++) {
128                                 bool match = true;
129                                 ParameterInfo [] pi = mi [i].GetParameters ();
130                                 
131                                 for (int par = 0; par < pi.Length; par++) {
132                                         XPathResultType required = argTypes [par];
133                                         if (required == XPathResultType.Any)
134                                                 continue; // dunno what it is
135                                         
136                                         XPathResultType actual = XPFuncImpl.GetXPathType (pi [par].ParameterType, p.CurrentNode);
137                                         if (actual != required && actual != XPathResultType.Any) {
138                                                 match = false;
139                                                 break;
140                                         }
141                                         
142                                         if (actual == XPathResultType.Any) {
143                                                 // try to get a stronger gind
144                                                 if (required != XPathResultType.NodeSet && !(pi [par].ParameterType == typeof (object)))
145                                                 {
146                                                         match = false;
147                                                         break;
148                                                 }
149                                         }
150                                 }
151                                 if (match) return mi [i]; // TODO look for exact match
152                         }
153                         return null;
154                 }
155                         
156                 public override IXsltContextVariable ResolveVariable (string prefix, string name)
157                 {
158                         throw new InvalidOperationException ("shouldn't get here");
159                 }
160                 
161                 public override IXsltContextFunction ResolveFunction (string prefix, string name, XPathResultType [] ArgTypes)
162                 {
163                         throw new InvalidOperationException ("XsltCompiledContext exception: shouldn't get here.");
164                 }
165                 
166                 internal override System.Xml.Xsl.IXsltContextVariable ResolveVariable(QName q)
167                 {
168                         return p.CompiledStyle.ResolveVariable (q);
169                 }
170
171                 public override int CompareDocument (string baseUri, string nextBaseUri) 
172                 {
173                         // it is implementation specific
174                         return baseUri.GetHashCode ().CompareTo (nextBaseUri.GetHashCode ());
175                 }
176
177                 public override bool PreserveWhitespace (XPathNavigator nav) 
178                 {
179                         return p.CompiledStyle.Style.GetPreserveWhitespace (nav.LocalName, nav.NamespaceURI);
180                 }
181
182                 public override bool Whitespace { get { return WhitespaceHandling; } }
183
184                 // Below are mimicking XmlNamespaceManager ;-)
185                 public bool IsCData {
186                         get { return scopes [scopeAt].IsCData; }
187                         set { scopes [scopeAt].IsCData = value; }
188                 }
189                 public bool WhitespaceHandling {
190                         get { return scopes [scopeAt].PreserveWhitespace; }
191                         set { scopes [scopeAt].PreserveWhitespace = value; }
192                 }
193                 public string ElementPrefix {
194                         get { return scopes [scopeAt].ElementPrefix; }
195                         set { scopes [scopeAt].ElementPrefix = value; }
196                 }
197                 public string ElementNamespace {
198                         get { return scopes [scopeAt].ElementNamespace; }
199                         set { scopes [scopeAt].ElementNamespace = value; }
200                 }
201
202                 struct XsltContextInfo
203                 {
204                         public bool IsCData;
205                         public bool PreserveWhitespace;
206                         public string ElementPrefix;
207                         public string ElementNamespace;
208                 }
209                 
210                 XsltContextInfo [] scopes = new XsltContextInfo [40];
211                 int scopeAt = 0;
212                 
213                 // precondition scopeAt == scopes.Length
214                 void ExtendScope ()
215                 {
216                         XsltContextInfo [] old = scopes;
217                         scopes = new XsltContextInfo [scopeAt * 2 + 1];
218                         if (scopeAt > 0)
219                                 Array.Copy (old, 0, scopes, 0, scopeAt);
220                 }
221
222                 public override bool PopScope ()
223                 {
224                         base.PopScope ();
225
226                         if (scopeAt == -1)
227                                 return false;
228                         scopeAt--;
229                         return true;
230                 }
231
232                 public override void PushScope ()
233                 {
234                         base.PushScope ();
235
236                         scopeAt++;
237                         if (scopeAt == scopes.Length)
238                                 ExtendScope ();
239                 }
240         }
241 }