XQueryCliFunction.cs 5.7 KB

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