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