/*
 *  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 java.util.Iterator;

import org.exist.xquery.util.ExpressionDumper;
import org.exist.xquery.value.Item;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.Type;
import org.exist.xquery.value.ValueSequence;

/**
 * An XQuery sequence constructor ",". For example, ($a, $b) constructs a new
 * sequence containing items $a and $b.
 * 
 * @author wolf
 */
public class SequenceConstructor extends PathExpr {
	
	/**
	 * @param context
	 */
	public SequenceConstructor(XQueryContext context) {
		super(context);
	}
	
    public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
        contextInfo.setParent(this);
        inPredicate = (contextInfo.getFlags() & IN_PREDICATE) > 0;
        contextId = contextInfo.getContextId();
        
        for (int i = 0; i < steps.size(); i++) {
            Expression expr = (Expression) steps.get(i);
            expr.analyze(contextInfo);
        }
    }
    
	/* (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 {
		ValueSequence result = new ValueSequence();
		Sequence temp;
		for(Iterator i = steps.iterator(); i.hasNext(); ) {
			temp = ((Expression)i.next()).eval(contextSequence, contextItem);
			if(temp != null && !temp.isEmpty())
				result.addAll(temp);
		}
		return result;
	}

	/* (non-Javadoc)
     * @see org.exist.xquery.PathExpr#dump(org.exist.xquery.util.ExpressionDumper)
     */
    public void dump(ExpressionDumper dumper) {
        dumper.display("(");
        dumper.startIndent();
        for(Iterator i = steps.iterator(); i.hasNext(); ) {
            ((Expression) i.next()).dump(dumper);
            dumper.display(", ");
        }
        dumper.endIndent();
        dumper.nl().display(")");
    }
    
    public String toString() {
    	StringBuffer result = new StringBuffer();
    	result.append("( ");
    	boolean moreThanOne = false;
        for(Iterator i = steps.iterator(); i.hasNext(); ) {
        	if (moreThanOne) result.append(", ");
        	moreThanOne = true;
        	result.append(((Expression) i.next()).toString());        	
        }        
        result.append(" )");
        return result.toString();
    }    
    
	/* (non-Javadoc)
	 * @see org.exist.xquery.Expression#returnsType()
	 */
	public int returnsType() {
		return Type.ITEM;
	}

	/* (non-Javadoc)
	 * @see org.exist.xquery.AbstractExpression#getCardinality()
	 */
	public int getCardinality() {
		return Cardinality.ZERO_OR_MORE;
	}

	/* (non-Javadoc)
	 * @see org.exist.xquery.AbstractExpression#resetState()
	 */
	public void resetState() {
		super.resetState();
	}
}

@@ -1825,20 +1825,166 @@
     
-expr.analyze
+//Create a new context info because each sequence expression could modify it (add/remove flags...)            %0A            expr.analyze(new AnalyzeContextInfo
 (con
@@ -1992,16 +1992,17 @@
 extInfo)
+)
 ;%0A      

Allocate a specific context information for each of the expressions of a sequence constructor. A context info is *mutable* ;-)