KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.faces.FacesException;
37 import javax.faces.component.UIComponent;
38 import javax.faces.context.ResponseWriter;
39 import java.io.IOException JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Arrays JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Map JavaDoc;
45
46 /**
47  * This class is used by ReponseWriter based renderers to render html
48  * pass thru attributes.
49  *
50  * @author gmccleary
51  */

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

139     public static void renderAttributes(ResponseWriter writer,
140                                         UIComponent uiComponent,
141                                         String JavaDoc[] excludedAttributes)
142             throws IOException JavaDoc {
143         renderNonBooleanAttributes(writer, uiComponent, excludedAttributes);
144         renderBooleanAttributes(writer, uiComponent, excludedAttributes);
145     }
146
147     /**
148      * ToDo: Render the icefaces onfocus handler to the root element. This
149      * should be restricted to input type elements and commandlinks.
150      *
151      * @param writer
152      * @throws IOException
153      */

154     public static void renderOnFocus(ResponseWriter writer)
155             throws IOException JavaDoc {
156         writer.writeAttribute("onfocus", "setFocus(this.id);", "onfocus");
157     }
158
159     /**
160      * ToDo: Render the icefaces onblur handler to the root element. This should
161      * be restricted to input type elements and commandlinks.
162      *
163      * @param writer
164      * @throws IOException
165      */

166     public static void renderOnBlur(ResponseWriter writer)
167             throws IOException JavaDoc {
168         writer.writeAttribute("onfocus", "setFocus('');", "onfocus");
169     }
170
171     private static void renderBooleanAttributes(
172             ResponseWriter writer, UIComponent uiComponent,
173             String JavaDoc[] excludedAttributes)
174             throws IOException JavaDoc {
175
176         if (writer == null) {
177             throw new FacesException("Null pointer exception");
178         }
179         if (uiComponent == null) {
180             throw new FacesException("Null pointer exception");
181         }
182
183         List JavaDoc excludedAttributesList = null;
184         if (excludedAttributes != null && excludedAttributes.length > 0)
185             excludedAttributesList = Arrays.asList(excludedAttributes);
186
187         Object JavaDoc nextPassThruAttributeName;
188         Object JavaDoc nextPassThruAttributeValue = null;
189         Iterator JavaDoc passThruNameIterator =
190                 booleanPassThruAttributeNames.iterator();
191         boolean primitiveAttributeValue;
192
193         while (passThruNameIterator.hasNext()) {
194             nextPassThruAttributeName = (passThruNameIterator.next());
195             if (excludedAttributesList != null) {
196                 if (excludedAttributesList.contains(nextPassThruAttributeName))
197                     continue;
198             }
199             nextPassThruAttributeValue = uiComponent.getAttributes().get(
200                     nextPassThruAttributeName);
201             if (nextPassThruAttributeValue != null) {
202                 if (nextPassThruAttributeValue instanceof Boolean JavaDoc) {
203                     primitiveAttributeValue = ((Boolean JavaDoc)
204                             nextPassThruAttributeValue).booleanValue();
205                 } else {
206                     if (!(nextPassThruAttributeValue instanceof String JavaDoc)) {
207                         nextPassThruAttributeValue =
208                                 nextPassThruAttributeValue.toString();
209                     }
210                     primitiveAttributeValue = (new Boolean JavaDoc((String JavaDoc)
211                             nextPassThruAttributeValue)).booleanValue();
212                 }
213                 if (primitiveAttributeValue) {
214                     writer.writeAttribute(nextPassThruAttributeName.toString(),
215                                           nextPassThruAttributeValue,
216                                           nextPassThruAttributeName.toString());
217                 }
218
219             }
220         }
221     }
222
223     private static void renderNonBooleanAttributes(
224             ResponseWriter writer, UIComponent uiComponent,
225             String JavaDoc[] excludedAttributes)
226
227             throws IOException JavaDoc {
228         if (writer == null) {
229             throw new FacesException("Null pointer exception");
230         }
231
232         if (uiComponent == null) {
233             throw new FacesException("Component instance is null");
234         }
235
236         List JavaDoc excludedAttributesList = null;
237         if (excludedAttributes != null && excludedAttributes.length > 0)
238             excludedAttributesList = Arrays.asList(excludedAttributes);
239
240         Object JavaDoc nextPassThruAttributeName = null;
241         Object JavaDoc nextPassThruAttributeValue = null;
242         Iterator JavaDoc passThruNameIterator = passThruAttributeNames.iterator();
243         while (passThruNameIterator.hasNext()) {
244             nextPassThruAttributeName = (passThruNameIterator.next());
245             if (excludedAttributesList != null) {
246                 if (excludedAttributesList.contains(nextPassThruAttributeName))
247                     continue;
248             }
249             nextPassThruAttributeValue =
250                     uiComponent.getAttributes().get(nextPassThruAttributeName);
251             // Only render non-null attributes.
252
// Some components have attribute values
253
// set to the Wrapper classes' minimum value - don't render
254
// an attribute with this sentinel value.
255
if (nextPassThruAttributeValue != null &&
256                 !attributeValueIsSentinel(nextPassThruAttributeValue)) {
257                 writer.writeAttribute(
258                         nextPassThruAttributeName.toString(),
259                         nextPassThruAttributeValue,
260                         nextPassThruAttributeValue.toString());
261             }
262         }
263     }
264
265     /**
266      * Determine whether any of the attributes defined for the UIComponent
267      * instance are pass thru attributes.
268      *
269      * @param uiComponent
270      * @return true if the UIComponent parameter has one or more attributes
271      * defined that are pass thru attributes
272      */

273     public static boolean passThruAttributeExists(UIComponent uiComponent) {
274         if (uiComponent == null) {
275             return false;
276         }
277         Map JavaDoc componentAttributes = uiComponent.getAttributes();
278         if (componentAttributes.size() <= 0) {
279             return false;
280         }
281         if (componentAttributesIncludePassThruAttribute(componentAttributes,
282                                                         passThruAttributeNames))
283         {
284             return true;
285         }
286         if (componentAttributesIncludePassThruAttribute(componentAttributes,
287                                                         booleanPassThruAttributeNames))
288         {
289             return true;
290         }
291         return false;
292     }
293
294     private static boolean attributeValueIsSentinel(Object JavaDoc value) {
295         if (value == null) {
296             return false;
297         }
298         if (value instanceof Boolean JavaDoc) {
299             if (((Boolean JavaDoc) value).booleanValue() == false) {
300                 return true;
301             }
302             return false;
303         }
304         if (value instanceof Number JavaDoc) {
305             if (value instanceof Integer JavaDoc) {
306                 if (((Integer JavaDoc) value).intValue() == Integer.MIN_VALUE) {
307                     return true;
308                 }
309                 return false;
310             }
311             if (value instanceof Long JavaDoc) {
312                 if (((Long JavaDoc) value).longValue() == Long.MIN_VALUE) {
313                     return true;
314                 }
315                 return false;
316             }
317             if (value instanceof Short JavaDoc) {
318                 if (((Short JavaDoc) value).shortValue() == Short.MIN_VALUE) {
319                     return true;
320                 }
321                 return false;
322             }
323             if (value instanceof Float JavaDoc) {
324                 if (((Float JavaDoc) value).floatValue() == Float.MIN_VALUE) {
325                     return true;
326                 }
327                 return false;
328             }
329             if (value instanceof Double JavaDoc) {
330                 if (((Double JavaDoc) value).doubleValue() == Double.MIN_VALUE) {
331                     return true;
332                 }
333                 return false;
334             }
335             if (value instanceof Byte JavaDoc) {
336                 if (((Byte JavaDoc) value).byteValue() == Byte.MIN_VALUE) {
337                     return true;
338                 }
339                 return false;
340             }
341         }
342         if (value instanceof Character JavaDoc) {
343             if (((Character JavaDoc) value).charValue() == Character.MIN_VALUE) {
344                 return true;
345             }
346             return false;
347         }
348         return false;
349     }
350
351     private static boolean componentAttributesIncludePassThruAttribute(
352             Map JavaDoc componentAttributes, List JavaDoc passThru) {
353         Object JavaDoc componentAttributeKey;
354         Object JavaDoc componentAttributeValue;
355         Iterator JavaDoc attributeKeys = componentAttributes.keySet().iterator();
356         while (attributeKeys.hasNext()) {
357             componentAttributeKey = attributeKeys.next();
358             if (passThru.contains(componentAttributeKey)) {
359                 componentAttributeValue =
360                         componentAttributes.get(componentAttributeKey);
361                 if ((componentAttributeValue != null) &&
362                     (componentAttributeValue != "")) {
363                     return true;
364                 }
365             }
366         }
367         return false;
368     }
369
370     static final List JavaDoc getpassThruAttributeNames() {
371         return passThruAttributeNames;
372     }
373 }
374
Popular Tags