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