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 SimpleHTMLDumpWriter 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 // prepare data 049 DumpRow[] rows = table.getRows(); 050 int cols=0; 051 for(int i=0;i<rows.length;i++)if(rows[i].getItems().length>cols)cols=rows[i].getItems().length; 052 053 054 TemplateLine tl=null; 055 if(!inside) tl=SystemUtil.getCurrentContext(); 056 String context=tl==null?"":tl.toString(); 057 058 if(rows.length==1 && rows[0].getItems().length==2){ 059 DumpData d = rows[0].getItems()[1]; 060 if(!(d instanceof DumpTable)){ 061 writer.write(StringUtil.escapeHTML(d.toString())); 062 return; 063 } 064 } 065 066 writer.write("<table cellpadding=\"1\" cellspacing=\"0\" "+(table.getWidth()!=null?" width=\""+table.getWidth()+"\"":"")+""+(table.getHeight()!=null?" height=\""+table.getHeight()+"\"":"")+" border=\"1\">"); 067 068 // header 069 if(!StringUtil.isEmpty(table.getTitle())) { 070 writer.write("<tr><td title=\""+context+"\" colspan=\""+cols+"\">"); 071 //isSetContext=true; 072 String contextPath=""; 073 pc = ThreadLocalPageContext.get(pc); 074 if(pc!=null){ 075 contextPath=pc. getHttpServletRequest().getContextPath(); 076 if(contextPath==null)contextPath=""; 077 } 078 writer.write("<b>"+ 079 (!StringUtil.isEmpty(table.getTitle())?table.getTitle():"")+"</b>"+(!StringUtil.isEmpty(table.getComment())?"<br>"+table.getComment():"")+ 080 "</td></tr>"); 081 } 082 083 084 085 086 // items 087 DumpData value; 088 for(int i=0;i<rows.length;i++) { 089 writer.write("<tr>"); 090 DumpData[] items=rows[i].getItems(); 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(" "); 095 //comperator*=2; 096 if(value==null)value=new SimpleDumpData("null"); 097 //else if(value.equals(""))value=" "; 098 if(!inside){ 099 writer.write("<td title=\""+context+"\">"); 100 } 101 else 102 writer.write("<td>"); 103 writeOut(pc,value, writer,expand,true); 104 writer.write("</td>"); 105 } 106 writer.write("</tr>"); 107 } 108 109 // footer 110 writer.write("</table>"); 111 } 112 113 @Override 114 public String toString(PageContext pc,DumpData data, boolean expand) { 115 StringWriter sw=new StringWriter(); 116 try { 117 writeOut(pc,data, sw,expand); 118 } 119 catch (IOException e) { 120 return ""; 121 } 122 return sw.toString(); 123 } 124 125}