Coverage Report - org.apache.myfaces.view.facelets.tag.jsf.ActionSourceRule
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionSourceRule
0%
0/15
0%
0/10
2
ActionSourceRule$ActionListenerMapper
0%
0/5
N/A
2
ActionSourceRule$ActionListenerMapper2
0%
0/9
0%
0/2
2
ActionSourceRule$ActionMapper
0%
0/5
N/A
2
ActionSourceRule$ActionMapper2
0%
0/6
N/A
2
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.myfaces.view.facelets.tag.jsf;
 20  
 
 21  
 import javax.el.MethodExpression;
 22  
 import javax.faces.component.ActionSource;
 23  
 import javax.faces.component.ActionSource2;
 24  
 import javax.faces.event.ActionEvent;
 25  
 import javax.faces.event.MethodExpressionActionListener;
 26  
 import javax.faces.view.facelets.FaceletContext;
 27  
 import javax.faces.view.facelets.MetaRule;
 28  
 import javax.faces.view.facelets.Metadata;
 29  
 import javax.faces.view.facelets.MetadataTarget;
 30  
 import javax.faces.view.facelets.TagAttribute;
 31  
 
 32  
 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
 33  
 import org.apache.myfaces.view.facelets.el.LegacyMethodBinding;
 34  
 
 35  
 /**
 36  
  * 
 37  
  * @author Jacob Hookom
 38  
  * @version $Id$
 39  
  */
 40  
 public final class ActionSourceRule extends MetaRule
 41  
 {
 42  0
     public final static Class<?>[] ACTION_SIG = new Class[0];
 43  
 
 44  0
     public final static Class<?>[] ACTION_LISTENER_SIG = new Class<?>[] { ActionEvent.class };
 45  
 
 46  
     final static class ActionMapper extends Metadata
 47  
     {
 48  
 
 49  
         private final TagAttribute attr;
 50  
 
 51  
         public ActionMapper(TagAttribute attr)
 52  0
         {
 53  0
             this.attr = attr;
 54  0
         }
 55  
 
 56  
         public void applyMetadata(FaceletContext ctx, Object instance)
 57  
         {
 58  0
             ((ActionSource) instance).setAction(new LegacyMethodBinding(
 59  
                     this.attr.getMethodExpression(ctx, null, ActionSourceRule.ACTION_SIG)));
 60  0
         }
 61  
     }
 62  
     
 63  
     final static class ActionMapper2 extends Metadata
 64  
     {
 65  
         private final TagAttribute _attr;
 66  
 
 67  
         public ActionMapper2(TagAttribute attr)
 68  0
         {
 69  0
             this._attr = attr;
 70  0
         }
 71  
 
 72  
         public void applyMetadata(FaceletContext ctx, Object instance)
 73  
         {
 74  0
             MethodExpression expr = _attr.getMethodExpression(ctx, null, ActionSourceRule.ACTION_SIG);
 75  0
             ((ActionSource2) instance).setActionExpression(expr);
 76  0
         }
 77  
     }
 78  
 
 79  
     final static class ActionListenerMapper extends Metadata
 80  
     {
 81  
 
 82  
         private final TagAttribute attr;
 83  
 
 84  
         public ActionListenerMapper(TagAttribute attr)
 85  0
         {
 86  0
             this.attr = attr;
 87  0
         }
 88  
 
 89  
         public void applyMetadata(FaceletContext ctx, Object instance)
 90  
         {
 91  0
             ((ActionSource) instance)
 92  
                     .setActionListener(new LegacyMethodBinding(this.attr
 93  
                             .getMethodExpression(ctx, null,
 94  
                                     ActionSourceRule.ACTION_LISTENER_SIG)));
 95  0
         }
 96  
 
 97  
     }
 98  
 
 99  
     final static class ActionListenerMapper2 extends Metadata
 100  
     {
 101  
         private final TagAttribute _attr;
 102  
 
 103  
         public ActionListenerMapper2(TagAttribute attr)
 104  0
         {
 105  0
             _attr = attr;
 106  0
         }
 107  
 
 108  
         public void applyMetadata(FaceletContext ctx, Object instance)
 109  
         {
 110  
             // From JSF 2.0 it is possible to have actionListener method without ActionEvent parameter. 
 111  
             // It seems that MethodExpressionActionListener from API contains support for it but there is one big
 112  
             // problem - one-arg constructor will not preserve the current VariableMapper.
 113  
             // This is a problem when using facelets and <ui:decorate/> with EL params (see MYFACES-2541 for details).
 114  
             // So we must create two MethodExpressions here - both are created from the current 
 115  
             // facelets context and thus varibale mapping will work.
 116  0
             final MethodExpression methodExpressionOneArg
 117  
                     = _attr.getMethodExpression(ctx, null, ActionSourceRule.ACTION_LISTENER_SIG);
 118  0
             final MethodExpression methodExpressionZeroArg
 119  
                     = _attr.getMethodExpression(ctx, null, ActionSourceRule.ACTION_SIG);
 120  0
             if (FaceletCompositionContext.getCurrentInstance(ctx).isUsingPSSOnThisView())
 121  
             {
 122  0
                 ((ActionSource2) instance).addActionListener(
 123  
                         new PartialMethodExpressionActionListener(methodExpressionOneArg, methodExpressionZeroArg));
 124  
             }
 125  
             else
 126  
             {
 127  0
                 ((ActionSource2) instance).addActionListener(
 128  
                         new MethodExpressionActionListener(methodExpressionOneArg, methodExpressionZeroArg));
 129  
             }
 130  0
         }
 131  
     }
 132  
 
 133  0
     public final static ActionSourceRule INSTANCE = new ActionSourceRule();
 134  
 
 135  
     public ActionSourceRule()
 136  
     {
 137  0
         super();
 138  0
     }
 139  
 
 140  
     public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta)
 141  
     {
 142  0
         if (meta.isTargetInstanceOf(ActionSource.class))
 143  
         {
 144  0
             if ("action".equals(name))
 145  
             {
 146  0
                 if (meta.isTargetInstanceOf(ActionSource2.class))
 147  
                 {
 148  0
                     return new ActionMapper2(attribute);
 149  
                 }
 150  
                 else
 151  
                 {
 152  0
                     return new ActionMapper(attribute);
 153  
                 }
 154  
             }
 155  
 
 156  0
             if ("actionListener".equals(name))
 157  
             {
 158  0
                 if (meta.isTargetInstanceOf(ActionSource2.class))
 159  
                 {
 160  0
                     return new ActionListenerMapper2(attribute);
 161  
                 }
 162  
                 else
 163  
                 {
 164  0
                     return new ActionListenerMapper(attribute);
 165  
                 }
 166  
             }
 167  
         }
 168  
         
 169  0
         return null;
 170  
     }
 171  
 }