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