1 6 14 15 package com.sun.javaee.blueprints.components.ui.example; 16 17 import java.io.IOException ; 18 import java.util.Map ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.net.URL ; 22 import java.io.Writer ; 23 import java.io.BufferedReader ; 24 import java.io.InputStreamReader ; 25 import java.io.InputStream ; 26 import java.net.URLConnection ; 27 28 import javax.faces.component.UIComponent; 29 import javax.faces.component.UIOutput; 30 import javax.faces.context.FacesContext; 31 import javax.faces.render.Renderer; 32 import javax.faces.context.ResponseWriter; 33 import javax.faces.context.ResponseStream; 34 import javax.faces.component.UIViewRoot; 35 import javax.faces.component.html.HtmlInputText; 36 import javax.el.ValueExpression; 37 38 42 public class CompBRenderer extends Renderer { 43 private static final boolean bDebug=false; 44 private static final String RENDERED_SCRIPT_KEY = "JSF_EXAMPLE_COMPB"; 45 private static final String COMPB_SCRIPT_RESOURCE="/jsf-example/compB/compB.js"; 46 private static final String COMPB_CSS_RESOURCE="/jsf-example/compB/compB.css"; 47 private static final String COMPB_TEMPLATE_RESOURCE="/META-INF/jsf-example/compB/compB_template.txt"; 48 49 50 56 public void encodeEnd(FacesContext context, UIComponent component) throws IOException { 57 if(bDebug) System.out.println("\n *** START encodeEnd"); 58 if ((context == null) || (component == null)) { 59 throw new NullPointerException (); 60 } 61 62 UIOutput outComp = (UIOutput)component; 63 ResponseWriter writer = context.getResponseWriter(); 64 65 renderScriptOnce(writer, component, context); 67 String clientId=component.getClientId(context); 68 69 URL urlxx=CompBRenderer.class.getResource(COMPB_TEMPLATE_RESOURCE); 71 String sxTemplate=readInFragmentAsString(urlxx); 72 73 writer.startElement("div", component); 74 writer.writeAttribute("id", clientId, "id"); 75 76 String styleClass=(String )getAttributeValue(context, component, "styleClass"); 77 if (styleClass != null) { 78 writer.writeAttribute("class", "bpui_compB_popup " + styleClass, "styleClass"); 80 } else { 81 writer.writeAttribute("class", "bpui_compB_popup", "styleClass"); 83 } 84 String style=(String )getAttributeValue(context, component, "style"); 85 if (style != null) { 86 writer.writeAttribute("style", style, "style"); 87 } 88 89 HashMap hmSub=new HashMap (); 91 hmSub.put("%%%ID%%%", clientId); 92 93 writer.write(parseString(sxTemplate, hmSub, false)); 95 96 writer.endElement("div"); 98 99 String url=(String )getAttributeValue(context, component, "url"); 101 if (url != null) { 102 url=context.getExternalContext().getRequestContextPath() + "/" + url; 103 } 104 105 writer.write("\n"); 106 writer.startElement("script", component); 107 writer.writeAttribute("type", "text/javascript", null); 108 writer.write("\n"); 109 writer.write("bpui.compB['" + clientId + "']=new bpui.compB.createPopup('"+ clientId +"', '"+ url +"');\n"); 110 writer.endElement("script"); 111 writer.write("\n"); 112 113 if(bDebug) System.out.println("\n *** END encodeEnd"); 114 } 115 116 117 120 private void renderScriptOnce(ResponseWriter writer, UIComponent component, FacesContext context) 121 throws IOException { 122 123 Map requestMap=context.getExternalContext().getRequestMap(); 126 Boolean scriptRendered=(Boolean )requestMap.get(RENDERED_SCRIPT_KEY); 127 128 if (scriptRendered != null && scriptRendered.equals(Boolean.TRUE)) { 129 return; 130 } 131 132 requestMap.put(RENDERED_SCRIPT_KEY, Boolean.TRUE); 133 String contextRoot=context.getExternalContext().getRequestContextPath(); 134 135 writer.startElement("script", component); 136 writer.writeAttribute("type", "text/javascript", null); 137 writer.writeAttribute("src", contextRoot + "/faces" + COMPB_SCRIPT_RESOURCE, null); 138 writer.endElement("script"); 139 writer.write("\n"); 140 141 writer.startElement("link", component); 142 writer.writeAttribute("type", "text/css", null); 143 writer.writeAttribute("rel", "stylesheet", null); 144 writer.writeAttribute("href", contextRoot + "/faces" + COMPB_CSS_RESOURCE, null); 145 writer.endElement("link"); 146 writer.write("\n"); 147 } 148 149 150 151 152 public static Object getAttributeValue(FacesContext context, UIComponent component, String key) { 153 Object value=null; 154 try { 155 ValueExpression valuexx=component.getValueExpression(key); 157 if(valuexx != null) { 158 value=valuexx.getValue(context.getELContext()); 159 } else { 160 value=component.getAttributes().get(key); 161 } 162 } catch(Exception ee) { 163 ee.printStackTrace(); 164 } 165 return value; 166 } 167 168 private static InputStream openStream(URL sxURL) throws IOException { 169 InputStream isx=null; 171 if(sxURL!= null) { 172 URLConnection urlConn = sxURL.openConnection(); 173 urlConn.setUseCaches(false); 174 isx=urlConn.getInputStream(); 175 } 176 return isx; 177 } 178 179 186 public static String parseString(String sxString, HashMap hmSub, boolean bLookup) { 187 188 int iPos=0; 189 String sxStringx=""; 190 191 if(bLookup) { 192 } else { 195 sxStringx=sxString; 196 } 197 198 if (sxStringx != null) { 199 Iterator iter=hmSub.keySet().iterator(); 201 String sxKey, sxValue; 202 while(iter.hasNext()) { 203 204 sxKey=(String )iter.next(); 206 sxValue=(String )hmSub.get(sxKey); 207 sxStringx=sxStringx.replaceAll(sxKey, sxValue); 208 } 209 } 210 return sxStringx; 211 } 212 213 214 220 public static String readInFragmentAsString(URL sxURL) throws IOException { 221 BufferedReader bfReader=null; 222 StringBuffer sxOut=new StringBuffer (); 223 224 try { 225 bfReader=new BufferedReader (new InputStreamReader (openStream(sxURL))); 226 227 int byteCnt=0; 228 char[] buffer=new char[4096]; 229 while ((byteCnt=bfReader.read(buffer)) != -1) { 230 if (byteCnt > 0) { 231 sxOut.append(buffer, 0, byteCnt); 232 } 233 } 234 } catch (IOException e) { 235 e.printStackTrace(); 236 throw e; 237 } finally { 238 if(bfReader != null) { 239 try { 240 bfReader.close(); 241 } catch (Exception ee){} 242 } 243 } 244 245 return sxOut.toString(); 246 } 247 } 248 | Popular Tags |