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