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.awt.Color;
022import java.io.IOException;
023import java.io.StringWriter;
024import java.io.Writer;
025
026import lucee.commons.color.ColorCaster;
027import lucee.commons.io.SystemUtil;
028import lucee.commons.io.SystemUtil.TemplateLine;
029import lucee.commons.lang.StringUtil;
030import lucee.runtime.PageContext;
031import lucee.runtime.engine.ThreadLocalPageContext;
032import lucee.runtime.exp.ExpressionException;
033
034public class ClassicHTMLDumpWriter implements DumpWriter {
035
036        
037        private static int count=0;
038
039        public void writeOut(PageContext pc,DumpData data, Writer writer, boolean expand) throws IOException {
040                writeOut(pc,data, writer, expand, false);
041        }
042        private void writeOut(PageContext pc,DumpData data, Writer writer, boolean expand, boolean inside) throws IOException {
043                
044                if(data==null) return;
045                if(!(data instanceof DumpTable)) {
046                        writer.write(StringUtil.escapeHTML(data.toString()));
047                        return;
048                }
049                DumpTable table=(DumpTable) data;
050                
051                String borderColor=table.getHighLightColor();
052                String h1Color=table.getHighLightColor();
053                String h2Color=table.getNormalColor();
054                String normalColor="white";
055                try {
056                        Color color = ColorCaster.toColor(table.getHighLightColor());
057                        borderColor=ColorCaster.toHexString(color.darker().darker());
058                } 
059                catch (ExpressionException e) {}
060                
061                
062                String id="_classic"+(count++);
063                // prepare data
064                DumpRow[] rows = table.getRows();
065                int cols=0;
066                for(int i=0;i<rows.length;i++)if(rows[i].getItems().length>cols)cols=rows[i].getItems().length;
067                
068                if(!inside) {
069                        writer.write("<script>");
070                        writer.write("function dumpOC(name){");
071                        writer.write("var tds=document.all?document.getElementsByTagName('tr'):document.getElementsByName('_'+name);");
072                        writer.write("var s=null;");
073                        writer.write("name='_'+name;");
074                        writer.write("for(var i=0;i<tds.length;i++) {");
075                        writer.write("if(document.all && tds[i].name!=name)continue;");
076                        writer.write("s=tds[i].style;");
077                        writer.write("if(s.display=='none') s.display='';");
078                        writer.write("else s.display='none';");
079                        writer.write("}");
080                        writer.write("}");
081                        writer.write("</script>");
082                }
083                
084
085                TemplateLine tl=null;
086                if(!inside) tl=SystemUtil.getCurrentContext();
087                String context=tl==null?"":tl.toString();
088                
089                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+";\" >");
090                
091                // header
092                if(!StringUtil.isEmpty(table.getTitle())) {
093                        writer.write("<tr><td title=\""+context+"\" onclick=\"dumpOC('"+id+"')\" colspan=\""+cols+"\" bgcolor=\""+h1Color+"\" style=\"color:white;border : 1px solid "+borderColor+";\">");
094                        //isSetContext=true;
095                        String contextPath="";
096                        pc = ThreadLocalPageContext.get(pc);
097                        if(pc!=null){
098                                contextPath=pc. getHttpServletRequest().getContextPath();
099                                if(contextPath==null)contextPath="";
100                        }
101                        writer.write("<span style=\"font-weight:bold;\">"+
102                                        (!StringUtil.isEmpty(table.getTitle())?table.getTitle():"")+"</span>"+(!StringUtil.isEmpty(table.getComment())?"<br>"+table.getComment():"")+
103                "</td></tr>");
104                }
105                else id=null;
106                
107                // items
108                DumpData value;
109                for(int i=0;i<rows.length;i++) {
110                        if(id!=null)writer.write("<tr name=\"_"+id+"\">");
111                        else writer.write("<tr>");
112                        DumpData[] items=rows[i].getItems();
113                        int hType=rows[i].getHighlightType();
114                        int comperator=1;
115                        for(int y=0;y<cols;y++) {
116                                if(y<=items.length-1) value=items[y];
117                                else value=new SimpleDumpData("&nbsp;");
118                                boolean highLightIt=hType==-1 || ((hType&(comperator))>0);
119                                comperator*=2;
120                                if(value==null)value=new SimpleDumpData("null");
121                                //else if(value.equals(""))value="&nbsp;";
122                                if(!inside){
123                                        writer.write("<td valign=\"top\" style=\"border : 1px solid "+borderColor+";\" title=\""+context+"\"");
124                                }
125                                else writer.write("<td valign=\"top\"");
126                                writer.write(" bgcolor=\""+((highLightIt)?h2Color:normalColor)+"\" style=\"border : 1px solid "+borderColor+";empty-cells:show;\">");
127                                writeOut(pc,value, writer,expand,true);
128                                writer.write("</td>");
129                        }
130                        writer.write("</tr>");
131                }
132                
133                // footer
134                writer.write("</table>");
135                if(!expand)writer.write("<script>dumpOC('"+id+"');</script>");
136        }
137
138        @Override
139        public String toString(PageContext pc,DumpData data, boolean expand) {
140                StringWriter sw=new StringWriter();
141                try {
142                        writeOut(pc,data, sw,expand);
143                } 
144                catch (IOException e) {
145                        return "";
146                }
147                return sw.toString();
148        }
149
150}