KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > style > StylesheetFunctionLibrary


1 package net.sf.saxon.style;
2
3 import net.sf.saxon.expr.Expression;
4 import net.sf.saxon.expr.UserFunctionCall;
5 import net.sf.saxon.functions.FunctionLibrary;
6 import net.sf.saxon.trans.XPathException;
7
8 /**
9  * A StylesheetFunctionLibrary contains functions defined by the user in a stylesheet. This library is used at
10  * compile time only, as it contains references to the actual XSLFunction objects. Binding to a function in this
11  * library registers the function call on a fix-up list to be notified when the actual compiled function becomes
12  * available.
13  */

14
15 public class StylesheetFunctionLibrary implements FunctionLibrary {
16
17     private XSLStylesheet stylesheet;
18     private boolean overriding;
19
20     /**
21      * Create a FunctionLibrary that provides access to stylesheet functions
22      * @param sheet The XSLStylesheet element of the principal stylesheet module
23      * @param overriding set to true if this library is to contain functions specifying override="yes",
24      * or to false if it is to contain functions specifying override="no". (XSLT uses two instances
25      * of this class, one for overriding functions and one for non-overriding functions.)
26      */

27     public StylesheetFunctionLibrary(XSLStylesheet sheet, boolean overriding) {
28         this.stylesheet = sheet;
29         this.overriding = overriding;
30     }
31
32     /**
33      * Test whether a Saxon function with a given name and arity is available. This supports
34      * the function-available() function in XSLT.
35      * @param uri The URI of the function name
36      * @param local The local part of the function name
37      * @param arity The number of arguments. This is set to -1 in the case of the single-argument
38      * function-available() function; in this case the method should return true if there is some
39      * matching extension function, regardless of its arity.
40      */

41
42     public boolean isAvailable(int fingerprint, String JavaDoc uri, String JavaDoc local, int arity) {
43         XSLFunction fn = stylesheet.getStylesheetFunction(fingerprint, arity);
44         return (fn != null);
45     }
46
47     /**
48      * Bind a function, given the URI and local parts of the function name,
49      * and the list of expressions supplied as arguments. This method is called at compile
50      * time.
51      * @param nameCode The namepool nameCode of the function name. The uri and local name are also
52      * supplied (redundantly) to avoid fetching them from the name pool.
53      * @param uri The URI of the function name
54      * @param local The local part of the function name
55      * @param staticArgs The expressions supplied statically in the function call. The intention is
56      * that the static type of the arguments (obtainable via getItemType() and getCardinality() may
57      * be used as part of the binding algorithm.
58      * @return An object representing the extension function to be called, if one is found;
59      * null if no extension function was found matching the required name and arity.
60      * @throws net.sf.saxon.trans.XPathException if a function is found with the required name and arity, but
61      * the implementation of the function cannot be loaded or used; or if an error occurs
62      * while searching for the function; or if this function library "owns" the namespace containing
63      * the function call, but no function was found.
64      */

65
66     public Expression bind(int nameCode, String JavaDoc uri, String JavaDoc local, Expression[] staticArgs)
67             throws XPathException {
68         int fingerprint = nameCode & 0xfffff;
69         XSLFunction fn = stylesheet.getStylesheetFunction(fingerprint, staticArgs.length);
70         if (fn==null) {
71             return null;
72         }
73         if (fn.isOverriding() != overriding) {
74             return null;
75         }
76         UserFunctionCall fc = new UserFunctionCall();
77         fn.registerReference(fc);
78         fc.setFunctionNameCode(nameCode);
79         fc.setArguments(staticArgs);
80         fc.setConfirmed(true);
81         return fc;
82     }
83
84     /**
85      * This method creates a copy of a FunctionLibrary: if the original FunctionLibrary allows
86      * new functions to be added, then additions to this copy will not affect the original, or
87      * vice versa.
88      *
89      * @return a copy of this function library. This must be an instance of the original class.
90      */

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