1 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 ; 14 15 19 20 public class Util extends Object { 21 22 29 private static String booleanPassthruAttributes[] = { 30 31 "disabled", 32 "readonly", 33 "ismap" 34 35 }; 36 37 46 47 private static String 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 (); 102 } 103 104 107 public static Class loadClass(String name) throws ClassNotFoundException { 108 ClassLoader 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 121 public static synchronized String generateId() { 122 if (id == Long.MAX_VALUE) { 123 id = 0; 124 } else { 125 id++; 126 } 127 return Long.toHexString(id); 128 } 129 130 161 public static Locale 162 getLocaleFromContextOrComponent(FacesContext context, 163 UIComponent component) { 164 Locale result = null; 165 String bundleName = null, bundleAttr = "bundle"; 166 167 if (null != 169 (bundleName = (String ) component.getAttributes().get(bundleAttr))) { 170 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 190 public static String renderBooleanPassthruAttributes(FacesContext context, 191 UIComponent component) { 192 int i = 0, len = booleanPassthruAttributes.length; 193 String value; 194 boolean thisIsTheFirstAppend = true; 195 StringBuffer renderedText = new StringBuffer (); 196 197 for (i = 0; i < len; i++) { 198 if (null != (value = (String ) 199 component.getAttributes().get(booleanPassthruAttributes[i]))) { 200 if (thisIsTheFirstAppend) { 201 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 225 226 227 public static String renderPassthruAttributes(FacesContext context, 228 UIComponent component) { 229 int i = 0, len = passthruAttributes.length; 230 String value; 231 boolean thisIsTheFirstAppend = true; 232 StringBuffer renderedText = new StringBuffer (); 233 234 for (i = 0; i < len; i++) { 235 if (null != (value = (String ) 236 component.getAttributes().get(passthruAttributes[i]))) { 237 if (thisIsTheFirstAppend) { 238 renderedText.append(' '); 240 thisIsTheFirstAppend = false; 241 } 242 renderedText.append(passthruAttributes[i] + "=\"" + value + 243 "\" "); 244 } 245 } 246 247 return renderedText.toString(); 248 } 249 250 254 public static ValueBinding getValueBinding(String valueRef) { 255 ApplicationFactory af = (ApplicationFactory) 256 FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); 257 Application a = af.getApplication(); 258 return (a.createValueBinding(valueRef)); 259 } 260 261 264 public static MethodBinding createConstantMethodBinding(String outcome) { 265 return new ConstantMethodBinding(outcome); 266 } 267 268 269 } 271 | Popular Tags |