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