Coverage Report - org.seasar.cubby.tags.FormTag
 
Classes in this File Line Coverage Branch Coverage Complexity
FormTag
83%
50/60
62%
5/8
1.615
 
 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.CubbyConstants.ATTR_FORM_WRAPPER_FACTORY;
 19  
 import static org.seasar.cubby.tags.TagUtils.getContextPath;
 20  
 import static org.seasar.cubby.tags.TagUtils.toAttr;
 21  
 
 22  
 import java.io.IOException;
 23  
 import java.net.MalformedURLException;
 24  
 import java.util.HashMap;
 25  
 import java.util.Map;
 26  
 
 27  
 import javax.servlet.http.HttpServletRequest;
 28  
 import javax.servlet.http.HttpServletResponse;
 29  
 import javax.servlet.jsp.JspException;
 30  
 import javax.servlet.jsp.JspWriter;
 31  
 import javax.servlet.jsp.tagext.BodyContent;
 32  
 import javax.servlet.jsp.tagext.BodyTagSupport;
 33  
 import javax.servlet.jsp.tagext.DynamicAttributes;
 34  
 
 35  
 import org.seasar.cubby.controller.FormWrapper;
 36  
 import org.seasar.cubby.controller.FormWrapperFactory;
 37  
 import org.seasar.cubby.util.LinkBuilder;
 38  
 
 39  
 /**
 40  
  * フォームを出力するタグライブラリ。
 41  
  * <p>
 42  
  * {@link InputTag}, {@link SelectTag}, {@link TextareaTag}を保持することができます。
 43  
  * </p>
 44  
  * 
 45  
  * @author agata
 46  
  * @author baba
 47  
  */
 48  39
 public class FormTag extends BodyTagSupport implements DynamicAttributes,
 49  
                 ParamParent {
 50  
 
 51  
         /** シリアルバージョン UID */
 52  
         private static final long serialVersionUID = 1L;
 53  
 
 54  
         /** DynamicAttributes */
 55  39
         private final Map<String, Object> dynamicAttributes = new HashMap<String, Object>();
 56  
 
 57  
         /** フォームのバインディング対象のBean。 */
 58  
         private Object value;
 59  
 
 60  
         /** 出力する URL を {@link HttpServletResponse#encodeURL(String)} でエンコードするか。 */
 61  39
         private boolean encodeURL = true;
 62  
 
 63  
         /** リンク用の補助クラス。 */
 64  39
         private final LinkSupport linkSupport = new LinkSupport();
 65  
 
 66  
         /** リンクビルダ。 */
 67  39
         private final LinkBuilder linkBuilder = new LinkBuilder();
 68  
 
 69  
         /** フォームオブジェクトのラッパー。 */
 70  
         private transient FormWrapper formWrapper;
 71  
 
 72  
         /**
 73  
          * {@inheritDoc} DynamicAttributeをセットします。
 74  
          */
 75  
         public void setDynamicAttribute(final String uri, final String localName,
 76  
                         final Object value) throws JspException {
 77  3
                 this.dynamicAttributes.put(localName, value);
 78  3
         }
 79  
 
 80  
         /**
 81  
          * DynamicAttributeを取得します。
 82  
          * 
 83  
          * @return DynamicAttribute
 84  
          */
 85  
         protected Map<String, Object> getDynamicAttribute() {
 86  6
                 return this.dynamicAttributes;
 87  
         }
 88  
 
 89  
         /**
 90  
          * フォームのバインディング対象のBeanをセットします。
 91  
          * 
 92  
          * @param value
 93  
          *            フォームのバインディング対象のBean
 94  
          */
 95  
         public void setValue(final Object value) {
 96  6
                 this.value = value;
 97  6
         }
 98  
 
 99  
         /**
 100  
          * アクションクラスを設定します。
 101  
          * 
 102  
          * @param actionClass
 103  
          *            アクションクラス
 104  
          */
 105  
         public void setActionClass(final String actionClass) {
 106  3
                 linkSupport.setActionClassName(actionClass);
 107  3
         }
 108  
 
 109  
         /**
 110  
          * アクションメソッドを設定します。
 111  
          * 
 112  
          * @param actionMethod
 113  
          *            アクションメソッド
 114  
          */
 115  
         public void setActionMethod(final String actionMethod) {
 116  3
                 linkSupport.setActionMethodName(actionMethod);
 117  3
         }
 118  
 
 119  
         /**
 120  
          * 出力する URL を {@link HttpServletResponse#encodeURL(String)} でエンコードするかを設定します。
 121  
          * 
 122  
          * @param encodeURL
 123  
          *            出力する URL を {@link HttpServletResponse#encodeURL(String)}
 124  
          *            でエンコードする場合は <code>true</code>、そうでない場合は <code>false</code>
 125  
          */
 126  
         public void setEncodeURL(final boolean encodeURL) {
 127  0
                 this.encodeURL = encodeURL;
 128  0
         }
 129  
 
 130  
         /**
 131  
          * 出力する URL のプロトコルを設定します。
 132  
          * 
 133  
          * @param protocol
 134  
          *            出力する URL のプロトコル
 135  
          */
 136  
         public void setProtocol(final String protocol) {
 137  0
                 linkBuilder.setProtocol(protocol);
 138  0
         }
 139  
 
 140  
         /**
 141  
          * 出力する URL のポートを設定します。
 142  
          * 
 143  
          * @param port
 144  
          *            出力する URL のポート
 145  
          */
 146  
         public void setPort(final int port) {
 147  0
                 linkBuilder.setPort(port);
 148  0
         }
 149  
 
 150  
         /**
 151  
          * 要求パラメータを追加します。
 152  
          * 
 153  
          * @param name
 154  
          *            パラメータ名
 155  
          * @param value
 156  
          *            値
 157  
          */
 158  
         public void addParameter(final String name, final String value) {
 159  4
                 linkSupport.addParameter(name, value);
 160  4
         }
 161  
 
 162  
         /**
 163  
          * {@inheritDoc}
 164  
          */
 165  
         @Override
 166  
         public int doStartTag() throws JspException {
 167  6
                 final FormWrapperFactory formWrapperFactory = (FormWrapperFactory) pageContext
 168  
                                 .findAttribute(ATTR_FORM_WRAPPER_FACTORY);
 169  6
                 this.formWrapper = formWrapperFactory.create(this.value);
 170  6
                 return EVAL_BODY_BUFFERED;
 171  
         }
 172  
 
 173  
         /**
 174  
          * {@inheritDoc}
 175  
          */
 176  
         @Override
 177  
         public int doEndTag() throws JspException {
 178  6
                 final String contextPath = getContextPath(pageContext);
 179  6
                 if (linkSupport.isLinkable()) {
 180  3
                         final String characterEncoding = pageContext.getRequest()
 181  
                                         .getCharacterEncoding();
 182  3
                         final String url = contextPath
 183  
                                         + linkSupport.getPath(characterEncoding);
 184  3
                         dynamicAttributes.put("action", url);
 185  
                 }
 186  
 
 187  6
                 if (encodeURL && dynamicAttributes.containsKey("action")) {
 188  6
                         final HttpServletRequest request = (HttpServletRequest) pageContext
 189  
                                         .getRequest();
 190  6
                         final HttpServletResponse response = (HttpServletResponse) pageContext
 191  
                                         .getResponse();
 192  6
                         final String actionPath = (String) dynamicAttributes.get("action");
 193  
                         final String url;
 194  
                         try {
 195  6
                                 url = linkBuilder.file(actionPath).toLink(request);
 196  0
                         } catch (final MalformedURLException e) {
 197  0
                                 throw new JspException(e);
 198  6
                         }
 199  6
                         final String encodedUrl = response.encodeURL(url);
 200  6
                         dynamicAttributes.put("action", encodedUrl);
 201  
                 }
 202  
 
 203  6
                 final JspWriter out = pageContext.getOut();
 204  
                 try {
 205  6
                         out.write("<form ");
 206  6
                         out.write(toAttr(getDynamicAttribute()));
 207  6
                         out.write(">");
 208  6
                         final BodyContent bodyContent = getBodyContent();
 209  6
                         if (bodyContent != null) {
 210  6
                                 bodyContent.writeOut(out);
 211  
                         }
 212  6
                         out.write("</form>");
 213  0
                 } catch (final IOException e) {
 214  0
                         throw new JspException(e);
 215  6
                 }
 216  6
                 reset();
 217  6
                 return EVAL_PAGE;
 218  
         }
 219  
 
 220  
         /**
 221  
          * このタグをリセットします。
 222  
          */
 223  
         private void reset() {
 224  6
                 linkSupport.clear();
 225  6
                 linkBuilder.clear();
 226  6
                 dynamicAttributes.clear();
 227  6
                 value = null;
 228  6
                 formWrapper = null;
 229  6
         }
 230  
 
 231  
         /**
 232  
          * フォームオブジェクトのラッパーを取得します。
 233  
          * 
 234  
          * @return フォームオブジェクトのラッパー
 235  
          */
 236  
         public FormWrapper getFormWrapper() {
 237  2
                 return formWrapper;
 238  
         }
 239  
 
 240  
 }