Coverage Report - org.seasar.teeda.core.util.ValueHolderUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
ValueHolderUtil
83%
29/35
80%
16/20
6.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.util;
 17  
 
 18  
 import java.lang.reflect.Array;
 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  
 import javax.faces.convert.Converter;
 25  
 
 26  
 import org.seasar.teeda.core.exception.NoValueHolderRuntimeException;
 27  
 
 28  
 /**
 29  
  * @author manhole
 30  
  */
 31  0
 public class ValueHolderUtil {
 32  
 
 33  
     public static Object getValue(final UIComponent component) {
 34  0
         if (!(component instanceof ValueHolder)) {
 35  0
             throw new NoValueHolderRuntimeException(component.getClass());
 36  
         }
 37  0
         final ValueHolder vh = (ValueHolder) component;
 38  0
         return vh.getValue();
 39  
     }
 40  
 
 41  
     public static String getValueForRender(final FacesContext context,
 42  
             final UIComponent component) {
 43  268
         if (!(component instanceof ValueHolder)) {
 44  1
             throw new IllegalArgumentException("component must be ValueHolder");
 45  
         }
 46  267
         if (component instanceof EditableValueHolder) {
 47  85
             final EditableValueHolder evh = (EditableValueHolder) component;
 48  85
             final Object submittedValue = evh.getSubmittedValue();
 49  85
             if (submittedValue != null) {
 50  4
                 if (submittedValue instanceof String) {
 51  4
                     return (String) submittedValue;
 52  
                 }
 53  0
                 return submittedValue.toString();
 54  
             }
 55  
         }
 56  263
         final ValueHolder vh = (ValueHolder) component;
 57  263
         final Object value = vh.getValue();
 58  263
         final Converter converter = ConverterUtil.getConverter(component);
 59  263
         return UIValueUtil.getValueAsString(context, component, value,
 60  
                 converter);
 61  
     }
 62  
 
 63  
     public static String[] getValuesForRender(final FacesContext context,
 64  
             final UIComponent component) {
 65  49
         if (!(component instanceof ValueHolder)) {
 66  1
             throw new IllegalArgumentException("component must be ValueHolder");
 67  
         }
 68  48
         if (component instanceof EditableValueHolder) {
 69  48
             final EditableValueHolder evh = (EditableValueHolder) component;
 70  48
             final Object submittedValue = evh.getSubmittedValue();
 71  48
             if (submittedValue instanceof String[]) {
 72  3
                 return (String[]) submittedValue;
 73  
             }
 74  
         }
 75  45
         final ValueHolder vh = (ValueHolder) component;
 76  45
         final Object value = vh.getValue();
 77  45
         if (value == null) {
 78  36
             return new String[0];
 79  
         }
 80  9
         final Converter converter = vh.getConverter();
 81  9
         final int length = Array.getLength(value);
 82  9
         final String[] values = new String[length];
 83  19
         for (int i = 0; i < length; ++i) {
 84  10
             values[i] = UIValueUtil.getValueAsString(context, component, Array
 85  
                     .get(value, i), converter);
 86  
         }
 87  9
         return values;
 88  
     }
 89  
 
 90  
 }