1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
//------------------------------------------------------------------------------
// <copyright file="XmlExtensionFunction.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Reflection;
using System.Globalization;
using System.Diagnostics;
namespace System.Xml.Xsl.Runtime {
using Res = System.Xml.Utils.Res;
/// <summary>
/// Table of bound extension functions. Once an extension function is bound and entered into the table, future bindings
/// will be very fast. This table is not thread-safe.
/// </summary>
internal class XmlExtensionFunctionTable {
private Dictionary<XmlExtensionFunction, XmlExtensionFunction> table;
private XmlExtensionFunction funcCached;
public XmlExtensionFunctionTable() {
this.table = new Dictionary<XmlExtensionFunction, XmlExtensionFunction>();
}
public XmlExtensionFunction Bind(string name, string namespaceUri, int numArgs, Type objectType, BindingFlags flags) {
XmlExtensionFunction func;
if (this.funcCached == null)
this.funcCached = new XmlExtensionFunction();
// If the extension function already exists in the table, then binding has already been performed
this.funcCached.Init(name, namespaceUri, numArgs, objectType, flags);
if (!this.table.TryGetValue(this.funcCached, out func)) {
// Function doesn't exist, so bind it and enter it into the table
func = this.funcCached;
this.funcCached = null;
func.Bind();
this.table.Add(func, func);
}
return func;
}
}
/// <summary>
/// This internal class contains methods that allow binding to extension functions and invoking them.
/// </summary>
internal class XmlExtensionFunction {
private string namespaceUri; // Extension object identifier
private string name; // Name of this method
private int numArgs; // Argument count
private Type objectType; // Type of the object which will be searched for matching methods
private BindingFlags flags; // Modifiers that were used to search for a matching signature
private int hashCode; // Pre-computed hashcode
private MethodInfo meth; // MethodInfo for extension function
private Type[] argClrTypes; // Type array for extension function arguments
private Type retClrType; // Type for extension function return value
private XmlQueryType[] argXmlTypes; // XmlQueryType array for extension function arguments
private XmlQueryType retXmlType; // XmlQueryType for extension function return value
/// <summary>
/// Constructor.
/// </summary>
public XmlExtensionFunction() {
}
/// <summary>
/// Constructor (directly binds to passed MethodInfo).
/// </summary>
public XmlExtensionFunction(string name, string namespaceUri, MethodInfo meth) {
this.name = name;
this.namespaceUri = namespaceUri;
Bind(meth);
}
/// <summary>
/// Constructor.
/// </summary>
public XmlExtensionFunction(string name, string namespaceUri, int numArgs, Type objectType, BindingFlags flags) {
Init(name, namespaceUri, numArgs, objectType, flags);
}
/// <summary>
/// Initialize, but do not bind.
/// </summary>
public void Init(string name, string namespaceUri, int numArgs, Type objectType, BindingFlags flags) {
this.name = name;
this.namespaceUri = namespaceUri;
this.numArgs = numArgs;
this.objectType = objectType;
this.flags = flags;
this.meth = null;
this.argClrTypes = null;
this.retClrType = null;
this.argXmlTypes = null;
this.retXmlType = null;
// Compute hash code so that it is not recomputed each time GetHashCode() is called
this.hashCode = namespaceUri.GetHashCode() ^ name.GetHashCode() ^ ((int) flags << 16) ^ (int) numArgs;
}
/// <summary>
/// Once Bind has been successfully called, Method will be non-null.
/// </summary>
public MethodInfo Method {
get { return this.meth; }
}
/// <summary>
/// Once Bind has been successfully called, the Clr type of each argument can be accessed.
/// Note that this may be different than Method.GetParameterInfo().ParameterType.
/// </summary>
public Type GetClrArgumentType(int index) {
return this.argClrTypes[index];
}
/// <summary>
/// Once Bind has been successfully called, the Clr type of the return value can be accessed.
/// Note that this may be different than Method.GetParameterInfo().ReturnType.
/// </summary>
public Type ClrReturnType {
get { return this.retClrType; }
}
/// <summary>
/// Once Bind has been successfully called, the inferred Xml types of the arguments can be accessed.
/// </summary>
public XmlQueryType GetXmlArgumentType(int index) {
return this.argXmlTypes[index];
}
/// <summary>
/// Once Bind has been successfully called, the inferred Xml type of the return value can be accessed.
/// </summary>
public XmlQueryType XmlReturnType {
get { return this.retXmlType; }
}
/// <summary>
/// Return true if the CLR type specified in the Init() call has a matching method.
/// </summary>
public bool CanBind() {
MethodInfo[] methods = this.objectType.GetMethods(this.flags);
bool ignoreCase = (this.flags & BindingFlags.IgnoreCase) != 0;
StringComparison comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
// Find method in object type
foreach (MethodInfo methSearch in methods) {
if (methSearch.Name.Equals(this.name, comparison) && (this.numArgs == -1 || methSearch.GetParameters().Length == this.numArgs)) {
// Binding to generic methods will never succeed
if (!methSearch.IsGenericMethodDefinition)
return true;
}
}
return false;
}
/// <summary>
/// Bind to the CLR type specified in the Init() call. If a matching method cannot be found, throw an exception.
/// </summary>
public void Bind() {
MethodInfo[] methods = this.objectType.GetMethods(this.flags);
MethodInfo methMatch = null;
bool ignoreCase = (this.flags & BindingFlags.IgnoreCase) != 0;
StringComparison comparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
// Find method in object type
foreach (MethodInfo methSearch in methods) {
if (methSearch.Name.Equals(this.name, comparison) && (this.numArgs == -1 || methSearch.GetParameters().Length == this.numArgs)) {
if (methMatch != null)
throw new XslTransformException(/*[XT_037]*/Res.XmlIl_AmbiguousExtensionMethod, this.namespaceUri, this.name, this.numArgs.ToString(CultureInfo.InvariantCulture));
methMatch = methSearch;
}
}
if (methMatch == null) {
methods = this.objectType.GetMethods(this.flags | BindingFlags.NonPublic);
foreach (MethodInfo methSearch in methods) {
if (methSearch.Name.Equals(this.name, comparison) && methSearch.GetParameters().Length == this.numArgs)
throw new XslTransformException(/*[XT_038]*/Res.XmlIl_NonPublicExtensionMethod, this.namespaceUri, this.name);
}
throw new XslTransformException(/*[XT_039]*/Res.XmlIl_NoExtensionMethod, this.namespaceUri, this.name, this.numArgs.ToString(CultureInfo.InvariantCulture));
}
if (methMatch.IsGenericMethodDefinition)
throw new XslTransformException(/*[XT_040]*/Res.XmlIl_GenericExtensionMethod, this.namespaceUri, this.name);
Debug.Assert(methMatch.ContainsGenericParameters == false);
Bind(methMatch);
}
/// <summary>
/// Bind to the specified MethodInfo.
/// </summary>
private void Bind(MethodInfo meth) {
ParameterInfo[] paramInfo = meth.GetParameters();
int i;
// Save the MethodInfo
this.meth = meth;
// Get the Clr type of each parameter
this.argClrTypes = new Type[paramInfo.Length];
for (i = 0; i < paramInfo.Length; i++)
this.argClrTypes[i] = GetClrType(paramInfo[i].ParameterType);
// Get the Clr type of the return value
this.retClrType = GetClrType(this.meth.ReturnType);
// Infer an Xml type for each Clr type
this.argXmlTypes = new XmlQueryType[paramInfo.Length];
for (i = 0; i < paramInfo.Length; i++) {
this.argXmlTypes[i] = InferXmlType(this.argClrTypes[i]);
//
if (this.namespaceUri.Length == 0) {
if ((object) this.argXmlTypes[i] == (object) XmlQueryTypeFactory.NodeNotRtf)
this.argXmlTypes[i] = XmlQueryTypeFactory.Node;
else if ((object) this.argXmlTypes[i] == (object) XmlQueryTypeFactory.NodeSDod)
this.argXmlTypes[i] = XmlQueryTypeFactory.NodeS;
}
else {
if ((object) this.argXmlTypes[i] == (object) XmlQueryTypeFactory.NodeSDod)
this.argXmlTypes[i] = XmlQueryTypeFactory.NodeNotRtfS;
}
}
// Infer an Xml type for the return Clr type
this.retXmlType = InferXmlType(this.retClrType);
}
/// <summary>
/// Convert the incoming arguments to an array of CLR objects, and then invoke the external function on the "extObj" object instance.
/// </summary>
public object Invoke(object extObj, object[] args) {
Debug.Assert(this.meth != null, "Must call Bind() before calling Invoke.");
Debug.Assert(args.Length == this.argClrTypes.Length, "Mismatched number of actual and formal arguments.");
try {
return this.meth.Invoke(extObj, this.flags, null, args, CultureInfo.InvariantCulture);
}
catch (TargetInvocationException e) {
throw new XslTransformException(e.InnerException, Res.XmlIl_ExtensionError, this.name);
}
catch (Exception e) {
if (!XmlException.IsCatchableException(e)) {
throw;
}
throw new XslTransformException(e, Res.XmlIl_ExtensionError, this.name);
}
}
/// <summary>
/// Return true if this XmlExtensionFunction has the same values as another XmlExtensionFunction.
/// </summary>
public override bool Equals(object other) {
XmlExtensionFunction that = other as XmlExtensionFunction;
Debug.Assert(that != null);
// Compare name, argument count, object type, and binding flags
return (this.hashCode == that.hashCode && this.name == that.name && this.namespaceUri == that.namespaceUri &&
this.numArgs == that.numArgs && this.objectType == that.objectType && this.flags == that.flags);
}
/// <summary>
/// Return this object's hash code, previously computed for performance.
/// </summary>
public override int GetHashCode() {
return this.hashCode;
}
/// <summary>
/// 1. Map enumerations to the underlying integral type.
/// 2. Throw an exception if the type is ByRef
/// </summary>
private Type GetClrType(Type clrType) {
if (clrType.IsEnum)
return Enum.GetUnderlyingType(clrType);
if (clrType.IsByRef)
throw new XslTransformException(/*[XT_050]*/Res.XmlIl_ByRefType, this.namespaceUri, this.name);
return clrType;
}
/// <summary>
/// Infer an Xml type from a Clr type using Xslt infererence rules
/// </summary>
private XmlQueryType InferXmlType(Type clrType) {
return XsltConvert.InferXsltType(clrType);
}
}
}
|