Coverage Report - javax.faces.component.ComponentUtil_
 
Classes in this File Line Coverage Branch Coverage Complexity
ComponentUtil_
91%
49/54
78%
45/58
2.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.component;
 17  
 
 18  
 import java.util.Iterator;
 19  
 import java.util.Locale;
 20  
 
 21  
 import javax.faces.application.ViewHandler;
 22  
 import javax.faces.context.FacesContext;
 23  
 import javax.faces.convert.Converter;
 24  
 import javax.faces.el.ValueBinding;
 25  
 import javax.faces.event.PhaseId;
 26  
 import javax.faces.model.SelectItem;
 27  
 import javax.faces.model.SelectItemGroup;
 28  
 
 29  
 import org.seasar.framework.util.ArrayIterator;
 30  
 import org.seasar.framework.util.AssertionUtil;
 31  
 
 32  
 /**
 33  
  * @author shot
 34  
  */
 35  
 public class ComponentUtil_ {
 36  
 
 37  
     private static final int LOCALE_LENGTH_SHORT = 2;
 38  
 
 39  
     private static final int LOCALE_LENGTH_LONG = 5;
 40  
 
 41  0
     private ComponentUtil_() {
 42  0
     }
 43  
 
 44  
     public static void processAppropriatePhaseAction(FacesContext context,
 45  
             UIComponent component, PhaseId phase) {
 46  52
         if (phase == PhaseId.APPLY_REQUEST_VALUES) {
 47  17
             component.processDecodes(context);
 48  35
         } else if (phase == PhaseId.PROCESS_VALIDATIONS) {
 49  17
             component.processValidators(context);
 50  18
         } else if (phase == PhaseId.UPDATE_MODEL_VALUES) {
 51  17
             component.processUpdates(context);
 52  
         } else {
 53  1
             throw new IllegalArgumentException();
 54  
         }
 55  51
     }
 56  
 
 57  
     public static String getValueBindingValueAsString(UIComponent component,
 58  
             String bindingName) {
 59  6410
         Object value = getValueBindingValue(component, bindingName);
 60  6410
         return (value != null) ? value.toString() : null;
 61  
     }
 62  
 
 63  
     public static Object getValueBindingValue(UIComponent component,
 64  
             String bindingName) {
 65  7962
         ValueBinding vb = component.getValueBinding(bindingName);
 66  7962
         return (vb != null) ? vb.getValue(component.getFacesContext()) : null;
 67  
     }
 68  
 
 69  
     public static Class getValueBindingType(UIComponent component,
 70  
             String bindingName) {
 71  5
         ValueBinding vb = component.getValueBinding(bindingName);
 72  5
         return (vb != null) ? vb.getType(component.getFacesContext()) : null;
 73  
     }
 74  
 
 75  
     public static Boolean convertToBoolean(boolean value) {
 76  64
         return (value) ? Boolean.TRUE : Boolean.FALSE;
 77  
     }
 78  
 
 79  
     public static Converter createConverter(FacesContext context, Class type) {
 80  2
         return context.getApplication().createConverter(type);
 81  
     }
 82  
 
 83  
     public static boolean isPerformNoConversion(Class type) {
 84  12
         return (type == null || type == String.class || type == Object.class);
 85  
     }
 86  
 
 87  
     public static boolean convertToPrimitiveBoolean(Object obj) {
 88  20
         if (obj instanceof Boolean) {
 89  18
             return (obj != null) ? ((Boolean) obj).booleanValue() : false;
 90  
         } else {
 91  2
             return false;
 92  
         }
 93  
     }
 94  
 
 95  
     public static Locale calculateLocale(FacesContext context) {
 96  3
         ViewHandler viewHandler = context.getApplication().getViewHandler();
 97  3
         return viewHandler.calculateLocale(context);
 98  
     }
 99  
 
 100  
     public static Locale getLocale(FacesContext context) {
 101  102
         AssertionUtil.assertNotNull("context", context);
 102  102
         final UIViewRoot viewRoot = context.getViewRoot();
 103  102
         return (viewRoot != null) ? viewRoot.getLocale() : Locale.getDefault();
 104  
     }
 105  
 
 106  
     public static boolean isLocaleShort(String locale) {
 107  6
         AssertionUtil.assertNotNull("locale", locale);
 108  5
         if (locale.length() == LOCALE_LENGTH_SHORT) {
 109  3
             if (locale.indexOf("-") == -1 || locale.indexOf("_") == -1) {
 110  3
                 return true;
 111  
             }
 112  
         }
 113  2
         return false;
 114  
     }
 115  
 
 116  
     public static boolean isLocaleLong(String locale) {
 117  3
         AssertionUtil.assertNotNull("locale", locale);
 118  2
         if (locale.length() == LOCALE_LENGTH_LONG) {
 119  2
             return true;
 120  
         }
 121  0
         return false;
 122  
     }
 123  
 
 124  
     public static boolean valueMatches(Object value, Iterator selectItems) {
 125  24
         while (selectItems.hasNext()) {
 126  20
             SelectItem item = (SelectItem) selectItems.next();
 127  20
             if (item instanceof SelectItemGroup) {
 128  1
                 SelectItem[] subitems = ((SelectItemGroup) item)
 129  
                         .getSelectItems();
 130  1
                 if ((subitems != null) && (subitems.length > 0)) {
 131  1
                     if (valueMatches(value, new ArrayIterator(subitems))) {
 132  1
                         return true;
 133  
                     }
 134  
                 }
 135  
             } else {
 136  19
                 Object itemValue = item.getValue();
 137  19
                 if (value == null) {
 138  1
                     if (itemValue == null) {
 139  1
                         return true;
 140  
                     }
 141  18
                 } else if (itemValue == null) {
 142  0
                     if (value == null) {
 143  0
                         return true;
 144  
                     }
 145  
                 } else {
 146  18
                     if (value.equals(itemValue)) {
 147  10
                         return true;
 148  
                     }
 149  
                 }
 150  
             }
 151  
         }
 152  4
         return false;
 153  
     }
 154  
 
 155  
     public static boolean isObjectArray(Object obj) {
 156  130
         return (obj instanceof Object[]);
 157  
     }
 158  
 }