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