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