001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.dump;
020
021import java.io.IOException;
022import java.io.StringWriter;
023import java.io.Writer;
024
025import lucee.commons.io.SystemUtil;
026import lucee.commons.io.SystemUtil.TemplateLine;
027import lucee.commons.lang.StringUtil;
028import lucee.runtime.PageContext;
029import lucee.runtime.engine.ThreadLocalPageContext;
030
031public class HTMLDumpWriter implements DumpWriter {
032
033        
034        private static int count=0;
035
036        public void writeOut(PageContext pc,DumpData data, Writer writer, boolean expand) throws IOException {
037                writeOut(pc,data, writer, expand, false);
038        }
039        private void writeOut(PageContext pc,DumpData data, Writer writer, boolean expand, boolean inside) throws IOException {
040                
041                if(data==null) return;
042                if(!(data instanceof DumpTable)) {
043                        writer.write(StringUtil.escapeHTML(data.toString()));
044                        return;
045                }
046                DumpTable table=(DumpTable) data;
047                
048                String id="_dump"+(count++);
049                // prepare data
050                DumpRow[] rows = table.getRows();
051                int cols=0;
052                for(int i=0;i<rows.length;i++)if(rows[i].getItems().length>cols)cols=rows[i].getItems().length;
053                
054                if(!inside) {
055                        writer.write("<script>");
056                        writer.write("function dumpOC(name){");
057                        writer.write("var tds=document.all?document.getElementsByTagName('tr'):document.getElementsByName('_'+name);");
058                        //writer.write("var button=document.images['__btn'+name];");
059                        writer.write("var s=null;");
060                        //writer.write("if(button.src.indexOf('plus')==-1) button.src=button.src.replace('minus','plus');");
061                        //writer.write("else button.src=button.src.replace('plus','minus');");
062                        writer.write("name='_'+name;");
063                        writer.write("for(var i=0;i<tds.length;i++) {");
064                        writer.write("if(document.all && tds[i].name!=name)continue;");
065                        writer.write("s=tds[i].style;");
066                        writer.write("if(s.display=='none') s.display='';");
067                        writer.write("else s.display='none';");
068                        writer.write("}");
069                        writer.write("}");
070                        writer.write("</script>");
071                        
072                }
073                
074                TemplateLine tl=null;
075                if(!inside) tl=SystemUtil.getCurrentContext();
076                String context=tl==null?"":tl.toString();
077                
078                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;\">");
079                
080                // header
081                if(!StringUtil.isEmpty(table.getTitle())) {
082                        writer.write("<tr><td title=\""+context+"\" onclick=\"dumpOC('"+id+"')\" colspan=\""+cols+"\" bgcolor=\""+table.getHighLightColor()+"\" style=\"border : 1px solid "+table.getBorderColor()+"; empty-cells:show;\">");
083                        //isSetContext=true;
084                        String contextPath="";
085                        pc = ThreadLocalPageContext.get(pc);
086                        if(pc!=null){
087                                contextPath=pc. getHttpServletRequest().getContextPath();
088                                if(contextPath==null)contextPath="";
089                        }
090                        writer.write("<span style=\"font-weight:bold;\">"+
091                                        (!StringUtil.isEmpty(table.getTitle())?table.getTitle():"")+"</span>"+(!StringUtil.isEmpty(table.getComment())?"<br>"+table.getComment():"")+
092                "</td></tr>");
093                }
094                else id=null;
095                
096                // items
097                DumpData value;
098                for(int i=0;i<rows.length;i++) {
099                        if(id!=null)writer.write("<tr name=\"_"+id+"\">");
100                        else writer.write("<tr>");
101                        
102                        DumpData[] items=rows[i].getItems();
103                        int hType=rows[i].getHighlightType();
104                        int comperator=1;
105                        for(int y=0;y<cols;y++) {
106                                if(y<=items.length-1) value=items[y];
107                                else value=new SimpleDumpData("&nbsp;");
108                                boolean highLightIt=hType==-1 || ((hType&(comperator))>0);
109                                comperator*=2;
110                                if(value==null)value=new SimpleDumpData("null");
111                                //else if(value.equals(""))value="&nbsp;";
112                                if(!inside){
113                                        writer.write("<td valign=\"top\" title=\""+context+"\"");
114                                }
115                                else writer.write("<td valign=\"top\"");
116                                writer.write(" bgcolor=\""+((highLightIt)?table.getHighLightColor():table.getNormalColor())+"\" style=\"border : 1px solid "+table.getBorderColor()+";empty-cells:show;\">");
117                                writeOut(pc,value, writer,expand,true);
118                                writer.write("</td>");
119                        }
120                        writer.write("</tr>");
121                }
122                
123                // footer
124                writer.write("</table>");
125                if(!expand)writer.write("<script>dumpOC('"+id+"');</script>");
126        }
127
128        @Override
129        public String toString(PageContext pc,DumpData data, boolean expand) {
130                StringWriter sw=new StringWriter();
131                try {
132                        writeOut(pc,data, sw,expand);
133                } 
134                catch (IOException e) {
135                        return "";
136                }
137                return sw.toString();
138        }
139
140}