001 package railo.runtime.type.util; 002 003 import java.util.Iterator; 004 import java.util.Map; 005 import java.util.Set; 006 007 import railo.commons.lang.CFTypes; 008 import railo.runtime.PageContext; 009 import railo.runtime.converter.LazyConverter; 010 import railo.runtime.dump.DumpData; 011 import railo.runtime.dump.DumpProperties; 012 import railo.runtime.exp.ExpressionException; 013 import railo.runtime.exp.PageException; 014 import railo.runtime.type.Collection; 015 import railo.runtime.type.KeyImpl; 016 import railo.runtime.type.Sizeable; 017 import railo.runtime.type.Struct; 018 import railo.runtime.type.UDF; 019 import railo.runtime.type.dt.DateTime; 020 import railo.runtime.type.it.KeyAsStringIterator; 021 022 public abstract class StructSupport implements Map,Struct,Sizeable { 023 024 private static final long serialVersionUID = 7433668961838400995L; 025 026 /** 027 * throw exception for invalid key 028 * @param key Invalid key 029 * @return returns an invalid key Exception 030 */ 031 public static ExpressionException invalidKey(Struct sct,Key key) { 032 033 StringBuilder sb=new StringBuilder(); 034 Iterator<Key> it = sct.keyIterator(); 035 Key k; 036 037 while(it.hasNext()){ 038 k = it.next(); 039 if( k.equals( key ) ) 040 return new ExpressionException( "the value from key [" + key.getString() + "] is NULL, which is the same as not existing in CFML" ); 041 if(sb.length()>0)sb.append(','); 042 sb.append(k.getString()); 043 } 044 045 return new ExpressionException( "key [" + key.getString() + "] doesn't exist (existing keys:" + sb.toString() + ")" ); 046 } 047 048 @Override 049 public long sizeOf() { 050 return StructUtil.sizeOf(this); 051 } 052 053 @Override 054 public Set entrySet() { 055 return StructUtil.entrySet(this); 056 } 057 058 @Override 059 public final Object get(Object key) { 060 return get(KeyImpl.toKey(key,null), null); 061 } 062 063 @Override 064 public final boolean isEmpty() { 065 return size()==0; 066 } 067 068 @Override 069 public Set keySet() { 070 return StructUtil.keySet(this); 071 } 072 073 @Override 074 public final Object put(Object key, Object value) { 075 return setEL(KeyImpl.toKey(key,null), value); 076 } 077 078 @Override 079 public final void putAll(Map t) { 080 StructUtil.putAll(this, t); 081 } 082 083 @Override 084 public final Object remove(Object key) { 085 return removeEL(KeyImpl.toKey(key,null)); 086 } 087 088 @Override 089 public final Object clone(){ 090 return duplicate(true); 091 } 092 093 @Override 094 public final boolean containsKey(Object key) { 095 return containsKey(KeyImpl.toKey(key,null)); 096 } 097 098 @Override 099 public final boolean containsKey(String key) { 100 return containsKey(KeyImpl.init(key)); 101 } 102 103 @Override 104 public final Object get(String key, Object defaultValue) { 105 return get(KeyImpl.init(key), defaultValue); 106 } 107 108 @Override 109 public final Object get(String key) throws PageException { 110 return get(KeyImpl.init(key)); 111 } 112 113 @Override 114 public final Object set(String key, Object value) throws PageException { 115 return set(KeyImpl.init(key), value); 116 } 117 118 @Override 119 public final Object setEL(String key, Object value) { 120 return setEL(KeyImpl.init(key), value); 121 } 122 123 @Override 124 public DumpData toDumpData(PageContext pageContext, int maxlevel,DumpProperties properties) { 125 return StructUtil.toDumpTable(this,"Struct",pageContext,maxlevel,properties); 126 } 127 128 @Override 129 public boolean castToBooleanValue() throws PageException { 130 throw new ExpressionException("can't cast Complex Object Type Struct to a boolean value"); 131 } 132 133 @Override 134 public Boolean castToBoolean(Boolean defaultValue) { 135 return defaultValue; 136 } 137 138 @Override 139 public double castToDoubleValue() throws PageException { 140 throw new ExpressionException("can't cast Complex Object Type Struct to a number value"); 141 } 142 143 @Override 144 public double castToDoubleValue(double defaultValue) { 145 return defaultValue; 146 } 147 148 @Override 149 public DateTime castToDateTime() throws PageException { 150 throw new ExpressionException("can't cast Complex Object Type Struct to a Date"); 151 } 152 153 @Override 154 public DateTime castToDateTime(DateTime defaultValue) { 155 return defaultValue; 156 } 157 158 @Override 159 public String castToString() throws PageException { 160 throw new ExpressionException("Can't cast Complex Object Type Struct to String", 161 "Use Built-In-Function \"serialize(Struct):String\" to create a String from Struct"); 162 } 163 164 @Override 165 public String castToString(String defaultValue) { 166 return defaultValue; 167 } 168 169 @Override 170 public int compareTo(boolean b) throws PageException { 171 throw new ExpressionException("can't compare Complex Object Type Struct with a boolean value"); 172 } 173 174 @Override 175 public int compareTo(DateTime dt) throws PageException { 176 throw new ExpressionException("can't compare Complex Object Type Struct with a DateTime Object"); 177 } 178 179 @Override 180 public int compareTo(double d) throws PageException { 181 throw new ExpressionException("can't compare Complex Object Type Struct with a numeric value"); 182 } 183 184 @Override 185 public int compareTo(String str) throws PageException { 186 throw new ExpressionException("can't compare Complex Object Type Struct with a String"); 187 } 188 189 @Override 190 public String toString() { 191 return LazyConverter.serialize(this); 192 } 193 194 @Override 195 public java.util.Collection values() { 196 return StructUtil.values(this); 197 } 198 199 @Override 200 public boolean containsValue(Object value) { 201 return values().contains(value); 202 } 203 204 @Override 205 public Iterator<String> keysAsStringIterator() { 206 return new KeyAsStringIterator(keyIterator()); 207 } 208 209 @Override 210 public Object get(PageContext pc, Key key, Object defaultValue) { 211 return get(key, defaultValue); 212 } 213 214 @Override 215 public Object get(PageContext pc, Key key) throws PageException { 216 return get(key); 217 } 218 219 @Override 220 public Object set(PageContext pc, Key propertyName, Object value) throws PageException { 221 return set(propertyName, value); 222 } 223 224 @Override 225 public Object setEL(PageContext pc, Key propertyName, Object value) { 226 return setEL(propertyName, value); 227 } 228 229 @Override 230 public Object call(PageContext pc, Key methodName, Object[] args) throws PageException { 231 Object obj = get(methodName,null); 232 if(obj instanceof UDF) { 233 return ((UDF)obj).call(pc,args,false); 234 } 235 return MemberUtil.call(pc, this, methodName, args, CFTypes.TYPE_STRUCT, "struct"); 236 } 237 238 @Override 239 public Object callWithNamedValues(PageContext pc, Key methodName, Struct args) throws PageException { 240 Object obj = get(methodName,null); 241 if(obj instanceof UDF) { 242 return ((UDF)obj).callWithNamedValues(pc,args,false); 243 } 244 return MemberUtil.callWithNamedValues(pc,this,methodName,args, CFTypes.TYPE_STRUCT, "struct"); 245 } 246 247 public java.util.Iterator<String> getIterator() { 248 return keysAsStringIterator(); 249 } 250 251 @Override 252 public boolean equals(Object obj){ 253 if(!(obj instanceof Collection)) return false; 254 return CollectionUtil.equals(this,(Collection)obj); 255 } 256 257 @Override 258 public int hashCode() { 259 return CollectionUtil.hashCode(this); 260 } 261 }