001    package railo.runtime.dump;
002    
003    import java.io.IOException;
004    import java.io.StringWriter;
005    import java.io.Writer;
006    
007    import railo.commons.lang.StringUtil;
008    import railo.runtime.PageContext;
009    import railo.runtime.engine.ThreadLocalPageContext;
010    
011    public class TextDumpWriter implements DumpWriter {
012    
013            //private static int count=0;
014    
015            public void writeOut(PageContext pc,DumpData data, Writer writer, boolean expand) throws IOException {
016                    writeOut(pc,data, writer, expand, 0);
017            }
018            private void writeOut(PageContext pc,DumpData data, Writer writer, boolean expand, int level) throws IOException {
019                    
020                    if(data==null) return;
021                    if(!(data instanceof DumpTable)) {
022                            writer.write(StringUtil.escapeHTML(data.toString()));
023                            return;
024                    }
025                    DumpTable table=(DumpTable) data;
026                    
027                    if(table==null) return;
028                    
029                    
030                    DumpRow[] rows = table.getRows();
031                    int cols=0;
032                    for(int i=0;i<rows.length;i++)if(rows[i].getItems().length>cols)cols=rows[i].getItems().length;
033                    
034                    
035                    // header
036                    if(!StringUtil.isEmpty(table.getTitle(),true)) {
037                            
038                            String contextPath="";
039                            pc = ThreadLocalPageContext.get(pc);
040                            if(pc!=null){
041                                    contextPath=pc. getHttpServletRequest().getContextPath();
042                                    if(contextPath==null)contextPath="";
043                            }
044                            String header=table.getTitle()+ (StringUtil.isEmpty(table.getComment())?"":"\n"+table.getComment());
045                            writer.write(header+"\n");
046                            if(level>0)writer.write(StringUtil.repeatString("    ", level));
047                    }
048                    
049                    // items
050                    DumpData value;
051                    for(int i=0;i<rows.length;i++) {
052                            DumpData[] items=rows[i].getItems();
053                            int comperator=1;
054                            for(int y=0;y<cols;y++) {
055                                    if(y<=items.length-1) value=items[y];
056                                    else value=new SimpleDumpData("");
057                                    comperator*=2;
058                                    if(value==null)value=new SimpleDumpData("null");
059                                    writeOut(pc,value, writer,expand,level+1);
060                                    writer.write(" ");
061                            }
062                            writer.write("\n");
063                            if(level>0)writer.write(StringUtil.repeatString("    ", level));
064                    }
065            }
066    
067            /**
068             * @see railo.runtime.dump.DumpWriter#toString(railo.runtime.dump.DumpData)
069             */
070            public String toString(PageContext pc,DumpData data, boolean expand) {
071                    StringWriter sw=new StringWriter();
072                    try {
073                            writeOut(pc,data, sw,expand);
074                    } 
075                    catch (IOException e) {
076                            return "";
077                    }
078                    return sw.toString();
079            }
080    
081    }