1 14 15 package com.sun.facelets.util; 16 17 import java.beans.BeanInfo ; 18 import java.beans.Introspector ; 19 import java.beans.PropertyDescriptor ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.FileNotFoundException ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.PrintWriter ; 25 import java.io.StringWriter ; 26 import java.io.Writer ; 27 import java.lang.reflect.Method ; 28 import java.text.DateFormat ; 29 import java.util.Arrays ; 30 import java.util.Collection ; 31 import java.util.Date ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 import java.util.SortedMap ; 35 import java.util.TreeMap ; 36 import java.util.logging.Logger ; 37 38 import javax.el.Expression; 39 import javax.faces.component.UIComponent; 40 import javax.faces.component.UIViewRoot; 41 import javax.faces.context.ExternalContext; 42 import javax.faces.context.FacesContext; 43 import javax.faces.el.MethodBinding; 44 import javax.faces.el.ValueBinding; 45 46 public final class DevTools { 47 48 private final static String TS = "<"; 49 50 private static final String ERROR_TEMPLATE = "META-INF/rsc/facelet-dev-error.xml"; 51 52 private static String [] ERROR_PARTS; 53 54 private static final String DEBUG_TEMPLATE = "META-INF/rsc/facelet-dev-debug.xml"; 55 56 private static String [] DEBUG_PARTS; 57 58 public DevTools() { 59 super(); 60 } 61 62 public static void main(String [] argv) throws Exception { 63 DevTools.init(); 64 } 65 66 private static void init() throws IOException { 67 if (ERROR_PARTS == null) { 68 ERROR_PARTS = splitTemplate(ERROR_TEMPLATE); 69 } 70 71 if (DEBUG_PARTS == null) { 72 DEBUG_PARTS = splitTemplate(DEBUG_TEMPLATE); 73 } 74 } 75 76 private static String [] splitTemplate(String rsc) throws IOException { 77 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(rsc); 78 if (is == null) { 79 throw new FileNotFoundException (rsc); 80 } 81 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 82 byte[] buff = new byte[512]; 83 int read; 84 while ((read = is.read(buff)) != -1) { 85 baos.write(buff, 0, read); 86 } 87 String str = baos.toString(); 88 return str.split("@@"); 89 } 90 91 public static void debugHtml(Writer writer, FacesContext faces, Exception e) throws IOException { 92 init(); 93 Date now = new Date (); 94 for (int i = 0; i < ERROR_PARTS.length; i++) { 95 if ("message".equals(ERROR_PARTS[i])) { 96 String msg = e.getMessage(); 97 if (msg != null) { 98 writer.write(msg.replaceAll("<", TS)); 99 } else { 100 writer.write(e.getClass().getName()); 101 } 102 } else if ("trace".equals(ERROR_PARTS[i])) { 103 writeException(writer, e); 104 } else if ("now".equals(ERROR_PARTS[i])) { 105 writer.write(DateFormat.getDateTimeInstance().format(now)); 106 } else if ("tree".equals(ERROR_PARTS[i])) { 107 writeComponent(writer, faces.getViewRoot()); 108 } else if ("vars".equals(ERROR_PARTS[i])) { 109 writeVariables(writer, faces); 110 } else { 111 writer.write(ERROR_PARTS[i]); 112 } 113 } 114 } 115 116 private static void writeException(Writer writer, Exception e) throws IOException { 117 StringWriter str = new StringWriter (256); 118 PrintWriter pstr = new PrintWriter (str); 119 e.printStackTrace(pstr); 120 pstr.close(); 121 writer.write(str.toString().replaceAll("<", TS)); 122 } 123 124 public static void debugHtml(Writer writer, FacesContext faces) throws IOException { 125 init(); 126 Date now = new Date (); 127 for (int i = 0; i < DEBUG_PARTS.length; i++) { 128 if ("message".equals(DEBUG_PARTS[i])) { 129 writer.write(faces.getViewRoot().getViewId()); 130 } else if ("now".equals(DEBUG_PARTS[i])) { 131 writer.write(DateFormat.getDateTimeInstance().format(now)); 132 } else if ("tree".equals(DEBUG_PARTS[i])) { 133 writeComponent(writer, faces.getViewRoot()); 134 } else if ("vars".equals(DEBUG_PARTS[i])) { 135 writeVariables(writer, faces); 136 } else { 137 writer.write(DEBUG_PARTS[i]); 138 } 139 } 140 } 141 142 private static void writeVariables(Writer writer, FacesContext faces) throws IOException { 143 ExternalContext ctx = faces.getExternalContext(); 144 writeVariables(writer, ctx.getRequestParameterMap(), "Request Parameters"); 145 writeVariables(writer, ctx.getRequestMap(), "Request Attributes"); 146 if (ctx.getSession(false) != null) { 147 writeVariables(writer, ctx.getSessionMap(), "Session Attributes"); 148 } 149 writeVariables(writer, ctx.getApplicationMap(), "Application Attributes"); 150 } 151 152 private static void writeVariables(Writer writer, Map vars, String caption) throws IOException { 153 writer.write("<table><caption>"); 154 writer.write(caption); 155 writer.write("</caption><thead><tr><th style=\"width: 10%; \">Name</th><th style=\"width: 90%; \">Value</th></tr></thead><tbody>"); 156 boolean written = false; 157 if (!vars.isEmpty()) { 158 SortedMap map = new TreeMap (vars); 159 Map.Entry entry = null; 160 String key = null; 161 for (Iterator itr = map.entrySet().iterator(); itr.hasNext(); ) { 162 entry = (Map.Entry ) itr.next(); 163 key = entry.getKey().toString(); 164 if (key.indexOf('.') == -1) { 165 writer.write("<tr><td>"); 166 writer.write(key.replaceAll("<", TS)); 167 writer.write("</td><td>"); 168 writer.write(entry.getValue().toString().replaceAll("<", TS)); 169 writer.write("</td></tr>"); 170 written = true; 171 } 172 } 173 } 174 if (!written) { 175 writer.write("<tr><td colspan=\"2\"><em>None</em></td></tr>"); 176 } 177 writer.write("</tbody></table>"); 178 } 179 180 private static void writeComponent(Writer writer, UIComponent c) throws IOException { 181 writer.write("<dl><dt"); 182 if (isText(c)) { 183 writer.write(" class=\"uicText\""); 184 } 185 writer.write(">"); 186 187 boolean hasChildren = c.getChildCount() > 0 || c.getFacets().size() > 0; 188 189 writeStart(writer, c, hasChildren); 190 writer.write("</dt>"); 191 if (hasChildren) { 192 if (c.getFacets().size() > 0) { 193 Map.Entry entry; 194 for (Iterator itr = c.getFacets().entrySet().iterator(); itr.hasNext(); ) { 195 entry = (Map.Entry ) itr.next(); 196 writer.write("<dd class=\"uicFacet\">"); 197 writer.write("<span>"); 198 writer.write((String ) entry.getKey()); 199 writer.write("</span>"); 200 writeComponent(writer, (UIComponent) entry.getValue()); 201 writer.write("</dd>"); 202 } 203 } 204 if (c.getChildCount() > 0) { 205 for (Iterator itr = c.getChildren().iterator(); itr.hasNext(); ) { 206 writer.write("<dd>"); 207 writeComponent(writer, (UIComponent) itr.next()); 208 writer.write("</dd>"); 209 } 210 } 211 writer.write("<dt>"); 212 writeEnd(writer, c); 213 writer.write("</dt>"); 214 } 215 writer.write("</dl>"); 216 } 217 218 private static void writeEnd(Writer writer, UIComponent c) throws IOException { 219 if (!isText(c)) { 220 writer.write(TS); 221 writer.write('/'); 222 writer.write(getName(c)); 223 writer.write('>'); 224 } 225 } 226 227 private final static String [] IGNORE = new String [] { "parent", "rendererType" }; 228 229 private static void writeAttributes(Writer writer, UIComponent c) { 230 try { 231 BeanInfo info = Introspector.getBeanInfo(c.getClass()); 232 PropertyDescriptor [] pd = info.getPropertyDescriptors(); 233 Method m = null; 234 Object v = null; 235 String str = null; 236 for (int i = 0; i < pd.length; i++) { 237 if (pd[i].getWriteMethod() != null && Arrays.binarySearch(IGNORE, pd[i].getName()) < 0) { 238 m = pd[i].getReadMethod(); 239 try { 240 v = m.invoke(c, null); 241 if (v != null) { 242 if (v instanceof Collection || v instanceof Map || v instanceof Iterator ) { 243 continue; 244 } 245 writer.write(" "); 246 writer.write(pd[i].getName()); 247 writer.write("=\""); 248 if (v instanceof Expression) { 249 str = ((Expression) v).getExpressionString(); 250 } else if (v instanceof ValueBinding) { 251 str = ((ValueBinding) v).getExpressionString(); 252 } else if (v instanceof MethodBinding) { 253 str = ((MethodBinding) v).getExpressionString(); 254 } else { 255 str = v.toString(); 256 } 257 writer.write(str.replaceAll("<", TS)); 258 writer.write("\""); 259 } 260 } catch (Exception e) { 261 } 263 } 264 } 265 266 ValueBinding binding = c.getValueBinding("binding"); 267 if (binding != null) { 268 writer.write(" binding=\""); 269 writer.write(binding.getExpressionString().replaceAll("<", TS)); 270 writer.write("\""); 271 } 272 } catch (Exception e) { 273 } 275 } 276 277 private static void writeStart(Writer writer, UIComponent c, boolean children) throws IOException { 278 if (isText(c)) { 279 String str = c.toString().trim(); 280 writer.write(str.replaceAll("<", TS)); 281 } else { 282 writer.write(TS); 283 writer.write(getName(c)); 284 writeAttributes(writer, c); 285 if (children) { 286 writer.write('>'); 287 } else { 288 writer.write("/>"); 289 } 290 } 291 } 292 293 private static String getName(UIComponent c) { 294 String nm = c.getClass().getName(); 295 return nm.substring(nm.lastIndexOf('.') + 1); 296 } 297 298 private static boolean isText(UIComponent c) { 299 return (c.getClass().getName().startsWith("com.sun.facelets.compiler")); 300 } 301 302 } 303 | Popular Tags |