1 14 15 package com.sun.facelets.tag.ui; 16 17 import java.io.IOException ; 18 import java.util.ArrayList ; 19 import java.util.LinkedHashMap ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.Map.Entry; 23 24 import javax.faces.component.UIComponentBase; 25 import javax.faces.context.FacesContext; 26 import javax.faces.context.ResponseWriter; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import com.sun.facelets.util.DevTools; 30 import com.sun.facelets.util.FastWriter; 31 32 36 public final class UIDebug extends UIComponentBase { 37 38 public final static String COMPONENT_TYPE = "facelets.ui.Debug"; 39 public final static String COMPONENT_FAMILY = "facelets"; 40 private static long nextId = System.currentTimeMillis(); 41 private final static String KEY = "facelets.ui.DebugOutput"; 42 public final static String DEFAULT_HOTKEY = "D"; 43 private String hotkey = DEFAULT_HOTKEY; 44 45 public UIDebug() { 46 super(); 47 this.setTransient(true); 48 this.setRendered(true); 49 this.setRendererType(null); 50 } 51 52 public String getFamily() { 53 return COMPONENT_FAMILY; 54 } 55 56 public List getChildren() { 57 return new ArrayList () { 58 public boolean add(Object o) { 59 throw new IllegalStateException ("<ui:debug> does not support children"); 60 } 61 62 public void add(int index, Object o) { 63 throw new IllegalStateException ("<ui:debug> does not support children"); 64 } 65 }; 66 } 67 68 public void encodeBegin(FacesContext faces) throws IOException { 69 70 String actionId = faces.getApplication().getViewHandler().getActionURL(faces, faces.getViewRoot().getViewId()); 71 72 StringBuffer sb = new StringBuffer (512); 73 sb.append("<script language=\"javascript\" type=\"text/javascript\">\n"); 74 sb.append("//<![CDATA[\n"); 75 sb.append("function faceletsDebug(URL) { day = new Date(); id = day.getTime(); eval(\"page\" + id + \" = window.open(URL, '\" + id + \"', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=600,left = 240,top = 212');\"); };"); 76 sb.append("var faceletsOrigKeyup = document.onkeyup; document.onkeyup = function(e) { if (window.event) e = window.event; if (String.fromCharCode(e.keyCode) == '" + this.getHotkey() + "' & e.shiftKey & e.ctrlKey) faceletsDebug('"); 77 sb.append(actionId); 78 sb.append('?'); 79 sb.append(KEY); 80 sb.append('='); 81 sb.append(writeDebugOutput(faces)); 82 sb.append("'); else if (faceletsOrigKeyup) faceletsOrigKeyup(e); };\n"); 83 sb.append("//]]>\n"); 84 sb.append("</script>\n"); 85 86 ResponseWriter writer = faces.getResponseWriter(); 87 writer.write(sb.toString()); 88 } 89 90 private static String writeDebugOutput(FacesContext faces) throws IOException { 91 FastWriter fw = new FastWriter(); 92 DevTools.debugHtml(fw, faces); 93 94 Map session = faces.getExternalContext().getSessionMap(); 95 Map debugs = (Map ) session.get(KEY); 96 if (debugs == null) { 97 debugs = new LinkedHashMap () { 98 protected boolean removeEldestEntry(Entry eldest) { 99 return (this.size() > 5); 100 } 101 }; 102 session.put(KEY, debugs); 103 } 104 String id = "" + nextId++; 105 debugs.put(id, fw.toString()); 106 return id; 107 } 108 109 private static String fetchDebugOutput(FacesContext faces, String id) { 110 Map session = faces.getExternalContext().getSessionMap(); 111 Map debugs = (Map ) session.get(KEY); 112 if (debugs != null) { 113 return (String ) debugs.get(id); 114 } 115 return null; 116 } 117 118 public static boolean debugRequest(FacesContext faces) { 119 String id = (String ) faces.getExternalContext().getRequestParameterMap().get(KEY); 120 if (id != null) { 121 Object resp = faces.getExternalContext().getResponse(); 122 if (!faces.getResponseComplete() 123 && resp instanceof HttpServletResponse ) { 124 try { 125 HttpServletResponse httpResp = (HttpServletResponse ) resp; 126 String page = fetchDebugOutput(faces, id); 127 if (page != null) { 128 httpResp.setContentType("text/html"); 129 httpResp.getWriter().write(page); 130 } else { 131 httpResp.setContentType("text/plain"); 132 httpResp.getWriter().write("No Debug Output Available"); 133 } 134 httpResp.flushBuffer(); 135 faces.responseComplete(); 136 } catch (IOException e) { 137 return false; 138 } 139 return true; 140 } 141 } 142 return false; 143 } 144 145 public String getHotkey() { 146 return this.hotkey; 147 } 148 149 public void setHotkey(String hotkey) { 150 this.hotkey = (hotkey != null) ? hotkey.toUpperCase() : ""; 151 } 152 153 } 154 | Popular Tags |