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