Coverage Report - org.apache.myfaces.taglib.core.GenericListenerTag
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericListenerTag
0%
0/56
0%
0/22
0
 
 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.taglib.core;
 20  
 
 21  
 import org.apache.myfaces.shared_impl.util.ClassUtils;
 22  
 
 23  
 import javax.el.ELException;
 24  
 import javax.el.ValueExpression;
 25  
 import javax.faces.component.UIComponent;
 26  
 import javax.faces.context.FacesContext;
 27  
 import javax.faces.event.ActionListener;
 28  
 import javax.faces.webapp.UIComponentClassicTagBase;
 29  
 import javax.faces.webapp.UIComponentELTag;
 30  
 import javax.servlet.jsp.JspException;
 31  
 import javax.servlet.jsp.tagext.Tag;
 32  
 import javax.servlet.jsp.tagext.TagSupport;
 33  
 
 34  
 /**
 35  
  * @author Andreas Berger (latest modification by $Author: lu4242 $)
 36  
  * @version $Revision: 695050 $ $Date: 2008-09-13 17:38:43 -0500 (Sat, 13 Sep 2008) $
 37  
  * @since 1.2
 38  
  */
 39  
 public abstract class GenericListenerTag<_Holder, _Listener>
 40  
         extends TagSupport
 41  
 {
 42  0
     private ValueExpression _type = null;
 43  0
     private ValueExpression _binding = null;
 44  
     private Class<_Holder> _holderClazz;
 45  
 
 46  
     protected GenericListenerTag(Class<_Holder> holderClazz)
 47  
     {
 48  0
         super();
 49  0
         _holderClazz = holderClazz;
 50  0
     }
 51  
 
 52  
     public void setType(ValueExpression type)
 53  
     {
 54  0
         _type = type;
 55  0
     }
 56  
 
 57  
     public void setBinding(ValueExpression binding)
 58  
     {
 59  0
         _binding = binding;
 60  0
     }
 61  
 
 62  
     public void release()
 63  
     {
 64  0
         super.release();
 65  0
         _type = null;
 66  0
         _binding = null;
 67  0
     }
 68  
 
 69  
     protected abstract void addListener(_Holder holder, _Listener listener);
 70  
 
 71  
     protected abstract _Listener createDelegateListener(ValueExpression type, ValueExpression binding);
 72  
     
 73  
     public int doStartTag() throws JspException
 74  
     {
 75  0
         UIComponentClassicTagBase componentTag = UIComponentELTag.getParentUIComponentClassicTagBase(pageContext);
 76  0
         if (componentTag == null)
 77  
         {
 78  0
             throw new JspException("no parent UIComponentTag found");
 79  
         }
 80  
 
 81  0
         if (_type == null && _binding == null)
 82  
         {
 83  0
             throw new JspException("\"actionListener\" must have binding and/or type attribute.");
 84  
         }
 85  
 
 86  0
         if (!componentTag.getCreated())
 87  
         {
 88  0
             return Tag.SKIP_BODY;
 89  
         }
 90  
 
 91  0
         _Holder holder = null;
 92  0
         UIComponent component = componentTag.getComponentInstance();
 93  
         try
 94  
         {
 95  0
             holder = (_Holder) component;
 96  0
         } catch (ClassCastException e)
 97  
         {
 98  0
             throw new JspException(
 99  
                     "Component " + ((UIComponent) holder).getId() + " is not instance of " + _holderClazz.getName());
 100  0
         }
 101  
         
 102  0
         if (_type != null && _type.isLiteralText())
 103  
         {
 104  0
             createListener(holder,component);
 105  
         }else{
 106  0
             addListener(holder, createDelegateListener(_type,_binding));
 107  
         }
 108  
         
 109  0
         return Tag.SKIP_BODY;
 110  
     }
 111  
     
 112  
     protected void createListener(_Holder holder, UIComponent component) 
 113  
         throws JspException
 114  
     {
 115  0
         FacesContext facesContext = FacesContext.getCurrentInstance();
 116  
         _Listener listener;
 117  
         // type and/or binding must be specified
 118  
         try
 119  
         {
 120  0
             if (null != _binding)
 121  
             {
 122  
                 try
 123  
                 {
 124  0
                     listener = (_Listener) _binding.getValue(facesContext.getELContext());
 125  0
                     if (null != listener)
 126  
                     {
 127  0
                         addListener(holder, listener);
 128  
                         // no need for further processing
 129  0
                         return;
 130  
                     }
 131  
                 }
 132  0
                 catch (ELException e)
 133  
                 {
 134  0
                     throw new JspException("Exception while evaluating the binding attribute of Component "
 135  
                             + component.getId(), e);
 136  0
                 }
 137  
             }
 138  0
             if (null != _type)
 139  
             {
 140  
                 String className;
 141  0
                 if (_type.isLiteralText())
 142  
                 {
 143  0
                     className = _type.getExpressionString();
 144  
                     //If type is literal text we should create
 145  
                     //a new instance
 146  0
                     listener = (_Listener) ClassUtils.newInstance(className);
 147  
                 } else
 148  
                 {
 149  0
                     className = (String) _type.getValue(facesContext.getELContext());
 150  0
                     listener = null;
 151  
                 }
 152  
                                 
 153  0
                 if (null != _binding)
 154  
                 {
 155  
                     try
 156  
                     {
 157  0
                         _binding.setValue(facesContext.getELContext(), listener);
 158  0
                     } catch (ELException e)
 159  
                     {
 160  0
                         throw new JspException("Exception while evaluating the binding attribute of Component "
 161  
                                 + component.getId(), e);
 162  0
                     }
 163  
                 }else{
 164  
                     //Type is a EL expression, and there is
 165  
                     //no binding property so we should create
 166  
                     //a new instance
 167  0
                     listener = (_Listener) ClassUtils.newInstance(className);
 168  
                 }
 169  0
                 addListener(holder, listener);
 170  
             }
 171  0
         } catch (ClassCastException e)
 172  
         {
 173  0
             throw new JspException(e);
 174  0
         }        
 175  0
     }
 176  
     
 177  
 }