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.JspWriter;
024
025import lucee.commons.lang.StringUtil;
026import lucee.runtime.exp.ApplicationException;
027import lucee.runtime.exp.PageRuntimeException;
028import lucee.runtime.ext.tag.BodyTagTryCatchFinallyImpl;
029import lucee.runtime.op.Caster;
030import lucee.runtime.writer.CFMLWriter;
031import lucee.runtime.writer.WhiteSpaceWriter;
032
033/**
034* Suppresses extra white space and other output, produced by CFML within the tag's scope.
035*
036*
037*
038**/
039public final class ProcessingDirective extends BodyTagTryCatchFinallyImpl {
040
041        /** A string literal; the character encoding to use to read the page. The value may be enclosed in single or double quotation marks, or none. */
042        //private String pageencoding=null;
043
044        private Boolean suppresswhitespace;
045    private boolean hasBody;
046        
047        @Override
048        public void release()   {
049                super.release();
050                //pageencoding=null;
051                suppresswhitespace=null;
052        }
053
054        /**
055        * constructor for the tag class
056        **/
057        public ProcessingDirective() {
058        }
059
060        /** set the value pageencoding
061        *  A string literal; the character encoding to use to read the page. The value may be enclosed in single or double quotation marks, or none.
062        * @param pageencoding value to set
063        **/
064        public void setPageencoding(String pageencoding)        {
065            //pageContext. get HttpServletResponse().set ContentType("text/html; charset="+pageencoding);
066                //this.pageencoding=pageencoding;
067        }
068
069        public void setExecutionlog(boolean executionlog)       {
070        }
071        public void setPreservecase(boolean b)  {
072        }
073        
074        
075        
076
077        /** set the value suppresswhitespace
078        *  Boolean indicating whether to suppress the white space and other output generated by the 
079        *               CFML tags within the cfprocessingdirective block.
080        * @param suppresswhitespace value to set
081        **/
082        public void setSuppresswhitespace(boolean suppresswhitespace)   {
083                this.suppresswhitespace=Caster.toBoolean(suppresswhitespace);
084        }
085
086
087        @Override
088        public int doStartTag() throws ApplicationException     {
089                if(suppresswhitespace!=null && !hasBody) {
090            throw new ApplicationException
091            ("for suppressing whitespaces you must define a end tag for tag [cfprocessingdirective]");
092        } 
093        if(suppresswhitespace!=null)return EVAL_BODY_BUFFERED;          
094        return EVAL_BODY_INCLUDE;
095    }
096        
097    @Override
098    public void doInitBody() {
099    }
100        
101    @Override
102    public int doAfterBody() {
103                return SKIP_BODY;
104    }
105    
106
107        /**
108         * sets if tag has a body or not
109         * @param hasBody
110         */
111        public void hasBody(boolean hasBody) {
112                this.hasBody=hasBody;
113        }
114
115    @Override
116    public void doFinally() {
117        if(suppresswhitespace!=null) {
118                try {
119                        JspWriter out = pageContext.getOut();
120                    if(suppresswhitespace.booleanValue()) {
121                        if(out instanceof WhiteSpaceWriter)out.write(bodyContent.getString());
122                        else out.write(StringUtil.suppressWhiteSpace(bodyContent.getString()));
123                    }
124                    else {
125                        if(out instanceof CFMLWriter){
126                                ((CFMLWriter)out).writeRaw(bodyContent.getString());
127                        }
128                        else 
129                                out.write(bodyContent.getString());
130                    }
131                } catch (IOException e) {
132                        throw new PageRuntimeException(Caster.toPageException(e));
133                }
134        }
135    }
136}