/*
 * Copyright
 *   2005 axYus - www.axyus.com
 *   2005 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.dgfip.utils.xml.xpath;

import java.util.Hashtable;

import javax.xml.namespace.QName;
import javax.xml.xpath.XPathFunction;
import javax.xml.xpath.XPathFunctionResolver;

/**
 *
 * @author chm
 */
public class FunctionResolver implements XPathFunctionResolver {
    
    private static Hashtable<QName,XPathFunction> functions = null;
    static {
        functions = new Hashtable<QName,XPathFunction>();
        functions.put(new QName("http://admisource.gouv.fr/projects/xemelios/ext","StringEquals","xem"),new FuncStringEquals());
        functions.put(new QName("http://admisource.gouv.fr/projects/xemelios/ext","StringDiffers","xem"),new FuncStringDiffers());
        functions.put(new QName("http://admisource.gouv.fr/projects/xemelios/ext","ends-with","xem"),new FuncEndsWith());
        functions.put(new QName("http://admisource.gouv.fr/projects/xemelios/ext","UpperCase","xem"),new FuncUppercase());
        functions.put(new QName("http://admisource.gouv.fr/projects/xemelios/ext","LowerCase","xem"),new FuncLowercase());
        functions.put(new QName("http://admisource.gouv.fr/projects/xemelios/ext","InList","xem"),new FuncStringInList());
        functions.put(new QName("http://admisource.gouv.fr/projects/xemelios/ext","escape-uri","xem"),new FuncEscapeUri());
        functions.put(new QName("http://admisource.gouv.fr/projects/xemelios/ext","DateBefore","xem"), new FuncDateBefore());
        functions.put(new QName("http://admisource.gouv.fr/projects/xemelios/ext","DateAfter","xem"), new FuncDateAfter());
        functions.put(new QName("http://admisource.gouv.fr/projects/xemelios/ext","DateEquals","xem"), new FuncDateEquals());
        functions.put(new QName("http://admisource.gouv.fr/projects/xemelios/ext","StringIsNull","xem"),new FuncStringIsNull());
        functions.put(new QName("http://admisource.gouv.fr/projects/xemelios/ext","StringIsNotNull","xem"),new FuncStringIsNotNull());
        functions.put(new QName("http://admisource.gouv.fr/projects/xemelios/ext","does-not-contain","xem"),new FuncStringDoNotContains());
    }
    
    /** Creates a new instance of FunctionResolver */
    public FunctionResolver() {
    }

    public XPathFunction resolveFunction(QName functionName, int arity) {
        QName fn = new QName(getNamespace(functionName.getPrefix()),functionName.getLocalPart(),functionName.getPrefix());
//        System.out.println("trying to resolve "+fn);
        XPathFunction function=functions.get(fn);
//        System.out.println("->> found "+function.getClass().getName());
        return function;
    }
    private static String getNamespace(String prefix) { return "http://admisource.gouv.fr/projects/xemelios/ext"; }
    
}
