Coverage Report - org.seasar.cubby.tags.TagUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
TagUtils
96%
68/71
88%
37/42
0
 
 1  
 /*
 2  
  * Copyright 2004-2008 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.cubby.tags;
 17  
 
 18  
 import static java.lang.Boolean.TRUE;
 19  
 import static javax.servlet.jsp.PageContext.REQUEST_SCOPE;
 20  
 import static org.seasar.cubby.CubbyConstants.ATTR_OUTPUT_VALUES;
 21  
 import static org.seasar.cubby.CubbyConstants.ATTR_PARAMS;
 22  
 import static org.seasar.cubby.CubbyConstants.ATTR_VALIDATION_FAIL;
 23  
 
 24  
 import java.util.Collection;
 25  
 import java.util.Map;
 26  
 import java.util.Map.Entry;
 27  
 
 28  
 import javax.servlet.jsp.JspContext;
 29  
 import javax.servlet.jsp.PageContext;
 30  
 
 31  
 import org.seasar.cubby.action.ActionErrors;
 32  
 import org.seasar.cubby.util.CubbyFunctions;
 33  
 import org.seasar.framework.util.StringUtil;
 34  
 
 35  
 /**
 36  
  * 
 37  
  * @author baba
 38  
  * 
 39  
  */
 40  1
 class TagUtils {
 41  
 
 42  
         public static ActionErrors errors(final JspContext context) {
 43  36
                 return (ActionErrors) context.getAttribute("errors", REQUEST_SCOPE);
 44  
         }
 45  
 
 46  
         @SuppressWarnings("unchecked")
 47  
         private static Object[] paramValues(final JspContext context,
 48  
                         final String name) {
 49  7
                 final Map<String, Object[]> valuesMap = (Map<String, Object[]>) context
 50  
                                 .getAttribute(ATTR_PARAMS, REQUEST_SCOPE);
 51  
                 final Object[] values;
 52  7
                 if (valuesMap == null || !valuesMap.containsKey(name)) {
 53  0
                         values = new Object[0];
 54  
                 } else {
 55  7
                         values = valuesMap.get(name);
 56  
                 }
 57  7
                 return values;
 58  
         }
 59  
 
 60  
         private static Object[] formValues(final Map<String, String[]> valuesMap,
 61  
                         final String name) {
 62  
                 final Object[] values;
 63  32
                 if (valuesMap == null || !valuesMap.containsKey(name)) {
 64  7
                         values = new Object[0];
 65  
                 } else {
 66  25
                         values = valuesMap.get(name);
 67  
                 }
 68  32
                 return values;
 69  
         }
 70  
 
 71  
         public static Object[] multipleFormValues(final JspContext context,
 72  
                         final Map<String, String[]> outputValuesMap, final String name) {
 73  17
                 return multipleFormValues(context, outputValuesMap, name, null);
 74  
         }
 75  
 
 76  
         public static Object[] multipleFormValues(final JspContext context,
 77  
                         final Map<String, String[]> outputValuesMap, final String name,
 78  
                         final String checkedValue) {
 79  
                 final Object[] values;
 80  32
                 if (isValidationFail(context)) {
 81  5
                         values = paramValues(context, name);
 82  
                 } else {
 83  27
                         if (checkedValue != null) {
 84  3
                                 values = new Object[] { checkedValue };
 85  
                         } else {
 86  24
                                 values = formValues(outputValuesMap, name);
 87  
                         }
 88  
                 }
 89  32
                 return values;
 90  
         }
 91  
 
 92  
         public static Object formValue(final JspContext context,
 93  
                         final Map<String, String[]> outputValuesMap, final String name,
 94  
                         final Integer index, final Object specifiedValue) {
 95  
                 final Object value;
 96  
 
 97  15
                 if (isValidationFail(context)) {
 98  2
                         final Object[] values = paramValues(context, name);
 99  2
                         value = value(values, index);
 100  2
                 } else {
 101  13
                         if (specifiedValue == null) {
 102  8
                                 final Object[] values = formValues(outputValuesMap, name);
 103  8
                                 value = value(values, index);
 104  8
                         } else {
 105  5
                                 value = specifiedValue;
 106  
                         }
 107  
                 }
 108  
 
 109  15
                 return value;
 110  
         }
 111  
 
 112  
         private static Object value(final Object[] values, final Integer index) {
 113  
                 final Object value;
 114  10
                 if (values == null) {
 115  0
                         value = "";
 116  
                 } else {
 117  10
                         if (index == null) {
 118  7
                                 value = getElement(values, 0);
 119  
                         } else {
 120  3
                                 value = getElement(values, index);
 121  
                         }
 122  
                 }
 123  10
                 return value;
 124  
         }
 125  
 
 126  
         private static Object getElement(final Object[] values, final Integer index) {
 127  
                 final Object value;
 128  10
                 if (values.length <= index) {
 129  3
                         value = "";
 130  
                 } else {
 131  7
                         value = values[index];
 132  
                 }
 133  10
                 return value;
 134  
         }
 135  
 
 136  
         private static boolean isValidationFail(final JspContext context) {
 137  47
                 return TRUE.equals(context.getAttribute(ATTR_VALIDATION_FAIL,
 138  
                                 REQUEST_SCOPE));
 139  
         }
 140  
 
 141  
         @SuppressWarnings("unchecked")
 142  
         public static Map<String, String[]> outputValues(final JspContext context) {
 143  36
                 final Map<String, String[]> outputValues = (Map<String, String[]>) context
 144  
                                 .getAttribute(ATTR_OUTPUT_VALUES, PageContext.PAGE_SCOPE);
 145  35
                 return outputValues;
 146  
         }
 147  
 
 148  
         public static String toAttr(final Map<String, Object> map) {
 149  41
                 final StringBuilder sb = new StringBuilder();
 150  41
                 for (final Entry<String, Object> entry : map.entrySet()) {
 151  14
                         final String key = entry.getKey();
 152  14
                         if ("value".equals(key) || "checkedValue".equals(key)) {
 153  0
                                 continue;
 154  
                         }
 155  14
                         sb.append(key);
 156  14
                         sb.append("=\"");
 157  14
                         sb.append(CubbyFunctions.escapeHtml(entry.getValue()));
 158  14
                         sb.append("\" ");
 159  14
                 }
 160  40
                 return sb.toString();
 161  
         }
 162  
 
 163  
         public static boolean isChecked(final String value, final Object values) {
 164  60
                 if (values instanceof Collection) {
 165  1
                         return ((Collection<?>) values).contains(value);
 166  59
                 } else if (values.getClass().isArray()) {
 167  95
                         for (final Object v : (Object[]) values) {
 168  62
                                 if (equalsValue(v, value)) {
 169  23
                                         return true;
 170  
                                 }
 171  
                         }
 172  32
                         return false;
 173  
                 } else {
 174  2
                         return equalsValue(values, value);
 175  
                 }
 176  
         }
 177  
 
 178  
         private static boolean equalsValue(final Object values, final Object value) {
 179  64
                 if (values == value) {
 180  10
                         return true;
 181  54
                 } else if (values == null) {
 182  4
                         return false;
 183  
                 } else {
 184  50
                         return values.toString().equals(value.toString());
 185  
                 }
 186  
         }
 187  
 
 188  
         public static void addClassName(final Map<String, Object> dyn, final String className) {
 189  3
                 String classValue = (String) dyn.get("class");
 190  2
                 if (StringUtil.isEmpty(classValue)) {
 191  1
                         classValue = className;
 192  
                 } else {
 193  1
                         classValue = classValue + " " + className;
 194  
                 }
 195  2
                 dyn.put("class", classValue);
 196  2
         }
 197  
 
 198  
 }