001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.tag;
020
021import lucee.runtime.exp.TemplateException;
022import lucee.runtime.ext.tag.TagImpl;
023import lucee.runtime.type.scope.Scope;
024
025/**
026* Used to: Abort the processing of the currently executing CFML custom tag, exit the template 
027*       within the currently executing CFML custom tag and reexecute a section of code within the currently
028*       executing CFML custom tag
029*
030*
031*
032**/
033public final class Exit extends TagImpl {
034
035        private static final short MODE_LOOP=0;
036        private static final short MODE_EXIT_TAG=1;
037        private static final short MODE_EXIT_TEMPLATE=2;
038        /**  */
039        private short method=MODE_EXIT_TAG;
040
041        @Override
042        public void release()   {
043                super.release();
044                method=MODE_EXIT_TAG;
045        }
046
047
048        /** set the value method
049        *  
050        * @param method value to set
051        **/
052        public void setMethod(String method)    {
053                method=method.toLowerCase();
054                if(method.equals("loop"))this.method=MODE_LOOP;
055                else if(method.equals("exittag"))this.method=MODE_EXIT_TAG;
056                else if(method.equals("exittemplate"))this.method=MODE_EXIT_TEMPLATE;
057        }
058
059
060        @Override
061        public int doStartTag() {
062                return SKIP_BODY;
063        }
064
065        @Override
066        public int doEndTag() throws TemplateException  {
067                Scope variables = pageContext.variablesScope();
068                Object thistagObj=variables.get("thistag",null);
069                boolean insideCT=(thistagObj !=null) && (thistagObj instanceof lucee.runtime.type.Collection);
070                //executebody
071
072                // Inside Custom Tag
073                if(insideCT) {
074                        lucee.runtime.type.Collection thistag=(lucee.runtime.type.Collection) thistagObj;
075                        //executionmode
076                        Object exeModeObj=thistag.get("executionmode",null);
077                        boolean isEndMode=(exeModeObj !=null) && (exeModeObj instanceof String) && exeModeObj.toString().equalsIgnoreCase("end");
078                        
079                        // Start
080                        if(!isEndMode) {
081                                if(method==MODE_LOOP) {
082                                        throw new TemplateException("invalid context for the tag exit, method loop can only be used in the end tag of a custom tag");
083                                }
084                                else if(method==MODE_EXIT_TAG) {
085                                        thistag.setEL("executebody",Boolean.FALSE);
086                                        return SKIP_PAGE;
087                                }
088                        }
089                        // End
090                        else if(method==MODE_LOOP) {
091                                thistag.setEL("executebody",Boolean.TRUE);
092                                return SKIP_PAGE;
093                        }
094                        return SKIP_PAGE;
095                }
096                
097                // OUTside Custom Tag
098                if(method==MODE_LOOP) throw new TemplateException("invalid context for the tag exit, method loop can only be used inside a custom tag");
099                return SKIP_PAGE;
100                
101        }       
102}