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.dump;
020
021import java.io.IOException;
022import java.io.StringWriter;
023import java.io.Writer;
024
025import lucee.commons.lang.StringUtil;
026import lucee.runtime.PageContext;
027import lucee.runtime.engine.ThreadLocalPageContext;
028
029public class TextDumpWriter implements DumpWriter {
030
031        //private static int count=0;
032
033        public void writeOut(PageContext pc,DumpData data, Writer writer, boolean expand) throws IOException {
034                writeOut(pc,data, writer, expand, 0);
035        }
036        private void writeOut(PageContext pc,DumpData data, Writer writer, boolean expand, int level) throws IOException {
037                
038                if(data==null) return;
039                if(!(data instanceof DumpTable)) {
040                        writer.write(StringUtil.escapeHTML(data.toString()));
041                        return;
042                }
043                DumpTable table=(DumpTable) data;
044                
045                DumpRow[] rows = table.getRows();
046                int cols=0;
047                for(int i=0;i<rows.length;i++)if(rows[i].getItems().length>cols)cols=rows[i].getItems().length;
048                
049                
050                // header
051                if(!StringUtil.isEmpty(table.getTitle(),true)) {
052                        
053                        String contextPath="";
054                        pc = ThreadLocalPageContext.get(pc);
055                        if(pc!=null){
056                                contextPath=pc. getHttpServletRequest().getContextPath();
057                                if(contextPath==null)contextPath="";
058                        }
059                        String header=table.getTitle()+ (StringUtil.isEmpty(table.getComment())?"":"\n"+table.getComment());
060                        writer.write(header+"\n");
061                        if(level>0)writer.write(StringUtil.repeatString("    ", level));
062                }
063                
064                // items
065                DumpData value;
066                for(int i=0;i<rows.length;i++) {
067                        DumpData[] items=rows[i].getItems();
068                        //int comperator=1;
069                        for(int y=0;y<cols;y++) {
070                                if(y<=items.length-1) value=items[y];
071                                else value=new SimpleDumpData("");
072                                //comperator*=2;
073                                if(value==null)value=new SimpleDumpData("null");
074                                writeOut(pc,value, writer,expand,level+1);
075                                writer.write(" ");
076                        }
077                        writer.write("\n");
078                        if(level>0)writer.write(StringUtil.repeatString("    ", level));
079                }
080        }
081
082        @Override
083        public String toString(PageContext pc,DumpData data, boolean expand) {
084                StringWriter sw=new StringWriter();
085                try {
086                        writeOut(pc,data, sw,expand);
087                } 
088                catch (IOException e) {
089                        return "";
090                }
091                return sw.toString();
092        }
093
094}