/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-04 The eXist Team
 *
 *  http://exist-db.org
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *  
 *  This program 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 Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id$
 */
package org.exist.xquery.functions.util;

import java.util.List;

import org.exist.dom.QName;
import org.exist.storage.Indexable;
import org.exist.storage.NativeValueIndexByQName;
import org.exist.xquery.Cardinality;
import org.exist.xquery.Dependency;
import org.exist.xquery.DynamicCardinalityCheck;
import org.exist.xquery.Expression;
import org.exist.xquery.Function;
import org.exist.xquery.FunctionSignature;
import org.exist.xquery.RootNode;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.util.Error;
import org.exist.xquery.util.ExpressionDumper;
import org.exist.xquery.util.Messages;
import org.exist.xquery.value.AtomicValue;
import org.exist.xquery.value.Item;
import org.exist.xquery.value.QNameValue;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.Type;

/**
 * @author J.M. Vanel
 */
public class QNameIndexLookup extends Function {

	public final static FunctionSignature signature = new FunctionSignature(
			new QName("qname-index-lookup", UtilModule.NAMESPACE_URI,
					UtilModule.PREFIX),
			"Fast retrieval of nodes by node name and content, "
					+ "using the new value index by QName's",
			new SequenceType[] {
					new SequenceType(Type.QNAME, Cardinality.EXACTLY_ONE),
					new SequenceType(Type.ATOMIC, Cardinality.EXACTLY_ONE) },
			new SequenceType(Type.NODE, Cardinality.ZERO_OR_MORE));

	public QNameIndexLookup(XQueryContext context) {
		super(context, signature);
	}

    
    /**
     * Overwritten: function can process the whole context sequence at once.
     * 
     * @see org.exist.xquery.Expression#getDependencies()
     */
    public int getDependencies() {
        return Dependency.CONTEXT_SET;
    }
    
    /**
     * Overwritten to disable automatic type checks. We check manually.
     * 
     * @see org.exist.xquery.Function#setArguments(java.util.List)
     */
    public void setArguments(List arguments) throws XPathException {
        // wrap arguments into a cardinality check, so an error will be generated if
        // one of the arguments returns an empty sequence
        Expression arg = (Expression) arguments.get(0);
        arg = new DynamicCardinalityCheck(context, Cardinality.ONE_OR_MORE, arg,
                new Error(Error.FUNC_PARAM_CARDINALITY, "1", mySignature));
        steps.add(arg);
        
        arg = (Expression) arguments.get(1);
        arg = new DynamicCardinalityCheck(context, Cardinality.ONE_OR_MORE, arg,
                new Error(Error.FUNC_PARAM_CARDINALITY, "2", mySignature));
        steps.add(arg);
    }
    
    public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
        if (contextSequence == null || contextSequence.getLength() == 0) {
            // if the context sequence is empty, we create a default context 
            RootNode rootNode = new RootNode(context);
            contextSequence = rootNode.eval(null, null);
        }
        Sequence[] args = getArguments(contextSequence, contextItem);
        
        Item item = args[0].itemAt(0);
        QNameValue qval;
        try {
            // attempt to convert the first argument to a QName
            qval = (QNameValue) item.convertTo(Type.QNAME);
        } catch (XPathException e) {
            // wrong type: generate a diagnostic error
            throw new XPathException(getASTNode(),
                    Messages.formatMessage(Error.FUNC_PARAM_TYPE, 
                            new Object[] { "1", mySignature.toString(), null,
                            Type.getTypeName(Type.QNAME), Type.getTypeName(item.getType()) }
                    ));
        }
        QName qname = qval.getQName();
        
        AtomicValue comparisonCriterium = args[1].itemAt(0).atomize();
        
        Sequence result = Sequence.EMPTY_SEQUENCE;

        if (comparisonCriterium instanceof Indexable) {
            NativeValueIndexByQName valueIndex = context.getBroker()
                    .getQNameValueIndex();
            result = valueIndex.findByQName(qname, comparisonCriterium,
                    contextSequence);
        } else {
            // TODO error message & log : 
            // "The comparison Criterium must be an Indexable: boolean, numeric, string, and not ...
        }
        return result;
    }
}

@@ -5049,53 +5049,24 @@
     
-// TODO error message & log : %0A            //
+String message =
  %22Th
@@ -5078,17 +5078,17 @@
 parison 
-C
+c
 riterium
@@ -5110,16 +5110,34 @@
 exable: 
+%22 +%0A            %09%22
 boolean,
@@ -5156,21 +5156,165 @@
 ring
-, and not ...
+; instead your criterium has type %22 +%0A            %09Type.getTypeName(comparisonCriterium.getType());%0A        %09throw new XPathException(getASTNode(), message);
 %0A   

add runtime error message