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