KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > xpath > XPathFunctionLibrary


1 package net.sf.saxon.xpath;
2
3 import net.sf.saxon.expr.Expression;
4 import net.sf.saxon.functions.FunctionLibrary;
5 import net.sf.saxon.trans.XPathException;
6
7 import javax.xml.namespace.QName JavaDoc;
8 import javax.xml.xpath.XPathFunction JavaDoc;
9 import javax.xml.xpath.XPathFunctionResolver JavaDoc;
10
11 /**
12  * The XPathFunctionLibrary is a FunctionLibrary that supports binding of XPath function
13  * calls to instances of the JAXP XPathFunction interface returned by an XPathFunctionResolver.
14  */

15
16 public class XPathFunctionLibrary implements FunctionLibrary {
17
18     private XPathFunctionResolver JavaDoc resolver;
19
20     /**
21      * Construct a XPathFunctionLibrary
22      */

23
24     public XPathFunctionLibrary() {
25     }
26
27     /**
28       * Set the resolver
29       * @param resolver The XPathFunctionResolver wrapped by this FunctionLibrary
30       */

31
32     public void setXPathFunctionResolver(XPathFunctionResolver JavaDoc resolver) {
33         this.resolver = resolver;
34     }
35
36     /**
37       * Get the resolver
38       * @return the XPathFunctionResolver wrapped by this FunctionLibrary
39       */

40     
41     public XPathFunctionResolver JavaDoc getXPathFunctionResolver() {
42         return resolver;
43     }
44
45      /**
46      * Test whether an XPath function with a given name and arity is available. This supports
47      * the function-available() function in XSLT. It is thus never used, and always returns false
48      * @param fingerprint The code that identifies the function name in the NamePool. This must
49      * match the supplied URI and local name.
50      * @param uri The URI of the function name
51      * @param local The local part of the function name
52      * @param arity The number of arguments. This is set to -1 in the case of the single-argument
53      * function-available() function; in this case the method should return true if there is some
54      * matching extension function, regardless of its arity.
55      */

56
57     public boolean isAvailable(int fingerprint, String JavaDoc uri, String JavaDoc local, int arity) {
58         return false;
59     }
60
61     /**
62      * Bind a function, given the URI and local parts of the function name,
63      * and the list of expressions supplied as arguments. This method is called at compile
64      * time.
65      * @param nameCode The namepool code of the function name. This must match the supplied
66      * URI and local name.
67      * @param uri The URI of the function name
68      * @param local The local part of the function name
69      * @param staticArgs The expressions supplied statically in the function call. The intention is
70      * that the static type of the arguments (obtainable via getItemType() and getCardinality() may
71      * be used as part of the binding algorithm.
72      * @return An object representing the extension function to be called, if one is found;
73      * null if no extension function was found matching the required name, arity, or signature.
74      */

75
76     public Expression bind(int nameCode, String JavaDoc uri, String JavaDoc local, Expression[] staticArgs)
77             throws XPathException {
78         if (resolver == null) {
79             return null;
80         }
81         QName JavaDoc name = new QName JavaDoc(uri, local);
82         XPathFunction JavaDoc function = resolver.resolveFunction(name, staticArgs.length);
83         if (function == null) {
84             return null;
85         }
86         XPathFunctionCall fc = new XPathFunctionCall(function);
87         fc.setArguments(staticArgs);
88         return fc;
89     }
90
91     /**
92      * This method creates a copy of a FunctionLibrary: if the original FunctionLibrary allows
93      * new functions to be added, then additions to this copy will not affect the original, or
94      * vice versa.
95      *
96      * @return a copy of this function library. This must be an instance of the original class.
97      */

98
99     public FunctionLibrary copy() {
100         XPathFunctionLibrary xfl = new XPathFunctionLibrary();
101         xfl.resolver = resolver;
102         return xfl;
103     }
104
105
106 }
107
108 //
109
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
110
// you may not use this file except in compliance with the License. You may obtain a copy of the
111
// License at http://www.mozilla.org/MPL/
112
//
113
// Software distributed under the License is distributed on an "AS IS" basis,
114
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
115
// See the License for the specific language governing rights and limitations under the License.
116
//
117
// The Original Code is: all this file.
118
//
119
// The Initial Developer of the Original Code is Michael H. Kay.
120
//
121
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
122
//
123
// Contributor(s): none.
124
//
Popular Tags