KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > TapestryUtils


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 JavaDoc;
18 import java.util.List JavaDoc;
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     public static void storeUniqueAttribute(IRequestCycle cycle, String JavaDoc key, Object JavaDoc object)
47     {
48         Defense.notNull(cycle, "cycle");
49         Defense.notNull(key, "key");
50         Defense.notNull(object, "object");
51
52         Object JavaDoc existing = cycle.getAttribute(key);
53         if (existing != null)
54             throw new IllegalStateException JavaDoc(TapestryMessages.nonUniqueAttribute(
55                     object,
56                     key,
57                     existing));
58
59         cycle.setAttribute(key, object);
60     }
61
62     public static final String JavaDoc PAGE_RENDER_SUPPORT_ATTRIBUTE = "org.apache.tapestry.PageRenderSupport";
63
64     public static final String JavaDoc FORM_ATTRIBUTE = "org.apache.tapestry.Form";
65
66     /**
67      * Stores the support object using {@link #storeUniqueAttribute(IRequestCycle, String, Object)}.
68      */

69
70     public static void storePageRenderSupport(IRequestCycle cycle, PageRenderSupport support)
71     {
72         storeUniqueAttribute(cycle, PAGE_RENDER_SUPPORT_ATTRIBUTE, support);
73     }
74
75     /**
76      * Store the IForm instance using {@link #storeUniqueAttribute(IRequestCycle, String, Object)}.
77      */

78
79     public static void storeForm(IRequestCycle cycle, IForm form)
80     {
81         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     public static PageRenderSupport getPageRenderSupport(IRequestCycle cycle, IComponent component)
96     {
97         Defense.notNull(cycle, "cycle");
98         Defense.notNull(component, "component");
99
100         PageRenderSupport result = (PageRenderSupport) cycle
101                 .getAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE);
102
103         if (result == null)
104             throw new ApplicationRuntimeException(TapestryMessages.noPageRenderSupport(component),
105                     component.getLocation(), null);
106
107         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     public static IForm getForm(IRequestCycle cycle, IComponent component)
121     {
122         Defense.notNull(cycle, "cycle");
123         Defense.notNull(component, "component");
124
125         IForm result = (IForm) cycle.getAttribute(FORM_ATTRIBUTE);
126
127         if (result == null)
128             throw new ApplicationRuntimeException(TapestryMessages.noForm(component), component
129                     .getLocation(), null);
130
131         return result;
132     }
133
134     public static void removePageRenderSupport(IRequestCycle cycle)
135     {
136         cycle.removeAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE);
137     }
138
139     public static void removeForm(IRequestCycle cycle)
140     {
141         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     public static PageRenderSupport getOptionalPageRenderSupport(IRequestCycle cycle)
152     {
153         return (PageRenderSupport) cycle.getAttribute(PAGE_RENDER_SUPPORT_ATTRIBUTE);
154     }
155
156     /**
157      * Splits a string using the default delimiter of ','.
158      */

159
160     public static String JavaDoc[] split(String JavaDoc input)
161     {
162         return split(input, ',');
163     }
164
165     /**
166      * Splits a single string into an array of strings, using a specific delimiter character.
167      */

168
169     public static String JavaDoc[] split(String JavaDoc input, char delimiter)
170     {
171         if (HiveMind.isBlank(input))
172             return new String JavaDoc[0];
173
174         List JavaDoc strings = new ArrayList JavaDoc();
175
176         char[] buffer = input.toCharArray();
177
178         int start = 0;
179         int length = 0;
180
181         for (int i = 0; i < buffer.length; i++)
182         {
183             if (buffer[i] != delimiter)
184             {
185                 length++;
186                 continue;
187             }
188
189             // Consecutive delimiters will result in a sequence
190
// of empty strings.
191

192             String JavaDoc token = new String JavaDoc(buffer, start, length);
193             strings.add(token);
194
195             start = i + 1;
196             length = 0;
197         }
198
199         // If the string contains no delimiters, then
200
// wrap it an an array and return it.
201

202         if (start == 0 && length == buffer.length)
203         {
204             return new String JavaDoc[]
205             { input };
206         }
207
208         // The final token.
209
String JavaDoc token = new String JavaDoc(buffer, start, length);
210         strings.add(token);
211
212         return (String JavaDoc[]) strings.toArray(new String JavaDoc[strings.size()]);
213     }
214 }
Popular Tags