2 // XsltCompiledContext.cs
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Atsushi Enomoto (atsushi@ximian.com)
8 // (C) 2004 Novell Inc.
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
33 using System.Collections;
34 using System.Collections.Specialized;
36 using System.Xml.Schema;
37 using System.Xml.XPath;
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
55 public bool PreserveWhitespace = true;
56 public string ElementPrefix;
57 public string ElementNamespace;
62 PreserveWhitespace = true;
63 ElementPrefix = ElementNamespace = null;
67 Hashtable keyNameCache = new Hashtable ();
68 Hashtable keyIndexTables = new Hashtable ();
69 Hashtable patternNavCaches = new Hashtable ();
71 XslTransformProcessor p;
72 XsltContextInfo [] scopes;
76 public XslTransformProcessor Processor { get { return p; }}
78 public XsltCompiledContext (XslTransformProcessor p) : base (new NameTable ())
81 scopes = new XsltContextInfo [10];
82 for (int i = 0; i < 10; i++)
83 scopes [i] = new XsltContextInfo ();
86 public override string DefaultNamespace { get { return String.Empty; }}
88 public XPathNavigator GetNavCache (Mono.Xml.XPath.Pattern p, XPathNavigator node)
91 patternNavCaches [p] as XPathNavigator;
92 if (nav == null || !nav.MoveTo (node)) {
94 patternNavCaches [p] = nav;
99 public object EvaluateKey (IStaticXsltContext staticContext,
101 Expression nameExpr, Expression valueExpr)
103 QName name = GetKeyName (staticContext, iter, nameExpr);
104 KeyIndexTable table = GetIndexTable (name);
105 return table.Evaluate (iter, valueExpr);
108 public bool MatchesKey (XPathNavigator nav,
109 IStaticXsltContext staticContext,
110 string name, string value)
112 QName qname = XslNameUtil.FromString (name, staticContext);
113 KeyIndexTable table = GetIndexTable (qname);
114 return table.Matches (nav, value, this);
117 private QName GetKeyName (IStaticXsltContext staticContext,
118 BaseIterator iter, Expression nameExpr)
121 if (nameExpr.HasStaticValue) {
122 name = (QName) keyNameCache [nameExpr];
124 name = XslNameUtil.FromString (
125 nameExpr.EvaluateString (iter),
127 keyNameCache [nameExpr] = name;
131 name = XslNameUtil.FromString (
132 nameExpr.EvaluateString (iter), this);
136 private KeyIndexTable GetIndexTable (QName name)
138 KeyIndexTable table =
139 keyIndexTables [name] as KeyIndexTable;
141 table = new KeyIndexTable (this, p.CompiledStyle.ResolveKey (name));
142 keyIndexTables [name] = table;
147 public override string LookupNamespace (string prefix)
149 throw new InvalidOperationException ("we should never get here");
152 internal override IXsltContextFunction ResolveFunction (XmlQualifiedName name, XPathResultType [] argTypes)
154 string ns = name.Namespace;
156 if (ns == null) return null;
158 object extension = null;
160 if (p.Arguments != null)
161 extension = p.Arguments.GetExtensionObject (ns);
163 bool isScript = false;
164 if (extension == null) {
165 extension = p.ScriptManager.GetExtensionObject (ns);
166 if (extension == null)
173 MethodInfo method = FindBestMethod (extension.GetType (), name.Name, argTypes, isScript);
176 return new XsltExtensionFunction (extension, method, p.CurrentNode);
180 MethodInfo FindBestMethod (Type t, string name, XPathResultType [] argTypes, bool isScript)
184 MethodInfo [] mi = t.GetMethods ((isScript ? BF.Public | BF.NonPublic : BF.Public) | BF.Instance | BF.Static);
188 if (argTypes == null)
189 return mi [0]; // if we dont have info on the arg types, nothing we can do
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];
210 for (int i = 0; i < length; i ++) {
212 ParameterInfo [] pi = mi [i].GetParameters ();
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
219 XPathResultType actual = XPFuncImpl.GetXPathType (pi [par].ParameterType, p.CurrentNode);
220 if (actual != required && actual != XPathResultType.Any) {
225 if (actual == XPathResultType.Any) {
226 // try to get a stronger gind
227 if (required != XPathResultType.NodeSet && !(pi [par].ParameterType == typeof (object)))
234 if (match) return mi [i]; // TODO look for exact match
239 public override IXsltContextVariable ResolveVariable (string prefix, string name)
241 throw new InvalidOperationException ("shouldn't get here");
244 public override IXsltContextFunction ResolveFunction (string prefix, string name, XPathResultType [] ArgTypes)
246 throw new InvalidOperationException ("XsltCompiledContext exception: shouldn't get here.");
249 internal override System.Xml.Xsl.IXsltContextVariable ResolveVariable(QName q)
251 return p.CompiledStyle.ResolveVariable (q);
254 public override int CompareDocument (string baseUri, string nextBaseUri)
256 // it is implementation specific
257 return baseUri.GetHashCode ().CompareTo (nextBaseUri.GetHashCode ());
260 public override bool PreserveWhitespace (XPathNavigator nav)
262 return p.CompiledStyle.Style.GetPreserveWhitespace (nav);
265 public override bool Whitespace { get { return WhitespaceHandling; } }
267 // Below are mimicking XmlNamespaceManager ;-)
268 public bool IsCData {
269 get { return scopes [scopeAt].IsCData; }
270 set { scopes [scopeAt].IsCData = value; }
272 public bool WhitespaceHandling {
273 get { return scopes [scopeAt].PreserveWhitespace; }
274 set { scopes [scopeAt].PreserveWhitespace = value; }
276 public string ElementPrefix {
277 get { return scopes [scopeAt].ElementPrefix; }
278 set { scopes [scopeAt].ElementPrefix = value; }
280 public string ElementNamespace {
281 get { return scopes [scopeAt].ElementNamespace; }
282 set { scopes [scopeAt].ElementNamespace = value; }
285 // precondition scopeAt == scopes.Length
288 XsltContextInfo [] old = scopes;
289 scopes = new XsltContextInfo [scopeAt * 2 + 1];
291 Array.Copy (old, 0, scopes, 0, scopeAt);
294 public override bool PopScope ()
304 public override void PushScope ()
309 if (scopeAt == scopes.Length)
311 if (scopes [scopeAt] == null)
312 scopes [scopeAt] = new XsltContextInfo ();
314 scopes [scopeAt].Clear ();