001    package railo.runtime.dump;
002    
003    import java.awt.Color;
004    import java.io.IOException;
005    import java.io.StringWriter;
006    import java.io.Writer;
007    
008    import railo.commons.color.ColorCaster;
009    import railo.commons.lang.StringUtil;
010    import railo.runtime.PageContext;
011    import railo.runtime.engine.ThreadLocalPageContext;
012    import railo.runtime.exp.ExpressionException;
013    import railo.runtime.functions.other.Dump;
014    
015    public class ClassicHTMLDumpWriter implements DumpWriter {
016    
017            
018            private static int count=0;
019    
020            public void writeOut(PageContext pc,DumpData data, Writer writer, boolean expand) throws IOException {
021                    writeOut(pc,data, writer, expand, false);
022            }
023            private void writeOut(PageContext pc,DumpData data, Writer writer, boolean expand, boolean inside) throws IOException {
024                    
025                    if(data==null) return;
026                    if(!(data instanceof DumpTable)) {
027                            writer.write(StringUtil.escapeHTML(data.toString()));
028                            return;
029                    }
030                    DumpTable table=(DumpTable) data;
031                    
032                    if(table==null) return;
033    
034                    String borderColor=table.getHighLightColor();
035                    String h1Color=table.getHighLightColor();
036                    String h2Color=table.getNormalColor();
037                    String normalColor="white";
038                    try {
039                            Color color = ColorCaster.toColor(table.getHighLightColor());
040                            borderColor=ColorCaster.toHexString(color.darker().darker());
041                    } 
042                    catch (ExpressionException e) {}
043                    
044                    
045                    String id="_classic"+(count++);
046                    // prepare data
047                    DumpRow[] rows = table.getRows();
048                    int cols=0;
049                    for(int i=0;i<rows.length;i++)if(rows[i].getItems().length>cols)cols=rows[i].getItems().length;
050                    
051                    if(!inside) {
052                            writer.write("<script>");
053                            writer.write("function dumpOC(name){");
054                            writer.write("var tds=document.all?document.getElementsByTagName('tr'):document.getElementsByName('_'+name);");
055                            writer.write("var s=null;");
056                            writer.write("name='_'+name;");
057                            writer.write("for(var i=0;i<tds.length;i++) {");
058                            writer.write("if(document.all && tds[i].name!=name)continue;");
059                            writer.write("s=tds[i].style;");
060                            writer.write("if(s.display=='none') s.display='';");
061                            writer.write("else s.display='none';");
062                            writer.write("}");
063                            writer.write("}");
064                            writer.write("</script>");
065                    }
066                    
067                    String context="";
068                    if(!inside) context=Dump.getContext();
069                    if(context==null) context="";
070                    //boolean isSetContext=false;
071                    
072                    writer.write("<table"+(table.getWidth()!=null?" width=\""+table.getWidth()+"\"":"")+""+(table.getHeight()!=null?" height=\""+table.getHeight()+"\"":"")+" cellpadding=\"3\" cellspacing=\"0\" style=\"font-family : Verdana, Geneva, Arial, Helvetica, sans-serif;font-size : 10;color :"+table.getFontColor()+" ;empty-cells:show; border : 1px solid "+borderColor+";\" >");
073                    
074                    // header
075                    if(!StringUtil.isEmpty(table.getTitle())) {
076                            writer.write("<tr><td title=\""+context+"\" onclick=\"dumpOC('"+id+"')\" colspan=\""+cols+"\" bgcolor=\""+h1Color+"\" style=\"color:white;border : 1px solid "+borderColor+";\">");
077                            //isSetContext=true;
078                            String contextPath="";
079                            pc = ThreadLocalPageContext.get(pc);
080                            if(pc!=null){
081                                    contextPath=pc. getHttpServletRequest().getContextPath();
082                                    if(contextPath==null)contextPath="";
083                            }
084                            
085                            
086                            //writer.write("<img src=\""+contextPath+"/railo-context/admin/resources/img/debug_minus.gif.cfm\" name=\"__btn"+id+"\"/>");
087                            
088                            writer.write("<span style=\"font-weight:bold;\">"+
089                                            (!StringUtil.isEmpty(table.getTitle())?table.getTitle():"")+"</span>"+(!StringUtil.isEmpty(table.getComment())?"<br>"+table.getComment():"")+
090                    "</td></tr>");
091                    }
092                    else id=null;
093                    
094                    // items
095                    DumpData value;
096                    for(int i=0;i<rows.length;i++) {
097                            if(id!=null)writer.write("<tr name=\"_"+id+"\">");
098                            else writer.write("<tr>");
099                            DumpData[] items=rows[i].getItems();
100                            int hType=rows[i].getHighlightType();
101                            int comperator=1;
102                            for(int y=0;y<cols;y++) {
103                                    if(y<=items.length-1) value=items[y];
104                                    else value=new SimpleDumpData("&nbsp;");
105                                    boolean highLightIt=hType==-1 || ((hType&(comperator))>0);
106                                    comperator*=2;
107                                    if(value==null)value=new SimpleDumpData("null");
108                                    //else if(value.equals(""))value="&nbsp;";
109                                    if(!inside){
110                                            writer.write("<td valign=\"top\" style=\"border : 1px solid "+borderColor+";\" title=\""+context+"\"");
111                                    }
112                                    else writer.write("<td valign=\"top\"");
113                                    writer.write(" bgcolor=\""+((highLightIt)?h2Color:normalColor)+"\" style=\"border : 1px solid "+borderColor+";empty-cells:show;\">");
114                                    writeOut(pc,value, writer,expand,true);
115                                    writer.write("</td>");
116                            }
117                            writer.write("</tr>");
118                    }
119                    
120                    // footer
121                    writer.write("</table>");
122                    if(!expand)writer.write("<script>dumpOC('"+id+"');</script>");
123            }
124    
125            /**
126             * @see railo.runtime.dump.DumpWriter#toString(railo.runtime.dump.DumpData)
127             */
128            public String toString(PageContext pc,DumpData data, boolean expand) {
129                    StringWriter sw=new StringWriter();
130                    try {
131                            writeOut(pc,data, sw,expand);
132                    } 
133                    catch (IOException e) {
134                            return "";
135                    }
136                    return sw.toString();
137            }
138    
139    }