KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > renderkit > dom_html_basic > PassThruAttributeRenderer


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.renderkit.dom_html_basic;
35
36 import com.icesoft.faces.context.DOMContext;
37 import com.icesoft.faces.context.effects.CurrentStyle;
38 import com.icesoft.faces.context.effects.LocalEffectEncoder;
39 import org.w3c.dom.Element JavaDoc;
40
41 import javax.faces.FacesException;
42 import javax.faces.component.UIComponent;
43 import javax.faces.component.html.HtmlSelectManyCheckbox;
44 import javax.faces.context.FacesContext;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Arrays JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.List JavaDoc;
49 import java.util.Map JavaDoc;
50
51 /**
52  * This class is responsible for the rendering of html pass thru attributes.
53  */

54 public class PassThruAttributeRenderer {
55
56     private static List JavaDoc passThruAttributeNames = new ArrayList JavaDoc();
57     private static List JavaDoc booleanPassThruAttributeNames = new ArrayList JavaDoc();
58
59     static {
60         passThruAttributeNames.add("accept");
61         passThruAttributeNames.add("accesskey");
62         passThruAttributeNames.add("alt");
63         passThruAttributeNames.add("bgcolor");
64         passThruAttributeNames.add("border");
65         passThruAttributeNames.add("cellpadding");
66         passThruAttributeNames.add("cellspacing");
67         passThruAttributeNames.add("charset");
68         passThruAttributeNames.add("cols");
69         passThruAttributeNames.add("coords");
70         passThruAttributeNames.add("dir");
71         passThruAttributeNames.add("enctype");
72         passThruAttributeNames.add("frame");
73         passThruAttributeNames.add("height");
74         passThruAttributeNames.add("hreflang");
75         passThruAttributeNames.add("lang");
76         passThruAttributeNames.add("longdesc");
77         passThruAttributeNames.add("maxlength");
78         passThruAttributeNames.add("onblur");
79         passThruAttributeNames.add("onchange");
80         passThruAttributeNames.add("onclick");
81         passThruAttributeNames.add("ondblclick");
82         passThruAttributeNames.add("onfocus");
83         passThruAttributeNames.add("onkeydown");
84         passThruAttributeNames.add("onkeypress");
85         passThruAttributeNames.add("onkeyup");
86         passThruAttributeNames.add("onload");
87         passThruAttributeNames.add("onmousedown");
88         passThruAttributeNames.add("onmousemove");
89         passThruAttributeNames.add("onmouseout");
90         passThruAttributeNames.add("onmouseover");
91         passThruAttributeNames.add("onmouseup");
92         passThruAttributeNames.add("onreset");
93         passThruAttributeNames.add("onselect");
94         passThruAttributeNames.add("onsubmit");
95         passThruAttributeNames.add("onunload");
96         passThruAttributeNames.add("rel");
97         passThruAttributeNames.add("rev");
98         passThruAttributeNames.add("rows");
99         passThruAttributeNames.add("rules");
100         passThruAttributeNames.add("shape");
101         passThruAttributeNames.add("size");
102         passThruAttributeNames.add("style");
103         passThruAttributeNames.add("summary");
104         passThruAttributeNames.add("tabindex");
105         passThruAttributeNames.add("target");
106         passThruAttributeNames.add("title");
107         passThruAttributeNames.add("usemap");
108         passThruAttributeNames.add("width");
109         passThruAttributeNames.add("width");
110         passThruAttributeNames.add("onclickeffect");
111         passThruAttributeNames.add("ondblclickeffect");
112         passThruAttributeNames.add("onmousedowneffect");
113         passThruAttributeNames.add("onmouseupeffect");
114         passThruAttributeNames.add("onmousemoveeffect");
115         passThruAttributeNames.add("onmouseovereffect");
116         passThruAttributeNames.add("onmouseouteffect");
117         passThruAttributeNames.add("onchangeeffect");
118         passThruAttributeNames.add("onreseteffect");
119         passThruAttributeNames.add("onsubmiteffect");
120         passThruAttributeNames.add("onkeypresseffect");
121         passThruAttributeNames.add("onkeydowneffect");
122         passThruAttributeNames.add("onkeyupeffect");
123         passThruAttributeNames.add("autocomplete");
124
125         booleanPassThruAttributeNames.add("disabled");
126         booleanPassThruAttributeNames.add("readonly");
127         booleanPassThruAttributeNames.add("ismap");
128     }
129
130     /**
131      * Render pass thru attributes to the root element of the DOMContext
132      * associated with the UIComponent parameter. The excludedAttributes
133      * argument is a String array of the names of attributes to omit. Do not
134      * render attributes contained in the excludedAttributes argument.
135      *
136      * @param facesContext
137      * @param uiComponent
138      * @param excludedAttributes attributes to exclude
139      */

140     public static void renderAttributes(FacesContext facesContext,
141                                         UIComponent uiComponent,
142                                         String JavaDoc[] excludedAttributes) {
143         renderAttributes(
144                 facesContext, uiComponent, null, null, excludedAttributes);
145     }
146     
147     /**
148      * Render pass thru attributes to the attributeElement (instead of root)
149      * associated with the UIComponent parameter. The excludedAttributes
150      * argument is a String array of the names of attributes to omit. Do not
151      * render attributes contained in the excludedAttributes argument.
152      *
153      * @param facesContext
154      * @param uiComponent
155      * @param attributeElement
156      * @param styleElement The Element to apply styling on
157      * @param excludedAttributes attributes to exclude
158      */

159     public static void renderAttributes(FacesContext facesContext,
160                                         UIComponent uiComponent,
161                                         Element JavaDoc attributeElement,
162                                         Element JavaDoc styleElement,
163                                         String JavaDoc[] excludedAttributes) {
164         renderNonBooleanAttributes(
165                 facesContext, uiComponent, attributeElement, excludedAttributes);
166         renderBooleanAttributes(
167                 facesContext, uiComponent, attributeElement, excludedAttributes);
168         CurrentStyle.apply(facesContext, uiComponent, styleElement, null);
169         
170         if(attributeElement == null) {
171             DOMContext domContext =
172                     DOMContext.getDOMContext(facesContext, uiComponent);
173             Element JavaDoc rootElement = (Element JavaDoc) domContext.getRootNode();
174             attributeElement = rootElement;
175         }
176         LocalEffectEncoder
177                 .encodeLocalEffects(uiComponent, attributeElement, facesContext);
178         renderOnFocus(uiComponent, attributeElement);
179         renderOnBlur(attributeElement);
180     }
181
182     /**
183      * Render the icefaces onfocus handler to the root element. This should be
184      * restricted to input type elements and commandlinks.
185      *
186      * @param uiComponent
187      * @param root
188      */

189     public static void renderOnFocus(UIComponent uiComponent, Element JavaDoc root) {
190         // check the type of the root node
191
String JavaDoc nodeName = root.getNodeName();
192
193         if (nodeName.equalsIgnoreCase(HTML.ANCHOR_ELEM) ||
194             nodeName.equalsIgnoreCase(HTML.INPUT_ELEM) ||
195             nodeName.equalsIgnoreCase(HTML.SELECT_ELEM)) {
196             String JavaDoc original =
197                     (String JavaDoc) uiComponent.getAttributes().get("onfocus");
198             String JavaDoc onfocus = "setFocus(this.id);";
199
200             if (original == null) {
201                 original = "";
202             }
203             root.setAttribute(HTML.ONFOCUS_ATTR, onfocus + original);
204         }
205     }
206
207     /**
208      * Render the icefaces onblur handler to the root element. This should be
209      * restricted to input type elements and commandlinks.
210      *
211      * @param root
212      */

213     public static void renderOnBlur(Element JavaDoc root) {
214         // check the type of the root node
215
// onblur will clear focus id
216
String JavaDoc nodeName = root.getNodeName();
217
218         if (nodeName.equalsIgnoreCase(HTML.ANCHOR_ELEM) ||
219             nodeName.equalsIgnoreCase(HTML.INPUT_ELEM) ||
220             nodeName.equalsIgnoreCase(HTML.SELECT_ELEM)) {
221             String JavaDoc original = root.getAttribute("onblur");
222             String JavaDoc onblur = "setFocus('');";
223
224             if (original == null) {
225                 original = "";
226             }
227             root.setAttribute(HTML.ONBLUR_ATTR, onblur + original);
228         }
229     }
230
231     private static void renderBooleanAttributes(
232             FacesContext facesContext, UIComponent uiComponent,
233             Element JavaDoc targetElement,
234             String JavaDoc[] excludedAttributes) {
235
236         if (facesContext == null) {
237             throw new FacesException("Null pointer exception");
238         }
239         if (uiComponent == null) {
240             throw new FacesException("Null pointer exception");
241         }
242         
243         if(targetElement == null) {
244             DOMContext domContext =
245                     DOMContext.getDOMContext(facesContext, uiComponent);
246             Element JavaDoc rootElement = (Element JavaDoc) domContext.getRootNode();
247             if (rootElement == null) {
248                 throw new FacesException("DOMContext is null");
249             }
250             targetElement = rootElement;
251         }
252
253         List JavaDoc excludedAttributesList = null;
254         if (excludedAttributes != null && excludedAttributes.length > 0) {
255             excludedAttributesList = Arrays.asList(excludedAttributes);
256         }
257
258         Object JavaDoc nextPassThruAttributeName;
259         Object JavaDoc nextPassThruAttributeValue = null;
260         Iterator JavaDoc passThruNameIterator =
261                 booleanPassThruAttributeNames.iterator();
262         boolean primitiveAttributeValue;
263
264         while (passThruNameIterator.hasNext()) {
265             nextPassThruAttributeName = (passThruNameIterator.next());
266             if (excludedAttributesList != null) {
267                 if (excludedAttributesList
268                         .contains(nextPassThruAttributeName)) {
269                     continue;
270                 }
271             }
272             nextPassThruAttributeValue = uiComponent.getAttributes().get(
273                     nextPassThruAttributeName);
274             if (nextPassThruAttributeValue != null) {
275                 if (nextPassThruAttributeValue instanceof Boolean JavaDoc) {
276                     primitiveAttributeValue = ((Boolean JavaDoc)
277                             nextPassThruAttributeValue).booleanValue();
278                 } else {
279                     if (!(nextPassThruAttributeValue instanceof String JavaDoc)) {
280                         nextPassThruAttributeValue =
281                                 nextPassThruAttributeValue.toString();
282                     }
283                     primitiveAttributeValue = (new Boolean JavaDoc((String JavaDoc)
284                             nextPassThruAttributeValue)).booleanValue();
285                 }
286                 if (primitiveAttributeValue) {
287                     targetElement.setAttribute(
288                             nextPassThruAttributeName.toString(),
289                             nextPassThruAttributeName.toString());
290                 } else {
291                     targetElement.removeAttribute(
292                             nextPassThruAttributeName.toString());
293                 }
294
295             } else {
296                 targetElement.removeAttribute(
297                         nextPassThruAttributeName.toString());
298             }
299         }
300     }
301
302     private static void renderNonBooleanAttributes(
303             FacesContext facesContext, UIComponent uiComponent,
304             Element JavaDoc targetElement,
305             String JavaDoc[] excludedAttributes) {
306
307         if (uiComponent == null) {
308             throw new FacesException("Component instance is null");
309         }
310         
311         if(targetElement == null) {
312             DOMContext domContext =
313                     DOMContext.getDOMContext(facesContext, uiComponent);
314             
315             Element JavaDoc rootElement = (Element JavaDoc) domContext.getRootNode();
316             if (rootElement == null) {
317                 throw new FacesException("DOMContext is not initialized");
318             }
319             targetElement = rootElement;
320         }
321         
322         List JavaDoc excludedAttributesList = null;
323         if (excludedAttributes != null && excludedAttributes.length > 0) {
324             excludedAttributesList = Arrays.asList(excludedAttributes);
325         }
326
327         Object JavaDoc nextPassThruAttributeName = null;
328         Object JavaDoc nextPassThruAttributeValue = null;
329         Iterator JavaDoc passThruNameIterator = passThruAttributeNames.iterator();
330         while (passThruNameIterator.hasNext()) {
331             nextPassThruAttributeName = (passThruNameIterator.next());
332             if (excludedAttributesList != null) {
333                 if (excludedAttributesList
334                         .contains(nextPassThruAttributeName)) {
335                     continue;
336                 }
337             }
338             nextPassThruAttributeValue =
339                     uiComponent.getAttributes().get(nextPassThruAttributeName);
340             // Only render non-null attributes.
341
// Some components have attribute values
342
// set to the Wrapper classes' minimum value - don't render
343
// an attribute with this sentinel value.
344
if (nextPassThruAttributeValue != null &&
345                 !attributeValueIsSentinel(nextPassThruAttributeValue)) {
346                 targetElement.setAttribute(
347                         nextPassThruAttributeName.toString(),
348                         nextPassThruAttributeValue.toString());
349             } else {
350                 targetElement.removeAttribute(
351                         nextPassThruAttributeName.toString());
352             }
353         }
354     }
355
356     /**
357      * Determine whether any of the attributes defined for the UIComponent
358      * instance are pass thru attributes.
359      *
360      * @param uiComponent
361      * @return true if the UIComponent parameter has one or more attributes
362      * defined that are pass thru attributes
363      */

364     public static boolean passThruAttributeExists(UIComponent uiComponent) {
365         if (uiComponent == null) {
366             return false;
367         }
368         Map JavaDoc componentAttributes = uiComponent.getAttributes();
369         if (componentAttributes.size() <= 0) {
370             return false;
371         }
372         if (componentAttributesIncludePassThruAttribute(componentAttributes,
373                                                         passThruAttributeNames)) {
374             return true;
375         }
376         if (componentAttributesIncludePassThruAttribute(componentAttributes,
377                                                         booleanPassThruAttributeNames)) {
378             return true;
379         }
380         return false;
381     }
382
383     private static boolean attributeValueIsSentinel(Object JavaDoc value) {
384         if (value == null) {
385             return false;
386         }
387         if (value instanceof Boolean JavaDoc) {
388             if (((Boolean JavaDoc) value).booleanValue() == false) {
389                 return true;
390             }
391             return false;
392         }
393         if (value instanceof Number JavaDoc) {
394             if (value instanceof Integer JavaDoc) {
395                 if (((Integer JavaDoc) value).intValue() == Integer.MIN_VALUE) {
396                     return true;
397                 }
398                 return false;
399             }
400             if (value instanceof Long JavaDoc) {
401                 if (((Long JavaDoc) value).longValue() == Long.MIN_VALUE) {
402                     return true;
403                 }
404                 return false;
405             }
406             if (value instanceof Short JavaDoc) {
407                 if (((Short JavaDoc) value).shortValue() == Short.MIN_VALUE) {
408                     return true;
409                 }
410                 return false;
411             }
412             if (value instanceof Float JavaDoc) {
413                 if (((Float JavaDoc) value).floatValue() == Float.MIN_VALUE) {
414                     return true;
415                 }
416                 return false;
417             }
418             if (value instanceof Double JavaDoc) {
419                 if (((Double JavaDoc) value).doubleValue() == Double.MIN_VALUE) {
420                     return true;
421                 }
422                 return false;
423             }
424             if (value instanceof Byte JavaDoc) {
425                 if (((Byte JavaDoc) value).byteValue() == Byte.MIN_VALUE) {
426                     return true;
427                 }
428                 return false;
429             }
430         }
431         if (value instanceof Character JavaDoc) {
432             if (((Character JavaDoc) value).charValue() == Character.MIN_VALUE) {
433                 return true;
434             }
435             return false;
436         }
437         return false;
438     }
439
440     private static boolean componentAttributesIncludePassThruAttribute(
441             Map JavaDoc componentAttributes, List JavaDoc passThru) {
442         Object JavaDoc componentAttributeKey;
443         Object JavaDoc componentAttributeValue;
444         Iterator JavaDoc attributeKeys = componentAttributes.keySet().iterator();
445         while (attributeKeys.hasNext()) {
446             componentAttributeKey = attributeKeys.next();
447             if (passThru.contains(componentAttributeKey)) {
448                 componentAttributeValue =
449                         componentAttributes.get(componentAttributeKey);
450                 if ((componentAttributeValue != null) &&
451                     (componentAttributeValue != "")) {
452                     return true;
453                 }
454             }
455         }
456         return false;
457     }
458
459     static final List JavaDoc getpassThruAttributeNames() {
460         return passThruAttributeNames;
461     }
462 }
463
Popular Tags