XQueryCliFunction.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.Xml;
  38. using System.Xml.Schema;
  39. using System.Xml.Query;
  40. using System.Xml.XPath;
  41. using System.Xml.Xsl;
  42. namespace Mono.Xml.XPath2
  43. {
  44. // Ideas:
  45. // declare function namespace cli = "http://mono-project.com/xquery/function/cli"
  46. // declare variable v = cli:invoke (cli:new (Microsoft.CSharp:CSharpCodeProvider), CreateCompiler);
  47. // declare variable v2 = System.Math:Abs (0.5);
  48. //
  49. public class XQueryCliFunction : XQueryFunction
  50. {
  51. internal static XQueryCliFunction CreateFromMethodInfo (MethodInfo mi)
  52. {
  53. return CreateFromMethodInfo (null, mi);
  54. }
  55. internal static XQueryCliFunction CreateFromMethodInfo (XmlQualifiedName name, MethodInfo mi)
  56. {
  57. return CreateFromMethodInfo (name, new MethodInfo [] {mi});
  58. }
  59. internal static XQueryCliFunction CreateFromMethodInfo (MethodInfo [] methods)
  60. {
  61. return CreateFromMethodInfo (null, methods);
  62. }
  63. internal static XQueryCliFunction CreateFromMethodInfo (XmlQualifiedName name, MethodInfo [] methodList)
  64. {
  65. if (methodList == null || methodList.Length == 0)
  66. throw new ArgumentException (String.Format ("Argument methods is missing or zero-length array. Name is {0}", name));
  67. Type cliReturnType = null;
  68. ArrayList arguments = new ArrayList ();
  69. if (name == null || name == XmlQualifiedName.Empty)
  70. name = new XmlQualifiedName (methodList [0].Name, methodList [0].DeclaringType.FullName);
  71. int maxArgs = 0;
  72. int minArgs = -1;
  73. Hashtable methods = new Hashtable ();
  74. foreach (MethodInfo mi in methodList) {
  75. if (cliReturnType == null)
  76. cliReturnType = mi.ReturnType;
  77. else if (mi.ReturnType != cliReturnType)
  78. throw new ArgumentException (String.Format ("Argument methodList have different return types. Method name is {0}.", mi.Name));
  79. ParameterInfo [] prms = mi.GetParameters ();
  80. int args = prms.Length;
  81. // Whether it takes "current context" or not.
  82. bool hasContextArg = (args > 0 && prms [0].ParameterType == typeof (XQueryContext));
  83. if (hasContextArg)
  84. args--;
  85. if (methods [args] != null)
  86. 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));
  87. methods.Add (args, mi);
  88. if (args < minArgs || minArgs < 0)
  89. minArgs = args;
  90. if (args > maxArgs)
  91. maxArgs = args;
  92. }
  93. MethodInfo m = (MethodInfo) methods [maxArgs];
  94. if (m == null)
  95. throw new SystemException ("Should not happen: maxArgs is " + maxArgs);
  96. ParameterInfo [] pl = m.GetParameters ();
  97. for (int i = 0; i < pl.Length; i++)
  98. if (pl [i].ParameterType != typeof (XQueryContext))
  99. arguments.Add (
  100. new XQueryFunctionArgument (new XmlQualifiedName (pl [i].Name), SequenceType.Create (pl [i].ParameterType)));
  101. return new XQueryCliFunction (name,
  102. arguments.ToArray (typeof (XQueryFunctionArgument)) as XQueryFunctionArgument [],
  103. SequenceType.Create (cliReturnType),
  104. methods,
  105. minArgs,
  106. maxArgs);
  107. }
  108. private XQueryCliFunction (XmlQualifiedName name,
  109. XQueryFunctionArgument [] args,
  110. SequenceType returnType,
  111. Hashtable methods,
  112. int minArgs,
  113. int maxArgs)
  114. : base (name, args, returnType)
  115. {
  116. this.methods = methods;
  117. this.maxArgs = maxArgs;
  118. this.minArgs = minArgs;
  119. }
  120. // instance members
  121. // [int argsize] -> MethodInfo (according to the spec 1.1,
  122. // there should be no overloads that accepts the same parameter
  123. // count in different types).
  124. Hashtable methods = new Hashtable ();
  125. int maxArgs = 0;
  126. int minArgs = -1;
  127. SequenceType returnType;
  128. public override int MinArgs {
  129. get { return minArgs; }
  130. }
  131. public override int MaxArgs {
  132. get { return maxArgs; }
  133. }
  134. public override object Invoke (XQueryContext context, object [] args)
  135. {
  136. MethodInfo mi = methods [args.Length] as MethodInfo;
  137. if (mi == null)
  138. throw new ArgumentException ("The number of custom function parameter does not match with the registered method's signature.");
  139. ParameterInfo [] prms = mi.GetParameters ();
  140. // Use Evidence and PermissionSet.Demand() here
  141. // before invoking external function.
  142. if (context.StaticContext.Evidence != null)
  143. SecurityManager.ResolvePolicy (context.StaticContext.Evidence).Demand ();
  144. if (prms.Length > 0 && prms [0].ParameterType == typeof (XQueryContext)) {
  145. ArrayList pl = new ArrayList (args);
  146. pl.Insert (0, context);
  147. return mi.Invoke (null, pl.ToArray ());
  148. }
  149. else
  150. return mi.Invoke (null, args);
  151. }
  152. }
  153. }
  154. #endif