001: /*
002: * $Id: Util.java 55441 2004-10-24 16:14:10Z cziegeler $
003: */
004:
005: /*
006: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
007: *
008: * Redistribution and use in source and binary forms, with or
009: * without modification, are permitted provided that the following
010: * conditions are met:
011: *
012: * - Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * - Redistribution in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials
018: * provided with the distribution.
019: *
020: * Neither the name of Sun Microsystems, Inc. or the names of
021: * contributors may be used to endorse or promote products derived
022: * from this software without specific prior written permission.
023: *
024: * This software is provided "AS IS," without a warranty of any
025: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
026: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
027: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
028: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
029: * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
030: * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
031: * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
032: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
033: * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
034: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
035: * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
036: * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
037: *
038: * You acknowledge that this software is not designed, licensed or
039: * intended for use in the design, construction, operation or
040: * maintenance of any nuclear facility.
041: */
042:
043: // Util.java
044: package org.apache.cocoon.faces.samples.components.renderkit;
045:
046: import javax.faces.FactoryFinder;
047: import javax.faces.application.Application;
048: import javax.faces.application.ApplicationFactory;
049: import javax.faces.component.UIComponent;
050: import javax.faces.context.FacesContext;
051: import javax.faces.el.MethodBinding;
052: import javax.faces.el.ValueBinding;
053:
054: /**
055: * <B>Util</B> is a class which houses common functionality used by
056: * other classes.
057: *
058: * @version $Id: Util.java 55441 2004-10-24 16:14:10Z cziegeler $
059: */
060:
061: public class Util extends Object {
062:
063: //
064: // Protected Constants
065: //
066:
067: //
068: // Class Variables
069: //
070:
071: /**
072: * This array contains attributes that have a boolean value in JSP,
073: * but have have no value in HTML. For example "disabled" or
074: * "readonly". <P>
075: *
076: * @see #renderBooleanPassthruAttributes
077: */
078:
079: private static String booleanPassthruAttributes[] = { "disabled",
080: "readonly", "ismap" };
081:
082: /**
083: * This array contains attributes whose value is just rendered
084: * straight to the content. This array should only contain
085: * attributes that require no interpretation by the Renderer. If an
086: * attribute requires interpretation by a Renderer, it should be
087: * removed from this array.<P>
088: *
089: * @see #renderPassthruAttributes
090: */
091: private static String passthruAttributes[] = { "accesskey", "alt",
092: "cols", "height", "lang", "longdesc", "maxlength",
093: "onblur", "onchange", "onclick", "ondblclick", "onfocus",
094: "onkeydown", "onkeypress", "onkeyup", "onload",
095: "onmousedown", "onmousemove", "onmouseout", "onmouseover",
096: "onmouseup", "onreset", "onselect", "onsubmit", "onunload",
097: "rows",
098: "size",
099: "tabindex",
100: //"class", PENDING(rlubke) revisit this for JSFA105
101: "title", "style", "width", "dir", "rules", "frame",
102: "border", "cellspacing", "cellpadding", "summary",
103: "bgcolor", "usemap", "enctype", "accept-charset", "accept",
104: "target", "onsubmit", "onreset" };
105:
106: private static long id = 0;
107:
108: //
109: // Instance Variables
110: //
111:
112: // Attribute Instance Variables
113:
114: // Relationship Instance Variables
115:
116: //
117: // Constructors and Initializers
118: //
119:
120: private Util() {
121: throw new IllegalStateException();
122: }
123:
124: //
125: // Class methods
126: //
127: public static Class loadClass(String name)
128: throws ClassNotFoundException {
129: ClassLoader loader = Thread.currentThread()
130: .getContextClassLoader();
131: if (loader == null) {
132: return Class.forName(name);
133: } else {
134: return loader.loadClass(name);
135: }
136: }
137:
138: /**
139: * Generate a new identifier currently used to uniquely identify
140: * components.
141: */
142: public static synchronized String generateId() {
143: if (id == Long.MAX_VALUE) {
144: id = 0;
145: } else {
146: id++;
147: }
148: return Long.toHexString(id);
149: }
150:
151: /**
152: * NOTE: Commented out to remove JSTL dependency.
153: *
154: * Return a Locale instance using the following algorithm: <P>
155: *
156: * <UL>
157: *
158: * <LI>
159: *
160: * If this component instance has an attribute named "bundle",
161: * interpret it as a model reference to a LocalizationContext
162: * instance accessible via FacesContext.getModelValue().
163: *
164: * </LI>
165: *
166: * <LI>
167: *
168: * If FacesContext.getModelValue() returns a LocalizationContext
169: * instance, return its Locale.
170: *
171: * </LI>
172: *
173: * <LI>
174: *
175: * If FacesContext.getModelValue() doesn't return a
176: * LocalizationContext, return the FacesContext's Locale.
177: *
178: * </LI>
179: *
180: * </UL>
181:
182: public static Locale
183: getLocaleFromContextOrComponent(FacesContext context,
184: UIComponent component) {
185: Locale result = null;
186: String bundleName = null, bundleAttr = "bundle";
187:
188: // ParameterCheck.nonNull(context);
189: // ParameterCheck.nonNull(component);
190:
191: // verify our component has the proper attributes for bundle.
192: if (null !=
193: (bundleName = (String) component.getAttributes().get(bundleAttr))) {
194: // verify there is a Locale for this modelReference
195: javax.servlet.jsp.jstl.fmt.LocalizationContext locCtx = null;
196: if (null != (locCtx =
197: (javax.servlet.jsp.jstl.fmt.LocalizationContext)
198: (Util.getValueBinding(bundleName)).getValue(context))) {
199: result = locCtx.getLocale();
200: // Assert.assert_it(null != result);
201: }
202: }
203: if (null == result) {
204: result = context.getViewRoot().getLocale();
205: }
206:
207: return result;
208: }
209: */
210:
211: /**
212: * Render any boolean "passthru" attributes.
213: * <P>
214: *
215: * @see #passthruAttributes
216: */
217:
218: public static String renderBooleanPassthruAttributes(
219: FacesContext context, UIComponent component) {
220: int i = 0, len = booleanPassthruAttributes.length;
221: String value;
222: boolean this IsTheFirstAppend = true;
223: StringBuffer renderedText = new StringBuffer();
224:
225: for (i = 0; i < len; i++) {
226: if (null != (value = (String) component.getAttributes()
227: .get(booleanPassthruAttributes[i]))) {
228: if (this IsTheFirstAppend) {
229: // prepend ' '
230: renderedText.append(' ');
231: this IsTheFirstAppend = false;
232: }
233: if (Boolean.valueOf(value).booleanValue()) {
234: renderedText
235: .append(booleanPassthruAttributes[i] + ' ');
236: }
237: }
238: }
239:
240: return renderedText.toString();
241: }
242:
243: /**
244: * Render any "passthru" attributes, where we simply just output the
245: * raw name and value of the attribute. This method is aware of the
246: * set of HTML4 attributes that fall into this bucket. Examples are
247: * all the javascript attributes, alt, rows, cols, etc. <P>
248: *
249: * @return the rendererd attributes as specified in the component.
250: * Padded with leading and trailing ' '. If there are no passthru
251: * attributes in the component, return the empty String.
252: *
253: * @see #passthruAttributes
254: */
255:
256: public static String renderPassthruAttributes(FacesContext context,
257: UIComponent component) {
258: int i = 0, len = passthruAttributes.length;
259: String value;
260: boolean this IsTheFirstAppend = true;
261: StringBuffer renderedText = new StringBuffer();
262:
263: for (i = 0; i < len; i++) {
264: if (null != (value = (String) component.getAttributes()
265: .get(passthruAttributes[i]))) {
266: if (this IsTheFirstAppend) {
267: // prepend ' '
268: renderedText.append(' ');
269: this IsTheFirstAppend = false;
270: }
271: renderedText.append(passthruAttributes[i] + "=\""
272: + value + "\" ");
273: }
274: }
275:
276: return renderedText.toString();
277: }
278:
279: public static ValueBinding getValueBinding(String valueRef) {
280: ApplicationFactory af = (ApplicationFactory) FactoryFinder
281: .getFactory(FactoryFinder.APPLICATION_FACTORY);
282: Application a = af.getApplication();
283: return (a.createValueBinding(valueRef));
284: }
285:
286: public static MethodBinding createConstantMethodBinding(
287: String outcome) {
288: return new ConstantMethodBinding(outcome);
289: }
290:
291: //
292: // General Methods
293: //
294:
295: } // end of class Util
|