Coverage Report - org.seasar.cubby.tags.InputTag
 
Classes in this File Line Coverage Branch Coverage Complexity
InputTag
89%
48/54
62%
10/16
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 org.seasar.cubby.tags.TagUtils.addClassName;
 19  
 import static org.seasar.cubby.tags.TagUtils.contains;
 20  
 import static org.seasar.cubby.tags.TagUtils.errors;
 21  
 import static org.seasar.cubby.tags.TagUtils.formValue;
 22  
 import static org.seasar.cubby.tags.TagUtils.getOutputValues;
 23  
 import static org.seasar.cubby.tags.TagUtils.multipleFormValues;
 24  
 import static org.seasar.cubby.tags.TagUtils.toAttr;
 25  
 
 26  
 import java.io.IOException;
 27  
 import java.util.Arrays;
 28  
 import java.util.Map;
 29  
 
 30  
 import javax.servlet.jsp.JspContext;
 31  
 import javax.servlet.jsp.JspException;
 32  
 import javax.servlet.jsp.JspTagException;
 33  
 import javax.servlet.jsp.JspWriter;
 34  
 
 35  
 import org.seasar.cubby.action.ActionErrors;
 36  
 import org.seasar.framework.message.MessageFormatter;
 37  
 import org.seasar.framework.util.ArrayUtil;
 38  
 
 39  
 /**
 40  
  * inputを出力するタグ。
 41  
  * <p>
 42  
  * 入力検証にエラーがある場合、class属性に「fieldError」を追加します。 なおこのタグはtype属性により挙動が変わります。
 43  
  * </p>
 44  
  * <ul>
 45  
  * <li>type値がcheckbox/radio -
 46  
  * value値をvalue属性の値として出力します。フォームオブジェクトの値とvalueが一致した場合checked="checked"を出力します。</li>
 47  
  * <li>その他 - value値をvalue属性の値として出力します。</li>
 48  
  * </ul>
 49  
  * 
 50  
  * @author agata
 51  
  * @author baba
 52  
  * @since 1.0.0
 53  
  */
 54  19
 public class InputTag extends DynamicAttributesTagSupport {
 55  
 
 56  1
         private static final String[] MULTIPLE_VALUE_TYPES = new String[] {
 57  
                         "checkbox", "radio" };
 58  
 
 59  
         private String type;
 60  
 
 61  
         private String name;
 62  
 
 63  
         private Object value;
 64  
 
 65  
         private String checkedValue;
 66  
 
 67  
         private Integer index;
 68  
 
 69  
         /**
 70  
          * type属性を設定します。
 71  
          * 
 72  
          * @param type
 73  
          *            type属性
 74  
          */
 75  
         public void setType(final String type) {
 76  18
                 this.type = type;
 77  18
         }
 78  
 
 79  
         /**
 80  
          * name属性を設定します。
 81  
          * 
 82  
          * @param name
 83  
          *            name属性
 84  
          */
 85  
         public void setName(final String name) {
 86  18
                 this.name = name;
 87  18
         }
 88  
 
 89  
         /**
 90  
          * checkedValue属性を設定します。
 91  
          * 
 92  
          * @param checkedValue
 93  
          *            checkedValue属性
 94  
          */
 95  
         public void setCheckedValue(final String checkedValue) {
 96  5
                 this.checkedValue = checkedValue;
 97  5
         }
 98  
 
 99  
         /**
 100  
          * value属性を設定します。
 101  
          * 
 102  
          * @param value
 103  
          *            value属性
 104  
          */
 105  
         public void setValue(final Object value) {
 106  13
                 this.value = value;
 107  13
         }
 108  
 
 109  
         /**
 110  
          * index属性を設定します。
 111  
          * 
 112  
          * @param index
 113  
          *            index属性
 114  
          */
 115  
         public void setIndex(final Integer index) {
 116  0
                 this.index = index;
 117  0
         }
 118  
 
 119  
         /**
 120  
          * {@inheritDoc}
 121  
          */
 122  
         @Override
 123  
         public void doTag() throws JspException, IOException {
 124  18
                 final JspContext context = this.getJspContext();
 125  18
                 final JspWriter out = context.getOut();
 126  18
                 final ActionErrors errors = errors(context);
 127  18
                 final Map<String, Object> dyn = this.getDynamicAttribute();
 128  18
                 final String[] outputValues = getOutputValues(this, this.name);
 129  
 
 130  18
                 if (this.index == null) {
 131  18
                         if (!errors.getFields().get(this.name).isEmpty()) {
 132  0
                                 addClassName(dyn, "fieldError");
 133  
                         }
 134  
                 } else {
 135  0
                         if (!errors.getIndexedFields().get(this.name).get(index).isEmpty()) {
 136  0
                                 addClassName(dyn, "fieldError");
 137  
                         }
 138  
                 }
 139  
 
 140  18
                 if (ArrayUtil.contains(MULTIPLE_VALUE_TYPES, this.type)) {
 141  13
                         if (this.value == null) {
 142  2
                                 throw new JspTagException(MessageFormatter.getMessage(
 143  
                                                 "ECUB1003", new Object[] {
 144  
                                                                 Arrays.deepToString(MULTIPLE_VALUE_TYPES),
 145  
                                                                 "value" }));
 146  
                         }
 147  11
                         final Object[] values = multipleFormValues(context, outputValues,
 148  
                                         this.name, this.checkedValue);
 149  
 
 150  11
                         out.write("<input type=\"");
 151  11
                         out.write(type);
 152  11
                         out.write("\" name=\"");
 153  11
                         out.write(this.name);
 154  11
                         out.write("\" value=\"");
 155  11
                         out.write(CubbyFunctions.out(this.value));
 156  11
                         out.write("\" ");
 157  11
                         out.write(toAttr(dyn));
 158  11
                         out.write(" ");
 159  11
                         out.write(checked(toString(this.value), values));
 160  11
                         out.write("/>");
 161  11
                 } else {
 162  5
                         final Object value = formValue(context, outputValues, this.name,
 163  
                                         this.index, this.value);
 164  
 
 165  5
                         out.write("<input type=\"");
 166  5
                         out.write(type);
 167  5
                         out.write("\" name=\"");
 168  5
                         out.write(this.name);
 169  5
                         out.write("\" value=\"");
 170  5
                         out.write(CubbyFunctions.out(toString(value)));
 171  5
                         out.write("\" ");
 172  5
                         out.write(toAttr(dyn));
 173  5
                         out.write("/>");
 174  
                 }
 175  16
         }
 176  
 
 177  
         private static String checked(final String value, final Object values) {
 178  11
                 if (value == null || values == null) {
 179  0
                         return "";
 180  
                 }
 181  11
                 if (contains(values, value)) {
 182  5
                         return "checked=\"checked\"";
 183  
                 } else {
 184  6
                         return "";
 185  
                 }
 186  
         }
 187  
 
 188  
 }