001 package railo.runtime.tag; 002 003 import java.util.Iterator; 004 import java.util.Map; 005 import java.util.Map.Entry; 006 007 import javax.servlet.jsp.tagext.Tag; 008 009 import railo.commons.lang.StringUtil; 010 import railo.runtime.PageContext; 011 import railo.runtime.exp.ApplicationException; 012 import railo.runtime.exp.PageException; 013 import railo.runtime.ext.tag.DynamicAttributes; 014 import railo.runtime.op.Caster; 015 import railo.runtime.reflection.Reflector; 016 import railo.runtime.reflection.pairs.MethodInstance; 017 import railo.runtime.type.Collection.Key; 018 import railo.runtime.type.KeyImpl; 019 import railo.runtime.type.Struct; 020 import railo.runtime.type.StructImpl; 021 import railo.runtime.type.UDFImpl; 022 import railo.runtime.type.util.ArrayUtil; 023 import railo.transformer.library.tag.TagLibTag; 024 025 public class TagUtil { 026 027 public static final short ORIGINAL_CASE = 0; 028 public static final short UPPER_CASE = 1; 029 public static final short LOWER_CASE = 2; 030 031 //private static final String "invalid call of the function ["+tlt.getName()+", you can not mix named on regular arguments]" = "invalid argument for function, only named arguments are allowed like struct(name:\"value\",name2:\"value2\")"; 032 033 public static void setAttributeCollection(PageContext pc,Tag tag, MissingAttribute[] missingAttrs, Struct attrs, int attrType) throws PageException { 034 // check missing tags 035 if(!ArrayUtil.isEmpty(missingAttrs)){ 036 Object value; 037 for(int i=0;i<missingAttrs.length;i++) { 038 value=attrs.get( 039 missingAttrs[i].getName() 040 ,null); 041 if(value==null) 042 throw new ApplicationException("attribute "+missingAttrs[i].getName().getString()+" is required but missing"); 043 //throw new ApplicationException("attribute "+missingAttrs[i].getName().getString()+" is required for tag "+tag.getFullName()); 044 attrs.put(missingAttrs[i].getName(), Caster.castTo(pc, missingAttrs[i].getType(), value, false)); 045 } 046 } 047 048 049 Key[] keys = attrs.keys(); 050 if(TagLibTag.ATTRIBUTE_TYPE_DYNAMIC==attrType) { 051 DynamicAttributes da=(DynamicAttributes) tag; 052 for(int i=0;i<keys.length;i++) { 053 da.setDynamicAttribute(null, keys[i].getString(),attrs.get(keys[i],null)); 054 } 055 } 056 else if(TagLibTag.ATTRIBUTE_TYPE_FIXED==attrType) { 057 Object value; 058 for(int i=0;i<keys.length;i++) { 059 value=attrs.get(keys[i],null); 060 if(value!=null)Reflector.callSetterEL(tag, keys[i].getString(),value); 061 //}catch(PageException pe){} 062 } 063 } 064 else if(TagLibTag.ATTRIBUTE_TYPE_MIXED==attrType) { 065 MethodInstance setter; 066 for(int i=0;i<keys.length;i++) { 067 setter = Reflector.getSetterEL(tag, keys[i].getString(),attrs.get(keys[i],null)); 068 if(setter!=null) { 069 try { 070 setter.invoke(tag); 071 } 072 catch (Exception e) { 073 throw Caster.toPageException(e); 074 } 075 } 076 else { 077 DynamicAttributes da=(DynamicAttributes) tag; 078 da.setDynamicAttribute(null, keys[i].getString(),attrs.get(keys[i],null)); 079 } 080 } 081 } 082 } 083 084 /** 085 * sets dynamic attributes 086 * @param attributes 087 * @param name 088 * @param value 089 */ 090 public static void setDynamicAttribute(StructImpl attributes,String name, Object value, short caseType) { 091 if(LOWER_CASE==caseType)name=StringUtil.toLowerCase(name); 092 else if(UPPER_CASE==caseType)name=StringUtil.toUpperCase(name); 093 if(name.equals("attributecollection")) { 094 if(value instanceof railo.runtime.type.Collection) { 095 railo.runtime.type.Collection coll=(railo.runtime.type.Collection)value; 096 railo.runtime.type.Collection.Key[] keys=coll.keys(); 097 railo.runtime.type.Collection.Key key; 098 for(int i=0;i<keys.length;i++) { 099 key=keys[i]; 100 if(attributes.get(key,null)==null) 101 attributes.setEL(key,coll.get(key,null)); 102 } 103 return; 104 } 105 else if(value instanceof Map) { 106 107 Map map=(Map) value; 108 Iterator it = map.entrySet().iterator(); 109 Map.Entry entry; 110 Key key; 111 while(it.hasNext()) { 112 entry=(Entry) it.next(); 113 key = UDFImpl.toKey(entry.getKey()); 114 if(!attributes.containsKey(key)){ 115 attributes.setEL(key,entry.getValue()); 116 } 117 } 118 return; 119 } 120 } 121 attributes.setEL(KeyImpl.getInstance(name), value); 122 } 123 124 125 }