|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover 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 | 193 |
public static void storeUniqueAttribute(IRequestCycle cycle, String key, Object object) |
47 |
{ |
|
48 | 193 |
Defense.notNull(cycle, "cycle");
|
49 | 193 |
Defense.notNull(key, "key");
|
50 | 193 |
Defense.notNull(object, "object");
|
51 |
|
|
52 | 193 |
Object existing = cycle.getAttribute(key); |
53 | 193 |
if (existing != null) |
54 | 1 |
throw new IllegalStateException(TapestryMessages.nonUniqueAttribute( |
55 |
object, |
|
56 |
key, |
|
57 |
existing)); |
|
58 |
|
|
59 | 192 |
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 | 93 |
public static void storePageRenderSupport(IRequestCycle cycle, PageRenderSupport support) |
71 |
{ |
|
72 | 93 |
storeUniqueAttribute(cycle, PAGE_RENDER_SUPPORT_ATTRIBUTE, support); |
73 |
} |
|
74 |
|
|
75 |
/**
|
|
76 |
* Store the IForm instance using {@link #storeUniqueAttribute(IRequestCycle, String, Object)}.
|
|
77 |
*/
|
|
78 |
|
|
79 | 98 |
public static void storeForm(IRequestCycle cycle, IForm form) |
80 |
{ |
|
81 | 98 |
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 | 14 |
public static PageRenderSupport getPageRenderSupport(IRequestCycle cycle, IComponent component) |
96 |
{ |
|
97 | 14 |
Defense.notNull(cycle, "cycle");
|
98 | 14 |
Defense.notNull(component, "component");
|
99 |
|
|
100 | 14 |
PageRenderSupport result = (PageRenderSupport) cycle |
101 |
.getAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE); |
|
102 |
|
|
103 | 14 |
if (result == null) |
104 | 3 |
throw new ApplicationRuntimeException(TapestryMessages.noPageRenderSupport(component), |
105 |
component.getLocation(), null);
|
|
106 |
|
|
107 | 11 |
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 | 157 |
public static IForm getForm(IRequestCycle cycle, IComponent component) |
121 |
{ |
|
122 | 157 |
Defense.notNull(cycle, "cycle");
|
123 | 157 |
Defense.notNull(component, "component");
|
124 |
|
|
125 | 157 |
IForm result = (IForm) cycle.getAttribute(FORM_ATTRIBUTE); |
126 |
|
|
127 | 157 |
if (result == null) |
128 | 1 |
throw new ApplicationRuntimeException(TapestryMessages.noForm(component), component |
129 |
.getLocation(), null);
|
|
130 |
|
|
131 | 156 |
return result;
|
132 |
} |
|
133 |
|
|
134 | 94 |
public static void removePageRenderSupport(IRequestCycle cycle) |
135 |
{ |
|
136 | 94 |
cycle.removeAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE); |
137 |
} |
|
138 |
|
|
139 | 99 |
public static void removeForm(IRequestCycle cycle) |
140 |
{ |
|
141 | 99 |
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 | 2 |
public static PageRenderSupport getOptionalPageRenderSupport(IRequestCycle cycle) |
152 |
{ |
|
153 | 2 |
return (PageRenderSupport) cycle.getAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE);
|
154 |
} |
|
155 |
|
|
156 |
/**
|
|
157 |
* Splits a string using the default delimiter of ','.
|
|
158 |
*/
|
|
159 |
|
|
160 | 116 |
public static String[] split(String input) |
161 |
{ |
|
162 | 116 |
return split(input, ',');
|
163 |
} |
|
164 |
|
|
165 |
/**
|
|
166 |
* Splits a single string into an array of strings, using a specific delimiter character.
|
|
167 |
*/
|
|
168 |
|
|
169 | 117 |
public static String[] split(String input, char delimiter) |
170 |
{ |
|
171 | 117 |
if (HiveMind.isBlank(input))
|
172 | 47 |
return new String[0]; |
173 |
|
|
174 | 70 |
List strings = new ArrayList();
|
175 |
|
|
176 | 70 |
char[] buffer = input.toCharArray();
|
177 |
|
|
178 | 70 |
int start = 0;
|
179 | 70 |
int length = 0;
|
180 |
|
|
181 | 70 |
for (int i = 0; i < buffer.length; i++) |
182 |
{ |
|
183 | 1011 |
if (buffer[i] != delimiter)
|
184 |
{ |
|
185 | 920 |
length++; |
186 | 920 |
continue;
|
187 |
} |
|
188 |
|
|
189 |
// Consecutive delimiters will result in a sequence
|
|
190 |
// of empty strings.
|
|
191 |
|
|
192 | 91 |
String token = new String(buffer, start, length);
|
193 | 91 |
strings.add(token); |
194 |
|
|
195 | 91 |
start = i + 1; |
196 | 91 |
length = 0; |
197 |
} |
|
198 |
|
|
199 |
// If the string contains no delimiters, then
|
|
200 |
// wrap it an an array and return it.
|
|
201 |
|
|
202 | 70 |
if (start == 0 && length == buffer.length)
|
203 |
{ |
|
204 | 25 |
return new String[] |
205 |
{ input }; |
|
206 |
} |
|
207 |
|
|
208 |
// The final token.
|
|
209 | 45 |
String token = new String(buffer, start, length);
|
210 | 45 |
strings.add(token); |
211 |
|
|
212 | 45 |
return (String[]) strings.toArray(new String[strings.size()]); |
213 |
} |
|
214 |
} |
|