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