**** Merged from MCS ****
[mono-project.git] / mcs / class / System.XML / Mono.Xml.Xsl / XsltCompiledContext.cs
blob2feee4faf59466ff2338affed3ea6ee690eda520
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 //
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.
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;
44 using BF = System.Reflection.BindingFlags;
45 using QName = System.Xml.XmlQualifiedName;
48 namespace Mono.Xml.Xsl
50 internal class XsltCompiledContext : XsltContext
52 XslTransformProcessor p;
54 public XslTransformProcessor Processor { get { return p; }}
56 public XsltCompiledContext (XslTransformProcessor p) : base (new NameTable ())
58 this.p = p;
61 public override string DefaultNamespace { get { return String.Empty; }}
64 public override string LookupNamespace (string prefix)
66 throw new InvalidOperationException ("we should never get here");
69 internal override IXsltContextFunction ResolveFunction (XmlQualifiedName name, XPathResultType [] argTypes)
71 string ns = name.Namespace;
73 if (ns == null) return null;
75 object extension = null;
77 if (p.Arguments != null)
78 extension = p.Arguments.GetExtensionObject (ns);
80 bool isScript = false;
81 if (extension == null) {
82 extension = p.ScriptManager.GetExtensionObject (ns);
83 if (extension == null)
84 return null;
86 isScript = true;
90 MethodInfo method = FindBestMethod (extension.GetType (), name.Name, argTypes, isScript);
92 if (method != null)
93 return new XsltExtensionFunction (extension, method, p.CurrentNode);
94 return null;
97 MethodInfo FindBestMethod (Type t, string name, XPathResultType [] argTypes, bool isScript)
99 int free, length;
101 MethodInfo [] mi = t.GetMethods ((isScript ? BF.Public | BF.NonPublic : BF.Public) | BF.Instance | BF.Static);
102 if (mi.Length == 0)
103 return null;
105 if (argTypes == null)
106 return mi [0]; // if we dont have info on the arg types, nothing we can do
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];
116 length = free;
118 // No method
119 if (length == 0)
120 return null;
122 // Thats it!
123 if (length == 1)
124 return mi [0];
126 free = 0;
127 for (int i = 0; i < length; i ++) {
128 bool match = true;
129 ParameterInfo [] pi = mi [i].GetParameters ();
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
136 XPathResultType actual = XPFuncImpl.GetXPathType (pi [par].ParameterType, p.CurrentNode);
137 if (actual != required && actual != XPathResultType.Any) {
138 match = false;
139 break;
142 if (actual == XPathResultType.Any) {
143 // try to get a stronger gind
144 if (required != XPathResultType.NodeSet && !(pi [par].ParameterType == typeof (object)))
146 match = false;
147 break;
151 if (match) return mi [i]; // TODO look for exact match
153 return null;
156 public override IXsltContextVariable ResolveVariable (string prefix, string name)
158 throw new InvalidOperationException ("shouldn't get here");
161 public override IXsltContextFunction ResolveFunction (string prefix, string name, XPathResultType [] ArgTypes)
163 throw new InvalidOperationException ("XsltCompiledContext exception: shouldn't get here.");
166 internal override System.Xml.Xsl.IXsltContextVariable ResolveVariable(QName q)
168 return p.CompiledStyle.ResolveVariable (q);
171 public override int CompareDocument (string baseUri, string nextBaseUri)
173 // it is implementation specific
174 return baseUri.GetHashCode ().CompareTo (nextBaseUri.GetHashCode ());
177 public override bool PreserveWhitespace (XPathNavigator nav)
179 return p.CompiledStyle.Style.GetPreserveWhitespace (nav.LocalName, nav.NamespaceURI);
182 public override bool Whitespace { get { return WhitespaceHandling; } }
184 // Below are mimicking XmlNamespaceManager ;-)
185 public bool IsCData {
186 get { return scopes [scopeAt].IsCData; }
187 set { scopes [scopeAt].IsCData = value; }
189 public bool WhitespaceHandling {
190 get { return scopes [scopeAt].PreserveWhitespace; }
191 set { scopes [scopeAt].PreserveWhitespace = value; }
194 struct XsltContextInfo
196 public bool IsCData;
197 public bool PreserveWhitespace;
200 XsltContextInfo [] scopes = new XsltContextInfo [40];
201 int scopeAt = 0;
203 // precondition scopeAt == scopes.Length
204 void ExtendScope ()
206 XsltContextInfo [] old = scopes;
207 scopes = new XsltContextInfo [scopeAt * 2 + 1];
208 if (scopeAt > 0)
209 Array.Copy (old, 0, scopes, 0, scopeAt);
212 public override bool PopScope ()
214 base.PopScope ();
216 if (scopeAt == -1)
217 return false;
218 scopeAt--;
219 return true;
222 public override void PushScope ()
224 base.PushScope ();
226 scopeAt++;
227 if (scopeAt == scopes.Length)
228 ExtendScope ();