|
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 |
441
| public static void storeUniqueAttribute(IRequestCycle cycle, String key, Object object)
|
|
54 |
| { |
|
55 |
441
| Defense.notNull(cycle, "cycle");
|
|
56 |
441
| Defense.notNull(key, "key");
|
|
57 |
441
| Defense.notNull(object, "object");
|
|
58 |
| |
|
59 |
441
| Object existing = cycle.getAttribute(key);
|
|
60 |
441
| if (existing != null)
|
|
61 |
3
| throw new IllegalStateException(TapestryMessages.nonUniqueAttribute(
|
|
62 |
| object, |
|
63 |
| key, |
|
64 |
| existing)); |
|
65 |
| |
|
66 |
438
| 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 |
207
| public static void storePageRenderSupport(IRequestCycle cycle, PageRenderSupport support)
|
|
78 |
| { |
|
79 |
207
| storeUniqueAttribute(cycle, PAGE_RENDER_SUPPORT_ATTRIBUTE, support);
|
|
80 |
| } |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
228
| public static void storeForm(IRequestCycle cycle, IForm form)
|
|
87 |
| { |
|
88 |
228
| storeUniqueAttribute(cycle, FORM_ATTRIBUTE, form);
|
|
89 |
| } |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
39
| public static PageRenderSupport getPageRenderSupport(IRequestCycle cycle, IComponent component)
|
|
103 |
| { |
|
104 |
39
| Defense.notNull(component, "component");
|
|
105 |
| |
|
106 |
39
| PageRenderSupport result = getOptionalPageRenderSupport(cycle);
|
|
107 |
39
| if (result == null)
|
|
108 |
3
| throw new ApplicationRuntimeException(TapestryMessages.noPageRenderSupport(component),
|
|
109 |
| component.getLocation(), null); |
|
110 |
| |
|
111 |
36
| return result;
|
|
112 |
| } |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
396
| public static IForm getForm(IRequestCycle cycle, IComponent component)
|
|
125 |
| { |
|
126 |
396
| Defense.notNull(cycle, "cycle");
|
|
127 |
396
| Defense.notNull(component, "component");
|
|
128 |
| |
|
129 |
396
| IForm result = (IForm) cycle.getAttribute(FORM_ATTRIBUTE);
|
|
130 |
| |
|
131 |
396
| if (result == null)
|
|
132 |
6
| throw new ApplicationRuntimeException(TapestryMessages.noForm(component), component
|
|
133 |
| .getLocation(), null); |
|
134 |
| |
|
135 |
390
| return result;
|
|
136 |
| } |
|
137 |
| |
|
138 |
210
| public static void removePageRenderSupport(IRequestCycle cycle)
|
|
139 |
| { |
|
140 |
210
| cycle.removeAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE);
|
|
141 |
| } |
|
142 |
| |
|
143 |
240
| public static void removeForm(IRequestCycle cycle)
|
|
144 |
| { |
|
145 |
240
| cycle.removeAttribute(FORM_ATTRIBUTE);
|
|
146 |
| } |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
324
| public static PageRenderSupport getOptionalPageRenderSupport(IRequestCycle cycle)
|
|
156 |
| { |
|
157 |
324
| return (PageRenderSupport) cycle.getAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE);
|
|
158 |
| } |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
1314
| public static String[] split(String input)
|
|
165 |
| { |
|
166 |
1314
| return split(input, ',');
|
|
167 |
| } |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
1470
| public static String[] split(String input, char delimiter)
|
|
174 |
| { |
|
175 |
1470
| if (HiveMind.isBlank(input))
|
|
176 |
1128
| return new String[0];
|
|
177 |
| |
|
178 |
342
| List strings = new ArrayList();
|
|
179 |
| |
|
180 |
342
| char[] buffer = input.toCharArray();
|
|
181 |
| |
|
182 |
342
| int start = 0;
|
|
183 |
342
| int length = 0;
|
|
184 |
| |
|
185 |
342
| for (int i = 0; i < buffer.length; i++)
|
|
186 |
| { |
|
187 |
3369
| if (buffer[i] != delimiter)
|
|
188 |
| { |
|
189 |
3096
| length++;
|
|
190 |
3096
| continue;
|
|
191 |
| } |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
273
| String token = new String(buffer, start, length);
|
|
197 |
273
| strings.add(token);
|
|
198 |
| |
|
199 |
273
| start = i + 1;
|
|
200 |
273
| length = 0;
|
|
201 |
| } |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
342
| if (start == 0 && length == buffer.length)
|
|
207 |
| { |
|
208 |
234
| return new String[]
|
|
209 |
| { input }; |
|
210 |
| } |
|
211 |
| |
|
212 |
| |
|
213 |
108
| String token = new String(buffer, start, length);
|
|
214 |
108
| strings.add(token);
|
|
215 |
| |
|
216 |
108
| return (String[]) strings.toArray(new String[strings.size()]);
|
|
217 |
| } |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
57
| public static String enquote(String input)
|
|
225 |
| { |
|
226 |
57
| if (input == null)
|
|
227 |
3
| return EMPTY_QUOTES;
|
|
228 |
| |
|
229 |
54
| char[] chars = input.toCharArray();
|
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
54
| StringBuffer buffer = new StringBuffer(chars.length + 5);
|
|
234 |
| |
|
235 |
54
| buffer.append(QUOTE);
|
|
236 |
| |
|
237 |
54
| for (int i = 0; i < chars.length; i++)
|
|
238 |
| { |
|
239 |
924
| char ch = chars[i];
|
|
240 |
| |
|
241 |
924
| if (ch == QUOTE || ch == BACKSLASH)
|
|
242 |
39
| buffer.append(BACKSLASH);
|
|
243 |
| |
|
244 |
924
| buffer.append(ch);
|
|
245 |
| } |
|
246 |
| |
|
247 |
54
| buffer.append(QUOTE);
|
|
248 |
| |
|
249 |
54
| return buffer.toString();
|
|
250 |
| } |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
519
| public static String convertTapestryIdToNMToken(String baseId)
|
|
259 |
| { |
|
260 |
519
| String result = baseId.replace('$', '_');
|
|
261 |
| |
|
262 |
519
| while (result.startsWith("_"))
|
|
263 |
234
| result = result.substring(1);
|
|
264 |
| |
|
265 |
519
| return result;
|
|
266 |
| } |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
3
| public static String buildClientElementReference(String clientId)
|
|
274 |
| { |
|
275 |
3
| Defense.notNull(clientId, "clientId");
|
|
276 |
| |
|
277 |
3
| return "document.getElementById('" + clientId + "')";
|
|
278 |
| } |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
9
| public static IComponent getComponent(IComponent container, String componentId,
|
|
285 |
| Class expectedType, Location location) |
|
286 |
| { |
|
287 |
9
| Defense.notNull(container, "container");
|
|
288 |
9
| Defense.notNull(componentId, "componentId");
|
|
289 |
9
| Defense.notNull(expectedType, "expectedType");
|
|
290 |
| |
|
291 |
| |
|
292 |
9
| IComponent component = null;
|
|
293 |
| |
|
294 |
9
| try
|
|
295 |
| { |
|
296 |
9
| component = container.getComponent(componentId);
|
|
297 |
| } |
|
298 |
| catch (Exception ex) |
|
299 |
| { |
|
300 |
3
| throw new ApplicationRuntimeException(ex.getMessage(), location, ex);
|
|
301 |
| } |
|
302 |
| |
|
303 |
6
| if (!expectedType.isAssignableFrom(component.getClass()))
|
|
304 |
3
| throw new ApplicationRuntimeException(TapestryMessages.componentWrongType(
|
|
305 |
| component, |
|
306 |
| expectedType), location, null); |
|
307 |
| |
|
308 |
3
| return component;
|
|
309 |
| } |
|
310 |
| } |