1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry; |
16 |
|
|
17 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
18 |
|
import org.apache.hivemind.HiveMind; |
19 |
|
import org.apache.hivemind.Location; |
20 |
|
import org.apache.hivemind.util.Defense; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.List; |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
public final class TapestryUtils |
32 |
|
{ |
33 |
|
public static final String PAGE_RENDER_SUPPORT_ATTRIBUTE = "org.apache.tapestry.PageRenderSupport"; |
34 |
|
|
35 |
|
public static final String FORM_ATTRIBUTE = "org.apache.tapestry.Form"; |
36 |
|
|
37 |
|
public static final String FIELD_PRERENDER = "org.apache.tapestry.form.Prerender"; |
38 |
|
|
39 |
|
private static final char QUOTE = '\''; |
40 |
|
|
41 |
|
private static final char BACKSLASH = '\\'; |
42 |
|
|
43 |
|
private static final String EMPTY_QUOTES = "''"; |
44 |
|
|
45 |
|
|
46 |
0 |
private TapestryUtils() { } |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
public static void storeUniqueAttribute(IRequestCycle cycle, String key, Object object) |
63 |
|
{ |
64 |
10 |
Defense.notNull(cycle, "cycle"); |
65 |
10 |
Defense.notNull(key, "key"); |
66 |
10 |
Defense.notNull(object, "object"); |
67 |
|
|
68 |
10 |
Object existing = cycle.getAttribute(key); |
69 |
10 |
if (existing != null) |
70 |
1 |
throw new IllegalStateException(TapestryMessages.nonUniqueAttribute( |
71 |
|
object, |
72 |
|
key, |
73 |
|
existing)); |
74 |
|
|
75 |
9 |
cycle.setAttribute(key, object); |
76 |
9 |
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
public static void storePageRenderSupport(IRequestCycle cycle, PageRenderSupport support) |
83 |
|
{ |
84 |
1 |
storeUniqueAttribute(cycle, PAGE_RENDER_SUPPORT_ATTRIBUTE, support); |
85 |
1 |
} |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
public static void storeForm(IRequestCycle cycle, IForm form) |
92 |
|
{ |
93 |
4 |
storeUniqueAttribute(cycle, FORM_ATTRIBUTE, form); |
94 |
4 |
} |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
public static void storePrerender(IRequestCycle cycle, IComponent component) |
103 |
|
{ |
104 |
2 |
storeUniqueAttribute(cycle, FIELD_PRERENDER, component); |
105 |
2 |
} |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
public static PageRenderSupport getPageRenderSupport(IRequestCycle cycle, IComponent component) |
119 |
|
{ |
120 |
28 |
Defense.notNull(component, "component"); |
121 |
|
|
122 |
28 |
PageRenderSupport result = getOptionalPageRenderSupport(cycle); |
123 |
28 |
if (result == null) |
124 |
1 |
throw new ApplicationRuntimeException(TapestryMessages.noPageRenderSupport(component), |
125 |
|
component.getLocation(), null); |
126 |
|
|
127 |
27 |
return result; |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
public static IForm getForm(IRequestCycle cycle, IComponent component) |
141 |
|
{ |
142 |
77 |
Defense.notNull(cycle, "cycle"); |
143 |
77 |
Defense.notNull(component, "component"); |
144 |
|
|
145 |
77 |
IForm result = (IForm) cycle.getAttribute(FORM_ATTRIBUTE); |
146 |
|
|
147 |
77 |
if (result == null) |
148 |
1 |
throw new ApplicationRuntimeException(TapestryMessages.noForm(component), component.getLocation(), null); |
149 |
|
|
150 |
76 |
return result; |
151 |
|
} |
152 |
|
|
153 |
|
public static void removePageRenderSupport(IRequestCycle cycle) |
154 |
|
{ |
155 |
2 |
cycle.removeAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE); |
156 |
2 |
} |
157 |
|
|
158 |
|
public static void removeForm(IRequestCycle cycle) |
159 |
|
{ |
160 |
9 |
cycle.removeAttribute(FORM_ATTRIBUTE); |
161 |
9 |
} |
162 |
|
|
163 |
|
public static void removePrerender(IRequestCycle cycle) |
164 |
|
{ |
165 |
2 |
cycle.removeAttribute(FIELD_PRERENDER); |
166 |
2 |
} |
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
public static PageRenderSupport getOptionalPageRenderSupport(IRequestCycle cycle) |
176 |
|
{ |
177 |
67 |
return (PageRenderSupport) cycle.getAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE); |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
public static String[] split(String input) |
185 |
|
{ |
186 |
43 |
return split(input, ','); |
187 |
|
} |
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
public static String[] split(String input, char delimiter) |
194 |
|
{ |
195 |
55 |
if (HiveMind.isBlank(input)) |
196 |
11 |
return new String[0]; |
197 |
|
|
198 |
44 |
List strings = new ArrayList(); |
199 |
|
|
200 |
44 |
char[] buffer = input.toCharArray(); |
201 |
|
|
202 |
44 |
int start = 0; |
203 |
44 |
int length = 0; |
204 |
|
|
205 |
646 |
for (int i = 0; i < buffer.length; i++) |
206 |
|
{ |
207 |
602 |
if (buffer[i] != delimiter) |
208 |
|
{ |
209 |
557 |
length++; |
210 |
557 |
continue; |
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
216 |
45 |
String token = new String(buffer, start, length); |
217 |
45 |
strings.add(token.trim()); |
218 |
|
|
219 |
45 |
start = i + 1; |
220 |
45 |
length = 0; |
221 |
|
} |
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
44 |
if (start == 0 && length == buffer.length) |
227 |
|
{ |
228 |
21 |
return new String[] { input }; |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
23 |
String token = new String(buffer, start, length); |
233 |
23 |
strings.add(token.trim()); |
234 |
|
|
235 |
23 |
return (String[]) strings.toArray(new String[strings.size()]); |
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
|
242 |
|
public static String capitalize(String input) |
243 |
|
{ |
244 |
8 |
if (input == null || input.length() < 1) |
245 |
2 |
return input; |
246 |
|
|
247 |
6 |
return input.substring(0, 1).toUpperCase() + input.substring(1); |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
public static String enquote(String input) |
256 |
|
{ |
257 |
4 |
if (input == null) |
258 |
1 |
return EMPTY_QUOTES; |
259 |
|
|
260 |
3 |
char[] chars = input.toCharArray(); |
261 |
|
|
262 |
|
|
263 |
|
|
264 |
3 |
StringBuffer buffer = new StringBuffer(chars.length + 5); |
265 |
|
|
266 |
3 |
buffer.append(QUOTE); |
267 |
|
|
268 |
54 |
for (int i = 0; i < chars.length; i++) |
269 |
|
{ |
270 |
51 |
char ch = chars[i]; |
271 |
|
|
272 |
51 |
if (ch == QUOTE || ch == BACKSLASH) |
273 |
4 |
buffer.append(BACKSLASH); |
274 |
|
|
275 |
51 |
buffer.append(ch); |
276 |
|
} |
277 |
|
|
278 |
3 |
buffer.append(QUOTE); |
279 |
|
|
280 |
3 |
return buffer.toString(); |
281 |
|
} |
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
|
287 |
|
|
288 |
|
|
289 |
|
public static String convertTapestryIdToNMToken(String baseId) |
290 |
|
{ |
291 |
23 |
String result = baseId.replace('$', '_'); |
292 |
|
|
293 |
25 |
while (result.startsWith("_")) |
294 |
2 |
result = result.substring(1); |
295 |
|
|
296 |
23 |
return result; |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
public static String buildClientElementReference(String clientId) |
305 |
|
{ |
306 |
1 |
Defense.notNull(clientId, "clientId"); |
307 |
|
|
308 |
1 |
return "document.getElementById('" + clientId + "')"; |
309 |
|
} |
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
public static IComponent getComponent(IComponent container, String componentId, |
316 |
|
Class expectedType, Location location) |
317 |
|
{ |
318 |
3 |
Defense.notNull(container, "container"); |
319 |
3 |
Defense.notNull(componentId, "componentId"); |
320 |
3 |
Defense.notNull(expectedType, "expectedType"); |
321 |
|
|
322 |
|
|
323 |
3 |
IComponent component = null; |
324 |
|
|
325 |
|
try |
326 |
|
{ |
327 |
3 |
component = container.getComponent(componentId); |
328 |
|
} |
329 |
1 |
catch (Exception ex) |
330 |
|
{ |
331 |
1 |
throw new ApplicationRuntimeException(ex.getMessage(), location, ex); |
332 |
2 |
} |
333 |
|
|
334 |
2 |
if (!expectedType.isAssignableFrom(component.getClass())) |
335 |
1 |
throw new ApplicationRuntimeException(TapestryMessages.componentWrongType( |
336 |
|
component, |
337 |
|
expectedType), location, null); |
338 |
|
|
339 |
1 |
return component; |
340 |
|
} |
341 |
|
} |