Coverage Report - org.seasar.cubby.tags.LinkTag
 
Classes in this File Line Coverage Branch Coverage Complexity
LinkTag
80%
54/67
62%
5/8
1.667
 
 1  
 /*
 2  
  * Copyright 2004-2010 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  
 
 17  
 package org.seasar.cubby.tags;
 18  
 
 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.util.LinkBuilder;
 36  
 
 37  
 /**
 38  
  * 指定されたアクションクラス、アクションメソッドへリンクする URL を特定の属性にもつタグを出力するカスタムタグです。
 39  
  * 
 40  
  * @author baba
 41  
  */
 42  4
 public class LinkTag extends BodyTagSupport implements DynamicAttributes,
 43  
                 ParamParent {
 44  
 
 45  
         /** シリアルバージョン UID */
 46  
         private static final long serialVersionUID = 1L;
 47  
 
 48  
         /** DynamicAttributes */
 49  4
         private final Map<String, Object> dynamicAttributes = new HashMap<String, Object>();
 50  
 
 51  
         /** リンクの補助クラス。 */
 52  4
         private final LinkSupport linkSupport = new LinkSupport();
 53  
 
 54  
         /** リンクビルダ。 */
 55  4
         private final LinkBuilder linkBuilder = new LinkBuilder();
 56  
 
 57  
         /** 出力するタグ。 */
 58  
         private String tag;
 59  
 
 60  
         /** リンクする URL を出力する属性。 */
 61  
         private String attr;
 62  
 
 63  
         /** 出力する URL を {@link HttpServletResponse#encodeURL(String)} でエンコードするか。 */
 64  4
         private boolean encodeURL = true;
 65  
 
 66  
         /**
 67  
          * {@inheritDoc} DynamicAttributeをセットします。
 68  
          */
 69  
         public void setDynamicAttribute(final String uri, final String localName,
 70  
                         final Object value) throws JspException {
 71  0
                 this.dynamicAttributes.put(localName, value);
 72  0
         }
 73  
 
 74  
         /**
 75  
          * 出力するタグを設定します。
 76  
          * 
 77  
          * @param tag
 78  
          *            出力するタグ
 79  
          */
 80  
         public void setTag(final String tag) {
 81  2
                 this.tag = tag;
 82  2
         }
 83  
 
 84  
         /**
 85  
          * リンクする URL を出力する属性を設定します。
 86  
          * 
 87  
          * @param attr
 88  
          *            リンクする URL を出力する属性
 89  
          */
 90  
         public void setAttr(final String attr) {
 91  2
                 this.attr = attr;
 92  2
         }
 93  
 
 94  
         /**
 95  
          * アクションクラスを設定します。
 96  
          * 
 97  
          * @param actionClass
 98  
          *            アクションクラス
 99  
          */
 100  
         public void setActionClass(final String actionClass) {
 101  4
                 linkSupport.setActionClassName(actionClass);
 102  4
         }
 103  
 
 104  
         /**
 105  
          * アクションメソッドを設定します。
 106  
          * 
 107  
          * @param actionMethod
 108  
          *            アクションメソッド
 109  
          */
 110  
         public void setActionMethod(final String actionMethod) {
 111  4
                 linkSupport.setActionMethodName(actionMethod);
 112  4
         }
 113  
 
 114  
         /**
 115  
          * 出力する URL を {@link HttpServletResponse#encodeURL(String)} でエンコードするかを設定します。
 116  
          * 
 117  
          * @param encodeURL
 118  
          *            出力する URL を {@link HttpServletResponse#encodeURL(String)}
 119  
          *            でエンコードする場合は <code>true</code>、そうでない場合は <code>false</code>
 120  
          */
 121  
         public void setEncodeURL(final boolean encodeURL) {
 122  0
                 this.encodeURL = encodeURL;
 123  0
         }
 124  
 
 125  
         /**
 126  
          * 出力する URL のプロトコルを設定します。
 127  
          * 
 128  
          * @param protocol
 129  
          *            出力する URL のプロトコル
 130  
          */
 131  
         public void setProtocol(final String protocol) {
 132  0
                 linkBuilder.setProtocol(protocol);
 133  0
         }
 134  
 
 135  
         /**
 136  
          * 出力する URL のポートを設定します。
 137  
          * 
 138  
          * @param port
 139  
          *            出力する URL のポート
 140  
          */
 141  
         public void setPort(final int port) {
 142  0
                 linkBuilder.setPort(port);
 143  0
         }
 144  
 
 145  
         /**
 146  
          * 要求パラメータを追加します。
 147  
          * 
 148  
          * @param name
 149  
          *            パラメータ名
 150  
          * @param value
 151  
          *            値
 152  
          */
 153  
         public void addParameter(final String name, final String value) {
 154  4
                 linkSupport.addParameter(name, value);
 155  4
         }
 156  
 
 157  
         /**
 158  
          * {@inheritDoc}
 159  
          */
 160  
         @Override
 161  
         public int doStartTag() throws JspException {
 162  4
                 return EVAL_BODY_BUFFERED;
 163  
         }
 164  
 
 165  
         /**
 166  
          * {@inheritDoc}
 167  
          */
 168  
         @Override
 169  
         public int doEndTag() throws JspException {
 170  4
                 final String contextPath = getContextPath(pageContext);
 171  
                 final String actionPath;
 172  4
                 final HttpServletRequest request = (HttpServletRequest) pageContext
 173  
                                 .getRequest();
 174  4
                 final String characterEncoding = request.getCharacterEncoding();
 175  4
                 if (encodeURL) {
 176  4
                         final HttpServletResponse response = (HttpServletResponse) pageContext
 177  
                                         .getResponse();
 178  4
                         actionPath = response.encodeURL(contextPath
 179  
                                         + linkSupport.getPath(characterEncoding));
 180  4
                 } else {
 181  0
                         actionPath = contextPath + linkSupport.getPath(characterEncoding);
 182  
                 }
 183  
 
 184  
                 final String url;
 185  
                 try {
 186  4
                         url = linkBuilder.file(actionPath).toLink(request);
 187  0
                 } catch (final MalformedURLException e) {
 188  0
                         throw new JspException(e);
 189  4
                 }
 190  
 
 191  
                 try {
 192  4
                         final JspWriter out = pageContext.getOut();
 193  4
                         if (tag == null) {
 194  2
                                 out.write(url);
 195  2
                                 final BodyContent bodyContent = getBodyContent();
 196  2
                                 if (bodyContent != null) {
 197  2
                                         bodyContent.writeOut(out);
 198  
                                 }
 199  2
                         } else {
 200  2
                                 dynamicAttributes.put(attr, url);
 201  2
                                 out.write("<");
 202  2
                                 out.write(tag);
 203  2
                                 out.write(" ");
 204  2
                                 out.write(toAttr(dynamicAttributes));
 205  2
                                 out.write(">");
 206  2
                                 final BodyContent bodyContent = getBodyContent();
 207  2
                                 if (bodyContent != null) {
 208  2
                                         bodyContent.writeOut(out);
 209  
                                 }
 210  2
                                 out.write("</");
 211  2
                                 out.write(tag);
 212  2
                                 out.write(">");
 213  
                         }
 214  0
                 } catch (final IOException e) {
 215  0
                         throw new JspException(e);
 216  4
                 }
 217  4
                 reset();
 218  4
                 return EVAL_PAGE;
 219  
         }
 220  
 
 221  
         /**
 222  
          * このタグをリセットします。
 223  
          */
 224  
         private void reset() {
 225  4
                 linkSupport.clear();
 226  4
                 linkBuilder.clear();
 227  4
                 dynamicAttributes.clear();
 228  4
                 tag = null;
 229  4
                 attr = null;
 230  4
                 encodeURL = true;
 231  4
         }
 232  
 
 233  
 }