KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > util > ScopeTools


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.util;
8
9
10 import javax.servlet.jsp.PageContext JavaDoc;
11
12
13 /**
14  * This class contains tools useful when dealing with scopes
15  * as integers and as Strings as well as conversions to JSP
16  * PageContext values.
17  *
18  * @author Brian Pontarelli
19  * @since 2.0
20  * @version 2.0
21  */

22 public class ScopeTools {
23
24     /**
25      * Not instantiable
26      */

27     private ScopeTools() {
28     }
29
30
31     /**
32      * Returns whether or not the given scope String is a valid Portal framework
33      * scope String
34      *
35      * @param scope The scope String to check
36      * @return True if the String is valid, false otherwise
37      */

38     public static boolean isValidScope(String JavaDoc scope) {
39
40         return (scope.equalsIgnoreCase(ScopeConstants.PAGE) ||
41             scope.equalsIgnoreCase(ScopeConstants.REQUEST) ||
42             scope.equalsIgnoreCase(ScopeConstants.SESSION) ||
43             scope.equalsIgnoreCase(ScopeConstants.APPLICATION));
44     }
45
46     /**
47      * Returns whether or not the given scope int is a valid Portal framework
48      * scope int.
49      *
50      * @param scope The scope int to check
51      * @return True if the int is valid, false otherwise
52      */

53     public static boolean isValidScope(int scope) {
54
55         return (scope >= ScopeConstants.PAGE_INT &&
56             scope <= ScopeConstants.APPLICATION_INT);
57     }
58
59     /**
60      * Converts the given scope from a String to an int.
61      *
62      * @param scope The scope string to convert
63      * @return The scope as an int
64      * @throws IllegalArgumentException If the int is not a valid scope int
65      */

66     public static int convertScope(String JavaDoc scope) {
67
68         if (scope.equalsIgnoreCase(ScopeConstants.PAGE)) {
69             return ScopeConstants.PAGE_INT;
70         } else if (scope.equalsIgnoreCase(ScopeConstants.REQUEST)) {
71             return ScopeConstants.REQUEST_INT;
72         } else if (scope.equalsIgnoreCase(ScopeConstants.SESSION)) {
73             return ScopeConstants.SESSION_INT;
74         } else if (scope.equalsIgnoreCase(ScopeConstants.APPLICATION)) {
75             return ScopeConstants.APPLICATION_INT;
76         }
77
78         throw new IllegalArgumentException JavaDoc("Invalid Portal framework scope String");
79     }
80
81     /**
82      * Converts the given scope from a int to an String.
83      *
84      * @param scope The scope int to convert
85      * @return The scope as an String
86      * @throws IllegalArgumentException If the int is not a valid scope int
87      */

88     public static String JavaDoc convertScope(int scope) {
89
90         switch (scope) {
91             case ScopeConstants.PAGE_INT:
92                 return ScopeConstants.PAGE;
93             case ScopeConstants.REQUEST_INT:
94                 return ScopeConstants.REQUEST;
95             case ScopeConstants.SESSION_INT:
96                 return ScopeConstants.SESSION;
97             case ScopeConstants.APPLICATION_INT:
98                 return ScopeConstants.APPLICATION;
99         }
100
101         throw new IllegalArgumentException JavaDoc("Invalid Portal framework scope int");
102     }
103
104     /**
105      * Returns whether or not the given String denotes the PAGE scope. This
106      * check is done in a case INSENSTIVE manner.
107      *
108      * @param scope The String to check
109      * @return True if the String denotes the PAGE scope, false otherwise
110      */

111     public static boolean isPage(String JavaDoc scope) {
112         return scope.equalsIgnoreCase(ScopeConstants.PAGE);
113     }
114
115     /**
116      * Returns whether or not the given String denotes the REQUEST scope. This
117      * check is done in a case INSENSTIVE manner.
118      *
119      * @param scope The String to check
120      * @return True if the String denotes the REQUEST scope, false otherwise
121      */

122     public static boolean isRequest(String JavaDoc scope) {
123         return scope.equalsIgnoreCase(ScopeConstants.REQUEST);
124     }
125
126     /**
127      * Returns whether or not the given String denotes the SESSION scope. This
128      * check is done in a case INSENSTIVE manner.
129      *
130      * @param scope The String to check
131      * @return True if the String denotes the SESSION scope, false otherwise
132      */

133     public static boolean isSession(String JavaDoc scope) {
134         return scope.equalsIgnoreCase(ScopeConstants.SESSION);
135     }
136
137     /**
138      * Returns whether or not the given String denotes the APPLICATION scope. This
139      * check is done in a case INSENSTIVE manner.
140      *
141      * @param scope The String to check
142      * @return True if the String denotes the APPLICATION scope, false otherwise
143      */

144     public static boolean isApplication(String JavaDoc scope) {
145         return scope.equalsIgnoreCase(ScopeConstants.APPLICATION);
146     }
147
148     /**
149      * Converts the given Portal framework Scope int to a J2EE compliant scope
150      * value. These are retrieved from the javax.servlet.jsp.PageContext class.
151      *
152      * @param scope The scope int to convert
153      * @return The J2EE compliant scope value
154      * @throws IllegalArgumentException If the int is not a valid scope int
155      */

156     public static int convertToJ2EE(int scope) {
157
158         switch (scope) {
159             case ScopeConstants.PAGE_INT:
160                 return PageContext.PAGE_SCOPE;
161             case ScopeConstants.REQUEST_INT:
162                 return PageContext.REQUEST_SCOPE;
163             case ScopeConstants.SESSION_INT:
164                 return PageContext.SESSION_SCOPE;
165             case ScopeConstants.APPLICATION_INT:
166                 return PageContext.APPLICATION_SCOPE;
167         }
168
169         throw new IllegalArgumentException JavaDoc("Invalid Portal framework scope int");
170     }
171
172     /**
173      * Converts the given Portal framework Scope String to a J2EE compliant scope
174      * value. These are retrieved from the javax.servlet.jsp.PageContext class.
175      * This conversion happens in a case INSENSITIVE manner.
176      *
177      * @param scope The scope String to convert
178      * @return The J2EE compliant scope value
179      * @throws IllegalArgumentException If the String is not a valid scope String
180      */

181     public static String JavaDoc convertToJ2EE(String JavaDoc scope) {
182
183         if (scope.equalsIgnoreCase(ScopeConstants.PAGE)) {
184             return PageContext.PAGE;
185         } else if (scope.equalsIgnoreCase(ScopeConstants.REQUEST)) {
186             return PageContext.REQUEST;
187         } else if (scope.equalsIgnoreCase(ScopeConstants.SESSION)) {
188             return PageContext.SESSION;
189         } else if (scope.equalsIgnoreCase(ScopeConstants.APPLICATION)) {
190             return PageContext.APPLICATION;
191         }
192
193         throw new IllegalArgumentException JavaDoc("Invalid Portal framework scope String");
194     }
195
196     /**
197      * Converts the given Portal framework Scope int to a J2EE compliant scope
198      * value. These are retrieved from the javax.servlet.jsp.PageContext class.
199      *
200      * @param scope The scope int to convert
201      * @return The J2EE compliant scope value
202      * @throws IllegalArgumentException If the int is not a valid scope int
203      */

204     public static int convertFromJ2EE(int scope) {
205
206         switch (scope) {
207             case PageContext.PAGE_SCOPE:
208                 return ScopeConstants.PAGE_INT;
209             case PageContext.REQUEST_SCOPE:
210                 return ScopeConstants.REQUEST_INT;
211             case PageContext.SESSION_SCOPE:
212                 return ScopeConstants.SESSION_INT;
213             case PageContext.APPLICATION_SCOPE:
214                 return ScopeConstants.APPLICATION_INT;
215         }
216
217         throw new IllegalArgumentException JavaDoc("Invalid Portal framework scope int");
218     }
219
220     /**
221      * Converts the given Portal framework Scope String to a J2EE compliant scope
222      * value. These are retrieved from the javax.servlet.jsp.PageContext class.
223      * This conversion happens in a case INSENSITIVE manner.
224      *
225      * @param scope The scope String to convert
226      * @return The J2EE compliant scope value
227      * @throws IllegalArgumentException If the String is not a valid scope String
228      */

229     public static String JavaDoc convertFromJ2EE(String JavaDoc scope) {
230
231         if (scope.equalsIgnoreCase(PageContext.PAGE)) {
232             return ScopeConstants.PAGE;
233         } else if (scope.equalsIgnoreCase(PageContext.REQUEST)) {
234             return ScopeConstants.REQUEST;
235         } else if (scope.equalsIgnoreCase(PageContext.SESSION)) {
236             return ScopeConstants.SESSION;
237         } else if (scope.equalsIgnoreCase(PageContext.APPLICATION)) {
238             return ScopeConstants.APPLICATION;
239         }
240
241         throw new IllegalArgumentException JavaDoc("Invalid Portal framework scope String");
242     }
243
244     /**
245      * <p>
246      * Compares two scope strings and returns the value of their difference.
247      * </p>
248      *
249      * <p>
250      * The value returned is positive if scope1 has a larger scope than scope2
251      * and negative if scope1 has a smaller scope than scope2. Zero is returned
252      * if the scopes are equal. A larger scope is defined to have more visibility,
253      * which means that APPLICATION scope is larger than REQUEST scope and SESSION
254      * scope.
255      * </p>
256      *
257      * <p>
258      * NOTE: This method only works because the integer constants setup for
259      * scopes in GlobalConstants are sequential from REQUEST to APPLICATION
260      * starting with the value 0 and ending with the value 2.
261      * </p>
262      *
263      * @param scope1 The first scope being compared
264      * @param scope2 The second scope being compared
265      * @return The result of the comparison
266      */

267     public static int compareScopes(String JavaDoc scope1, String JavaDoc scope2) {
268         int scope1Int = convertScope(scope1);
269         int scope2Int = convertScope(scope2);
270
271         return scope1Int - scope2Int;
272     }
273 }
274
Popular Tags