1 33 34 package com.icesoft.faces.renderkit.dom_html_basic; 35 36 import javax.faces.component.UIComponent; 37 import javax.faces.component.UIOutput; 38 import javax.faces.context.FacesContext; 39 import javax.faces.context.ResponseWriter; 40 import java.io.IOException ; 41 import java.util.Iterator ; 42 import java.util.Map ; 43 44 public class OutputLinkRenderer extends DomBasicRenderer { 45 48 public boolean getRendersChildren() { 49 return true; 50 } 51 52 58 protected Object getValue(UIComponent uiComponent) { 59 return ((UIOutput) uiComponent).getValue(); 60 } 61 62 String clientId; 63 64 69 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) 70 throws IOException { 71 if (facesContext == null || uiComponent == null) { 72 throw new NullPointerException ( 73 "facesContext or uiComponent is null"); 74 } 75 76 UIOutput output = (UIOutput) uiComponent; 77 String linkVal = getValue(facesContext, uiComponent); 78 79 if (!output.isRendered()) return; 80 81 ResponseWriter writer = facesContext.getResponseWriter(); 82 83 if (writer == null) { 84 throw new NullPointerException ("ResponseWriter is null"); 85 } 86 87 writer.startElement("a", uiComponent); 88 writer.writeAttribute("id", uiComponent.getClientId(facesContext), 89 "id"); 90 91 if (null == linkVal || 0 == linkVal.length()) { 92 linkVal = ""; 93 } 94 95 clientId = output.getClientId(facesContext); 96 97 linkVal = appendParameters(facesContext, uiComponent, linkVal); 98 99 if (!checkDisabled(uiComponent)) 100 writer.writeURIAttribute("href", 101 facesContext 102 .getExternalContext().encodeResourceURL( 103 linkVal), "href"); 104 105 PassThruAttributeWriter.renderAttributes(writer, uiComponent, null); 106 107 String styleClass = (String ) output.getAttributes().get("styleClass"); 108 if (styleClass != null) 109 writer.writeAttribute("class", styleClass, "styleClass"); 110 111 writer.flush(); 112 } 113 114 118 protected boolean checkDisabled(UIComponent uiComponent) { 119 return false; 120 } 121 122 128 private String appendParameters(FacesContext facesContext, UIComponent 129 uiComponent, String hrefAttribute) { 130 StringBuffer hrefBuffer = new StringBuffer (hrefAttribute); 131 Map parameters = getParameterMap(uiComponent); 132 int numberOfParameters = parameters.size(); 133 boolean parametersExist = numberOfParameters > 0; 134 if (parametersExist) { 135 hrefBuffer.append("?"); 136 Iterator parameterKeys = parameters.keySet().iterator(); 137 while (parameterKeys.hasNext()) { 138 String nextKey = (String ) parameterKeys.next(); 139 Object nextValue = parameters.get(nextKey); 140 hrefBuffer.append(nextKey); 141 hrefBuffer.append("="); 142 hrefBuffer.append( 143 nextValue == null ? null : nextValue.toString()); 144 if (parameterKeys.hasNext()) { 145 hrefBuffer.append("&"); 146 } 147 } 148 } 149 return hrefBuffer.toString(); 150 } 151 152 157 public void encodeChildren(FacesContext facesContext, 158 UIComponent uiComponent) 159 throws IOException { 160 validateParameters(facesContext, uiComponent, UIOutput.class); 161 Iterator children = uiComponent.getChildren().iterator(); 162 while (children.hasNext()) { 163 UIComponent nextChild = (UIComponent) children.next(); 164 nextChild.encodeBegin(facesContext); 165 if (nextChild.getRendersChildren()) { 166 nextChild.encodeChildren(facesContext); 167 } 168 nextChild.encodeEnd(facesContext); 169 } 170 } 171 172 177 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) 178 throws IOException { 179 180 if (facesContext == null || uiComponent == null) { 181 throw new NullPointerException ( 182 "facesContext or uiComponent is null"); 183 } 184 185 UIOutput output = (UIOutput) uiComponent; 186 187 if (!output.isRendered()) return; 188 189 ResponseWriter writer = facesContext.getResponseWriter(); 190 191 if (writer == null) { 192 throw new NullPointerException ("ResponseWriter is null"); 193 } 194 195 writer.endElement("a"); 196 197 } 198 199 200 } | Popular Tags |