001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.servlet.taglib;
016    
017    import javax.servlet.jsp.JspException;
018    import javax.servlet.jsp.PageContext;
019    import javax.servlet.jsp.tagext.Tag;
020    
021    /**
022     * <p>
023     * See http://issues.liferay.com/browse/LPS-13878.
024     * </p>
025     *
026     * @author Shuyang Zhou
027     */
028    public class TagSupport implements Tag {
029    
030            public static Tag findAncestorWithClass(Tag fromTag, Class<?> clazz) {
031                    boolean isInterface = false;
032    
033                    if ((fromTag == null) || (clazz == null) ||
034                            (!Tag.class.isAssignableFrom(clazz) &&
035                             !(isInterface = clazz.isInterface()))) {
036    
037                            return null;
038                    }
039    
040                    while (true) {
041                            Tag parentTag = fromTag.getParent();
042    
043                            if (parentTag == null) {
044                                    return null;
045                            }
046    
047                            if ((isInterface && clazz.isInstance(parentTag)) ||
048                                    clazz.isAssignableFrom(parentTag.getClass())) {
049    
050                                    return parentTag;
051                            }
052                            else {
053                                    fromTag = parentTag;
054                            }
055                    }
056            }
057    
058            @SuppressWarnings("unused")
059            public int doEndTag() throws JspException {
060                    return EVAL_PAGE;
061            }
062    
063            @SuppressWarnings("unused")
064            public int doStartTag() throws JspException {
065                    return SKIP_BODY;
066            }
067    
068            public Tag getParent() {
069                    return _parent;
070            }
071    
072            public void release() {
073                    _parent = null;
074            }
075    
076            public void setPageContext(PageContext pageContext) {
077                    this.pageContext = pageContext;
078            }
079    
080            public void setParent(Tag tag) {
081                    _parent = tag;
082            }
083    
084            protected PageContext pageContext;
085    
086            private Tag _parent;
087    
088    }