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 java.io.IOException;
022
023import javax.servlet.jsp.JspException;
024
025import lucee.runtime.ext.tag.BodyTagTryCatchFinallyImpl;
026import lucee.runtime.listener.ApplicationContextSupport;
027import lucee.runtime.writer.BodyContentImpl;
028
029public final class Silent extends BodyTagTryCatchFinallyImpl {
030
031
032    private Boolean bufferOutput=true;
033        private BodyContentImpl bc;
034        private boolean wasSilent;
035
036
037
038        /**
039         * @param bufferoutput the bufferoutput to set
040         */
041        public void setBufferoutput(boolean bufferOutput) {
042                this.bufferOutput = bufferOutput?Boolean.TRUE:Boolean.FALSE;
043        }
044
045
046        @Override
047    public int doStartTag() throws JspException {
048        if(bufferOutput==null)
049                bufferOutput=((ApplicationContextSupport)pageContext.getApplicationContext()).getBufferOutput()?Boolean.TRUE:Boolean.FALSE;
050        
051        if(bufferOutput.booleanValue()) bc = (BodyContentImpl) pageContext.pushBody();
052        else wasSilent=pageContext.setSilent();
053        
054        return EVAL_BODY_INCLUDE;
055    }
056    
057
058        @Override
059        public void doCatch(Throwable t) throws Throwable {
060                if(bufferOutput.booleanValue()){
061                try {
062                                bc.flush();
063                        } catch (IOException e) {}
064                        pageContext.popBody();
065                        bc=null;
066        }
067        else if(!wasSilent)pageContext.unsetSilent();
068            super.doCatch(t);
069        }
070
071    
072    @Override
073    public void doFinally() {
074        if(bufferOutput.booleanValue()){
075                if(bc!=null){
076                        bc.clearBody();
077                        pageContext.popBody();
078                }
079        }
080        else if(!wasSilent)pageContext.unsetSilent();
081    }
082
083
084        @Override
085        public void release() {
086                super.release();
087                bc=null;
088                this.bufferOutput=null;
089        }
090
091
092}