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.taglib.core;
016    
017    import com.liferay.portal.kernel.servlet.taglib.TagSupport;
018    import com.liferay.portal.kernel.util.StringUtil;
019    
020    import javax.servlet.jsp.JspTagException;
021    import javax.servlet.jsp.PageContext;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public abstract class ConditionalTagSupport extends TagSupport {
027    
028            @Override
029            @SuppressWarnings("unused")
030            public int doStartTag() throws JspTagException {
031                    _result = condition();
032    
033                    if (_var != null) {
034                            pageContext.setAttribute(_var, _result, _scope);
035                    }
036    
037                    if (_result) {
038                            return EVAL_BODY_INCLUDE;
039                    }
040                    else {
041                            return SKIP_BODY;
042                    }
043            }
044    
045            @Override
046            public void release() {
047                    super.release();
048    
049                    _result = false;
050                    _scope = PageContext.PAGE_SCOPE;
051                    _var = null;
052            }
053    
054            public void setScope(String scope) {
055                    String scopeLowerCase = StringUtil.toLowerCase(scope);
056    
057                    if (scopeLowerCase.equals("application")) {
058                            _scope = PageContext.APPLICATION_SCOPE;
059                    }
060                    else if (scopeLowerCase.equals("page")) {
061                            _scope = PageContext.PAGE_SCOPE;
062                    }
063                    else if (scopeLowerCase.equals("request")) {
064                            _scope = PageContext.REQUEST_SCOPE;
065                    }
066                    else if (scopeLowerCase.equals("session")) {
067                            _scope = PageContext.SESSION_SCOPE;
068                    }
069            }
070    
071            public void setVar(String var) {
072                    _var = var;
073            }
074    
075            protected abstract boolean condition();
076    
077            private boolean _result;
078            private int _scope = PageContext.PAGE_SCOPE;
079            private String _var;
080    
081    }