/**
 *  Copyright (C) 2004 Orbeon, Inc.
 *
 *  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.1 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.
 *
 *  The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.transformer.xupdate;

import org.orbeon.oxf.transformer.xupdate.statement.Param;
import org.orbeon.oxf.transformer.xupdate.statement.Utils;

import javax.xml.transform.URIResolver;
import java.util.ArrayList;
import java.util.List;

public class Closure {

    private URIResolver uriResolver;
    private Object context;
    private VariableContextImpl variableContext;
    private Statement[] statements;

    public Closure(URIResolver uriResolver, Object context, Statement[] statements) {
        this.uriResolver = uriResolver;
        this.context = context;
        this.variableContext = variableContext;
        this.statements = statements;
    }

    public void setVariableContext(VariableContextImpl variableContext) {
        this.variableContext = variableContext;
    }

    public Object execute(List args) {
        int paramCount = 0;
        VariableContextImpl currentVariableContext = variableContext;
        List statementsToExecute = new ArrayList();
        for (int i = 0; i < statements.length; i++) {
            Statement statement = statements[i];
            if (statement instanceof Param) {
                Param param = (Param) statement;
                Object value = paramCount < args.size()
                        ? args.get(paramCount)
                        : param.execute(uriResolver, context, currentVariableContext);
                currentVariableContext = new VariableContextImpl(currentVariableContext, param.getName(), value);
                paramCount++;
            } else {
                statementsToExecute.add(statement);
            }
        }
        return Utils.execute(uriResolver, context, currentVariableContext, 
                (Statement[]) statementsToExecute.toArray(new Statement[statementsToExecute.size()]));
    }
}

@@ -1116,21 +1116,16 @@
 r uriRes
-olver
 , Object
@@ -1126,20 +1126,17 @@
 Object c
-onte
+t
 xt, Stat
@@ -1145,21 +1145,17 @@
 ent%5B%5D st
-ateme
+m
 nts) %7B%0A 
@@ -1161,21 +1161,16 @@
         
-this.
 uriResol
@@ -1177,29 +1177,24 @@
 ver = uriRes
-olver
 ;%0A        th
@@ -1191,21 +1191,16 @@
         
-this.
 context 
@@ -1202,20 +1202,17 @@
 text = c
-onte
+t
 xt;%0A    
@@ -1219,61 +1219,8 @@
     
-this.variableContext = variableContext;%0A        this.
 stat
@@ -1230,21 +1230,17 @@
 nts = st
-ateme
+m
 nts;%0A   

Fix 'assignment has no affect' warning.