001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.component;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038: import org.xml.sax.Attributes;
039: import org.xml.sax.helpers.AttributesImpl;
040:
041: import javax.faces.component.UIComponentBase;
042: import javax.faces.context.FacesContext;
043: import java.lang.reflect.InvocationTargetException;
044: import java.lang.reflect.Method;
045: import java.util.Collections;
046: import java.util.HashMap;
047: import java.util.Iterator;
048: import java.util.Map;
049:
050: public class UIXhtmlComponent extends UIComponentBase {
051: public static final String COMPONENT_FAMILY = "com.icesoft.faces.XhtmlComponent";
052: public static final String RENDERER_TYPE = "com.icesoft.domXhtml";
053:
054: private static final Attributes EMPTY_ATTRIBUTES = new AttributesImpl();
055: private static final Log log = LogFactory
056: .getLog(UIXhtmlComponent.class);
057: private static Method getELContextMethod;
058: private static Method getValueMethod;
059:
060: private String tag;
061: private Attributes xmlAttributes = EMPTY_ATTRIBUTES;
062: private Map standardAttributes = Collections.EMPTY_MAP;
063: private Map elValueExpressions = Collections.EMPTY_MAP;
064: private boolean createdByFacelets = false;
065:
066: static {
067: try {
068: Class ELAdaptorClass = Class
069: .forName("com.sun.facelets.el.ELAdaptor");
070: getELContextMethod = ELAdaptorClass.getMethod(
071: "getELContext", new Class[] { FacesContext.class });
072: Class ValueExpressionClass = Class
073: .forName("javax.el.ValueExpression");
074: Class ELContextClass = Class.forName("javax.el.ELContext");
075: getValueMethod = ValueExpressionClass.getMethod("getValue",
076: new Class[] { ELContextClass });
077: } catch (Throwable e) {
078: //EL libraries not available, which either means that we're
079: //not using Facelets, or someone forgot to include a JAR
080: if (log.isDebugEnabled()) {
081: log
082: .debug("EL libraries not detected; Facelets are not supported by this configuration: "
083: + e.getMessage());
084: }
085: }
086: }
087:
088: public UIXhtmlComponent() {
089: setRendererType(RENDERER_TYPE);
090: }
091:
092: public String getFamily() {
093: return COMPONENT_FAMILY;
094: }
095:
096: public String getTag() {
097: return tag;
098: }
099:
100: public Map getTagAttributes() {
101: Map allAttributes = new HashMap();
102: int length = xmlAttributes.getLength();
103: for (int i = 0; i < length; i++) {
104: allAttributes.put(xmlAttributes.getQName(i), xmlAttributes
105: .getValue(i));
106: }
107:
108: // Straight text attributes from Facelets
109: Iterator attributeIterator = standardAttributes.entrySet()
110: .iterator();
111: while (attributeIterator.hasNext()) {
112: Map.Entry attribute = (Map.Entry) attributeIterator.next();
113: allAttributes.put(attribute.getKey().toString(), attribute
114: .getValue().toString());
115: }
116:
117: // EL expression attributes from Facelets
118: if (getELContextMethod != null && getValueMethod != null) {
119: try {
120: Object elContext = getELContextMethod.invoke(null,
121: new Object[] { FacesContext
122: .getCurrentInstance() });
123:
124: if (elContext != null) {
125: Iterator elAttributeIterator = elValueExpressions
126: .entrySet().iterator();
127: while (elAttributeIterator.hasNext()) {
128: Map.Entry attribute = (Map.Entry) elAttributeIterator
129: .next();
130: Object name = attribute.getKey();
131: Object value = attribute.getValue();
132: if (value != null) {
133: Object evaluatedValue = getValueMethod
134: .invoke(value,
135: new Object[] { elContext });
136: if (evaluatedValue != null) {
137: allAttributes.put(name.toString(),
138: evaluatedValue.toString());
139: }
140: }
141: }
142: }
143: } catch (IllegalAccessException iae) {
144: // It shouldn't be possible for these reflection exceptions to happen
145: throw new RuntimeException(iae);
146: } catch (InvocationTargetException ite) {
147: // It shouldn't be possible for these reflection exceptions to happen
148: throw new RuntimeException(ite);
149: }
150: }
151:
152: return allAttributes;
153: }
154:
155: public boolean isCreatedByFacelets() {
156: return createdByFacelets;
157: }
158:
159: public void setTag(String tag) {
160: this .tag = tag;
161: }
162:
163: public void setXmlAttributes(Attributes attr) {
164: xmlAttributes = attr == null ? xmlAttributes : attr;
165: }
166:
167: public void addStandardAttribute(String key, Object value) {
168: if (standardAttributes == Collections.EMPTY_MAP)
169: standardAttributes = new HashMap();
170: standardAttributes.put(key, value);
171: }
172:
173: /**
174: * Since we might not always include the EL jars, we can't refer to those
175: * classes in our method signatures, so the second param to this method has
176: * to be "Object", even though it must specifically take a ValueExpression
177: *
178: * @param key
179: * @param valueExpression Must be a javax.el.ValueExpression
180: */
181: public void addELValueExpression(String key, Object valueExpression) {
182: if (elValueExpressions == Collections.EMPTY_MAP)
183: elValueExpressions = new HashMap();
184: elValueExpressions.put(key, valueExpression);
185: }
186:
187: public void setCreatedByFacelets() {
188: createdByFacelets = true;
189: }
190:
191: public String toString() {
192: return this .getClass() + "@" + this .hashCode() + ":tag=["
193: + this .getTag() + "]";
194: }
195: }
|