KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > SystemFunctionLibrary


1 package net.sf.saxon.functions;
2
3 import net.sf.saxon.expr.Expression;
4 import net.sf.saxon.om.NamespaceConstant;
5 import net.sf.saxon.trans.StaticError;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.Err;
8
9 /**
10  * The SystemFunctionLibrary represents the collection of functions in the fn: namespace. That is, the
11  * functions defined in the "Functions and Operators" specification, optionally augmented by the additional
12  * functions defined in XSLT.
13  */

14
15 public class SystemFunctionLibrary implements FunctionLibrary {
16
17     private int functionSet;
18
19     public static final int XPATH_ONLY = 0;
20     public static final int FULL_XSLT = 1;
21     public static final int USE_WHEN = 2;
22
23     /**
24      * Create a SystemFunctionLibrary
25      * @param functionSet determines the set of functions allowed. One of
26      * {@link #XPATH_ONLY}, {@link #FULL_XSLT}, {@link #USE_WHEN}
27      */

28
29     public SystemFunctionLibrary(int functionSet) {
30         this.functionSet = functionSet;
31     }
32
33     /**
34      * Test whether a system function with a given name and arity is available. This supports
35      * the function-available() function in XSLT. This method may be called either at compile time
36      * or at run time.
37      * @param uri The URI of the function name
38      * @param local The local part of the function name
39      * @param arity The number of arguments. This is set to -1 in the case of the single-argument
40      * function-available() function; in this case the method should return true if there is some
41      * matching extension function, regardless of its arity.
42      */

43
44     public boolean isAvailable(int fingerprint, String JavaDoc uri, String JavaDoc local, int arity) {
45         if (uri.equals(NamespaceConstant.FN)) {
46             StandardFunction.Entry entry = StandardFunction.getFunction(local, arity);
47             if (entry == null) {
48                 return false;
49             }
50             return (arity == -1 ||
51                     (arity >= entry.minArguments && arity <= entry.maxArguments));
52         } else {
53             return false;
54         }
55     }
56
57     /**
58      * Bind an extension function, given the URI and local parts of the function name,
59      * and the list of expressions supplied as arguments. This method is called at compile
60      * time.
61      * @param uri The URI of the function name
62      * @param local The local part of the function name
63      * @param staticArgs The expressions supplied statically in the function call. The intention is
64      * that the static type of the arguments (obtainable via getItemType() and getCardinality() may
65      * be used as part of the binding algorithm.
66      * @return An object representing the extension function to be called, if one is found;
67      * null if no extension function was found matching the required name and arity.
68      * @throws net.sf.saxon.trans.XPathException if a function is found with the required name and arity, but
69      * the implementation of the function cannot be loaded or used; or if an error occurs
70      * while searching for the function; or if this function library "owns" the namespace containing
71      * the function call, but no function was found.
72      */

73
74     public Expression bind(int nameCode, String JavaDoc uri, String JavaDoc local, Expression[] staticArgs)
75             throws XPathException {
76         if (uri.equals(NamespaceConstant.FN)) {
77             StandardFunction.Entry entry = StandardFunction.getFunction(local, staticArgs.length);
78             if (entry == null) {
79                 if (StandardFunction.getFunction(local, -1) == null) {
80                     StaticError err = new StaticError("Unknown system function " + local + "()");
81                     err.setErrorCode("XPST0017");
82                     throw err;
83                 } else {
84                     StaticError err = new StaticError("System function " + local + "() cannot be called with "
85                         + pluralArguments(staticArgs.length));
86                     err.setErrorCode("XPST0017");
87                     throw err;
88                 }
89             }
90             Class JavaDoc functionClass = entry.implementationClass;
91             SystemFunction f;
92             try {
93                 f = (SystemFunction)functionClass.newInstance();
94             } catch (Exception JavaDoc err) {
95                 throw new AssertionError JavaDoc("Failed to load system function: " + err.getMessage());
96             }
97             f.setDetails(entry);
98             f.setFunctionNameCode(nameCode);
99             if (functionSet != FULL_XSLT) {
100                 if (f instanceof XSLTFunction || (f instanceof NamePart && entry.opcode==NamePart.GENERATE_ID)) {
101                     if (functionSet == XPATH_ONLY) {
102                         StaticError err = new StaticError(
103                                 "Cannot use the " + local + "() function in a non-XSLT context");
104                         err.setErrorCode("XPST0017");
105                         throw err;
106                     } else if (functionSet == USE_WHEN &&
107                             !(f instanceof Available || f instanceof SystemProperty)) {
108                         StaticError err = new StaticError(
109                                 "Cannot use the " + local + "() function in a use-when expression");
110                         err.setErrorCode("XPST0017");
111                         throw err;
112                     }
113                 }
114             }
115             f.setArguments(staticArgs);
116             checkArgumentCount(staticArgs.length, entry.minArguments, entry.maxArguments, local);
117             return f;
118         } else {
119             return null;
120         }
121     }
122
123     /**
124     * Check number of arguments. <BR>
125     * A convenience routine for use in subclasses.
126     * @param min the minimum number of arguments allowed
127     * @param max the maximum number of arguments allowed
128     * @return the actual number of arguments
129     * @throws net.sf.saxon.trans.XPathException if the number of arguments is out of range
130     */

131
132     private int checkArgumentCount(int numArgs, int min, int max, String JavaDoc local) throws XPathException {
133         if (min==max && numArgs != min) {
134             throw new StaticError("Function " + Err.wrap(local, Err.FUNCTION) + " must have "
135                     + min + pluralArguments(min));
136         }
137         if (numArgs < min) {
138             throw new StaticError("Function " + Err.wrap(local, Err.FUNCTION) + " must have at least "
139                     + min + pluralArguments(min));
140         }
141         if (numArgs > max) {
142             throw new StaticError("Function " + Err.wrap(local, Err.FUNCTION) + " must have no more than "
143                     + max + pluralArguments(max));
144         }
145         return numArgs;
146     }
147
148     /**
149     * Utility routine used in constructing error messages
150     */

151
152     private static String JavaDoc pluralArguments(int num) {
153         if (num==1) return " argument";
154         return " arguments";
155     }
156
157     /**
158      * This method creates a copy of a FunctionLibrary: if the original FunctionLibrary allows
159      * new functions to be added, then additions to this copy will not affect the original, or
160      * vice versa.
161      *
162      * @return a copy of this function library. This must be an instance of the original class.
163      */

164
165     public FunctionLibrary copy() {
166         return this;
167     }
168 }
169 //
170
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
171
// you may not use this file except in compliance with the License. You may obtain a copy of the
172
// License at http://www.mozilla.org/MPL/
173
//
174
// Software distributed under the License is distributed on an "AS IS" basis,
175
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
176
// See the License for the specific language governing rights and limitations under the License.
177
//
178
// The Original Code is: all this file.
179
//
180
// The Initial Developer of the Original Code is Michael H. Kay.
181
//
182
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
183
//
184
// Contributor(s): none.
185
//
Popular Tags