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 com.allaire.cfx;
020
021import java.util.Enumeration;
022import java.util.Hashtable;
023
024import lucee.loader.engine.CFMLEngineFactory;
025import lucee.runtime.cfx.QueryWrap;
026
027
028
029/**
030 * 
031 */
032public final class DebugResponse implements Response {
033        
034        private StringBuffer write=new StringBuffer();
035        private StringBuffer writeDebug=new StringBuffer();
036        private Hashtable variables=new Hashtable();
037        private Hashtable queries=new Hashtable();
038        
039        
040        /**
041         * @see com.allaire.cfx.Response#addQuery(java.lang.String, java.lang.String[])
042         */
043        public Query addQuery(String name, String[] columns) {
044                QueryWrap query = new QueryWrap(CFMLEngineFactory.getInstance().getCreationUtil().createQuery(columns,0,name),name.toLowerCase());
045                queries.put(name.toLowerCase(),query);
046                return query;
047        }
048
049    /**
050         * @see com.allaire.cfx.Response#setVariable(java.lang.String, java.lang.String)
051         */
052        public void setVariable(String key, String value) {
053                variables.put(key.toLowerCase(),value);
054        }
055
056        /**
057         * @see com.allaire.cfx.Response#write(java.lang.String)
058         */
059        public void write(String str) {
060                write.append(str);
061        }
062
063        /**
064         * @see com.allaire.cfx.Response#writeDebug(java.lang.String)
065         */
066        public void writeDebug(String str) {
067                writeDebug.append(str);
068        }
069        
070        /**
071         *  print out the response
072         */
073        public void printResults() {
074                System.out.println("[ --- Lucee Debug Response --- ]");
075                System.out.println();
076                
077                System.out.println("----------------------------");
078                System.out.println("|          Output          |");
079                System.out.println("----------------------------");
080                System.out.println(write);
081                System.out.println();
082                
083                System.out.println("----------------------------");
084                System.out.println("|       Debug Output       |");
085                System.out.println("----------------------------");
086                System.out.println(writeDebug);
087                System.out.println();
088                
089                System.out.println("----------------------------");
090                System.out.println("|        Variables         |");
091                System.out.println("----------------------------");
092                
093                Enumeration e = variables.keys();
094                while(e.hasMoreElements()) {
095                        Object key=e.nextElement();
096                        System.out.println("[Variable:"+key+"]");
097                        System.out.println(escapeString(variables.get(key).toString()));
098                }
099                System.out.println();
100                
101                e = queries.keys();
102                while(e.hasMoreElements()) {
103                        Query query=(Query) queries.get(e.nextElement());
104                        printQuery(query);
105                        System.out.println();
106                }
107                
108        }
109        
110        /**
111         * print out a query
112         * @param query
113         */
114        public void printQuery(Query query) {
115                if(query!=null) {
116                        String[] cols = query.getColumns();
117                        int rows = query.getRowCount();
118                        System.out.println("[Query:"+query.getName()+"]");
119                        for(int i=0;i<cols.length;i++) {
120                                if(i>0)System.out.print(", ");
121                                System.out.print(cols[i]);
122                        }
123                        System.out.println();
124                        
125                        for(int row=1;row<=rows;row++) {
126                                for(int col=1;col<=cols.length;col++) {
127                                        if(col>1)System.out.print(", ");
128                                        System.out.print(escapeString(query.getData(row,col)));
129                                }
130                                System.out.println();
131                        }
132                }
133        }
134        
135        private String escapeString(String string) {
136                int len=string.length();
137                StringBuffer sb=new StringBuffer(len);
138                for(int i=0;i<len;i++) {
139                        char c=string.charAt(i);
140                        if(c=='\n')sb.append("\\n");
141                        else if(c=='\t')sb.append("\\t");
142                        else if(c=='\\')sb.append("\\\\");
143                        else if(c=='\b')sb.append("\\b");
144                        else if(c=='\r')sb.append("\\r");
145                        else if(c=='\"')sb.append("\\\"");
146                        else sb.append(c);
147                }
148                
149                
150                
151                return "\""+sb.toString()+"\"";
152        }
153
154        
155}