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 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    }