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    import railo.runtime.functions.other.Dump;
011    
012    public class SimpleHTMLDumpWriter implements DumpWriter {
013    
014            
015            private static int count=0;
016    
017            public void writeOut(PageContext pc,DumpData data, Writer writer, boolean expand) throws IOException {
018                    writeOut(pc,data, writer, expand, false);
019            }
020            private void writeOut(PageContext pc,DumpData data, Writer writer, boolean expand, boolean inside) throws IOException {
021                    
022                    if(data==null) return;
023                    if(!(data instanceof DumpTable)) {
024                            writer.write(StringUtil.escapeHTML(data.toString()));
025                            return;
026                    }
027                    DumpTable table=(DumpTable) data;
028                                    
029                    // prepare data
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                    
036                    
037                    String context="";
038                    if(!inside) context=Dump.getContext();
039                    if(context==null) context="";
040                    //boolean isSetContext=false;
041                    
042                    if(rows.length==1 && rows[0].getItems().length==2){
043                            DumpData d = rows[0].getItems()[1];
044                            if(!(d instanceof DumpTable)){
045                                    writer.write(StringUtil.escapeHTML(d.toString()));
046                                    return;
047                            }
048                    }
049                    
050                    writer.write("<table  cellpadding=\"1\" cellspacing=\"0\" "+(table.getWidth()!=null?" width=\""+table.getWidth()+"\"":"")+""+(table.getHeight()!=null?" height=\""+table.getHeight()+"\"":"")+" border=\"1\">");
051                    
052                    // header
053                    if(!StringUtil.isEmpty(table.getTitle())) {
054                            writer.write("<tr><td title=\""+context+"\" colspan=\""+cols+"\">");
055                            //isSetContext=true;
056                            String contextPath="";
057                            pc = ThreadLocalPageContext.get(pc);
058                            if(pc!=null){
059                                    contextPath=pc. getHttpServletRequest().getContextPath();
060                                    if(contextPath==null)contextPath="";
061                            }
062                            
063                            
064                            //writer.write("<img src=\""+contextPath+"/railo-context/admin/resources/img/debug_minus.gif.cfm\" name=\"__btn"+id+"\"/>");
065                            
066                            writer.write("<b>"+
067                                            (!StringUtil.isEmpty(table.getTitle())?table.getTitle():"")+"</b>"+(!StringUtil.isEmpty(table.getComment())?"<br>"+table.getComment():"")+
068                    "</td></tr>");
069                    }
070                    
071                    
072                    
073                    
074                    // items
075                    DumpData value;
076                    for(int i=0;i<rows.length;i++) {
077                            writer.write("<tr>");
078                            DumpData[] items=rows[i].getItems();
079                            //int comperator=1;
080                            for(int y=0;y<cols;y++) {
081                                    if(y<=items.length-1) value=items[y];
082                                    else value=new SimpleDumpData("&nbsp;");
083                                    //comperator*=2;
084                                    if(value==null)value=new SimpleDumpData("null");
085                                    //else if(value.equals(""))value="&nbsp;";
086                                    if(!inside){
087                                            writer.write("<td title=\""+context+"\">");
088                                    }
089                                    else 
090                                            writer.write("<td>");
091                                    writeOut(pc,value, writer,expand,true);
092                                    writer.write("</td>");
093                            }
094                            writer.write("</tr>");
095                    }
096                    
097                    // footer
098                    writer.write("</table>");
099            }
100    
101            @Override
102            public String toString(PageContext pc,DumpData data, boolean expand) {
103                    StringWriter sw=new StringWriter();
104                    try {
105                            writeOut(pc,data, sw,expand);
106                    } 
107                    catch (IOException e) {
108                            return "";
109                    }
110                    return sw.toString();
111            }
112    
113    }