2
0

XQueryCliFunction.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // XQueryCliFunction.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. //
  8. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. //
  30. // Runtime type method wrapper for XPath2 function.
  31. //
  32. #if NET_2_0
  33. using System;
  34. using System.Collections;
  35. using System.Reflection;
  36. using System.Security;
  37. using System.Security.Policy;
  38. using System.Xml;
  39. using System.Xml.Schema;
  40. using System.Xml.Query;
  41. using System.Xml.XPath;
  42. using System.Xml.Xsl;
  43. namespace Mono.Xml.XPath2
  44. {
  45. // Ideas:
  46. // declare function namespace cli = "http://mono-project.com/xquery/function/cli"
  47. // declare variable v = cli:invoke (cli:new (Microsoft.CSharp:CSharpCodeProvider), CreateCompiler);
  48. // declare variable v2 = System.Math:Abs (0.5);
  49. //
  50. public class XQueryCliFunction : XQueryFunction
  51. {
  52. internal static XQueryCliFunction CreateFromMethodInfo (MethodInfo mi)
  53. {
  54. return CreateFromMethodInfo (null, mi);
  55. }
  56. internal static XQueryCliFunction CreateFromMethodInfo (XmlQualifiedName name, MethodInfo mi)
  57. {
  58. return CreateFromMethodInfo (name, new MethodInfo [] {mi});
  59. }
  60. internal static XQueryCliFunction CreateFromMethodInfo (MethodInfo [] methods)
  61. {
  62. return CreateFromMethodInfo (null, methods);
  63. }
  64. internal static XQueryCliFunction CreateFromMethodInfo (XmlQualifiedName name, MethodInfo [] methodList)
  65. {
  66. if (methodList == null || methodList.Length == 0)
  67. throw new ArgumentException (String.Format ("Argument methods is missing or zero-length array. Name is {0}", name));
  68. Type cliReturnType = null;
  69. ArrayList arguments = new ArrayList ();
  70. if (name == null || name == XmlQualifiedName.Empty)
  71. name = new XmlQualifiedName (methodList [0].Name, methodList [0].DeclaringType.FullName);
  72. int maxArgs = 0;
  73. int minArgs = -1;
  74. Hashtable methods = new Hashtable ();
  75. foreach (MethodInfo mi in methodList) {
  76. if (cliReturnType == null)
  77. cliReturnType = mi.ReturnType;
  78. else if (mi.ReturnType != cliReturnType)
  79. throw new ArgumentException (String.Format ("Every XQuery functions which share the same name must have the same return type. Method name is {0}.", mi.Name));
  80. ParameterInfo [] prms = mi.GetParameters ();
  81. int args = prms.Length;
  82. // Whether it takes "current context" or not.
  83. Type t = args > 0 ? prms [0].ParameterType : null;
  84. bool ctxSeq = mi.GetCustomAttributes (typeof (XQueryFunctionContextAttribute), false).Length > 0;
  85. bool hasContextArg = ctxSeq || t == typeof (XQueryContext);
  86. if (ctxSeq || hasContextArg)
  87. args--;
  88. if (methods [args] != null)
  89. throw new ArgumentException (String.Format ("XQuery does not allow functions that accepts such methods that have the same number of parameters in different types. Method name is {0}", mi.Name));
  90. methods.Add ((int) args, mi);
  91. if (args < minArgs || minArgs < 0)
  92. minArgs = args;
  93. if (args > maxArgs)
  94. maxArgs = args;
  95. }
  96. MethodInfo m = (MethodInfo) methods [(int) maxArgs];
  97. if (m == null)
  98. throw new SystemException ("Should not happen: maxArgs is " + maxArgs);
  99. ParameterInfo [] pl = m.GetParameters ();
  100. for (int i = 0; i < pl.Length; i++) {
  101. Type t = pl [i].ParameterType;
  102. if (t != typeof (XQueryContext))
  103. arguments.Add (
  104. new XQueryFunctionArgument (new XmlQualifiedName (pl [i].Name), SequenceType.Create (pl [i].ParameterType)));
  105. }
  106. return new XQueryCliFunction (name,
  107. arguments.ToArray (typeof (XQueryFunctionArgument)) as XQueryFunctionArgument [],
  108. SequenceType.Create (cliReturnType),
  109. methods,
  110. minArgs,
  111. maxArgs);
  112. }
  113. private XQueryCliFunction (XmlQualifiedName name,
  114. XQueryFunctionArgument [] args,
  115. SequenceType returnType,
  116. Hashtable methods,
  117. int minArgs,
  118. int maxArgs)
  119. : base (name, args, returnType)
  120. {
  121. this.methods = methods;
  122. this.maxArgs = maxArgs;
  123. this.minArgs = minArgs;
  124. }
  125. // instance members
  126. // [int argsize] -> MethodInfo (according to the spec 1.1,
  127. // there should be no overloads that accepts the same parameter
  128. // count in different types).
  129. Hashtable methods = new Hashtable ();
  130. int maxArgs = 0;
  131. int minArgs = -1;
  132. SequenceType returnType;
  133. public override int MinArgs {
  134. get { return minArgs; }
  135. }
  136. public override int MaxArgs {
  137. get { return maxArgs; }
  138. }
  139. public override object Invoke (XPathSequence current, object [] args)
  140. {
  141. MethodInfo mi = methods [args.Length] as MethodInfo;
  142. if (mi == null)
  143. throw new ArgumentException ("The number of custom function parameter does not match with the registered method's signature.");
  144. ParameterInfo [] prms = mi.GetParameters ();
  145. // Use Evidence and PermissionSet.Demand() here
  146. // before invoking external function.
  147. Evidence e = current.Context.StaticContext.Evidence;
  148. if (e != null)
  149. SecurityManager.ResolvePolicy (e).Demand ();
  150. Type t = prms.Length > 0 ? prms [0].ParameterType : null;
  151. bool ctxSeq = mi.GetCustomAttributes (
  152. typeof (XQueryFunctionContextAttribute),
  153. false).Length > 0;
  154. if (t == typeof (XQueryContext)) {
  155. ArrayList pl = new ArrayList (args);
  156. pl.Insert (0, current.Context);
  157. args = pl.ToArray ();
  158. }
  159. else if (ctxSeq) {
  160. ArrayList pl = new ArrayList (args);
  161. pl.Insert (0, current);
  162. args = pl.ToArray ();
  163. }
  164. if (args.Length != prms.Length)
  165. throw new XmlQueryException (String.Format ("Argument numbers were different for function {0}. Signature requires {1} while actual call was {2}.", mi.Name, prms.Length, args.Length));
  166. // If native parameter type is XPathSequence and the actual values are not, adjust them
  167. for (int i = 0; i < args.Length; i++) {
  168. if (prms [i].ParameterType == typeof (XPathSequence) && !(args [i] is XPathSequence)) {
  169. XPathItem item = args [i] as XPathItem;
  170. if (item == null)
  171. item = args [i] == null ? null : new XPathAtomicValue (args [i], XmlSchemaType.GetBuiltInType (XPathAtomicValue.XmlTypeCodeFromRuntimeType (prms [i].ParameterType, true)));
  172. if (item == null)
  173. args [i] = new XPathEmptySequence (current.Context);
  174. else
  175. args [i] = new SingleItemIterator (item, current.Context);
  176. }
  177. }
  178. return mi.Invoke (null, args);
  179. }
  180. }
  181. }
  182. #endif