1 16 package org.apache.myfaces.renderkit.html; 17 18 import org.apache.myfaces.renderkit.JSFAttr; 19 import org.apache.myfaces.renderkit.RendererUtils; 20 21 import javax.faces.component.UIComponent; 22 import javax.faces.component.UIInput; 23 import javax.faces.component.UIOutput; 24 import javax.faces.component.UIViewRoot; 25 import javax.faces.component.html.HtmlInputText; 26 import javax.faces.component.html.HtmlOutputText; 27 import javax.faces.context.FacesContext; 28 import javax.faces.context.ResponseWriter; 29 import javax.faces.convert.ConverterException; 30 import java.io.IOException ; 31 32 65 public class HtmlTextRendererBase 66 extends HtmlRenderer 67 { 68 70 public void encodeEnd(FacesContext facesContext, UIComponent component) 71 throws IOException 72 { 73 RendererUtils.checkParamValidity(facesContext,component,null); 74 75 if (component instanceof UIInput) 76 { 77 renderInput(facesContext, component); 78 } 79 else if (component instanceof UIOutput) 80 { 81 renderOutput(facesContext, component); 82 } 83 else 84 { 85 throw new IllegalArgumentException ("Unsupported component class " + component.getClass().getName()); 86 } 87 } 88 89 90 protected static void renderOutput(FacesContext facesContext, UIComponent component) 91 throws IOException 92 { 93 String text = RendererUtils.getStringValue(facesContext, component); 94 boolean escape; 95 if (component instanceof HtmlOutputText) 96 { 97 escape = ((HtmlOutputText)component).isEscape(); 98 } 99 else 100 { 101 escape = RendererUtils.getBooleanAttribute(component, JSFAttr.ESCAPE_ATTR, 102 true); } 104 renderOutputText(facesContext, component, text, escape); 105 } 106 107 108 public static void renderOutputText(FacesContext facesContext, 109 UIComponent component, 110 String text, 111 boolean escape) 112 throws IOException 113 { 114 if (text != null) 115 { 116 ResponseWriter writer = facesContext.getResponseWriter(); 117 boolean span = false; 118 119 if(component.getId()!=null && !component.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) 120 { 121 span = true; 122 123 writer.startElement(HTML.SPAN_ELEM, component); 124 125 HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext); 126 127 HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.COMMON_PASSTROUGH_ATTRIBUTES); 128 129 } 130 else 131 { 132 span = HtmlRendererUtils.renderHTMLAttributesWithOptionalStartElement(writer,component, 133 HTML.SPAN_ELEM,HTML.COMMON_PASSTROUGH_ATTRIBUTES); 134 } 135 136 if (escape) 137 { 138 writer.writeText(text, JSFAttr.VALUE_ATTR); 139 } 140 else 141 { 142 writer.write(text); 143 } 144 145 if(span) 146 { 147 writer.endElement(HTML.SPAN_ELEM); 148 } 149 } 150 } 151 152 153 protected void renderInput(FacesContext facesContext, UIComponent component) 154 throws IOException 155 { 156 ResponseWriter writer = facesContext.getResponseWriter(); 157 158 String clientId = component.getClientId(facesContext); 159 String value = RendererUtils.getStringValue(facesContext, component); 160 161 writer.startElement(HTML.INPUT_ELEM, component); 162 writer.writeAttribute(HTML.ID_ATTR, clientId, null); 163 writer.writeAttribute(HTML.NAME_ATTR, clientId, null); 164 writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_TEXT, null); 165 if (value != null) 166 { 167 writer.writeAttribute(HTML.VALUE_ATTR, value, JSFAttr.VALUE_ATTR); 168 } 169 170 HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED); 171 if (isDisabled(facesContext, component)) 172 { 173 writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null); 174 } 175 176 writer.endElement(HTML.INPUT_ELEM); 177 } 178 179 protected boolean isDisabled(FacesContext facesContext, UIComponent component) 180 { 181 if (component instanceof HtmlInputText) 183 { 184 return ((HtmlInputText)component).isDisabled(); 185 } 186 else 187 { 188 return RendererUtils.getBooleanAttribute(component, HTML.DISABLED_ATTR, false); 189 } 190 } 191 192 193 public void decode(FacesContext facesContext, UIComponent component) 194 { 195 RendererUtils.checkParamValidity(facesContext,component,null); 196 197 if (component instanceof UIInput) 198 { 199 HtmlRendererUtils.decodeUIInput(facesContext, component); 200 } 201 else if (component instanceof UIOutput) 202 { 203 } 205 else 206 { 207 throw new IllegalArgumentException ("Unsupported component class " + component.getClass().getName()); 208 } 209 } 210 211 212 public Object getConvertedValue(FacesContext facesContext, UIComponent component, Object submittedValue) throws ConverterException 213 { 214 RendererUtils.checkParamValidity(facesContext, component, UIOutput.class); 215 return RendererUtils.getConvertedUIOutputValue(facesContext, 216 (UIOutput)component, 217 submittedValue); 218 } 219 220 } 221 | Popular Tags |