KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > faces > samples > components > renderkit > Util


1 /*
2  * $Id: Util.java 55441 2004-10-24 16:14:10Z cziegeler $
3  */

4
5 /*
6  * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
7  *
8  * Redistribution and use in source and binary forms, with or
9  * without modification, are permitted provided that the following
10  * conditions are met:
11  *
12  * - Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * - Redistribution in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials
18  * provided with the distribution.
19  *
20  * Neither the name of Sun Microsystems, Inc. or the names of
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * This software is provided "AS IS," without a warranty of any
25  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
26  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
28  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
29  * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
30  * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
31  * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
32  * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
33  * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
34  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
35  * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
36  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37  *
38  * You acknowledge that this software is not designed, licensed or
39  * intended for use in the design, construction, operation or
40  * maintenance of any nuclear facility.
41  */

42
43 // Util.java
44

45 package org.apache.cocoon.faces.samples.components.renderkit;
46
47 import javax.faces.FactoryFinder;
48 import javax.faces.application.Application;
49 import javax.faces.application.ApplicationFactory;
50 import javax.faces.component.UIComponent;
51 import javax.faces.context.FacesContext;
52 import javax.faces.el.MethodBinding;
53 import javax.faces.el.ValueBinding;
54
55 /**
56  * <B>Util</B> is a class which houses common functionality used by
57  * other classes.
58  *
59  * @version $Id: Util.java 55441 2004-10-24 16:14:10Z cziegeler $
60  */

61
62 public class Util extends Object JavaDoc {
63
64 //
65
// Protected Constants
66
//
67

68 //
69
// Class Variables
70
//
71

72     /**
73      * This array contains attributes that have a boolean value in JSP,
74      * but have have no value in HTML. For example "disabled" or
75      * "readonly". <P>
76      *
77      * @see #renderBooleanPassthruAttributes
78      */

79
80     private static String JavaDoc booleanPassthruAttributes[] = {
81         "disabled",
82         "readonly",
83         "ismap"
84     };
85
86     /**
87      * This array contains attributes whose value is just rendered
88      * straight to the content. This array should only contain
89      * attributes that require no interpretation by the Renderer. If an
90      * attribute requires interpretation by a Renderer, it should be
91      * removed from this array.<P>
92      *
93      * @see #renderPassthruAttributes
94      */

95     private static String JavaDoc passthruAttributes[] = {
96         "accesskey",
97         "alt",
98         "cols",
99         "height",
100         "lang",
101         "longdesc",
102         "maxlength",
103         "onblur",
104         "onchange",
105         "onclick",
106         "ondblclick",
107         "onfocus",
108         "onkeydown",
109         "onkeypress",
110         "onkeyup",
111         "onload",
112         "onmousedown",
113         "onmousemove",
114         "onmouseout",
115         "onmouseover",
116         "onmouseup",
117         "onreset",
118         "onselect",
119         "onsubmit",
120         "onunload",
121         "rows",
122         "size",
123         "tabindex",
124         //"class", PENDING(rlubke) revisit this for JSFA105
125
"title",
126         "style",
127         "width",
128         "dir",
129         "rules",
130         "frame",
131         "border",
132         "cellspacing",
133         "cellpadding",
134         "summary",
135         "bgcolor",
136         "usemap",
137         "enctype",
138         "accept-charset",
139         "accept",
140         "target",
141         "onsubmit",
142         "onreset"
143     };
144
145     private static long id = 0;
146
147
148 //
149
// Instance Variables
150
//
151

152 // Attribute Instance Variables
153

154 // Relationship Instance Variables
155

156 //
157
// Constructors and Initializers
158
//
159

160     private Util() {
161         throw new IllegalStateException JavaDoc();
162     }
163
164 //
165
// Class methods
166
//
167
public static Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
168         ClassLoader JavaDoc loader =
169             Thread.currentThread().getContextClassLoader();
170         if (loader == null) {
171             return Class.forName(name);
172         } else {
173             return loader.loadClass(name);
174         }
175     }
176
177
178     /**
179      * Generate a new identifier currently used to uniquely identify
180      * components.
181      */

182     public static synchronized String JavaDoc generateId() {
183         if (id == Long.MAX_VALUE) {
184             id = 0;
185         } else {
186             id++;
187         }
188         return Long.toHexString(id);
189     }
190
191
192     /**
193      * NOTE: Commented out to remove JSTL dependency.
194      *
195      * Return a Locale instance using the following algorithm: <P>
196      *
197      * <UL>
198      *
199      * <LI>
200      *
201      * If this component instance has an attribute named "bundle",
202      * interpret it as a model reference to a LocalizationContext
203      * instance accessible via FacesContext.getModelValue().
204      *
205      * </LI>
206      *
207      * <LI>
208      *
209      * If FacesContext.getModelValue() returns a LocalizationContext
210      * instance, return its Locale.
211      *
212      * </LI>
213      *
214      * <LI>
215      *
216      * If FacesContext.getModelValue() doesn't return a
217      * LocalizationContext, return the FacesContext's Locale.
218      *
219      * </LI>
220      *
221      * </UL>
222
223     public static Locale
224         getLocaleFromContextOrComponent(FacesContext context,
225                                         UIComponent component) {
226         Locale result = null;
227         String bundleName = null, bundleAttr = "bundle";
228
229 // ParameterCheck.nonNull(context);
230 // ParameterCheck.nonNull(component);
231
232         // verify our component has the proper attributes for bundle.
233         if (null !=
234             (bundleName = (String) component.getAttributes().get(bundleAttr))) {
235             // verify there is a Locale for this modelReference
236             javax.servlet.jsp.jstl.fmt.LocalizationContext locCtx = null;
237             if (null != (locCtx =
238                 (javax.servlet.jsp.jstl.fmt.LocalizationContext)
239                 (Util.getValueBinding(bundleName)).getValue(context))) {
240                 result = locCtx.getLocale();
241 // Assert.assert_it(null != result);
242             }
243         }
244         if (null == result) {
245             result = context.getViewRoot().getLocale();
246         }
247
248         return result;
249     }
250      */

251
252
253     /**
254      * Render any boolean "passthru" attributes.
255      * <P>
256      *
257      * @see #passthruAttributes
258      */

259
260     public static String JavaDoc renderBooleanPassthruAttributes(FacesContext context,
261                                                          UIComponent component) {
262         int i = 0, len = booleanPassthruAttributes.length;
263         String JavaDoc value;
264         boolean thisIsTheFirstAppend = true;
265         StringBuffer JavaDoc renderedText = new StringBuffer JavaDoc();
266
267         for (i = 0; i < len; i++) {
268             if (null != (value = (String JavaDoc)
269                 component.getAttributes().get(booleanPassthruAttributes[i]))) {
270                 if (thisIsTheFirstAppend) {
271                     // prepend ' '
272
renderedText.append(' ');
273                     thisIsTheFirstAppend = false;
274                 }
275                 if (Boolean.valueOf(value).booleanValue()) {
276                     renderedText.append(booleanPassthruAttributes[i] + ' ');
277                 }
278             }
279         }
280
281         return renderedText.toString();
282     }
283
284
285     /**
286      * Render any "passthru" attributes, where we simply just output the
287      * raw name and value of the attribute. This method is aware of the
288      * set of HTML4 attributes that fall into this bucket. Examples are
289      * all the javascript attributes, alt, rows, cols, etc. <P>
290      *
291      * @return the rendererd attributes as specified in the component.
292      * Padded with leading and trailing ' '. If there are no passthru
293      * attributes in the component, return the empty String.
294      *
295      * @see #passthruAttributes
296      */

297
298     public static String JavaDoc renderPassthruAttributes(FacesContext context,
299                                                   UIComponent component) {
300         int i = 0, len = passthruAttributes.length;
301         String JavaDoc value;
302         boolean thisIsTheFirstAppend = true;
303         StringBuffer JavaDoc renderedText = new StringBuffer JavaDoc();
304
305         for (i = 0; i < len; i++) {
306             if (null != (value = (String JavaDoc)
307                 component.getAttributes().get(passthruAttributes[i]))) {
308                 if (thisIsTheFirstAppend) {
309                     // prepend ' '
310
renderedText.append(' ');
311                     thisIsTheFirstAppend = false;
312                 }
313                 renderedText.append(passthruAttributes[i] + "=\"" + value +
314                                     "\" ");
315             }
316         }
317
318         return renderedText.toString();
319     }
320
321
322     public static ValueBinding getValueBinding(String JavaDoc valueRef) {
323         ApplicationFactory af = (ApplicationFactory)
324             FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
325         Application a = af.getApplication();
326         return (a.createValueBinding(valueRef));
327     }
328
329
330     public static MethodBinding createConstantMethodBinding(String JavaDoc outcome) {
331         return new ConstantMethodBinding(outcome);
332     }
333
334 //
335
// General Methods
336
//
337

338 } // end of class Util
339
Popular Tags