KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.functions;
2
3 import net.sf.saxon.expr.Expression;
4 import net.sf.saxon.trans.XPathException;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 /**
11  * A FunctionLibraryList is a list of FunctionLibraries. It is also a FunctionLibrary in its own right.
12  * When required, it searches the list of FunctionLibraries to find the required function.
13  */

14 public class FunctionLibraryList implements FunctionLibrary {
15
16     public List JavaDoc libraryList = new ArrayList JavaDoc(8);
17
18     /**
19      * Add a new FunctionLibrary to the list of FunctionLibraries in this FunctionLibraryList. Note
20      * that libraries are searched in the order they are added to the list.
21      * @param lib A function library to be added to the list of function libraries to be searched.
22      */

23
24     public void addFunctionLibrary(FunctionLibrary lib) {
25         libraryList.add(lib);
26     }
27
28     /**
29      * Test whether an extension function with a given name and arity is available. This supports
30      * the function-available() function in XSLT. This method may be called either at compile time
31      * or at run time.
32      * @param uri The URI of the function name
33      * @param local The local part of the function name
34      * @param arity The number of arguments. This is set to -1 in the case of the single-argument
35      * function-available() function; in this case the method should return true if there is some
36      * matching extension function, regardless of its arity.
37      */

38
39     public boolean isAvailable(int fingerprint, String JavaDoc uri, String JavaDoc local, int arity) {
40         for (Iterator JavaDoc it=libraryList.iterator(); it.hasNext();) {
41             FunctionLibrary lib = (FunctionLibrary)it.next();
42             if (lib.isAvailable(fingerprint, uri, local, arity)) {
43                 return true;
44             }
45         }
46         return false;
47     }
48
49     /**
50      * Bind an extension function, given the URI and local parts of the function name,
51      * and the list of expressions supplied as arguments. This method is called at compile
52      * time.
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 arguments to the function call.
56      * The length of this array represents the arity of the function. The intention is
57      * that the static type of the arguments (obtainable via getItemType() and getCardinality() may
58      * be used as part of the binding algorithm. In some cases it may be possible for the function
59      * to be pre-evaluated at compile time, for example if these expressions are all constant values.
60      * @return An object representing the extension function to be called, if one is found;
61      * null if no extension function was found matching the required name and arity.
62      * @throws net.sf.saxon.trans.XPathException if a function is found with the required name and arity, but
63      * the implementation of the function cannot be loaded or used; or if an error occurs
64      * while searching for the function.
65      */

66
67     public Expression bind(int nameCode, String JavaDoc uri, String JavaDoc local, Expression[] staticArgs)
68             throws XPathException {
69         for (Iterator JavaDoc it=libraryList.iterator(); it.hasNext();) {
70             FunctionLibrary lib = (FunctionLibrary)it.next();
71             Expression func = lib.bind(nameCode, uri, local, staticArgs);
72             if (func != null) {
73                 return func;
74             }
75         }
76         return null;
77     }
78
79     /**
80      * Get the list of contained FunctionLibraries. This method allows the caller to modify
81      * the library list, for example by adding a new FunctionLibrary at a chosen position,
82      * by removing a library from the list, or by changing the order of libraries in the list.
83      * Note that such changes may violate rules in the
84      * language specifications, or assumptions made within the product.
85      * @return a list whose members are of class FunctionLibrary
86      */

87
88     public List JavaDoc getLibraryList() {
89         return libraryList;
90     }
91
92     /**
93      * This method creates a copy of a FunctionLibrary: if the original FunctionLibrary allows
94      * new functions to be added, then additions to this copy will not affect the original, or
95      * vice versa.
96      *
97      * @return a copy of this function library. This must be an instance of the original class.
98      */

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