/*
 * FunctionResolver.java
 *
 * Created on 10 avril 2006, 17:43
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */


package fr.gouv.finances.cp.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());
    }
    
    /** 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"; }
    
}
