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