1 package fr.improve.struts.taglib.layout.util; 2 import java.util.Stack ; 3 4 import javax.servlet.ServletRequest ; 5 import javax.servlet.jsp.PageContext ; 6 import javax.servlet.jsp.tagext.Tag ; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import fr.improve.struts.taglib.layout.LayoutTag; 12 13 17 public class ParentFinder { 18 public static final String TAGS_KEY = "fr.improve.struts.taglib.layout.util.ParentFinder"; 19 private static Log LOG = LogFactory.getLog(ParentFinder.class); 20 21 24 public static Tag findLayoutTag(LayoutTag tag, Class clazz) { 25 Stack lc_stack = getTagsStack(tag.getPageContext().getRequest()); 27 Tag lc_result = null; 28 29 int i = lc_stack.lastIndexOf(tag) - 1; 32 33 if (i==-2) { 36 LOG.error("The requested tag" + tag + " was not found in the following stack :\n "+ lc_stack); 37 } 38 39 while (lc_result==null && i>=0) { 41 Tag lc_someTag = (Tag ) lc_stack.get(i); 42 if (clazz.isInstance(lc_someTag)) { 43 lc_result = lc_someTag; 44 } 45 i--; 46 } 47 48 return lc_result; 49 } 50 51 public static final Tag getLastTag(PageContext in_context) { 52 Stack lc_stack = getTagsStack(in_context.getRequest()); 54 55 return (Tag ) lc_stack.lastElement(); 56 } 57 58 public static void registerTag(PageContext context, Tag tag) { 59 LOG.debug("Registering tag " + tag); 60 Stack lc_stack = getTagsStack(context.getRequest()); 61 lc_stack.push(tag); 62 } 63 64 public static void deregisterTag(PageContext context) { 65 Stack lc_stack = getTagsStack(context.getRequest()); 66 Object o = lc_stack.pop(); 67 LOG.debug("Deregistering tag " + o); 68 69 } 70 71 private static Stack getTagsStack(ServletRequest request) { 72 Stack lc_stack = (Stack ) request.getAttribute(TAGS_KEY); 73 if (lc_stack==null) { 74 lc_stack = new Stack (); 75 request.setAttribute(TAGS_KEY, lc_stack); 76 } 77 return lc_stack; 78 } 79 } 80 | Popular Tags |