1 33 34 package com.icesoft.faces.renderkit.dom_html_basic; 35 36 import com.icesoft.faces.context.DOMContext; 37 import com.icesoft.faces.util.Debug; 38 import org.w3c.dom.Element ; 39 40 import javax.faces.component.UIComponent; 41 import javax.faces.component.UIGraphic; 42 import javax.faces.context.FacesContext; 43 import javax.faces.context.ResponseWriter; 44 import java.io.IOException ; 45 46 public class ImageRenderer extends DomBasicRenderer { 47 48 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) 49 throws IOException { 50 51 validateParameters(facesContext, uiComponent, UIGraphic.class); 52 UIGraphic uiGraphic = (UIGraphic) uiComponent; 53 54 DOMContext domContext = 55 DOMContext.attachDOMContext(facesContext, uiComponent); 56 if (!domContext.isInitialized()) { 57 Element root = domContext.createElement("img"); 58 domContext.setRootNode(root); 59 } 60 Element root = (Element ) domContext.getRootNode(); 61 62 setRootElementId(facesContext, root, uiGraphic); 63 64 String srcAttribute = processSrcAttribute(facesContext, uiGraphic); 65 root.setAttribute("src", srcAttribute); 66 67 String altAttribute = (String ) uiComponent.getAttributes().get("alt"); 68 if (altAttribute == null) { 69 altAttribute = ""; 70 } 71 root.setAttribute("alt", altAttribute); 72 73 String styleClass = String.valueOf(uiComponent.getAttributes().get("styleClass")); 74 if (styleClass != null) { 75 root.setAttribute("class", styleClass); 76 } 77 78 ResponseWriter responseWriter = facesContext.getResponseWriter(); 79 Debug.assertTrue(responseWriter != null, "ResponseWriter is null"); 80 PassThruAttributeRenderer 81 .renderAttributes(facesContext, uiComponent, null); 82 83 domContext.stepOver(); 84 85 domContext.streamWrite(facesContext, uiComponent); 86 } 87 88 public void encodeChildren(FacesContext facesContext, 89 UIComponent uiComponent) { 90 validateParameters(facesContext, uiComponent, UIGraphic.class); 91 } 92 93 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) 94 throws IOException { 95 validateParameters(facesContext, uiComponent, UIGraphic.class); 96 } 97 98 private String processSrcAttribute(FacesContext facesContext, UIGraphic 99 uiGraphic) { 100 String value = (String ) uiGraphic.getValue(); 101 if (value == null) { 103 value = uiGraphic.getUrl(); 104 } 105 if (value != null) { 106 value = facesContext.getApplication().getViewHandler() 107 .getResourceURL(facesContext, value); 108 return value; 109 } else { 110 return ""; 111 } 112 } 113 } 114 115 | Popular Tags |