001 /** 002 * Implements the CFML 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.DumpUtil; 018 import railo.runtime.dump.SimpleDumpData; 019 import railo.runtime.ext.function.Function; 020 import railo.runtime.type.Query; 021 import railo.runtime.type.QueryImpl; 022 import railo.runtime.type.Struct; 023 import railo.runtime.type.StructImpl; 024 import railo.runtime.type.util.KeyConstants; 025 import railo.runtime.type.util.ListUtil; 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)?ListUtil.listToSet(show.toLowerCase(),",",true):null; 062 Set setHide=(hide!=null)?ListUtil.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,KeyConstants._comment, dt.getComment()); 101 StructUtil.setELIgnoreWhenNull(sct,"fontColor", toShortColor(dt.getFontColor())); 102 StructUtil.setELIgnoreWhenNull(sct,KeyConstants._height, dt.getHeight()); 103 StructUtil.setELIgnoreWhenNull(sct,KeyConstants._width, dt.getWidth()); 104 StructUtil.setELIgnoreWhenNull(sct,"highLightColor", toShortColor(dt.getHighLightColor())); 105 StructUtil.setELIgnoreWhenNull(sct,"normalColor", toShortColor(dt.getNormalColor())); 106 StructUtil.setELIgnoreWhenNull(sct,KeyConstants._title, dt.getTitle()); 107 108 if(!StringUtil.isEmpty(dt.getType()))sct.setEL(KeyConstants._type, dt.getType()); 109 if(!StringUtil.isEmpty(dt.getId()))sct.setEL(KeyConstants._id, dt.getId()); 110 111 if("ref".equals(dt.getType())){ 112 hasReference.setValue(true); 113 sct.setEL(KeyConstants._ref, dt.getRef()); 114 } 115 116 117 DumpRow[] drs = dt.getRows(); 118 DumpRow dr; 119 Query qry=null; 120 DumpData[] items; 121 for(int r=0;r<drs.length;r++){ 122 dr=drs[r]; 123 items = dr.getItems(); 124 if(qry==null)qry=new QueryImpl(toColumns(items),drs.length,"data"); 125 for(int c=1;c<=items.length;c++){ 126 qry.setAtEL("data"+c, r+1, toCFML(items[c-1],object,hasReference)); 127 } 128 qry.setAtEL("highlight", r+1, new Double(dr.getHighlightType())); 129 130 } 131 if(qry!=null)sct.setEL(KeyConstants._data, qry); 132 return sct; 133 } 134 135 private static String[] toColumns(DumpData[] items) { 136 String[] columns=new String[items.length+1]; 137 columns[0]="highlight"; 138 for(int i=1;i<columns.length;i++){ 139 columns[i]="data"+i; 140 } 141 return columns; 142 } 143 144 public static String getContext() { 145 //Throwable cause = t.getCause(); 146 StackTraceElement[] traces = new Exception("Stack trace").getStackTrace(); 147 148 int line=0; 149 String template; 150 StackTraceElement trace=null; 151 for(int i=0;i<traces.length;i++) { 152 trace=traces[i]; 153 template=trace.getFileName(); 154 if((line=trace.getLineNumber())<=0 || template==null || ResourceUtil.getExtension(template,"").equals("java")) continue; 155 return template+":"+line; 156 } 157 return null; 158 } 159 160 private static Object toShortColor(String color) { 161 if(color!=null && color.length()==7 && color.startsWith("#")) { 162 if(color.charAt(1)==color.charAt(2) && color.charAt(3)==color.charAt(4) && color.charAt(5)==color.charAt(6)) 163 return "#"+color.charAt(1)+color.charAt(3)+color.charAt(5); 164 165 166 } 167 168 169 return color; 170 } 171 }