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 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
131
132 public boolean isLinkable() {
133 return actionClassName != null && actionMethodName != null;
134 }
135
136
137
138
139
140
141
142
143
144
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 }