001    package railo.runtime.tag;
002    
003    import java.io.IOException;
004    
005    import javax.servlet.jsp.JspWriter;
006    
007    import railo.commons.lang.StringUtil;
008    import railo.runtime.exp.ApplicationException;
009    import railo.runtime.exp.PageRuntimeException;
010    import railo.runtime.ext.tag.BodyTagTryCatchFinallyImpl;
011    import railo.runtime.op.Caster;
012    import railo.runtime.writer.CFMLWriter;
013    import railo.runtime.writer.WhiteSpaceWriter;
014    
015    /**
016    * Suppresses extra white space and other output, produced by CFML within the tag's scope.
017    *
018    *
019    *
020    **/
021    public final class ProcessingDirective extends BodyTagTryCatchFinallyImpl {
022    
023            /** 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. */
024            //private String pageencoding=null;
025    
026            private Boolean suppresswhitespace;
027        private boolean hasBody;
028            
029            /**
030            * @see javax.servlet.jsp.tagext.Tag#release()
031            */
032            public void release()   {
033                    super.release();
034                    //pageencoding=null;
035                    suppresswhitespace=null;
036            }
037    
038            /**
039            * constructor for the tag class
040            **/
041            public ProcessingDirective() {
042            }
043    
044            /** set the value pageencoding
045            *  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.
046            * @param pageencoding value to set
047            **/
048            public void setPageencoding(String pageencoding)        {
049                //pageContext. get HttpServletResponse().set ContentType("text/html; charset="+pageencoding);
050                    //this.pageencoding=pageencoding;
051            }
052            
053            public void setExecutionlog(boolean executionlog)       {
054            }
055            
056            
057            
058    
059            /** set the value suppresswhitespace
060            *  Boolean indicating whether to suppress the white space and other output generated by the 
061            *               CFML tags within the cfprocessingdirective block.
062            * @param suppresswhitespace value to set
063            **/
064            public void setSuppresswhitespace(boolean suppresswhitespace)   {
065                    this.suppresswhitespace=Caster.toBoolean(suppresswhitespace);
066            }
067    
068    
069            /**
070            * @throws ApplicationException 
071             * @see javax.servlet.jsp.tagext.Tag#doStartTag()
072            */
073            public int doStartTag() throws ApplicationException     {
074                    if(suppresswhitespace!=null && !hasBody) {
075                throw new ApplicationException
076                ("for suppressing whitespaces you must define a end tag for tag [cfprocessingdirective]");
077            } 
078            if(suppresswhitespace!=null)return EVAL_BODY_BUFFERED;          
079            return EVAL_BODY_INCLUDE;
080        }
081            
082        /**
083         * @see javax.servlet.jsp.tagext.BodyTag#doInitBody()
084         */
085        public void doInitBody() {
086        }
087            
088        /**
089         * @see javax.servlet.jsp.tagext.IterationTag#doAfterBody()
090         */
091        public int doAfterBody() {
092                    return SKIP_BODY;
093        }
094        
095    
096            /**
097             * sets if tag has a body or not
098             * @param hasBody
099             */
100            public void hasBody(boolean hasBody) {
101                    this.hasBody=hasBody;
102            }
103    
104        /**
105         * @see railo.runtime.ext.tag.BodyTagTryCatchFinallyImpl#doFinally()
106         */
107        public void doFinally() {
108            if(suppresswhitespace!=null) {
109                    try {
110                            JspWriter out = pageContext.getOut();
111                        if(suppresswhitespace.booleanValue()) {
112                            if(out instanceof WhiteSpaceWriter)out.write(bodyContent.getString());
113                            else out.write(StringUtil.suppressWhiteSpace(bodyContent.getString()));
114                        }
115                        else {
116                            if(out instanceof CFMLWriter){
117                                    ((CFMLWriter)out).writeRaw(bodyContent.getString());
118                            }
119                            else 
120                                    out.write(bodyContent.getString());
121                        }
122                    } catch (IOException e) {
123                            throw new PageRuntimeException(Caster.toPageException(e));
124                    }
125            }
126        }
127    }