Coverage Report - org.seasar.cubby.tags.LinkSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
LinkSupport
93%
27/29
75%
6/8
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 java.util.ArrayList;
 19  
 import java.util.HashMap;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 import java.util.Map.Entry;
 23  
 
 24  
 import javax.servlet.jsp.JspTagException;
 25  
 
 26  
 import org.seasar.cubby.action.Action;
 27  
 import org.seasar.cubby.routing.PathResolver;
 28  
 import org.seasar.framework.container.S2Container;
 29  
 import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
 30  
 
 31  
 /**
 32  
  * リンク用の補助クラスです。
 33  
  * 
 34  
  * @author baba
 35  
  * @since 1.1.0
 36  
  */
 37  49
 class LinkSupport {
 38  
 
 39  
         /** アクションクラス名。 */
 40  
         private String actionClassName;
 41  
 
 42  
         /** アクションメソッド名。 */
 43  
         private String actionMethodName;
 44  
 
 45  
         /** リクエストパラメータの {@link Map} */
 46  49
         private final Map<String, List<String>> parameters = new HashMap<String, List<String>>();
 47  
 
 48  
         /**
 49  
          * アクションクラスを設定します。
 50  
          * 
 51  
          * @param actionClassName
 52  
          *            アクションクラス名
 53  
          */
 54  
         public void setActionClassName(final String actionClassName) {
 55  10
                 this.actionClassName = actionClassName;
 56  10
         }
 57  
 
 58  
         /**
 59  
          * アクションメソッドを設定します。
 60  
          * 
 61  
          * @param actionMethodName
 62  
          *            アクションメソッド名
 63  
          */
 64  
         public void setActionMethodName(final String actionMethodName) {
 65  10
                 this.actionMethodName = actionMethodName;
 66  10
         }
 67  
 
 68  
         /**
 69  
          * リクエストパラメータを追加します。
 70  
          * 
 71  
          * @param name
 72  
          *            パラメータ名
 73  
          * @param value
 74  
          *            値
 75  
          */
 76  
         public void addParameter(final String name, final String value) {
 77  8
                 if (!parameters.containsKey(name)) {
 78  8
                         parameters.put(name, new ArrayList<String>());
 79  
                 }
 80  8
                 final List<String> values = parameters.get(name);
 81  8
                 values.add(value);
 82  8
         }
 83  
 
 84  
         /**
 85  
          * リクエストパラメータの {@link Map} を取得します。
 86  
          * 
 87  
          * @return リクエストパラメータの {@link Map}
 88  
          */
 89  
         public Map<String, String[]> getParameters() {
 90  10
                 final Map<String, String[]> parameters = new HashMap<String, String[]>();
 91  10
                 for (final Entry<String, List<String>> entry : this.parameters
 92  
                                 .entrySet()) {
 93  8
                         parameters.put(entry.getKey(), entry.getValue().toArray(
 94  
                                         new String[0]));
 95  
                 }
 96  10
                 return parameters;
 97  
         }
 98  
 
 99  
         /**
 100  
          * パスを取得します。
 101  
          * <p>
 102  
          * このパスにはコンテキストパスは含まれません。
 103  
          * </p>
 104  
          * 
 105  
          * @param characterEncoding
 106  
          *            URI のエンコーディング
 107  
          * @return パス
 108  
          * @throws JspTagException
 109  
          *             アクションクラスが見つからなかった場合
 110  
          */
 111  
         public String getPath(final String characterEncoding)
 112  
                         throws JspTagException {
 113  
                 final Class<? extends Action> actionClass;
 114  
                 try {
 115  10
                         actionClass = forName(actionClassName);
 116  0
                 } catch (final ClassNotFoundException e) {
 117  0
                         throw new JspTagException(e);
 118  10
                 }
 119  
 
 120  10
                 final S2Container container = SingletonS2ContainerFactory
 121  
                                 .getContainer();
 122  10
                 final PathResolver pathResolver = PathResolver.class.cast(container
 123  
                                 .getComponent(PathResolver.class));
 124  10
                 final String path = pathResolver.reverseLookup(actionClass,
 125  
                                 actionMethodName, getParameters(), characterEncoding);
 126  
 
 127  10
                 return path;
 128  
         }
 129  
 
 130  
         /**
 131  
          * リンク可能かを示します。
 132  
          * 
 133  
          * @return リンク可能な場合は <code>true</code>、そうでない場合は <code>false</code>
 134  
          */
 135  
         public boolean isLinkable() {
 136  9
                 return actionClassName != null && actionMethodName != null;
 137  
         }
 138  
 
 139  
         /**
 140  
          * 特定の型のクラスオブジェクトを返します。
 141  
          * 
 142  
          * @param <T>
 143  
          *            型
 144  
          * @param className
 145  
          *            クラス名
 146  
          * @return クラスオブジェクト
 147  
          * @throws ClassNotFoundException
 148  
          *             指定されたクラスが見つからない場合
 149  
          */
 150  
         @SuppressWarnings("unchecked")
 151  
         private static <T> Class<T> forName(final String className)
 152  
                         throws ClassNotFoundException {
 153  10
                 return (Class<T>) Class.forName(className);
 154  
         }
 155  
 
 156  
         /**
 157  
          * リソースを開放します。
 158  
          */
 159  
         public void clear() {
 160  16
                 this.actionClassName = null;
 161  16
                 this.actionMethodName = null;
 162  16
                 this.parameters.clear();
 163  16
         }
 164  
 
 165  
 }