001    /**
002     * Copyright (c) 2000-present 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.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 https://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                    if ((fromTag == null) || (clazz == null) ||
032                            (!Tag.class.isAssignableFrom(clazz) && !clazz.isInterface())) {
033    
034                            return null;
035                    }
036    
037                    while (true) {
038                            Tag parentTag = fromTag.getParent();
039    
040                            if (parentTag == null) {
041                                    return null;
042                            }
043    
044                            if (clazz.isInstance(parentTag)) {
045                                    return parentTag;
046                            }
047    
048                            fromTag = parentTag;
049                    }
050            }
051    
052            @Override
053            @SuppressWarnings("unused")
054            public int doEndTag() throws JspException {
055                    return EVAL_PAGE;
056            }
057    
058            @Override
059            @SuppressWarnings("unused")
060            public int doStartTag() throws JspException {
061                    return SKIP_BODY;
062            }
063    
064            @Override
065            public Tag getParent() {
066                    return _parent;
067            }
068    
069            @Override
070            public void release() {
071                    _parent = null;
072            }
073    
074            @Override
075            public void setPageContext(PageContext pageContext) {
076                    this.pageContext = pageContext;
077            }
078    
079            @Override
080            public void setParent(Tag tag) {
081                    _parent = tag;
082            }
083    
084            protected PageContext pageContext;
085    
086            private Tag _parent;
087    
088    }