View Javadoc

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