View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.shared_orchestra.renderkit.html;
20  
21  import org.apache.myfaces.shared_orchestra.renderkit.JSFAttr;
22  import org.apache.myfaces.shared_orchestra.renderkit.RendererUtils;
23  import org.apache.myfaces.shared_orchestra.renderkit.html.HTML;
24  import org.apache.myfaces.shared_orchestra.renderkit.html.HtmlRenderer;
25  import org.apache.myfaces.shared_orchestra.renderkit.html.HtmlRendererUtils;
26  import org.apache.myfaces.shared_orchestra.component.EscapeCapable;
27  
28  import javax.faces.component.*;
29  import javax.faces.component.html.HtmlInputText;
30  import javax.faces.component.html.HtmlOutputText;
31  import javax.faces.context.FacesContext;
32  import javax.faces.context.ResponseWriter;
33  import javax.faces.convert.ConverterException;
34  import java.io.IOException;
35  
36  /**
37   * @author Thomas Spiegl (latest modification by $Author: grantsmith $)
38   * @author Manfred Geiler
39   * @version $Revision: 472618 $ $Date: 2006-11-08 15:06:54 -0500 (Wed, 08 Nov 2006) $
40   */
41  public class HtmlTextRendererBase
42          extends HtmlRenderer
43  {
44      //private static final Log log = LogFactory.getLog(HtmlTextRenderer.class);
45  
46      public void encodeEnd(FacesContext facesContext, UIComponent component)
47          throws IOException
48      {
49          org.apache.myfaces.shared_orchestra.renderkit.RendererUtils.checkParamValidity(facesContext,component,null);
50  
51          if (component instanceof EditableValueHolder)
52          {
53              renderInput(facesContext, component);
54          }
55          else if (component instanceof ValueHolder)
56          {
57              renderOutput(facesContext, component);
58          }
59          else
60          {
61              throw new IllegalArgumentException("Unsupported component class " + component.getClass().getName());
62          }
63      }
64  
65  
66      protected static void renderOutput(FacesContext facesContext, UIComponent component)
67          throws IOException
68      {
69          String text = org.apache.myfaces.shared_orchestra.renderkit.RendererUtils.getStringValue(facesContext, component);
70          boolean escape;
71          if (component instanceof HtmlOutputText || component instanceof EscapeCapable)
72          {
73              escape = ((HtmlOutputText)component).isEscape();
74          }
75          else
76          {
77              escape = RendererUtils.getBooleanAttribute(component, org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.ESCAPE_ATTR,
78                                                         true); //default is to escape
79          }
80          renderOutputText(facesContext, component, text, escape);
81      }
82  
83  
84      public static void renderOutputText(FacesContext facesContext,
85                                          UIComponent component,
86                                          String text,
87                                          boolean escape)
88          throws IOException
89      {
90          if (text != null)
91          {
92              ResponseWriter writer = facesContext.getResponseWriter();
93              boolean span = false;
94  
95              if(component.getId()!=null && !component.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
96              {
97                  span = true;
98  
99                  writer.startElement(HTML.SPAN_ELEM, component);
100 
101                 HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
102 
103                 HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.COMMON_PASSTROUGH_ATTRIBUTES);
104 
105             }
106             else
107             {
108                 span = HtmlRendererUtils.renderHTMLAttributesWithOptionalStartElement(writer,component,
109                         HTML.SPAN_ELEM,HTML.COMMON_PASSTROUGH_ATTRIBUTES);
110             }
111 
112             if (escape)
113             {
114                 writer.writeText(text, org.apache.myfaces.shared_orchestra.renderkit.JSFAttr.VALUE_ATTR);
115             }
116             else
117             {
118                 writer.write(text);
119             }
120 
121             if(span)
122             {
123                 writer.endElement(org.apache.myfaces.shared_orchestra.renderkit.html.HTML.SPAN_ELEM);
124             }
125         }
126     }
127 
128 
129     protected void renderInput(FacesContext facesContext, UIComponent component)
130         throws IOException
131     {
132         ResponseWriter writer = facesContext.getResponseWriter();
133 
134         String clientId = component.getClientId(facesContext);
135         String value = org.apache.myfaces.shared_orchestra.renderkit.RendererUtils.getStringValue(facesContext, component);
136 
137         writer.startElement(HTML.INPUT_ELEM, component);
138         writer.writeAttribute(HTML.ID_ATTR, clientId, null);
139         writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
140         writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_TEXT, null);
141         if (value != null)
142         {
143             writer.writeAttribute(HTML.VALUE_ATTR, value, JSFAttr.VALUE_ATTR);
144         }
145 
146         HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
147         if (isDisabled(facesContext, component))
148         {
149             writer.writeAttribute(HTML.DISABLED_ATTR, Boolean.TRUE, null);
150         }
151 
152         writer.endElement(HTML.INPUT_ELEM);
153     }
154 
155     protected boolean isDisabled(FacesContext facesContext, UIComponent component)
156     {
157         //TODO: overwrite in extended HtmlTextRenderer and check for enabledOnUserRole
158         if (component instanceof HtmlInputText)
159         {
160             return ((HtmlInputText)component).isDisabled();
161         }
162         else
163         {
164             return org.apache.myfaces.shared_orchestra.renderkit.RendererUtils.getBooleanAttribute(component, HTML.DISABLED_ATTR, false);
165         }
166     }
167 
168 
169     public void decode(FacesContext facesContext, UIComponent component)
170     {
171         RendererUtils.checkParamValidity(facesContext,component,null);
172 
173         if (component instanceof EditableValueHolder)
174         {
175             HtmlRendererUtils.decodeUIInput(facesContext, component);
176         }
177         else if (component instanceof ValueHolder)
178         {
179             //nothing to decode
180         }
181         else
182         {
183             throw new IllegalArgumentException("Unsupported component class " + component.getClass().getName());
184         }
185     }
186 
187 
188     public Object getConvertedValue(FacesContext facesContext, UIComponent component, Object submittedValue) throws ConverterException
189     {
190         org.apache.myfaces.shared_orchestra.renderkit.RendererUtils.checkParamValidity(facesContext, component, UIOutput.class);
191         return RendererUtils.getConvertedUIOutputValue(facesContext,
192                                                        (UIOutput)component,
193                                                        submittedValue);
194     }
195 
196 }