|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
TapestryUtils.java | 100% | 100% | 100% | 100% |
|
1 | // Copyright 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
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 | * Constants and static methods. | |
26 | * | |
27 | * @author Howard M. Lewis Ship | |
28 | * @since 4.0 | |
29 | */ | |
30 | public class TapestryUtils | |
31 | { | |
32 | /** | |
33 | * Stores an attribute into the request cycle, verifying that no object with that key is already | |
34 | * present. | |
35 | * | |
36 | * @param cycle | |
37 | * the cycle to store the attribute into | |
38 | * @param key | |
39 | * the key to store the attribute as | |
40 | * @param object | |
41 | * the attribute value to store | |
42 | * @throws IllegalStateException | |
43 | * if a non-null value has been stored into the cycle with the provided key. | |
44 | */ | |
45 | ||
46 | 149 | public static void storeUniqueAttribute(IRequestCycle cycle, String key, Object object) |
47 | { | |
48 | 149 | Defense.notNull(cycle, "cycle"); |
49 | 149 | Defense.notNull(key, "key"); |
50 | 149 | Defense.notNull(object, "object"); |
51 | ||
52 | 149 | Object existing = cycle.getAttribute(key); |
53 | 149 | if (existing != null) |
54 | 1 | throw new IllegalStateException(TapestryMessages.nonUniqueAttribute( |
55 | object, | |
56 | key, | |
57 | existing)); | |
58 | ||
59 | 148 | cycle.setAttribute(key, object); |
60 | } | |
61 | ||
62 | public static final String PAGE_RENDER_SUPPORT_ATTRIBUTE = "org.apache.tapestry.PageRenderSupport"; | |
63 | ||
64 | public static final String FORM_ATTRIBUTE = "org.apache.tapestry.Form"; | |
65 | ||
66 | /** | |
67 | * Stores the support object using {@link #storeUniqueAttribute(IRequestCycle, String, Object)}. | |
68 | */ | |
69 | ||
70 | 76 | public static void storePageRenderSupport(IRequestCycle cycle, PageRenderSupport support) |
71 | { | |
72 | 76 | storeUniqueAttribute(cycle, PAGE_RENDER_SUPPORT_ATTRIBUTE, support); |
73 | } | |
74 | ||
75 | /** | |
76 | * Store the IForm instance using {@link #storeUniqueAttribute(IRequestCycle, String, Object)}. | |
77 | */ | |
78 | ||
79 | 71 | public static void storeForm(IRequestCycle cycle, IForm form) |
80 | { | |
81 | 71 | storeUniqueAttribute(cycle, FORM_ATTRIBUTE, form); |
82 | } | |
83 | ||
84 | /** | |
85 | * Gets the previously stored {@link org.apache.tapestry.PageRenderSupport} object. | |
86 | * | |
87 | * @param cycle | |
88 | * the request cycle storing the support object | |
89 | * @param component | |
90 | * the component which requires the support (used to report exceptions) | |
91 | * @throws ApplicationRuntimeException | |
92 | * if no support object has been stored | |
93 | */ | |
94 | ||
95 | 15 | public static PageRenderSupport getPageRenderSupport(IRequestCycle cycle, IComponent component) |
96 | { | |
97 | 15 | Defense.notNull(cycle, "cycle"); |
98 | 15 | Defense.notNull(component, "component"); |
99 | ||
100 | 15 | PageRenderSupport result = (PageRenderSupport) cycle |
101 | .getAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE); | |
102 | ||
103 | 15 | if (result == null) |
104 | 2 | throw new ApplicationRuntimeException(TapestryMessages.noPageRenderSupport(component), |
105 | component.getLocation(), null); | |
106 | ||
107 | 13 | return result; |
108 | } | |
109 | ||
110 | /** | |
111 | * Gets the previously stored {@link IForm} object. | |
112 | * | |
113 | * @param cycle | |
114 | * the request cycle storing the support object | |
115 | * @param component | |
116 | * the component which requires the form (used to report exceptions) | |
117 | * @throws ApplicationRuntimeException | |
118 | * if no form object has been stored | |
119 | */ | |
120 | 117 | public static IForm getForm(IRequestCycle cycle, IComponent component) |
121 | { | |
122 | 117 | Defense.notNull(cycle, "cycle"); |
123 | 117 | Defense.notNull(component, "component"); |
124 | ||
125 | 117 | IForm result = (IForm) cycle.getAttribute(FORM_ATTRIBUTE); |
126 | ||
127 | 117 | if (result == null) |
128 | 2 | throw new ApplicationRuntimeException(TapestryMessages.noForm(component), component |
129 | .getLocation(), null); | |
130 | ||
131 | 115 | return result; |
132 | } | |
133 | ||
134 | 77 | public static void removePageRenderSupport(IRequestCycle cycle) |
135 | { | |
136 | 77 | cycle.removeAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE); |
137 | } | |
138 | ||
139 | 72 | public static void removeForm(IRequestCycle cycle) |
140 | { | |
141 | 72 | cycle.removeAttribute(FORM_ATTRIBUTE); |
142 | } | |
143 | ||
144 | /** | |
145 | * Returns the {@link PageRenderSupport} object if previously stored, or null otherwise. | |
146 | * This is used in the rare case that a component wishes to adjust its behavior based on whether | |
147 | * the page render support services are avaiable (typically, adjust for whether enclosed by a | |
148 | * Body component, or not). | |
149 | */ | |
150 | ||
151 | 52 | public static PageRenderSupport getOptionalPageRenderSupport(IRequestCycle cycle) |
152 | { | |
153 | 52 | return (PageRenderSupport) cycle.getAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE); |
154 | } | |
155 | ||
156 | /** | |
157 | * Splits a string using the default delimiter of ','. | |
158 | */ | |
159 | ||
160 | 214 | public static String[] split(String input) |
161 | { | |
162 | 214 | return split(input, ','); |
163 | } | |
164 | ||
165 | /** | |
166 | * Splits a single string into an array of strings, using a specific delimiter character. | |
167 | */ | |
168 | ||
169 | 268 | public static String[] split(String input, char delimiter) |
170 | { | |
171 | 268 | if (HiveMind.isBlank(input)) |
172 | 158 | return new String[0]; |
173 | ||
174 | 110 | List strings = new ArrayList(); |
175 | ||
176 | 110 | char[] buffer = input.toCharArray(); |
177 | ||
178 | 110 | int start = 0; |
179 | 110 | int length = 0; |
180 | ||
181 | 110 | for (int i = 0; i < buffer.length; i++) |
182 | { | |
183 | 849 | if (buffer[i] != delimiter) |
184 | { | |
185 | 788 | length++; |
186 | 788 | continue; |
187 | } | |
188 | ||
189 | // Consecutive delimiters will result in a sequence | |
190 | // of empty strings. | |
191 | ||
192 | 61 | String token = new String(buffer, start, length); |
193 | 61 | strings.add(token); |
194 | ||
195 | 61 | start = i + 1; |
196 | 61 | length = 0; |
197 | } | |
198 | ||
199 | // If the string contains no delimiters, then | |
200 | // wrap it an an array and return it. | |
201 | ||
202 | 110 | if (start == 0 && length == buffer.length) |
203 | { | |
204 | 80 | return new String[] |
205 | { input }; | |
206 | } | |
207 | ||
208 | // The final token. | |
209 | 30 | String token = new String(buffer, start, length); |
210 | 30 | strings.add(token); |
211 | ||
212 | 30 | return (String[]) strings.toArray(new String[strings.size()]); |
213 | } | |
214 | } |
|