001 /** 002 * Implements the Cold Fusion Function dump 003 */ 004 package railo.runtime.functions.other; 005 006 import java.util.Set; 007 008 import railo.commons.io.res.util.ResourceUtil; 009 import railo.commons.lang.StringUtil; 010 import railo.commons.lang.types.RefBoolean; 011 import railo.commons.lang.types.RefBooleanImpl; 012 import railo.runtime.PageContext; 013 import railo.runtime.dump.DumpData; 014 import railo.runtime.dump.DumpProperties; 015 import railo.runtime.dump.DumpRow; 016 import railo.runtime.dump.DumpTable; 017 import railo.runtime.dump.DumpTablePro; 018 import railo.runtime.dump.DumpUtil; 019 import railo.runtime.dump.SimpleDumpData; 020 import railo.runtime.ext.function.Function; 021 import railo.runtime.type.List; 022 import railo.runtime.type.Query; 023 import railo.runtime.type.QueryImpl; 024 import railo.runtime.type.Struct; 025 import railo.runtime.type.StructImpl; 026 import railo.runtime.type.util.StructUtil; 027 028 public final class DumpStruct implements Function { 029 030 public static Struct call(PageContext pc , Object object) { 031 return call(pc, object,9999,null,null,9999,true); 032 } 033 034 public static Struct call(PageContext pc , Object object, double maxLevel) { 035 return call(pc, object,maxLevel,null,null,9999,true,true,null); 036 } 037 038 public static Struct call(PageContext pc , Object object, double maxLevel, String show) { 039 return call(pc, object,maxLevel,show,null,9999,true,true,null); 040 } 041 042 public static Struct call(PageContext pc , Object object, double maxLevel, String show, String hide) { 043 return call(pc,object,maxLevel,show,hide,9999,true,true,null); 044 } 045 046 public static Struct call(PageContext pc , Object object, double maxLevel, String show, String hide,double keys) { 047 return call(pc , object, maxLevel, show, hide,keys,true,true,null); 048 } 049 public static Struct call(PageContext pc , Object object, double maxLevel, String show, String hide,double keys,boolean metainfo) { 050 return call(pc , object, maxLevel, show, hide,keys,metainfo,true,null); 051 } 052 053 public static Struct call(PageContext pc , Object object, double maxLevel, String show, String hide,double keys,boolean metainfo, boolean showUDFs) { 054 return call(pc , object, maxLevel, show, hide,keys,metainfo,showUDFs,null); 055 } 056 057 public static Struct call(PageContext pc,Object object,double maxLevel, String show, String hide,double keys,boolean metainfo, boolean showUDFs, String label) { 058 if(show!=null && "all".equalsIgnoreCase(show.trim()))show=null; 059 if(hide!=null && "all".equalsIgnoreCase(hide.trim()))hide=null; 060 061 Set setShow=(show!=null)?List.listToSet(show.toLowerCase(),",",true):null; 062 Set setHide=(hide!=null)?List.listToSet(hide.toLowerCase(),",",true):null; 063 064 DumpProperties properties=new DumpProperties((int)maxLevel,setShow,setHide,(int)keys,metainfo,showUDFs); 065 DumpData dd = DumpUtil.toDumpData(object, pc,(int)maxLevel,properties); 066 067 if(!StringUtil.isEmpty(label)) { 068 DumpTable table=new DumpTable("#ffffff","#cccccc","#000000"); 069 table.appendRow(1,new SimpleDumpData(label)); 070 table.appendRow(0,dd); 071 dd=table; 072 } 073 RefBoolean hasReference=new RefBooleanImpl(false); 074 Struct sct = toStruct(dd,object,hasReference); 075 sct.setEL("hasReference", hasReference.toBoolean()); 076 return sct; 077 } 078 079 private static Struct toStruct(DumpData dd, Object object, RefBoolean hasReference) { 080 DumpTable table; 081 if(dd instanceof DumpTable) table=(DumpTable) dd; 082 else { 083 if(dd==null) dd= new SimpleDumpData("null"); 084 table=new DumpTable("#ffffff","#cccccc","#000000"); 085 table.appendRow(1,dd); 086 } 087 return toCFML(table,object,hasReference); 088 } 089 090 091 private static Object toCFML(DumpData dd, Object object, RefBoolean hasReference) { 092 if(dd instanceof DumpTable)return toCFML((DumpTable) dd,object,hasReference); 093 if(dd==null) return new SimpleDumpData("null"); 094 return dd.toString(); 095 } 096 097 private static Struct toCFML(DumpTable dt, Object object, RefBoolean hasReference) { 098 Struct sct=new StructImpl(); 099 StructUtil.setELIgnoreWhenNull(sct,"borderColor", toShortColor(dt.getBorderColor())); 100 StructUtil.setELIgnoreWhenNull(sct,"comment", dt.getComment()); 101 StructUtil.setELIgnoreWhenNull(sct,"fontColor", toShortColor(dt.getFontColor())); 102 StructUtil.setELIgnoreWhenNull(sct,"height", dt.getHeight()); 103 StructUtil.setELIgnoreWhenNull(sct,"width", dt.getWidth()); 104 StructUtil.setELIgnoreWhenNull(sct,"highLightColor", toShortColor(dt.getHighLightColor())); 105 StructUtil.setELIgnoreWhenNull(sct,"normalColor", toShortColor(dt.getNormalColor())); 106 StructUtil.setELIgnoreWhenNull(sct,"title", dt.getTitle()); 107 108 if(dt instanceof DumpTablePro){ 109 DumpTablePro dtp = (DumpTablePro)dt; 110 sct.setEL("type", dtp.getType()); 111 sct.setEL("id", dtp.getId()); 112 113 if("ref".equals(dtp.getType())){ 114 hasReference.setValue(true); 115 sct.setEL("ref", dtp.getRef()); 116 } 117 } 118 119 DumpRow[] drs = dt.getRows(); 120 DumpRow dr; 121 Query qry=null; 122 DumpData[] items; 123 for(int r=0;r<drs.length;r++){ 124 dr=drs[r]; 125 items = dr.getItems(); 126 if(qry==null)qry=new QueryImpl(toColumns(items),drs.length,"data"); 127 for(int c=1;c<=items.length;c++){ 128 qry.setAtEL("data"+c, r+1, toCFML(items[c-1],object,hasReference)); 129 } 130 qry.setAtEL("highlight", r+1, new Double(dr.getHighlightType())); 131 132 } 133 sct.setEL("data", qry); 134 return sct; 135 } 136 137 private static String[] toColumns(DumpData[] items) { 138 String[] columns=new String[items.length+1]; 139 columns[0]="highlight"; 140 for(int i=1;i<columns.length;i++){ 141 columns[i]="data"+i; 142 } 143 return columns; 144 } 145 146 public static String getContext() { 147 //Throwable cause = t.getCause(); 148 StackTraceElement[] traces = new Exception("Stack trace").getStackTrace(); 149 150 int line=0; 151 String template; 152 StackTraceElement trace=null; 153 for(int i=0;i<traces.length;i++) { 154 trace=traces[i]; 155 template=trace.getFileName(); 156 if((line=trace.getLineNumber())<=0 || template==null || ResourceUtil.getExtension(template,"").equals("java")) continue; 157 return template+":"+line; 158 } 159 return null; 160 } 161 162 private static Object toShortColor(String color) { 163 if(color!=null && color.length()==7 && color.startsWith("#")) { 164 if(color.charAt(1)==color.charAt(2) && color.charAt(3)==color.charAt(4) && color.charAt(5)==color.charAt(6)) 165 return "#"+color.charAt(1)+color.charAt(3)+color.charAt(5); 166 167 168 } 169 170 171 return color; 172 } 173 }