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 **/ 019/** 020 * Implements the CFML Function dump 021 */ 022package lucee.runtime.functions.other; 023 024import java.io.IOException; 025import java.util.Set; 026 027import lucee.commons.io.IOUtil; 028import lucee.commons.io.res.Resource; 029import lucee.commons.io.res.util.ResourceUtil; 030import lucee.commons.lang.StringUtil; 031import lucee.runtime.PageContext; 032import lucee.runtime.PageContextImpl; 033import lucee.runtime.dump.DumpData; 034import lucee.runtime.dump.DumpProperties; 035import lucee.runtime.dump.DumpTable; 036import lucee.runtime.dump.DumpUtil; 037import lucee.runtime.dump.DumpWriter; 038import lucee.runtime.dump.HTMLDumpWriter; 039import lucee.runtime.dump.SimpleDumpData; 040import lucee.runtime.exp.NativeException; 041import lucee.runtime.exp.PageException; 042import lucee.runtime.ext.function.Function; 043import lucee.runtime.type.util.ListUtil; 044 045public final class Dump implements Function { 046 047 private static final int OUTPUT_TYPE_NONE = 0; 048 private static final int OUTPUT_TYPE_BROWSER = 1; 049 private static final int OUTPUT_TYPE_CONSOLE = 2; 050 private static final int OUTPUT_TYPE_RESOURCE = 3; 051 052 //private static final int FORMAT_TYPE_HTML = 0; 053 //private static final int FORMAT_TYPE_TEXT = 1; 054 055 public static String call(PageContext pc , Object object) throws PageException { 056 return call(pc, object,null,true,9999,null,null,null,null,9999,true,true); 057 } 058 public static String call(PageContext pc , Object object, String label) throws PageException { 059 return call(pc, object,label,true,9999,null,null,null,null,9999,true,true); 060 } 061 public static String call(PageContext pc , Object object, String label,boolean expand) throws PageException { 062 return call(pc, object,label,expand,9999,null,null,null,null,9999,true,true); 063 } 064 065 public static String call(PageContext pc , Object object, String label,boolean expand,double maxLevel) throws PageException { 066 return call(pc, object,label,expand,maxLevel,null,null,null,null,9999,true,true); 067 } 068 069 public static String call(PageContext pc , Object object, String label,boolean expand,double maxLevel, String show) throws PageException { 070 return call(pc, object,label,expand,maxLevel,show,null,null,null,9999,true,true); 071 } 072 073 public static String call(PageContext pc , Object object, String label,boolean expand,double maxLevel, String show, String hide) throws PageException { 074 return call(pc, object,label,expand,maxLevel,show,hide,null,null,9999,true,true); 075 } 076 077 public static String call(PageContext pc , Object object, String label,boolean expand,double maxLevel, String show, String hide,String output) throws PageException { 078 return call(pc , object, label,expand,maxLevel, show, hide,output,null,9999,true,true); 079 } 080 081 public static String call(PageContext pc , Object object, String label,boolean expand,double maxLevel, String show, String hide,String output,String format) throws PageException { 082 return call(pc , object, label,expand,maxLevel, show, hide,output,format,9999,true,true); 083 } 084 085 public static String call(PageContext pc , Object object, String label,boolean expand,double maxLevel, String show, String hide,String output,String format,double keys) throws PageException { 086 return call(pc , object, label,expand,maxLevel, show, hide,output,format,keys,true,true); 087 } 088 public static String call(PageContext pc , Object object, String label,boolean expand,double maxLevel, String show, String hide,String output,String format,double keys,boolean metainfo) throws PageException { 089 return call(pc , object, label,expand,maxLevel, show, hide,output,format,keys,metainfo,true); 090 } 091 092 public static String call(PageContext pc , Object object, String label,boolean expand,double maxLevel, String show, String hide,String output,String format,double keys,boolean metainfo, boolean showUDFs) throws PageException { 093 if(show!=null && "all".equalsIgnoreCase(show.trim()))show=null; 094 if(hide!=null && "all".equalsIgnoreCase(hide.trim()))hide=null; 095 096 097 //String context = getContext(); 098 //PageContext pcc = pc; 099 try { 100 101 // output 102 int defType=HTMLDumpWriter.DEFAULT_RICH; 103 int outputType=OUTPUT_TYPE_NONE; 104 Resource outputRes=null; 105 if(!StringUtil.isEmpty(output,true)){ 106 output=output.trim(); 107 if("browser".equalsIgnoreCase(output)){ 108 outputType=OUTPUT_TYPE_BROWSER; 109 defType=HTMLDumpWriter.DEFAULT_RICH; 110 } 111 else if("console".equalsIgnoreCase(output)){ 112 outputType=OUTPUT_TYPE_CONSOLE; 113 defType=HTMLDumpWriter.DEFAULT_PLAIN; 114 } 115 else { 116 outputType=OUTPUT_TYPE_RESOURCE; 117 defType=HTMLDumpWriter.DEFAULT_RICH; 118 outputRes=ResourceUtil.toResourceNotExisting(pc, output); 119 } 120 } 121 122 // format 123 DumpWriter writer=pc.getConfig().getDumpWriter(format,defType); 124 125 Set setShow=(show!=null)?ListUtil.listToSet(show.toLowerCase(),",",true):null; 126 Set setHide=(hide!=null)?ListUtil.listToSet(hide.toLowerCase(),",",true):null; 127 128 DumpProperties properties=new DumpProperties((int)maxLevel,setShow,setHide,(int)keys,metainfo,showUDFs); 129 DumpData dd = DumpUtil.toDumpData(object, pc,(int)maxLevel,properties); 130 131 if(!StringUtil.isEmpty(label)) { 132 DumpTable table=new DumpTable("#ffffff","#cccccc","#000000"); 133 table.appendRow(1,new SimpleDumpData(label)); 134 //table.appendRow(1,new SimpleDumpData(getContext())); 135 table.appendRow(0,dd); 136 dd=table; 137 } 138 139 boolean isText="text".equalsIgnoreCase(format);//formatType==FORMAT_TYPE_TEXT 140 if(OUTPUT_TYPE_BROWSER==outputType || outputType==OUTPUT_TYPE_NONE) { 141 if(isText) pc.forceWrite("<pre>"); 142 pc.forceWrite(writer.toString(pc,dd,expand)); 143 if(isText) pc.forceWrite("</pre>"); 144 } 145 else if(OUTPUT_TYPE_CONSOLE==outputType) 146 System.out.println(writer.toString(pc,dd,expand)); 147 else if(OUTPUT_TYPE_RESOURCE==outputType) 148 IOUtil.write( 149 outputRes, 150 writer.toString(pc,dd,expand)+ 151 "\n************************************************************************************\n", 152 ((PageContextImpl)pc).getResourceCharset(), true); 153 154 } 155 catch (IOException e) { 156 throw new NativeException(e); 157 } 158 159 return ""; 160 } 161}