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