1 16 17 package org.springframework.web.util; 18 19 import javax.servlet.jsp.PageContext ; 20 import javax.servlet.jsp.tagext.Tag ; 21 22 import org.springframework.util.Assert; 23 24 45 public abstract class TagUtils { 46 47 48 public static final String SCOPE_PAGE = "page"; 49 50 51 public static final String SCOPE_REQUEST = "request"; 52 53 54 public static final String SCOPE_SESSION = "session"; 55 56 57 public static final String SCOPE_APPLICATION = "application"; 58 59 60 68 public static int getScope(String 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 95 public static boolean hasAncestorOfType(Tag tag, Class ancestorTagClass) { 96 Assert.notNull(tag, "Tag cannot be null"); 97 Assert.notNull(ancestorTagClass, "Ancestor tag class cannot be null"); 98 if (!Tag .class.isAssignableFrom(ancestorTagClass)) { 99 throw new IllegalArgumentException ( 100 "Class '" + ancestorTagClass.getName() + "' is not a valid Tag type"); 101 } 102 Tag 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 128 public static void assertHasAncestorOfType(Tag tag, Class ancestorTagClass, String tagName, String 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 ("The '" + tagName + "' tag can only be used inside a valid '" + ancestorTagName + "' tag."); 133 } 134 } 135 136 } 137 | Popular Tags |