001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jxpath;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Set;
023:
024: /**
025: * An object that aggregates Functions objects into a group Functions object.
026: * Since JXPathContext can only register a single Functions object,
027: * FunctionLibrary should always be used to group all Functions objects
028: * that need to be registered.
029: *
030: * @author Dmitri Plotnikov
031: * @version $Revision: 1.5 $ $Date: 2004/02/29 14:17:42 $
032: */
033: public class FunctionLibrary implements Functions {
034: private List allFunctions = new ArrayList();
035: private HashMap byNamespace = null;
036:
037: /**
038: * Add functions to the library
039: */
040: public void addFunctions(Functions functions) {
041: allFunctions.add(functions);
042: byNamespace = null;
043: }
044:
045: /**
046: * Remove functions from the library.
047: */
048: public void removeFunctions(Functions functions) {
049: allFunctions.remove(functions);
050: byNamespace = null;
051: }
052:
053: /**
054: * Returns a set containing all namespaces used by the aggregated
055: * Functions.
056: */
057: public Set getUsedNamespaces() {
058: if (byNamespace == null) {
059: prepareCache();
060: }
061: return byNamespace.keySet();
062: }
063:
064: /**
065: * Returns a Function, if any, for the specified namespace,
066: * name and parameter types.
067: */
068: public Function getFunction(String namespace, String name,
069: Object[] parameters) {
070: if (byNamespace == null) {
071: prepareCache();
072: }
073: Object candidates = byNamespace.get(namespace);
074: if (candidates instanceof Functions) {
075: return ((Functions) candidates).getFunction(namespace,
076: name, parameters);
077: } else if (candidates instanceof List) {
078: List list = (List) candidates;
079: int count = list.size();
080: for (int i = 0; i < count; i++) {
081: Function function = ((Functions) list.get(i))
082: .getFunction(namespace, name, parameters);
083: if (function != null) {
084: return function;
085: }
086: }
087: }
088: return null;
089: }
090:
091: private void prepareCache() {
092: byNamespace = new HashMap();
093: int count = allFunctions.size();
094: for (int i = 0; i < count; i++) {
095: Functions funcs = (Functions) allFunctions.get(i);
096: Set namespaces = funcs.getUsedNamespaces();
097: for (Iterator it = namespaces.iterator(); it.hasNext();) {
098: String ns = (String) it.next();
099: Object candidates = byNamespace.get(ns);
100: if (candidates == null) {
101: byNamespace.put(ns, funcs);
102: } else if (candidates instanceof Functions) {
103: List lst = new ArrayList();
104: lst.add(candidates);
105: lst.add(funcs);
106: byNamespace.put(ns, lst);
107: } else {
108: ((List) candidates).add(funcs);
109: }
110: }
111: }
112: }
113: }
|