001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.shared_impl.renderkit.html;
017:
018: import org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020: import org.apache.myfaces.shared_impl.component.EscapeCapable;
021: import org.apache.myfaces.shared_impl.renderkit.JSFAttr;
022: import org.apache.myfaces.shared_impl.renderkit.RendererUtils;
023:
024: import javax.faces.component.UIComponent;
025: import javax.faces.component.UIInput;
026: import javax.faces.component.UIOutput;
027: import javax.faces.component.UIViewRoot;
028: import javax.faces.component.html.HtmlInputText;
029: import javax.faces.component.html.HtmlOutputText;
030: import javax.faces.context.FacesContext;
031: import javax.faces.context.ResponseWriter;
032: import javax.faces.convert.ConverterException;
033: import java.io.IOException;
034:
035: /**
036: * @author Thomas Spiegl (latest modification by $Author: baranda $)
037: * @author Manfred Geiler
038: * @version $Revision: 540279 $ $Date: 2007-05-21 22:38:56 +0200 (Mo, 21 Mai 2007) $
039: */
040: public class HtmlTextRendererBase extends HtmlRenderer {
041: private static final Log log = LogFactory
042: .getLog(HtmlTextRendererBase.class);
043:
044: private static final String AUTOCOMPLETE_VALUE_OFF = "off";
045:
046: public void encodeEnd(FacesContext facesContext,
047: UIComponent component) throws IOException {
048: org.apache.myfaces.shared_impl.renderkit.RendererUtils
049: .checkParamValidity(facesContext, component, null);
050:
051: if (component instanceof UIInput) {
052: renderInput(facesContext, component);
053: } else if (component instanceof UIOutput) {
054: renderOutput(facesContext, component);
055: } else {
056: throw new IllegalArgumentException(
057: "Unsupported component class "
058: + component.getClass().getName());
059: }
060: }
061:
062: protected static void renderOutput(FacesContext facesContext,
063: UIComponent component) throws IOException {
064:
065: String text = org.apache.myfaces.shared_impl.renderkit.RendererUtils
066: .getStringValue(facesContext, component);
067: if (log.isDebugEnabled())
068: log.debug("renderOutput '" + text + "'");
069: boolean escape;
070: if (component instanceof HtmlOutputText
071: || component instanceof EscapeCapable) {
072: escape = ((HtmlOutputText) component).isEscape();
073: } else {
074: escape = RendererUtils
075: .getBooleanAttribute(
076: component,
077: org.apache.myfaces.shared_impl.renderkit.JSFAttr.ESCAPE_ATTR,
078: true); //default is to escape
079: }
080: renderOutputText(facesContext, component, text, escape);
081: }
082:
083: public static void renderOutputText(FacesContext facesContext,
084: UIComponent component, String text, boolean escape)
085: throws IOException {
086: if (text != null) {
087: ResponseWriter writer = facesContext.getResponseWriter();
088: boolean span = false;
089:
090: if (component.getId() != null
091: && !component.getId().startsWith(
092: UIViewRoot.UNIQUE_ID_PREFIX)) {
093: span = true;
094:
095: writer.startElement(HTML.SPAN_ELEM, component);
096:
097: HtmlRendererUtils.writeIdIfNecessary(writer, component,
098: facesContext);
099:
100: HtmlRendererUtils.renderHTMLAttributes(writer,
101: component, HTML.COMMON_PASSTROUGH_ATTRIBUTES);
102:
103: } else {
104: span = HtmlRendererUtils
105: .renderHTMLAttributesWithOptionalStartElement(
106: writer, component, HTML.SPAN_ELEM,
107: HTML.COMMON_PASSTROUGH_ATTRIBUTES);
108: }
109:
110: if (escape) {
111: if (log.isDebugEnabled())
112: log
113: .debug("renderOutputText writing '" + text
114: + "'");
115: writer
116: .writeText(
117: text,
118: org.apache.myfaces.shared_impl.renderkit.JSFAttr.VALUE_ATTR);
119: } else {
120: writer.write(text);
121: }
122:
123: if (span) {
124: writer
125: .endElement(org.apache.myfaces.shared_impl.renderkit.html.HTML.SPAN_ELEM);
126: }
127: }
128: }
129:
130: protected void renderInput(FacesContext facesContext,
131: UIComponent component) throws IOException {
132: ResponseWriter writer = facesContext.getResponseWriter();
133:
134: String clientId = component.getClientId(facesContext);
135: String value = org.apache.myfaces.shared_impl.renderkit.RendererUtils
136: .getStringValue(facesContext, component);
137: if (log.isDebugEnabled())
138: log.debug("renderInput '" + value + "'");
139: writer.startElement(HTML.INPUT_ELEM, component);
140: writer.writeAttribute(HTML.ID_ATTR, clientId, null);
141: writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
142: writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_TEXT,
143: null);
144: if (value != null) {
145: writer.writeAttribute(HTML.VALUE_ATTR, value,
146: JSFAttr.VALUE_ATTR);
147: }
148:
149: HtmlRendererUtils.renderHTMLAttributes(writer, component,
150: HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
151: if (isDisabled(facesContext, component)) {
152: writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE,
153: null);
154: }
155:
156: if (isAutocompleteOff(facesContext, component)) {
157: writer.writeAttribute(HTML.AUTOCOMPLETE_ATTR,
158: AUTOCOMPLETE_VALUE_OFF, HTML.AUTOCOMPLETE_ATTR);
159: }
160:
161: writer.endElement(HTML.INPUT_ELEM);
162: }
163:
164: protected boolean isDisabled(FacesContext facesContext,
165: UIComponent component) {
166: //TODO: overwrite in extended HtmlTextRenderer and check for enabledOnUserRole
167: if (component instanceof HtmlInputText) {
168: return ((HtmlInputText) component).isDisabled();
169: }
170:
171: return org.apache.myfaces.shared_impl.renderkit.RendererUtils
172: .getBooleanAttribute(component, HTML.DISABLED_ATTR,
173: false);
174:
175: }
176:
177: /**
178: * If autocomplete is "on" or not set, do not render it
179: */
180: protected boolean isAutocompleteOff(FacesContext facesContext,
181: UIComponent component) {
182: if (component instanceof HtmlInputText) {
183: String autocomplete = ((HtmlInputText) component)
184: .getAutocomplete();
185: if (autocomplete != null) {
186: return autocomplete.equals(AUTOCOMPLETE_VALUE_OFF);
187: }
188: }
189:
190: return false;
191: }
192:
193: public void decode(FacesContext facesContext, UIComponent component) {
194: RendererUtils.checkParamValidity(facesContext, component, null);
195:
196: if (component instanceof UIInput) {
197: HtmlRendererUtils.decodeUIInput(facesContext, component);
198: } else if (component instanceof UIOutput) {
199: //nothing to decode
200: } else {
201: throw new IllegalArgumentException(
202: "Unsupported component class "
203: + component.getClass().getName());
204: }
205: }
206:
207: public Object getConvertedValue(FacesContext facesContext,
208: UIComponent component, Object submittedValue)
209: throws ConverterException {
210: org.apache.myfaces.shared_impl.renderkit.RendererUtils
211: .checkParamValidity(facesContext, component,
212: UIOutput.class);
213: return RendererUtils.getConvertedUIOutputValue(facesContext,
214: (UIOutput) component, submittedValue);
215: }
216:
217: }
|