/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-06 Wolfgang M. Meier
 *  wolfgang@exist-db.org
 *  http://exist.sourceforge.net
 *  
 *  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;

import org.exist.dom.QName;
import org.exist.xquery.value.IntegerValue;
import org.exist.xquery.value.Item;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.Type;
import org.exist.xquery.value.ValueSequence;

/**
 * An XQuery range expression, like "1 to 10".
 * 
 * @author wolf
 */
public class RangeExpression extends Function {

	public final static FunctionSignature signature =
		new FunctionSignature(
			new QName("to", Function.BUILTIN_FUNCTION_NS),
			new SequenceType[] {
				new SequenceType(Type.INTEGER, Cardinality.EXACTLY_ONE),
				new SequenceType(Type.INTEGER, Cardinality.EXACTLY_ONE)
			},
			new SequenceType(Type.INTEGER, Cardinality.ONE_OR_MORE));

	/**
	 * @param context
	 */
	public RangeExpression(XQueryContext context) {
		super(context, signature);
	}

	/* (non-Javadoc)
	 * @see org.exist.xquery.Expression#eval(org.exist.dom.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
	 */
	public Sequence eval(
		Sequence contextSequence,
		Item contextItem)
		throws XPathException {
		long start = ((IntegerValue)getArgument(0).eval(contextSequence, contextItem).
			convertTo(Type.INTEGER)).getLong();
		long end = ((IntegerValue)getArgument(1).eval(contextSequence, contextItem).
			convertTo(Type.INTEGER)).getLong();
		ValueSequence result = new ValueSequence();
		for(long i = start; i <= end; i++) {
			result.add(new IntegerValue(i));
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see org.exist.xquery.functions.Function#getDependencies()
	 */
	public int getDependencies() {
		return Dependency.NO_DEPENDENCY;
	}
}

@@ -1515,31 +1515,31 @@
 Cardinality.
-EXACTLY
+ZERO_OR
 _ONE),%0A%09%09%09%09n
@@ -1584,15 +1584,15 @@
 ity.
-EXACTLY
+ZERO_OR
 _ONE
@@ -1645,19 +1645,20 @@
 inality.
-ONE
+ZERO
 _OR_MORE
@@ -1949,19 +1949,16 @@
 ce eval(
-%0A%09%09
 Sequence
@@ -1974,19 +1974,17 @@
 equence,
-%0A%09%09
+ 
 Item con
@@ -1992,19 +1992,17 @@
 extItem)
-%0A%09%09
+ 
 throws X
@@ -2020,39 +2020,47 @@
 on %7B
+%09%09
 %0A%09%09
-long start = ((IntegerValue)
+Sequence result;%0A%09%09Sequence seq = 
 getA
@@ -2096,37 +2096,154 @@
 ce, contextItem)
-.%0A%09%09%09
+;%0A%09%09if(seq.getLength() == 0)%0A%09%09%09result = Sequence.EMPTY_SEQUENCE;%0A        else %7B%0A        %09long start = ((IntegerValue)seq.
 convertTo(Type.I
@@ -2266,36 +2266,23 @@
 ();%0A
-%09%09long end = ((IntegerValue)
+        %09seq = 
 getA
@@ -2330,13 +2330,132 @@
 tem)
-.%0A%09%09%09
+;%0A    %09%09if(seq.getLength() == 0)%0A    %09%09%09result = Sequence.EMPTY_SEQUENCE;%0A    %09%09else %7B%0A    %09%09%09long end = ((IntegerValue)seq.
 conv
@@ -2489,25 +2489,25 @@
 g();
-%0A%09%09ValueSequence 
+        %09%0A    %09%09%09
 resu
@@ -2532,16 +2532,21 @@
 ence();%0A
+    %09
 %09%09for(lo
@@ -2576,19 +2576,24 @@
  i++) %7B%0A
+    
 %09%09%09
+%09
 result.a
@@ -2617,18 +2617,41 @@
 ue(i));%0A
-%09%09
+    %09%09%09%7D%0A    %09%09%7D%0A        
 %7D%0A%09%09retu

Range expressions now accept empty sequences as arguments