1 33 34 package com.icesoft.faces.component; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.xml.sax.Attributes ; 39 import org.xml.sax.helpers.AttributesImpl ; 40 41 import javax.faces.component.UIComponentBase; 42 import javax.faces.context.FacesContext; 43 import java.lang.reflect.InvocationTargetException ; 44 import java.lang.reflect.Method ; 45 import java.util.Collections ; 46 import java.util.HashMap ; 47 import java.util.Iterator ; 48 import java.util.Map ; 49 50 public class UIXhtmlComponent extends UIComponentBase { 51 public static final String COMPONENT_FAMILY = 52 "com.icesoft.faces.XhtmlComponent"; 53 public static final String RENDERER_TYPE = 54 "com.icesoft.domXhtml"; 55 56 private static final Attributes EMPTY_ATTRIBUTES = new AttributesImpl (); 57 private static final Log log = LogFactory.getLog(UIXhtmlComponent.class); 58 private static Method getELContextMethod; 59 private static Method getValueMethod; 60 61 private String tag; 62 private Attributes xmlAttributes = EMPTY_ATTRIBUTES; 63 private Map standardAttributes = Collections.EMPTY_MAP; 64 private Map elValueExpressions = Collections.EMPTY_MAP; 65 private boolean createdByFacelets = false; 66 67 static { 68 try { 69 Class ELAdaptorClass = 70 Class.forName("com.sun.facelets.el.ELAdaptor"); 71 getELContextMethod = ELAdaptorClass 72 .getMethod("getELContext", new Class []{FacesContext.class}); 73 Class ValueExpressionClass = 74 Class.forName("javax.el.ValueExpression"); 75 Class ELContextClass = Class.forName("javax.el.ELContext"); 76 getValueMethod = ValueExpressionClass 77 .getMethod("getValue", new Class []{ELContextClass}); 78 } 79 catch (Throwable e) { 80 if (log.isDebugEnabled()) { 83 log.debug( 84 "EL libraries not detected; Facelets are not supported by this configuration: " + 85 e.getMessage()); 86 } 87 } 88 } 89 90 public UIXhtmlComponent() { 91 setRendererType( RENDERER_TYPE ); 92 } 93 94 public String getFamily() { 95 return COMPONENT_FAMILY; 96 } 97 98 public String getTag() { 99 return tag; 100 } 101 102 public Map getTagAttributes() { 103 Map allAttributes = new HashMap (); 104 int length = xmlAttributes.getLength(); 105 for (int i = 0; i < length; i++) { 106 allAttributes 107 .put(xmlAttributes.getQName(i), xmlAttributes.getValue(i)); 108 } 109 110 Iterator attributeIterator = standardAttributes.entrySet().iterator(); 112 while (attributeIterator.hasNext()) { 113 Map.Entry attribute = (Map.Entry ) attributeIterator.next(); 114 allAttributes.put(attribute.getKey().toString(), 115 attribute.getValue().toString()); 116 } 117 118 if (getELContextMethod != null && getValueMethod != null) { 120 try { 121 Object elContext = getELContextMethod.invoke(null, 122 new Object []{ 123 FacesContext.getCurrentInstance()}); 124 125 if (elContext != null) { 126 Iterator elAttributeIterator = 127 elValueExpressions.entrySet().iterator(); 128 while (elAttributeIterator.hasNext()) { 129 Map.Entry attribute = 130 (Map.Entry ) elAttributeIterator.next(); 131 Object name = attribute.getKey(); 132 Object value = attribute.getValue(); 133 if (value != null) { 134 Object evaluatedValue = getValueMethod 135 .invoke(value, new Object []{elContext}); 136 if (evaluatedValue != null) { 137 allAttributes.put(name.toString(), 138 evaluatedValue.toString()); 139 } 140 } 141 } 142 } 143 } 144 catch (IllegalAccessException iae) { 145 throw new RuntimeException (iae); 147 } 148 catch (InvocationTargetException ite) { 149 throw new RuntimeException (ite); 151 } 152 } 153 154 return allAttributes; 155 } 156 157 public boolean isCreatedByFacelets() { 158 return createdByFacelets; 159 } 160 161 public void setTag(String tag) { 162 this.tag = tag; 163 } 164 165 public void setXmlAttributes(Attributes attr) { 166 xmlAttributes = attr == null ? xmlAttributes : attr; 167 } 168 169 public void addStandardAttribute(String key, Object value) { 170 if (standardAttributes == Collections.EMPTY_MAP) 171 standardAttributes = new HashMap (); 172 standardAttributes.put(key, value); 173 } 174 175 183 public void addELValueExpression(String key, Object valueExpression) { 184 if (elValueExpressions == Collections.EMPTY_MAP) 185 elValueExpressions = new HashMap (); 186 elValueExpressions.put(key, valueExpression); 187 } 188 189 public void setCreatedByFacelets() { 190 createdByFacelets = true; 191 } 192 193 public String toString() { 194 return this.getClass() + "@" + this.hashCode() + ":tag=[" + 195 this.getTag() + "]"; 196 } 197 } 198 | Popular Tags |