001    /**
002     * Copyright (c) 2000-2013 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    
053                            fromTag = parentTag;
054                    }
055            }
056    
057            @Override
058            @SuppressWarnings("unused")
059            public int doEndTag() throws JspException {
060                    return EVAL_PAGE;
061            }
062    
063            @Override
064            @SuppressWarnings("unused")
065            public int doStartTag() throws JspException {
066                    return SKIP_BODY;
067            }
068    
069            @Override
070            public Tag getParent() {
071                    return _parent;
072            }
073    
074            @Override
075            public void release() {
076                    _parent = null;
077            }
078    
079            @Override
080            public void setPageContext(PageContext pageContext) {
081                    this.pageContext = pageContext;
082            }
083    
084            @Override
085            public void setParent(Tag tag) {
086                    _parent = tag;
087            }
088    
089            protected PageContext pageContext;
090    
091            private Tag _parent;
092    
093    }