Coverage Report - org.seasar.teeda.core.render.AbstractDecoder
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractDecoder
100%
26/26
100%
6/6
1.667
AbstractDecoder$ValueHolderWrapper
60%
6/10
25%
1/4
1.667
 
 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 org.seasar.teeda.core.render;
 17  
 
 18  
 import java.util.Map;
 19  
 
 20  
 import javax.faces.component.EditableValueHolder;
 21  
 import javax.faces.component.UIComponent;
 22  
 import javax.faces.component.ValueHolder;
 23  
 import javax.faces.context.FacesContext;
 24  
 
 25  
 import org.seasar.framework.util.AssertionUtil;
 26  
 
 27  
 /**
 28  
  * @author shot
 29  
  */
 30  412
 public abstract class AbstractDecoder implements Decoder {
 31  
 
 32  
     public void decode(FacesContext context, UIComponent component) {
 33  20
         AssertionUtil.assertNotNull("context is null.", context);
 34  19
         AssertionUtil.assertNotNull("component is null.", component);
 35  18
         ValueHolderWrapper wrapper = createValueHolderWrapper(component);
 36  17
         Map paramMap = getRequestParameterMap(context);
 37  17
         String clientId = getClientId(component, context);
 38  17
         if (paramMap.containsKey(clientId)) {
 39  9
             Object value = paramMap.get(clientId);
 40  9
             wrapper.setValue(value);
 41  
         }
 42  17
     }
 43  
 
 44  
     public void decodeMany(FacesContext context, UIComponent component) {
 45  9
         AssertionUtil.assertNotNull("context is null.", context);
 46  8
         AssertionUtil.assertNotNull("component is null.", component);
 47  7
         ValueHolderWrapper wrapper = createValueHolderWrapper(component);
 48  6
         Map paramValuesMap = getRequestParameterValuesMap(context);
 49  6
         String clientId = getClientId(component, context);
 50  6
         String[] value = null;
 51  6
         if (paramValuesMap.containsKey(clientId)) {
 52  3
             value = (String[]) paramValuesMap.get(clientId);
 53  
         }
 54  6
         if (value != null) {
 55  3
             wrapper.setValue(value);
 56  
         } else {
 57  3
             wrapper.setValue(new String[0]);
 58  
         }
 59  6
     }
 60  
 
 61  
     protected abstract ValueHolderWrapper createValueHolderWrapper(
 62  
             UIComponent component);
 63  
 
 64  
     protected Map getRequestParameterMap(FacesContext context) {
 65  17
         return context.getExternalContext().getRequestParameterMap();
 66  
     }
 67  
 
 68  
     protected Map getRequestParameterValuesMap(FacesContext context) {
 69  6
         return context.getExternalContext().getRequestParameterValuesMap();
 70  
     }
 71  
 
 72  
     protected String getClientId(UIComponent component, FacesContext context) {
 73  23
         return component.getClientId(context);
 74  
     }
 75  
 
 76  412
     protected static class ValueHolderWrapper {
 77  
 
 78  
         private ValueHolder holder;
 79  
 
 80  23
         public ValueHolderWrapper(ValueHolder holder) {
 81  23
             this.holder = holder;
 82  23
         }
 83  
 
 84  
         public void setValue(Object value) {
 85  15
             if (holder instanceof EditableValueHolder) {
 86  15
                 ((EditableValueHolder) holder).setSubmittedValue(value);
 87  
             } else {
 88  0
                 holder.setValue(value);
 89  
             }
 90  15
         }
 91  
 
 92  
         public Object getValue() {
 93  0
             if (holder instanceof EditableValueHolder) {
 94  0
                 return ((EditableValueHolder) holder).getSubmittedValue();
 95  
             } else {
 96  0
                 return holder.getValue();
 97  
             }
 98  
         }
 99  
     }
 100  
 
 101  
 }