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            @Override
020            public void release() {
021                    super.release();
022                    type=TYPE_DEBUG;
023                    label="";
024            }
025    
026            /**
027             * @param label the label to set
028             */
029            public void setLabel(String label) {
030                    this.label = label;
031            }
032    
033            /**
034             * @param type the type to set
035             * @throws ApplicationException 
036             */
037            public void setType(String strType) throws ApplicationException {
038                    strType = strType.toLowerCase().trim();
039                    if("comment".equals(strType)) type=TYPE_COMMENT;
040                    else if("debug".equals(strType)) type=TYPE_DEBUG;
041                    else if("inline".equals(strType)) type=TYPE_INLINE;
042                    else if("outline".equals(strType)) type=TYPE_OUTLINE;
043                    else throw new ApplicationException("invalid value ["+strType+"] for attribute [type], valid values are [comment,debug,inline,outline]");
044            }
045    
046            @Override
047            public int doStartTag() {
048                    time=System.currentTimeMillis();
049                    if(TYPE_OUTLINE==type) {
050                            try {
051                                    pageContext.write("<fieldset class=\"cftimer\">");
052                            } 
053                            catch (IOException e) {}
054                    }
055                    return EVAL_BODY_INCLUDE;
056            }
057            
058            
059            @Override
060            public int doEndTag() throws PageException {
061                    try {
062                            _doEndTag();
063                    }
064                    catch (IOException e) {}
065                    return EVAL_PAGE;
066            }
067            
068            public void _doEndTag() throws IOException {
069                    long exe = (System.currentTimeMillis()-time);
070                    if(TYPE_INLINE==type) {
071                            pageContext.write(""+label+": "+exe+"ms");
072                    }
073                    else if(TYPE_OUTLINE==type) {
074                            pageContext.write("<legend align=\"top\">"+label+": "+exe+"ms</legend></fieldset>");
075                    }
076                    else if(TYPE_COMMENT==type) {
077                            pageContext.write("<!-- "+label+": "+exe+"ms -->");
078                    }
079                    else if(TYPE_DEBUG==type) {
080    
081                pageContext.getDebugger().addTimer(label,exe,pageContext.getCurrentTemplatePageSource().getDisplayPath());
082                    }
083                    
084    
085                    /*<legend align='top'>aaa</legend></fieldset>*/
086                    
087            }
088    
089            @Override
090            public void doInitBody()        {
091                    
092            }
093    
094            @Override
095            public int doAfterBody()        {
096                    return SKIP_BODY;
097            }
098    
099    }