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 HTMLDumpWriter 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                    String id="_dump"+(count++);
031                    // prepare data
032                    DumpRow[] rows = table.getRows();
033                    int cols=0;
034                    for(int i=0;i<rows.length;i++)if(rows[i].getItems().length>cols)cols=rows[i].getItems().length;
035                    
036                    if(!inside) {
037                            writer.write("<script>");
038                            writer.write("function dumpOC(name){");
039                            writer.write("var tds=document.all?document.getElementsByTagName('tr'):document.getElementsByName('_'+name);");
040                            //writer.write("var button=document.images['__btn'+name];");
041                            writer.write("var s=null;");
042                            //writer.write("if(button.src.indexOf('plus')==-1) button.src=button.src.replace('minus','plus');");
043                            //writer.write("else button.src=button.src.replace('plus','minus');");
044                            writer.write("name='_'+name;");
045                            writer.write("for(var i=0;i<tds.length;i++) {");
046                            writer.write("if(document.all && tds[i].name!=name)continue;");
047                            writer.write("s=tds[i].style;");
048                            writer.write("if(s.display=='none') s.display='';");
049                            writer.write("else s.display='none';");
050                            writer.write("}");
051                            writer.write("}");
052                            writer.write("</script>");
053                            
054                    }
055                    
056                    String context="";
057                    if(!inside) context=Dump.getContext();
058                    if(context==null) context="";
059                    //boolean isSetContext=false;
060                    
061                    writer.write("<table"+(table.getWidth()!=null?" width=\""+table.getWidth()+"\"":"")+""+(table.getHeight()!=null?" height=\""+table.getHeight()+"\"":"")+" cellpadding=\"3\" cellspacing=\"1\" style=\"font-family : Verdana, Geneva, Arial, Helvetica, sans-serif;font-size : 11px;color :"+table.getFontColor()+" ;empty-cells:show;\">");
062                    
063                    // header
064                    if(!StringUtil.isEmpty(table.getTitle())) {
065                            writer.write("<tr><td title=\""+context+"\" onclick=\"dumpOC('"+id+"')\" colspan=\""+cols+"\" bgcolor=\""+table.getHighLightColor()+"\" style=\"border : 1px solid "+table.getBorderColor()+"; empty-cells:show;\">");
066                            //isSetContext=true;
067                            String contextPath="";
068                            pc = ThreadLocalPageContext.get(pc);
069                            if(pc!=null){
070                                    contextPath=pc. getHttpServletRequest().getContextPath();
071                                    if(contextPath==null)contextPath="";
072                            }
073                            
074                            
075                            //writer.write("<img src=\""+contextPath+"/railo-context/admin/resources/img/debug_minus.gif.cfm\" style=\"margin:2px 2px 0px 0px;\" name=\"__btn"+id+"\"/>");
076                            
077                            
078                            writer.write("<span style=\"font-weight:bold;\">"+
079                                            (!StringUtil.isEmpty(table.getTitle())?table.getTitle():"")+"</span>"+(!StringUtil.isEmpty(table.getComment())?"<br>"+table.getComment():"")+
080                    "</td></tr>");
081                    }
082                    else id=null;
083                    
084                    // items
085                    DumpData value;
086                    for(int i=0;i<rows.length;i++) {
087                            if(id!=null)writer.write("<tr name=\"_"+id+"\">");
088                            else writer.write("<tr>");
089                            
090                            DumpData[] items=rows[i].getItems();
091                            int hType=rows[i].getHighlightType();
092                            int comperator=1;
093                            for(int y=0;y<cols;y++) {
094                                    if(y<=items.length-1) value=items[y];
095                                    else value=new SimpleDumpData("&nbsp;");
096                                    boolean highLightIt=hType==-1 || ((hType&(comperator))>0);
097                                    comperator*=2;
098                                    if(value==null)value=new SimpleDumpData("null");
099                                    //else if(value.equals(""))value="&nbsp;";
100                                    if(!inside){
101                                            writer.write("<td valign=\"top\" title=\""+context+"\"");
102                                    }
103                                    else writer.write("<td valign=\"top\"");
104                                    writer.write(" bgcolor=\""+((highLightIt)?table.getHighLightColor():table.getNormalColor())+"\" style=\"border : 1px solid "+table.getBorderColor()+";empty-cells:show;\">");
105                                    writeOut(pc,value, writer,expand,true);
106                                    writer.write("</td>");
107                            }
108                            writer.write("</tr>");
109                    }
110                    
111                    // footer
112                    writer.write("</table>");
113                    if(!expand)writer.write("<script>dumpOC('"+id+"');</script>");
114            }
115    
116            /**
117             * @see railo.runtime.dump.DumpWriter#toString(railo.runtime.dump.DumpData)
118             */
119            public String toString(PageContext pc,DumpData data, boolean expand) {
120                    StringWriter sw=new StringWriter();
121                    try {
122                            writeOut(pc,data, sw,expand);
123                    } 
124                    catch (IOException e) {
125                            return "";
126                    }
127                    return sw.toString();
128            }
129    
130    }