Coverage Report - org.seasar.cubby.tags.InputTag
 
Classes in this File Line Coverage Branch Coverage Complexity
InputTag
90%
52/58
62%
10/16
0
 
 1  
 /*
 2  
  * Copyright 2004-2009 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.internal.util.LogMessages.format;
 19  
 import static org.seasar.cubby.tags.TagUtils.addCSSClassName;
 20  
 import static org.seasar.cubby.tags.TagUtils.contains;
 21  
 import static org.seasar.cubby.tags.TagUtils.errors;
 22  
 import static org.seasar.cubby.tags.TagUtils.formValue;
 23  
 import static org.seasar.cubby.tags.TagUtils.getFormWrapper;
 24  
 import static org.seasar.cubby.tags.TagUtils.multipleFormValues;
 25  
 import static org.seasar.cubby.tags.TagUtils.toAttr;
 26  
 
 27  
 import java.io.IOException;
 28  
 import java.util.Collections;
 29  
 import java.util.HashSet;
 30  
 import java.util.Map;
 31  
 import java.util.Set;
 32  
 
 33  
 import javax.servlet.jsp.JspContext;
 34  
 import javax.servlet.jsp.JspException;
 35  
 import javax.servlet.jsp.JspTagException;
 36  
 import javax.servlet.jsp.JspWriter;
 37  
 
 38  
 import org.seasar.cubby.action.ActionErrors;
 39  
 import org.seasar.cubby.internal.controller.FormWrapper;
 40  
 
 41  
 /**
 42  
  * <code>&lt;input&gt;</code> タグを出力します。
 43  
  * <p>
 44  
  * このタグは type 属性により挙動が変わります。
 45  
  * <table>
 46  
  * <tbody>
 47  
  * <tr>
 48  
  * <td><code>type</code> 属性が <code>checkbox</code> または </code> radio の場合</td>
 49  
  * <td>
 50  
  * <code>value</code> 属性をそのまま <code>value</code> 属性として出力します。 フォームオブジェクトのプロパティ値と
 51  
  * <code>value</code> 属性が一致した場合 <code>checked=&quot;checked&quot;</code> を出力します。
 52  
  * </td>
 53  
  * </tr>
 54  
  * <tr>
 55  
  * <td>上記以外の場合</td>
 56  
  * <td>
 57  
  * <code>value</code> 属性が指定されている場合はそのまま <code>value</code> 属性として出力します。
 58  
  * <code>value</code> 属性が指定されていない場合はフォームオブジェクトのプロパティを <code>value</code>
 59  
  * 属性として出力します。</td>
 60  
  * </tr>
 61  
  * </tbody>
 62  
  * </table>
 63  
  * バリデーションエラーが発生している場合は、入力された値を復元します。
 64  
  * </p>
 65  
  * 
 66  
  * @author agata
 67  
  * @author baba
 68  
  * @since 1.0.0
 69  
  */
 70  19
 public class InputTag extends DynamicAttributesSimpleTagSupport {
 71  
 
 72  
         private static final Set<String> MULTIPLE_VALUE_TYPES;
 73  
         static {
 74  1
                 final Set<String> set = new HashSet<String>();
 75  1
                 set.add("checkbox");
 76  1
                 set.add("radio");
 77  1
                 MULTIPLE_VALUE_TYPES = Collections.unmodifiableSet(set);
 78  1
         }
 79  
 
 80  
         /** <code>type</code> 属性。 */
 81  
         private String type;
 82  
 
 83  
         /** <code>name</code> 属性。 */
 84  
         private String name;
 85  
 
 86  
         /** <code>value</code> 属性。 */
 87  
         private Object value;
 88  
 
 89  
         /** <code>checkedValue</code> 属性。 */
 90  
         private String checkedValue;
 91  
 
 92  
         /** <code>index</code> 属性。 */
 93  
         private Integer index;
 94  
 
 95  
         /**
 96  
          * <code>type</code> 属性を設定します。
 97  
          * 
 98  
          * @param type
 99  
          *            <code>type</code> 属性
 100  
          */
 101  
         public void setType(final String type) {
 102  18
                 this.type = type;
 103  18
         }
 104  
 
 105  
         /**
 106  
          * <code>name</code> 属性を設定します。
 107  
          * 
 108  
          * @param name
 109  
          *            <code>name</code> 属性
 110  
          */
 111  
         public void setName(final String name) {
 112  18
                 this.name = name;
 113  18
         }
 114  
 
 115  
         /**
 116  
          * <code>checkedValue</code> 属性を設定します。
 117  
          * 
 118  
          * @param checkedValue
 119  
          *            <code>checkedValue</code> 属性
 120  
          */
 121  
         public void setCheckedValue(final String checkedValue) {
 122  5
                 this.checkedValue = checkedValue;
 123  5
         }
 124  
 
 125  
         /**
 126  
          * <code>value</code> 属性を設定します。
 127  
          * 
 128  
          * @param value
 129  
          *            <code>value</code> 属性
 130  
          */
 131  
         public void setValue(final Object value) {
 132  13
                 this.value = value;
 133  13
         }
 134  
 
 135  
         /**
 136  
          * <code>index</code> 属性を設定します。
 137  
          * 
 138  
          * @param index
 139  
          *            <code>index</code> 属性
 140  
          */
 141  
         public void setIndex(final Integer index) {
 142  0
                 this.index = index;
 143  0
         }
 144  
 
 145  
         /**
 146  
          * {@inheritDoc}
 147  
          */
 148  
         @Override
 149  
         public void doTag() throws JspException, IOException {
 150  18
                 final JspContext context = this.getJspContext();
 151  18
                 final JspWriter out = context.getOut();
 152  18
                 final ActionErrors errors = errors(context);
 153  18
                 final Map<String, Object> dyn = this.getDynamicAttributes();
 154  18
                 final FormWrapper formWrapper = getFormWrapper(this);
 155  
                 // final String[] outputValues = getOutputValues(this, this.name);
 156  
 
 157  18
                 if (this.index == null) {
 158  18
                         if (!errors.getFields().get(this.name).isEmpty()) {
 159  0
                                 addCSSClassName(dyn, "fieldError");
 160  
                         }
 161  
                 } else {
 162  0
                         if (!errors.getIndexedFields().get(this.name).get(index).isEmpty()) {
 163  0
                                 addCSSClassName(dyn, "fieldError");
 164  
                         }
 165  
                 }
 166  
 
 167  18
                 if (MULTIPLE_VALUE_TYPES.contains(this.type)) {
 168  13
                         if (this.value == null) {
 169  2
                                 throw new JspTagException(format("ECUB1003",
 170  
                                                 MULTIPLE_VALUE_TYPES, "value"));
 171  
                         }
 172  11
                         final Object[] values = multipleFormValues(context, formWrapper,
 173  
                                         this.name, this.checkedValue);
 174  
 
 175  11
                         out.write("<input type=\"");
 176  11
                         out.write(type);
 177  11
                         out.write("\" name=\"");
 178  11
                         out.write(this.name);
 179  11
                         out.write("\" value=\"");
 180  11
                         out.write(CubbyFunctions.out(this.value));
 181  11
                         out.write("\" ");
 182  11
                         out.write(toAttr(dyn));
 183  11
                         out.write(" ");
 184  11
                         out.write(checked(TagUtils.toString(this.value), values));
 185  11
                         out.write("/>");
 186  11
                 } else {
 187  5
                         final Object value = formValue(context, formWrapper, this.name,
 188  
                                         this.index, this.value);
 189  
 
 190  5
                         out.write("<input type=\"");
 191  5
                         out.write(type);
 192  5
                         out.write("\" name=\"");
 193  5
                         out.write(this.name);
 194  5
                         out.write("\" value=\"");
 195  5
                         out.write(CubbyFunctions.out(TagUtils.toString(value)));
 196  5
                         out.write("\" ");
 197  5
                         out.write(toAttr(dyn));
 198  5
                         out.write("/>");
 199  
                 }
 200  16
         }
 201  
 
 202  
         private static String checked(final String value, final Object values) {
 203  11
                 if (value == null || values == null) {
 204  0
                         return "";
 205  
                 }
 206  11
                 if (contains(values, value)) {
 207  5
                         return "checked=\"checked\"";
 208  
                 } else {
 209  6
                         return "";
 210  
                 }
 211  
         }
 212  
 
 213  
 }