KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > UIXhtmlComponent


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

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 JavaDoc;
39 import org.xml.sax.helpers.AttributesImpl JavaDoc;
40
41 import javax.faces.component.UIComponentBase;
42 import javax.faces.context.FacesContext;
43 import java.lang.reflect.InvocationTargetException JavaDoc;
44 import java.lang.reflect.Method JavaDoc;
45 import java.util.Collections JavaDoc;
46 import java.util.HashMap JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.Map JavaDoc;
49
50 public class UIXhtmlComponent extends UIComponentBase {
51     public static final String JavaDoc COMPONENT_FAMILY =
52             "com.icesoft.faces.XhtmlComponent";
53     public static final String JavaDoc RENDERER_TYPE =
54             "com.icesoft.domXhtml";
55     
56     private static final Attributes JavaDoc EMPTY_ATTRIBUTES = new AttributesImpl JavaDoc();
57     private static final Log log = LogFactory.getLog(UIXhtmlComponent.class);
58     private static Method JavaDoc getELContextMethod;
59     private static Method JavaDoc getValueMethod;
60
61     private String JavaDoc tag;
62     private Attributes JavaDoc xmlAttributes = EMPTY_ATTRIBUTES;
63     private Map JavaDoc standardAttributes = Collections.EMPTY_MAP;
64     private Map JavaDoc elValueExpressions = Collections.EMPTY_MAP;
65     private boolean createdByFacelets = false;
66
67     static {
68         try {
69             Class JavaDoc ELAdaptorClass =
70                     Class.forName("com.sun.facelets.el.ELAdaptor");
71             getELContextMethod = ELAdaptorClass
72                     .getMethod("getELContext", new Class JavaDoc[]{FacesContext.class});
73             Class JavaDoc ValueExpressionClass =
74                     Class.forName("javax.el.ValueExpression");
75             Class JavaDoc ELContextClass = Class.forName("javax.el.ELContext");
76             getValueMethod = ValueExpressionClass
77                     .getMethod("getValue", new Class JavaDoc[]{ELContextClass});
78         }
79         catch (Throwable JavaDoc e) {
80             //EL libraries not available, which either means that we're
81
//not using Facelets, or someone forgot to include a JAR
82
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 JavaDoc getFamily() {
95         return COMPONENT_FAMILY;
96     }
97
98     public String JavaDoc getTag() {
99         return tag;
100     }
101
102     public Map JavaDoc getTagAttributes() {
103         Map JavaDoc allAttributes = new HashMap JavaDoc();
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         // Straight text attributes from Facelets
111
Iterator JavaDoc attributeIterator = standardAttributes.entrySet().iterator();
112         while (attributeIterator.hasNext()) {
113             Map.Entry JavaDoc attribute = (Map.Entry JavaDoc) attributeIterator.next();
114             allAttributes.put(attribute.getKey().toString(),
115                               attribute.getValue().toString());
116         }
117
118         // EL expression attributes from Facelets
119
if (getELContextMethod != null && getValueMethod != null) {
120             try {
121                 Object JavaDoc elContext = getELContextMethod.invoke(null,
122                                                              new Object JavaDoc[]{
123                                                                      FacesContext.getCurrentInstance()});
124
125                 if (elContext != null) {
126                     Iterator JavaDoc elAttributeIterator =
127                             elValueExpressions.entrySet().iterator();
128                     while (elAttributeIterator.hasNext()) {
129                         Map.Entry JavaDoc attribute =
130                                 (Map.Entry JavaDoc) elAttributeIterator.next();
131                         Object JavaDoc name = attribute.getKey();
132                         Object JavaDoc value = attribute.getValue();
133                         if (value != null) {
134                             Object JavaDoc evaluatedValue = getValueMethod
135                                     .invoke(value, new Object JavaDoc[]{elContext});
136                             if (evaluatedValue != null) {
137                                 allAttributes.put(name.toString(),
138                                                   evaluatedValue.toString());
139                             }
140                         }
141                     }
142                 }
143             }
144             catch (IllegalAccessException JavaDoc iae) {
145                 // It shouldn't be possible for these reflection exceptions to happen
146
throw new RuntimeException JavaDoc(iae);
147             }
148             catch (InvocationTargetException JavaDoc ite) {
149                 // It shouldn't be possible for these reflection exceptions to happen
150
throw new RuntimeException JavaDoc(ite);
151             }
152         }
153
154         return allAttributes;
155     }
156
157     public boolean isCreatedByFacelets() {
158         return createdByFacelets;
159     }
160
161     public void setTag(String JavaDoc tag) {
162         this.tag = tag;
163     }
164
165     public void setXmlAttributes(Attributes JavaDoc attr) {
166         xmlAttributes = attr == null ? xmlAttributes : attr;
167     }
168
169     public void addStandardAttribute(String JavaDoc key, Object JavaDoc value) {
170         if (standardAttributes == Collections.EMPTY_MAP)
171             standardAttributes = new HashMap JavaDoc();
172         standardAttributes.put(key, value);
173     }
174
175     /**
176      * Since we might not always include the EL jars, we can't refer to those
177      * classes in our method signatures, so the second param to this method has
178      * to be "Object", even though it must specifically take a ValueExpression
179      *
180      * @param key
181      * @param valueExpression Must be a javax.el.ValueExpression
182      */

183     public void addELValueExpression(String JavaDoc key, Object JavaDoc valueExpression) {
184         if (elValueExpressions == Collections.EMPTY_MAP)
185             elValueExpressions = new HashMap JavaDoc();
186         elValueExpressions.put(key, valueExpression);
187     }
188
189     public void setCreatedByFacelets() {
190         createdByFacelets = true;
191     }
192
193     public String JavaDoc toString() {
194         return this.getClass() + "@" + this.hashCode() + ":tag=[" +
195                this.getTag() + "]";
196     }
197 }
198
Popular Tags