001: package net.sf.saxon.functions;
002:
003: import net.sf.saxon.expr.Expression;
004: import net.sf.saxon.trans.XPathException;
005: import net.sf.saxon.query.XQueryFunctionBinder;
006: import net.sf.saxon.query.XQueryFunction;
007:
008: import java.util.ArrayList;
009: import java.util.Iterator;
010: import java.util.List;
011:
012: /**
013: * A FunctionLibraryList is a list of FunctionLibraries. It is also a FunctionLibrary in its own right.
014: * When required, it searches the list of FunctionLibraries to find the required function.
015: */
016: public class FunctionLibraryList implements FunctionLibrary,
017: XQueryFunctionBinder {
018:
019: public List libraryList = new ArrayList(8);
020:
021: /**
022: * Add a new FunctionLibrary to the list of FunctionLibraries in this FunctionLibraryList. Note
023: * that libraries are searched in the order they are added to the list.
024: * @param lib A function library to be added to the list of function libraries to be searched.
025: * @return the position of the library in the list
026: */
027:
028: public int addFunctionLibrary(FunctionLibrary lib) {
029: libraryList.add(lib);
030: return libraryList.size() - 1;
031: }
032:
033: /**
034: * Get the n'th function library in the list
035: */
036:
037: public FunctionLibrary get(int n) {
038: return (FunctionLibrary) libraryList.get(n);
039: }
040:
041: /**
042: * Test whether an extension function with a given name and arity is available. This supports
043: * the function-available() function in XSLT. This method may be called either at compile time
044: * or at run time.
045: * @param uri The URI of the function name
046: * @param local The local part of the function name
047: * @param arity The number of arguments. This is set to -1 in the case of the single-argument
048: * function-available() function; in this case the method should return true if there is some
049: * matching extension function, regardless of its arity.
050: */
051:
052: public boolean isAvailable(int fingerprint, String uri,
053: String local, int arity) {
054: for (Iterator it = libraryList.iterator(); it.hasNext();) {
055: FunctionLibrary lib = (FunctionLibrary) it.next();
056: if (lib.isAvailable(fingerprint, uri, local, arity)) {
057: return true;
058: }
059: }
060: return false;
061: }
062:
063: /**
064: * Bind an extension function, given the URI and local parts of the function name,
065: * and the list of expressions supplied as arguments. This method is called at compile
066: * time.
067: * @param uri The URI of the function name
068: * @param local The local part of the function name
069: * @param staticArgs The expressions supplied statically in arguments to the function call.
070: * The length of this array represents the arity of the function. The intention is
071: * that the static type of the arguments (obtainable via getItemType() and getCardinality() may
072: * be used as part of the binding algorithm. In some cases it may be possible for the function
073: * to be pre-evaluated at compile time, for example if these expressions are all constant values.
074: * @return An object representing the extension function to be called, if one is found;
075: * null if no extension function was found matching the required name and arity.
076: * @throws net.sf.saxon.trans.XPathException if a function is found with the required name and arity, but
077: * the implementation of the function cannot be loaded or used; or if an error occurs
078: * while searching for the function.
079: */
080:
081: public Expression bind(int nameCode, String uri, String local,
082: Expression[] staticArgs) throws XPathException {
083: for (Iterator it = libraryList.iterator(); it.hasNext();) {
084: FunctionLibrary lib = (FunctionLibrary) it.next();
085: Expression func = lib
086: .bind(nameCode, uri, local, staticArgs);
087: if (func != null) {
088: return func;
089: }
090: }
091: return null;
092: }
093:
094: /**
095: * Get the function declaration corresponding to a given function name and arity
096: *
097: * @return the XQueryFunction if there is one, or null if not.
098: */
099:
100: public XQueryFunction getDeclaration(int nameCode, String uri,
101: String local, Expression[] staticArgs) {
102: for (Iterator it = libraryList.iterator(); it.hasNext();) {
103: FunctionLibrary lib = (FunctionLibrary) it.next();
104: if (lib instanceof XQueryFunctionBinder) {
105: XQueryFunction func = ((XQueryFunctionBinder) lib)
106: .getDeclaration(nameCode, uri, local,
107: staticArgs);
108: if (func != null) {
109: return func;
110: }
111: }
112: }
113: return null;
114: }
115:
116: /**
117: * Get the list of contained FunctionLibraries. This method allows the caller to modify
118: * the library list, for example by adding a new FunctionLibrary at a chosen position,
119: * by removing a library from the list, or by changing the order of libraries in the list.
120: * Note that such changes may violate rules in the
121: * language specifications, or assumptions made within the product.
122: * @return a list whose members are of class FunctionLibrary
123: */
124:
125: public List getLibraryList() {
126: return libraryList;
127: }
128:
129: /**
130: * This method creates a copy of a FunctionLibrary: if the original FunctionLibrary allows
131: * new functions to be added, then additions to this copy will not affect the original, or
132: * vice versa.
133: *
134: * @return a copy of this function library. This must be an instance of the original class.
135: */
136:
137: public FunctionLibrary copy() {
138: FunctionLibraryList fll = new FunctionLibraryList();
139: fll.libraryList = new ArrayList(libraryList.size());
140: for (int i = 0; i < libraryList.size(); i++) {
141: fll.libraryList.add(((FunctionLibrary) libraryList.get(i))
142: .copy());
143: }
144: return fll;
145: }
146: }
147: //
148: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
149: // you may not use this file except in compliance with the License. You may obtain a copy of the
150: // License at http://www.mozilla.org/MPL/
151: //
152: // Software distributed under the License is distributed on an "AS IS" basis,
153: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
154: // See the License for the specific language governing rights and limitations under the License.
155: //
156: // The Original Code is: all this file.
157: //
158: // The Initial Developer of the Original Code is Michael H. Kay.
159: //
160: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
161: //
162: // Contributor(s): none.
163: //
|