001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.xalan.extensions;
020    
021    import java.util.List;
022    import java.util.Vector;
023    
024    import javax.xml.transform.TransformerException;
025    import javax.xml.xpath.XPathFunction;
026    import javax.xml.xpath.XPathFunctionException;
027    
028    /**
029     * A sample implementation of XPathFunction, with support for
030     * EXSLT extension functions and Java extension functions.
031     */
032    public class XPathFunctionImpl implements XPathFunction
033    {
034        private ExtensionHandler m_handler;
035        private String m_funcName;
036        
037        /**
038         * Construct an instance of XPathFunctionImpl from the
039         * ExtensionHandler and function name.
040         */
041        public XPathFunctionImpl(ExtensionHandler handler, String funcName)
042        {
043            m_handler = handler;
044            m_funcName = funcName;
045        }
046            
047        /**
048         * @see javax.xml.xpath.XPathFunction#evaluate(java.util.List)
049         */
050        public Object evaluate(List args)
051            throws XPathFunctionException
052        {
053            Vector  argsVec = listToVector(args);
054            
055            try {
056                // The method key and ExpressionContext are set to null.
057                return m_handler.callFunction(m_funcName, argsVec, null, null);
058            }
059            catch (TransformerException e)
060            {
061                throw new XPathFunctionException(e);
062            }
063        }
064        
065        /**
066         * Convert a java.util.List to a java.util.Vector. 
067         * No conversion is done if the List is already a Vector.
068         */
069        private static Vector listToVector(List args)
070        {
071            if (args == null)
072                return null;
073            else if (args instanceof Vector)
074                return (Vector)args;
075            else
076            {
077                Vector result = new Vector();
078                result.addAll(args);
079                return result;
080            }        
081        }
082    }