Coverage Report - javax.faces.webapp.ConverterTag
 
Classes in this File Line Coverage Branch Coverage Complexity
ConverterTag
29%
10/35
8%
1/12
3.6
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package javax.faces.webapp;
 17  
 
 18  
 import javax.faces.component.UIComponent;
 19  
 import javax.faces.component.ValueHolder;
 20  
 import javax.faces.context.FacesContext;
 21  
 import javax.faces.convert.Converter;
 22  
 import javax.faces.convert.ConverterException;
 23  
 import javax.faces.internal.WebAppUtil;
 24  
 import javax.servlet.jsp.JspException;
 25  
 import javax.servlet.jsp.tagext.TagSupport;
 26  
 
 27  
 /**
 28  
  * @author shot
 29  
  */
 30  
 public class ConverterTag extends TagSupport {
 31  
 
 32  
     private static final long serialVersionUID = 1L;
 33  
 
 34  30
     private String converterId = null;
 35  
 
 36  30
     public ConverterTag() {
 37  30
     }
 38  
 
 39  
     public void setConverterId(String converterId) {
 40  4
         this.converterId = converterId;
 41  4
     }
 42  
 
 43  
     public int doStartTag() throws JspException {
 44  0
         UIComponentTag tag = UIComponentTag
 45  
                 .getParentUIComponentTag(pageContext);
 46  0
         if (tag == null) {
 47  0
             throw new JspException("No nested in UIComponentTag");
 48  
         }
 49  
 
 50  0
         if (!tag.getCreated()) {
 51  0
             return (SKIP_BODY);
 52  
         }
 53  
 
 54  0
         Converter converter = createConverter();
 55  
 
 56  0
         UIComponent component = tag.getComponentInstance();
 57  0
         if (component == null || !(component instanceof ValueHolder)) {
 58  0
             throw new JspException("Component is null or not value holder.");
 59  
         }
 60  0
         ValueHolder valueHolder = (ValueHolder) component;
 61  
 
 62  0
         valueHolder.setConverter(converter);
 63  
 
 64  0
         Object localValue = valueHolder.getLocalValue();
 65  0
         if (localValue instanceof String) {
 66  
             try {
 67  0
                 String str = (String) localValue;
 68  0
                 FacesContext context = WebAppUtil.getFacesContext();
 69  0
                 localValue = converter.getAsObject(context, component, str);
 70  0
                 valueHolder.setValue(localValue);
 71  0
             } catch (ConverterException e) {
 72  0
                 JspException ex = new JspException(e);
 73  0
                 throw ex;
 74  0
             }
 75  
         }
 76  0
         return SKIP_BODY;
 77  
     }
 78  
 
 79  
     public void release() {
 80  2
         converterId = null;
 81  2
     }
 82  
 
 83  
     protected Converter createConverter() throws JspException {
 84  
         try {
 85  4
             String id = this.converterId;
 86  4
             if (UIComponentTag.isValueReference(id)) {
 87  0
                 id = (String) WebAppUtil.getValueFromCreatedValueBinding(id);
 88  
             }
 89  4
             return WebAppUtil.createConverter(id);
 90  0
         } catch (Exception e) {
 91  0
             throw new JspException(e);
 92  
         }
 93  
     }
 94  
 }