View Javadoc

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  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  	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  		this.actionClassName = actionClassName;
58  	}
59  
60  	/**
61  	 * アクションメソッドを設定します。
62  	 * 
63  	 * @param actionMethodName
64  	 *            アクションメソッド名
65  	 */
66  	public void setActionMethodName(final String actionMethodName) {
67  		this.actionMethodName = actionMethodName;
68  	}
69  
70  	/**
71  	 * 要求パラメータを追加します。
72  	 * 
73  	 * @param name
74  	 *            パラメータ名
75  	 * @param value
76  	 *            値
77  	 */
78  	public void addParameter(final String name, final String value) {
79  		if (!parameters.containsKey(name)) {
80  			parameters.put(name, new ArrayList<String>());
81  		}
82  		final List<String> values = parameters.get(name);
83  		values.add(value);
84  	}
85  
86  	/**
87  	 * 要求パラメータの {@link Map} を取得します。
88  	 * 
89  	 * @return 要求パラメータの {@link Map}
90  	 */
91  	public Map<String, String[]> getParameters() {
92  		final Map<String, String[]> parameters = new HashMap<String, String[]>();
93  		for (final Entry<String, List<String>> entry : this.parameters
94  				.entrySet()) {
95  			parameters.put(entry.getKey(), entry.getValue().toArray(
96  					new String[0]));
97  		}
98  		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 			actionClass = forName(actionClassName);
118 		} catch (final ClassNotFoundException e) {
119 			throw new JspTagException(e);
120 		}
121 
122 		final PathResolverProvider pathResolverProvider = ProviderFactory
123 				.get(PathResolverProvider.class);
124 		final PathResolver pathResolver = pathResolverProvider
125 				.getPathResolver();
126 		final String path = pathResolver.reverseLookup(actionClass,
127 				actionMethodName, getParameters(), characterEncoding);
128 
129 		return path;
130 	}
131 
132 	/**
133 	 * リンク可能かを示します。
134 	 * 
135 	 * @return リンク可能な場合は <code>true</code>、そうでない場合は <code>false</code>
136 	 */
137 	public boolean isLinkable() {
138 		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 		return (Class<T>) Class.forName(className);
156 	}
157 
158 	/**
159 	 * リソースを開放します。
160 	 */
161 	public void clear() {
162 		this.actionClassName = null;
163 		this.actionMethodName = null;
164 		this.parameters.clear();
165 	}
166 
167 }