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.routing.PathResolver;
27 import org.seasar.cubby.spi.PathResolverProvider;
28 import org.seasar.cubby.spi.ProviderFactory;
29
30
31
32
33
34
35
36 class LinkSupport {
37
38
39 private String actionClassName;
40
41
42 private String actionMethodName;
43
44
45 private final Map<String, List<String>> parameters = new HashMap<String, List<String>>();
46
47
48
49
50
51
52
53 public void setActionClassName(final String actionClassName) {
54 this.actionClassName = actionClassName;
55 }
56
57
58
59
60
61
62
63 public void setActionMethodName(final String actionMethodName) {
64 this.actionMethodName = actionMethodName;
65 }
66
67
68
69
70
71
72
73
74
75 public void addParameter(final String name, final String value) {
76 if (!parameters.containsKey(name)) {
77 parameters.put(name, new ArrayList<String>());
78 }
79 final List<String> values = parameters.get(name);
80 values.add(value);
81 }
82
83
84
85
86
87
88 public Map<String, String[]> getParameters() {
89 final Map<String, String[]> parameters = new HashMap<String, String[]>();
90 for (final Entry<String, List<String>> entry : this.parameters
91 .entrySet()) {
92 parameters.put(entry.getKey(), entry.getValue().toArray(
93 new String[0]));
94 }
95 return parameters;
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110 public String getPath(final String characterEncoding)
111 throws JspTagException {
112 final Class<?> actionClass;
113 try {
114 actionClass = forName(actionClassName);
115 } catch (final ClassNotFoundException e) {
116 throw new JspTagException(e);
117 }
118
119 final PathResolverProvider pathResolverProvider = ProviderFactory
120 .get(PathResolverProvider.class);
121 final PathResolver pathResolver = pathResolverProvider
122 .getPathResolver();
123 final String path = pathResolver.reverseLookup(actionClass,
124 actionMethodName, getParameters(), characterEncoding);
125
126 return path;
127 }
128
129
130
131
132
133
134 public boolean isLinkable() {
135 return actionClassName != null && actionMethodName != null;
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149 @SuppressWarnings("unchecked")
150 private static <T> Class<T> forName(final String className)
151 throws ClassNotFoundException {
152 return (Class<T>) Class.forName(className);
153 }
154
155
156
157
158 public void clear() {
159 this.actionClassName = null;
160 this.actionMethodName = null;
161 this.parameters.clear();
162 }
163
164 }