001    package railo.runtime.tag;
002    
003    import java.io.IOException;
004    
005    import railo.runtime.exp.ApplicationException;
006    import railo.runtime.exp.PageException;
007    import railo.runtime.ext.tag.BodyTagImpl;
008    
009    public final class Timer extends BodyTagImpl {
010    
011            private static final int TYPE_DEBUG = 0;
012            private static final int TYPE_INLINE = 1;
013            private static final int TYPE_OUTLINE = 2;
014            private static final int TYPE_COMMENT = 3;
015            
016            private String label="";
017            private int type=TYPE_DEBUG;
018            private long time;
019            /**
020             *
021             * @see railo.runtime.ext.tag.BodyTagImpl#release()
022             */
023            public void release() {
024                    super.release();
025                    type=TYPE_DEBUG;
026                    label="";
027            }
028    
029            /**
030             * @param label the label to set
031             */
032            public void setLabel(String label) {
033                    this.label = label;
034            }
035    
036            /**
037             * @param type the type to set
038             * @throws ApplicationException 
039             */
040            public void setType(String strType) throws ApplicationException {
041                    strType = strType.toLowerCase().trim();
042                    if("comment".equals(strType)) type=TYPE_COMMENT;
043                    else if("debug".equals(strType)) type=TYPE_DEBUG;
044                    else if("inline".equals(strType)) type=TYPE_INLINE;
045                    else if("outline".equals(strType)) type=TYPE_OUTLINE;
046                    else throw new ApplicationException("invalid value ["+strType+"] for attribute [type], valid values are [comment,debug,inline,outline]");
047            }
048    
049            /**
050             *
051             * @see railo.runtime.ext.tag.TagImpl#doStartTag()
052             */
053            public int doStartTag() {
054                    time=System.currentTimeMillis();
055                    if(TYPE_OUTLINE==type) {
056                            try {
057                                    pageContext.write("<fieldset class=\"cftimer\">");
058                            } 
059                            catch (IOException e) {}
060                    }
061                    return EVAL_BODY_INCLUDE;
062            }
063            
064            
065            /**
066             *
067             * @throws PageException 
068             * @throws IOException 
069             * @see railo.runtime.ext.tag.TagImpl#doEndTag()
070             */
071            public int doEndTag() throws PageException {
072                    try {
073                            _doEndTag();
074                    }
075                    catch (IOException e) {}
076                    return EVAL_PAGE;
077            }
078            
079            public void _doEndTag() throws IOException {
080                    long exe = (System.currentTimeMillis()-time);
081                    if(TYPE_INLINE==type) {
082                            pageContext.write(""+label+": "+exe+"ms");
083                    }
084                    else if(TYPE_OUTLINE==type) {
085                            pageContext.write("<legend align=\"top\">"+label+": "+exe+"ms</legend></fieldset>");
086                    }
087                    else if(TYPE_COMMENT==type) {
088                            pageContext.write("<!-- "+label+": "+exe+"ms -->");
089                    }
090                    else if(TYPE_DEBUG==type) {
091    
092                pageContext.getDebugger().addTimer(label,exe,pageContext.getCurrentTemplatePageSource().getDisplayPath());
093                    }
094                    
095    
096                    /*<legend align='top'>aaa</legend></fieldset>*/
097                    
098            }
099            
100            /**
101            * @see javax.servlet.jsp.tagext.BodyTag#doInitBody()
102            */
103            public void doInitBody()        {
104                    
105            }
106    
107            /**
108            * @see javax.servlet.jsp.tagext.BodyTag#doAfterBody()
109            */
110            public int doAfterBody()        {
111                    return SKIP_BODY;
112            }
113    
114    }