KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > util > TagUtils


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.util;
18
19 import javax.servlet.jsp.PageContext JavaDoc;
20 import javax.servlet.jsp.tagext.Tag JavaDoc;
21
22 import org.springframework.util.Assert;
23
24 /**
25  * Utility class for tag library related code, exposing functionality
26  * such as translating {@link String Strings} to web scopes.
27  *
28  * <p>
29  * <ul>
30  * <li><code>page</code> will be transformed to
31  * {@link javax.servlet.jsp.PageContext#PAGE_SCOPE PageContext.PAGE_SCOPE}
32  * <li><code>request</code> will be transformed to
33  * {@link javax.servlet.jsp.PageContext#REQUEST_SCOPE PageContext.REQUEST_SCOPE}
34  * <li><code>session</code> will be transformed to
35  * {@link javax.servlet.jsp.PageContext#SESSION_SCOPE PageContext.SESSION_SCOPE}
36  * <li><code>application</code> will be transformed to
37  * {@link javax.servlet.jsp.PageContext#APPLICATION_SCOPE PageContext.APPLICATION_SCOPE}
38  * </ul>
39  *
40  * @author Alef Arendsen
41  * @author Rob Harrop
42  * @author Juergen Hoeller
43  * @author Rick Evans
44  */

45 public abstract class TagUtils {
46
47     /** Constant identifying the page scope */
48     public static final String JavaDoc SCOPE_PAGE = "page";
49
50     /** Constant identifying the request scope */
51     public static final String JavaDoc SCOPE_REQUEST = "request";
52
53     /** Constant identifying the session scope */
54     public static final String JavaDoc SCOPE_SESSION = "session";
55
56     /** Constant identifying the application scope */
57     public static final String JavaDoc SCOPE_APPLICATION = "application";
58
59
60     /**
61      * Determines the scope for a given input <code>String</code>.
62      * <p>If the <code>String</code> does not match 'request', 'session',
63      * 'page' or 'application', the method will return {@link PageContext#PAGE_SCOPE}.
64      * @param scope the <code>String</code> to inspect
65      * @return the scope found, or {@link PageContext#PAGE_SCOPE} if no scope matched
66      * @throws IllegalArgumentException if the supplied <code>scope</code> is <code>null</code>
67      */

68     public static int getScope(String JavaDoc scope) {
69         Assert.notNull(scope, "Scope to search for cannot be null");
70         if (scope.equals(SCOPE_REQUEST)) {
71             return PageContext.REQUEST_SCOPE;
72         }
73         else if (scope.equals(SCOPE_SESSION)) {
74             return PageContext.SESSION_SCOPE;
75         }
76         else if (scope.equals(SCOPE_APPLICATION)) {
77             return PageContext.APPLICATION_SCOPE;
78         }
79         else {
80             return PageContext.PAGE_SCOPE;
81         }
82     }
83
84     /**
85      * Determine whether the supplied {@link Tag} has any ancestor tag
86      * of the supplied type.
87      * @param tag the tag whose ancestors are to be checked
88      * @param ancestorTagClass the ancestor {@link Class} being searched for
89      * @return <code>true</code> if the supplied {@link Tag} has any ancestor tag
90      * of the supplied type
91      * @throws IllegalArgumentException if either of the supplied arguments is <code>null</code>;
92      * or if the supplied <code>ancestorTagClass</code> is not type-assignable to
93      * the {@link Tag} class
94      */

95     public static boolean hasAncestorOfType(Tag JavaDoc tag, Class JavaDoc ancestorTagClass) {
96         Assert.notNull(tag, "Tag cannot be null");
97         Assert.notNull(ancestorTagClass, "Ancestor tag class cannot be null");
98         if (!Tag JavaDoc.class.isAssignableFrom(ancestorTagClass)) {
99             throw new IllegalArgumentException JavaDoc(
100                     "Class '" + ancestorTagClass.getName() + "' is not a valid Tag type");
101         }
102         Tag JavaDoc ancestor = tag.getParent();
103         while (ancestor != null) {
104             if (ancestorTagClass.isAssignableFrom(ancestor.getClass())) {
105                 return true;
106             }
107             ancestor = ancestor.getParent();
108         }
109         return false;
110     }
111
112     /**
113      * Determine whether the supplied {@link Tag} has any ancestor tag
114      * of the supplied type, throwing an {@link IllegalStateException}
115      * if not.
116      * @param tag the tag whose ancestors are to be checked
117      * @param ancestorTagClass the ancestor {@link Class} being searched for
118      * @param tagName the name of the <code>tag</code>; for example '<code>option</code>'
119      * @param ancestorTagName the name of the ancestor <code>tag</code>; for example '<code>select</code>'
120      * @throws IllegalStateException if the supplied <code>tag</code> does not
121      * have a tag of the supplied <code>parentTagClass</code> as an ancestor
122      * @throws IllegalArgumentException if any of the supplied arguments is <code>null</code>,
123      * or in the case of the {@link String}-typed arguments, is composed wholly
124      * of whitespace; or if the supplied <code>ancestorTagClass</code> is not
125      * type-assignable to the {@link Tag} class
126      * @see #hasAncestorOfType(javax.servlet.jsp.tagext.Tag, Class)
127      */

128     public static void assertHasAncestorOfType(Tag JavaDoc tag, Class JavaDoc ancestorTagClass, String JavaDoc tagName, String JavaDoc ancestorTagName) {
129         Assert.hasText(tagName, "'tagName' must not be empty");
130         Assert.hasText(ancestorTagName, "'ancestorTagName' must not be empty");
131         if (!TagUtils.hasAncestorOfType(tag, ancestorTagClass)) {
132             throw new IllegalStateException JavaDoc("The '" + tagName + "' tag can only be used inside a valid '" + ancestorTagName + "' tag.");
133         }
134     }
135
136 }
137
Popular Tags