01: package net.sf.saxon.functions;
02:
03: import net.sf.saxon.expr.Expression;
04: import net.sf.saxon.Configuration;
05:
06: import java.io.Serializable;
07: import java.lang.reflect.AccessibleObject;
08:
09: /**
10: * This class acts as a factory for creating expressions that call extension functions.
11: * A different factory may be registered with the Configuration in order to customize the
12: * behaviour. Alternatively, this factory class can be customized by calling setExtensionFunctionClass
13: * to nominate a subclass of ExtensionFunctionCall to be used to implement calls on extension functions.
14: */
15:
16: public class ExtensionFunctionFactory implements Serializable {
17:
18: public ExtensionFunctionFactory(Configuration config) {
19: this .config = config;
20: }
21:
22: private Class extensionFunctionCallClass = ExtensionFunctionCall.class;
23: private Configuration config;
24:
25: /**
26: * Set the class to be used to represent extension function calls. This must be a subclass
27: * of {@link ExtensionFunctionCall}
28: * @param subclass the subclass of ExtensionFunctionCall to be used
29: */
30:
31: public void setExtensionFunctionClass(Class subclass) {
32: extensionFunctionCallClass = subclass;
33: }
34:
35: /**
36: * Factory method to create an expression that calls a Java extension function.
37: * This is always called at XPath compile time.
38: * @param nameCode the name of the function name, as represented in the name pool
39: * @param theClass the Java class containing the extension function
40: * @param method The "accessibleObject" representing a constructor, method, or field corresponding
41: * to the extension function
42: * @param arguments Array containing the expressions supplied as arguments to the function call.
43: * @return the constructed ExtensionFunctionCall object (a subclass might return any expression
44: * representing the extension function call).
45: */
46:
47: public Expression makeExtensionFunctionCall(int nameCode,
48: Class theClass, AccessibleObject method,
49: Expression[] arguments) {
50: ExtensionFunctionCall fn;
51: try {
52: fn = (ExtensionFunctionCall) (extensionFunctionCallClass
53: .newInstance());
54: } catch (InstantiationException e) {
55: throw new IllegalArgumentException(e.getMessage());
56: } catch (IllegalAccessException e) {
57: throw new IllegalArgumentException(e.getMessage());
58: }
59: fn.init(nameCode, theClass, method, config);
60: fn.setArguments(arguments);
61: return fn;
62: }
63:
64: }
65:
66: //
67: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
68: // you may not use this file except in compliance with the License. You may obtain a copy of the
69: // License at http://www.mozilla.org/MPL/
70: //
71: // Software distributed under the License is distributed on an "AS IS" basis,
72: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
73: // See the License for the specific language governing rights and limitations under the License.
74: //
75: // The Original Code is: all this file.
76: //
77: // The Initial Developer of the Original Code is Michael H. Kay.
78: //
79: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
80: //
81: // Contributor(s): Gunther Schadow (changes to allow access to public fields; also wrapping
82: // of extensions and mapping of null to empty sequence).
83: //
|