001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.taglib.core;
016    
017    import javax.servlet.jsp.JspTagException;
018    import javax.servlet.jsp.tagext.Tag;
019    
020    /**
021     * @author Shuyang Zhou
022     */
023    public class WhenTag extends ConditionalTagSupport {
024    
025            @Override
026            public int doStartTag() throws JspTagException {
027                    Tag parentTag = getParent();
028    
029                    if (!(parentTag instanceof ChooseTag)) {
030                            throw new JspTagException(
031                                    "The when tag must exist under a choose tag");
032                    }
033    
034                    ChooseTag chooseTag = (ChooseTag)parentTag;
035    
036                    if (!chooseTag.canRun()) {
037                            return SKIP_BODY;
038                    }
039    
040                    if (condition()) {
041                            chooseTag.markRan();
042    
043                            return EVAL_BODY_INCLUDE;
044                    }
045                    else {
046                            return SKIP_BODY;
047                    }
048            }
049    
050            @Override
051            public void release() {
052                    super.release();
053    
054                    _test = false;
055            }
056    
057            public void setTest(boolean test) {
058                    _test = test;
059            }
060    
061            @Override
062            protected boolean condition() {
063                    return _test;
064            }
065    
066            private boolean _test;
067    
068    }