KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > renderkit > html > HtmlTextRendererBase


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
31
32 /**
33  * @author Thomas Spiegl (latest modification by $Author: mmarinschek $)
34  * @author Manfred Geiler
35  * @version $Revision: 1.9 $ $Date: 2004/12/23 13:03:09 $
36  * $Log: HtmlTextRendererBase.java,v $
37  * Revision 1.9 2004/12/23 13:03:09 mmarinschek
38  * id's not rendered (or not conditionally rendered); changes in jslistener to support both ie and firefox now
39  *
40  * Revision 1.8 2004/10/13 11:51:01 matze
41  * renamed packages to org.apache
42  *
43  * Revision 1.7 2004/09/02 17:23:25 tinytoony
44  * fix for the span-element for other than the output-text
45  *
46  * Revision 1.6 2004/09/02 16:44:38 tinytoony
47  * fix for the fix ;) for span-element bug
48  *
49  * Revision 1.5 2004/08/30 17:50:34 tinytoony
50  * fix for span-element bug
51  *
52  * Revision 1.4 2004/08/30 17:29:26 tinytoony
53  * fix for span-element bug
54  *
55  * Revision 1.3 2004/07/01 22:00:56 mwessendorf
56  * ASF switch
57  *
58  * Revision 1.2 2004/05/25 07:33:06 manolito
59  * no longer depends on specific component classes
60  *
61  * Revision 1.1 2004/05/18 14:31:39 manolito
62  * user role support completely moved to components source tree
63  *
64  */

65 public class HtmlTextRendererBase
66         extends HtmlRenderer
67 {
68     //private static final Log log = LogFactory.getLog(HtmlTextRenderer.class);
69

70     public void encodeEnd(FacesContext facesContext, UIComponent component)
71         throws IOException JavaDoc
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 JavaDoc("Unsupported component class " + component.getClass().getName());
86         }
87     }
88
89
90     protected static void renderOutput(FacesContext facesContext, UIComponent component)
91         throws IOException JavaDoc
92     {
93         String JavaDoc 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); //default is to escape
103
}
104         renderOutputText(facesContext, component, text, escape);
105     }
106
107
108     public static void renderOutputText(FacesContext facesContext,
109                                         UIComponent component,
110                                         String JavaDoc text,
111                                         boolean escape)
112         throws IOException JavaDoc
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 JavaDoc
155     {
156         ResponseWriter writer = facesContext.getResponseWriter();
157
158         String JavaDoc clientId = component.getClientId(facesContext);
159         String JavaDoc 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         //TODO: overwrite in extended HtmlTextRenderer and check for enabledOnUserRole
182
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             //nothing to decode
204
}
205         else
206         {
207             throw new IllegalArgumentException JavaDoc("Unsupported component class " + component.getClass().getName());
208         }
209     }
210
211
212     public Object JavaDoc getConvertedValue(FacesContext facesContext, UIComponent component, Object JavaDoc 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