KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > catalog > validator > Util


1 /* Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at: http://developer.sun.com/berkeley_license.html
2 $Id: Util.java,v 1.2 2005/03/23 14:49:22 jenniferb Exp $ */

3
4 package com.sun.j2ee.blueprints.catalog.validator;
5
6 import javax.faces.FactoryFinder;
7 import javax.faces.application.Application;
8 import javax.faces.application.ApplicationFactory;
9 import javax.faces.component.UIComponent;
10 import javax.faces.context.FacesContext;
11 import javax.faces.el.MethodBinding;
12 import javax.faces.el.ValueBinding;
13 import java.util.Locale JavaDoc;
14
15 /**
16  * This class provides common functionality used by
17  * other classes.
18  */

19
20 public class Util extends Object JavaDoc {
21     
22     /**
23      * This array contains attributes that have a boolean value in JSP,
24      * but have have no value in HTML. For example "disabled" or
25      * "readonly". <P>
26      *
27      * @see renderBooleanPassthruAttributes
28      */

29     private static String JavaDoc booleanPassthruAttributes[] = {
30
31         "disabled",
32         "readonly",
33         "ismap"
34
35     };
36
37     /**
38      * This array contains attributes whose values are rendered
39      * straight to the content. This array should only contain
40      * attributes that require no interpretation by the renderer. If an
41      * attribute requires interpretation by a renderer, it should be
42      * removed from this array.<P>
43      *
44      * @see renderPassthruAttributes
45      */

46
47     private static String JavaDoc passthruAttributes[] = {
48
49         "accesskey",
50         "alt",
51         "cols",
52         "height",
53         "lang",
54         "longdesc",
55         "maxlength",
56         "onblur",
57         "onchange",
58         "onclick",
59         "ondblclick",
60         "onfocus",
61         "onkeydown",
62         "onkeypress",
63         "onkeyup",
64         "onload",
65         "onmousedown",
66         "onmousemove",
67         "onmouseout",
68         "onmouseover",
69         "onmouseup",
70         "onreset",
71         "onselect",
72         "onsubmit",
73         "onunload",
74         "rows",
75         "size",
76         "tabindex",
77         "title",
78         "style",
79         "width",
80         "dir",
81         "rules",
82         "frame",
83         "border",
84         "cellspacing",
85         "cellpadding",
86         "summary",
87         "bgcolor",
88         "usemap",
89         "enctype",
90         "accept-charset",
91         "accept",
92         "target",
93         "onsubmit",
94         "onreset"
95     };
96
97
98     private static long id = 0;
99
100     private Util() {
101         throw new IllegalStateException JavaDoc();
102     }
103
104     /**
105      * Loads the class of the specified <code>name</code> from the current thread.
106      */

107     public static Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
108         ClassLoader JavaDoc loader =
109             Thread.currentThread().getContextClassLoader();
110         if (loader == null) {
111             return Class.forName(name);
112         } else {
113             return loader.loadClass(name);
114         }
115     }
116
117     /**
118      * Generates a new identifier currently used to uniquely identify
119      * components.
120      */

121     public static synchronized String JavaDoc generateId() {
122         if (id == Long.MAX_VALUE) {
123             id = 0;
124         } else {
125             id++;
126         }
127         return Long.toHexString(id);
128     }
129
130     /**
131      * Returns a Locale instance using the following algorithm: <P>
132      *
133      * <UL>
134      *
135      * <LI>
136      *
137      * If this component instance has an attribute named "bundle",
138      * interpret it as a model reference to a LocalizationContext
139      * instance accessible via FacesContext.getModelValue().
140      *
141      * </LI>
142      *
143      * <LI>
144      *
145      * If <code>FacesContext.getModelValue</code> returns a
146      * <code>LocalizationContext</code>
147      * instance, return its Locale.
148      *
149      * </LI>
150      *
151      * <LI>
152      *
153      * If <code>FacesContext.getModelValue</code> doesn't return a
154      * <code>LocalizationContext</code>, return the <code>FacesContext</code>
155      * instance's <code>Locale</code>.
156      *
157      * </LI>
158      *
159      * </UL>
160      */

161     public static Locale JavaDoc
162         getLocaleFromContextOrComponent(FacesContext context,
163                                         UIComponent component) {
164         Locale JavaDoc result = null;
165         String JavaDoc bundleName = null, bundleAttr = "bundle";
166
167     // verify our component has the proper attributes for bundle.
168
if (null !=
169             (bundleName = (String JavaDoc) component.getAttributes().get(bundleAttr))) {
170             // verify there is a Locale for this modelReference
171
javax.servlet.jsp.jstl.fmt.LocalizationContext locCtx = null;
172             if (null != (locCtx =
173                 (javax.servlet.jsp.jstl.fmt.LocalizationContext)
174                 (Util.getValueBinding(bundleName)).getValue(context))) {
175                 result = locCtx.getLocale();
176             }
177         }
178         if (null == result) {
179             result = context.getViewRoot().getLocale();
180         }
181         return result;
182     }
183
184     /**
185      * Renders any boolean "passthru" attributes.
186      * <P>
187      *
188      * @see passthruAttributes
189      */

190     public static String JavaDoc renderBooleanPassthruAttributes(FacesContext context,
191                                                          UIComponent component) {
192         int i = 0, len = booleanPassthruAttributes.length;
193         String JavaDoc value;
194         boolean thisIsTheFirstAppend = true;
195         StringBuffer JavaDoc renderedText = new StringBuffer JavaDoc();
196     
197         for (i = 0; i < len; i++) {
198             if (null != (value = (String JavaDoc)
199                 component.getAttributes().get(booleanPassthruAttributes[i]))) {
200                 if (thisIsTheFirstAppend) {
201                     // prepend ' '
202
renderedText.append(' ');
203                     thisIsTheFirstAppend = false;
204                 }
205                 if (Boolean.valueOf(value).booleanValue()) {
206                     renderedText.append(booleanPassthruAttributes[i] + ' ');
207                 }
208             }
209         }
210         return renderedText.toString();
211     }
212
213     /**
214      * Renders any "passthru" attributes, where we simply just output the
215      * raw name and value of the attribute. This method is aware of the
216      * set of HTML4 attributes that fall into this bucket. Examples are
217      * all the javascript attributes, alt, rows, cols, etc. <P>
218      *
219      * @return the rendered attributes as specified in the component.
220      * Padded with leading and trailing ' '. If there are no passthru
221      * attributes in the component, return the empty String.
222      *
223      * @see passthruAttributes
224      */

225
226
227     public static String JavaDoc renderPassthruAttributes(FacesContext context,
228                                                   UIComponent component) {
229         int i = 0, len = passthruAttributes.length;
230         String JavaDoc value;
231         boolean thisIsTheFirstAppend = true;
232         StringBuffer JavaDoc renderedText = new StringBuffer JavaDoc();
233
234         for (i = 0; i < len; i++) {
235             if (null != (value = (String JavaDoc)
236                 component.getAttributes().get(passthruAttributes[i]))) {
237                 if (thisIsTheFirstAppend) {
238                     // prepend ' '
239
renderedText.append(' ');
240                     thisIsTheFirstAppend = false;
241                 }
242                 renderedText.append(passthruAttributes[i] + "=\"" + value +
243                                     "\" ");
244             }
245         }
246
247         return renderedText.toString();
248     }
249
250     /**
251      * Returns the <code>ValueBinding</code> used to calculate the value
252      * for the specified attribute or property.
253      */

254     public static ValueBinding getValueBinding(String JavaDoc valueRef) {
255         ApplicationFactory af = (ApplicationFactory)
256             FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
257         Application a = af.getApplication();
258         return (a.createValueBinding(valueRef));
259     }
260
261     /**
262      * Creates a new <code>ConstantMethodBinding</code> object.
263      */

264     public static MethodBinding createConstantMethodBinding(String JavaDoc outcome) {
265         return new ConstantMethodBinding(outcome);
266     }
267
268
269 } // end of class Util
270

271
Popular Tags