1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
35
36
37 class LinkSupport {
38
39
40 private String actionClassName;
41
42
43 private String actionMethodName;
44
45
46 private final Map<String, List<String>> parameters = new HashMap<String, List<String>>();
47
48
49
50
51
52
53
54 public void setActionClassName(final String actionClassName) {
55 this.actionClassName = actionClassName;
56 }
57
58
59
60
61
62
63
64 public void setActionMethodName(final String actionMethodName) {
65 this.actionMethodName = actionMethodName;
66 }
67
68
69
70
71
72
73
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
86
87
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
102
103
104
105
106
107
108
109
110
111 public String getPath(final String characterEncoding)
112 throws JspTagException {
113 final Class<? extends Action> actionClass;
114 try {
115 actionClass = forName(actionClassName);
116 } catch (final ClassNotFoundException e) {
117 throw new JspTagException(e);
118 }
119
120 final S2Container container = SingletonS2ContainerFactory
121 .getContainer();
122 final PathResolver pathResolver = PathResolver.class.cast(container
123 .getComponent(PathResolver.class));
124 final String path = pathResolver.reverseLookup(actionClass,
125 actionMethodName, getParameters(), characterEncoding);
126
127 return path;
128 }
129
130
131
132
133
134
135 public boolean isLinkable() {
136 return actionClassName != null && actionMethodName != null;
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150 @SuppressWarnings("unchecked")
151 private static <T> Class<T> forName(final String className)
152 throws ClassNotFoundException {
153 return (Class<T>) Class.forName(className);
154 }
155
156
157
158
159 public void clear() {
160 this.actionClassName = null;
161 this.actionMethodName = null;
162 this.parameters.clear();
163 }
164
165 }