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.PageException;
022import lucee.runtime.ext.tag.BodyTagTryCatchFinallyImpl;
023import lucee.runtime.interpreter.VariableInterpreter;
024import lucee.runtime.op.Caster;
025
026/**
027* Saves the generated content inside the tag body in a variable.
028*
029*
030*
031**/
032public final class SaveContent extends BodyTagTryCatchFinallyImpl {
033
034        /** The name of the variable in which to save the generated content inside the tag. */
035        private String variable;
036        private boolean trim;
037        private boolean append;
038        
039        @Override
040        public void release()   {
041                super.release();
042                variable=null;
043                trim=false;
044                append=false;
045        }
046
047
048        /** set the value variable
049        *  The name of the variable in which to save the generated content inside the tag.
050        * @param variable value to set
051        **/
052        public void setVariable(String variable)        {
053                this.variable=variable;
054        }
055        
056
057        public void setTrim(boolean trim)       {
058                this.trim=trim;
059        }
060        
061        /**
062        * if true, and a variable with the passed name already exists, the content will be appended to the variable instead of overwriting it
063        */
064        public void setAppend(boolean append)   {
065                this.append=append;
066        }
067        
068        @Override
069        public int doStartTag() {
070                return EVAL_BODY_BUFFERED;
071        }
072
073
074        @Override
075        public int doAfterBody() throws PageException   {
076        
077                String value = trim ? bodyContent.getString().trim() : bodyContent.getString();
078                
079                if ( append ) {
080                
081                        value = Caster.toString( VariableInterpreter.getVariableEL( pageContext, variable, "" ), "" ) + value;  // prepend the current variable or empty-string if not found
082                }
083                
084                pageContext.setVariable( variable, value );
085                bodyContent.clearBody();
086                
087                return SKIP_BODY;
088        }
089
090        
091}