/**
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.sun.facelets.tag;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;

import javax.el.ELException;
import javax.el.VariableMapper;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;

import com.sun.facelets.FaceletContext;
import com.sun.facelets.FaceletException;
import com.sun.facelets.FaceletHandler;
import com.sun.facelets.el.VariableMapperWrapper;
import com.sun.facelets.tag.ui.TemplateClient;
import com.sun.facelets.tag.ui.TemplateManager;

/**
 * A Tag that is specified in a FaceletFile. Takes all attributes specified and
 * sets them on the FaceletContext before including the targeted Facelet file.
 * 
 * @author Jacob Hookom
 * @version $Id: UserTagHandler.java,v 1.4 2005-08-24 04:38:48 jhook Exp $
 */
final class UserTagHandler extends TagHandler implements TemplateClient {

    protected final TagAttribute[] vars;

    protected final URL location;

    /**
     * @param config
     */
    public UserTagHandler(TagConfig config, URL location) {
        super(config);
        this.vars = this.tag.getAttributes().getAll();
        this.location = location;
    }

    /**
     * Iterate over all TagAttributes and set them on the FaceletContext's
     * VariableMapper, then include the target Facelet. Finally, replace the old
     * VariableMapper.
     * 
     * @see TagAttribute#getValueExpression(FaceletContext, Class)
     * @see VariableMapper
     * @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext,
     *      javax.faces.component.UIComponent)
     */
    public void apply(FaceletContext ctx, UIComponent parent)
            throws IOException, FacesException, FaceletException, ELException {
        VariableMapper orig = ctx.getVariableMapper();
        TemplateManager mngr = TemplateManager.getInstance(ctx);
        try {
            if (this.vars.length > 0) {
                VariableMapper varMapper = new VariableMapperWrapper(orig);
                ctx.setVariableMapper(varMapper);
                for (int i = 0; i < this.vars.length; i++) {
                    varMapper.setVariable(this.vars[i].getLocalName(),
                            this.vars[i].getValueExpression(ctx, Object.class));
                }
            }
            mngr.pushClient(this);
            ctx.includeFacelet(parent, this.location);
        } catch (FileNotFoundException e) {
            throw new TagException(this.tag, e.getMessage());
        } finally {
            mngr.popClient();
            ctx.setVariableMapper(orig);
        }
    }

    public FaceletHandler getHandler(FaceletContext ctx, String name) {
        return (name == null) ? this.nextHandler : null;
    }

}

@@ -1300,17 +1300,17 @@
 ava,v 1.
-4
+5
  2005-08
@@ -1315,18 +1315,18 @@
 08-2
-4 04:38:48
+5 14:15:24
  jho
@@ -2407,18 +2407,41 @@
     
-try %7B
 %0A    
+    // setup a variable map%0A
     
@@ -2468,28 +2468,24 @@
 ngth %3E 0) %7B%0A
-    
             
@@ -2548,62 +2548,8 @@
 g);%0A
-                ctx.setVariableMapper(varMapper);%0A    
     
@@ -2597,28 +2597,24 @@
 gth; i++) %7B%0A
-    
             
@@ -2667,16 +2667,29 @@
 lName(),
+ this.vars%5Bi%5D
 %0A       
@@ -2709,24 +2709,8 @@
     
-    this.vars%5Bi%5D
 .get
@@ -2747,27 +2747,79 @@
 ass));%0A     
+       %7D%0A
    
+         ctx.setVariableMapper(varMapper);%0A
         %7D%0A  
@@ -2816,37 +2816,70 @@
       %7D%0A        
+%0A  
     
-%7D
+  // eval include%0A        try %7B
 %0A            mng
@@ -3075,24 +3075,82 @@
 %7D finally %7B%0A
+            %0A            // make sure we undo our changes%0A
             

fix for issue #27 - new variablemapper must be set after the new valueexpressions are created