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