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.renderkit.html;
20  
21  import java.io.IOException;
22  import java.text.MessageFormat;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.logging.Level;
26  import java.util.logging.Logger;
27  
28  import javax.faces.component.UIComponent;
29  import javax.faces.component.UIOutput;
30  import javax.faces.component.UIParameter;
31  import javax.faces.component.UIViewRoot;
32  import javax.faces.component.html.HtmlOutputFormat;
33  import javax.faces.context.FacesContext;
34  import javax.faces.context.ResponseWriter;
35  
36  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFRenderer;
37  import org.apache.myfaces.shared.renderkit.JSFAttr;
38  import org.apache.myfaces.shared.renderkit.RendererUtils;
39  import org.apache.myfaces.shared.renderkit.html.CommonPropertyConstants;
40  import org.apache.myfaces.shared.renderkit.html.CommonPropertyUtils;
41  import org.apache.myfaces.shared.renderkit.html.HTML;
42  import org.apache.myfaces.shared.renderkit.html.HtmlRenderer;
43  import org.apache.myfaces.shared.renderkit.html.HtmlRendererUtils;
44  
45  /**
46   * 
47   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
48   * @author Thomas Spiegl
49   * @version $Revision: 1364637 $ $Date: 2012-07-23 09:00:28 -0500 (Mon, 23 Jul 2012) $
50   */
51  @JSFRenderer(renderKitId = "HTML_BASIC", family = "javax.faces.Output", type = "javax.faces.Format")
52  public class HtmlFormatRenderer extends HtmlRenderer
53  {
54      //private static final Log log = LogFactory.getLog(HtmlFormatRenderer.class);
55      private static final Logger log = Logger.getLogger(HtmlFormatRenderer.class.getName());
56  
57      private static final Object[] EMPTY_ARGS = new Object[0];
58      
59      @Override
60      protected boolean isCommonPropertiesOptimizationEnabled(FacesContext facesContext)
61      {
62          return true;
63      }
64  
65      @Override
66      protected boolean isCommonEventsOptimizationEnabled(FacesContext facesContext)
67      {
68          return true;
69      }
70  
71      @Override
72      public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException
73      {
74      }
75  
76      @Override
77      public void encodeChildren(FacesContext facescontext, UIComponent uicomponent) throws IOException
78      {
79      }
80  
81      @Override
82      public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException
83      {
84          RendererUtils.checkParamValidity(facesContext, component, UIOutput.class);
85  
86          String text = getOutputFormatText(facesContext, component);
87          boolean escape;
88          if (component instanceof HtmlOutputFormat)
89          {
90              escape = ((HtmlOutputFormat) component).isEscape();
91          }
92          else
93          {
94              escape = RendererUtils.getBooleanAttribute(component, JSFAttr.ESCAPE_ATTR, true);
95          }
96          if (text != null)
97          {
98              ResponseWriter writer = facesContext.getResponseWriter();
99              boolean span = false;
100 
101             if (isCommonPropertiesOptimizationEnabled(facesContext))
102             {
103                 long commonPropertiesMarked = CommonPropertyUtils.getCommonPropertiesMarked(component);
104                 
105                 if ( (commonPropertiesMarked & ~(CommonPropertyConstants.ESCAPE_PROP)) > 0)
106                 {
107                     span = true;
108                     writer.startElement(HTML.SPAN_ELEM, component);
109                     HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
110                 }
111                 else if (CommonPropertyUtils.isIdRenderingNecessary(component))
112                 {
113                     span = true;
114                     writer.startElement(HTML.SPAN_ELEM, component);
115                     writer.writeAttribute(HTML.ID_ATTR, component.getClientId(facesContext), null);
116                 }
117                 
118                 CommonPropertyUtils.renderUniversalProperties(writer, commonPropertiesMarked, component);
119                 CommonPropertyUtils.renderStyleProperties(writer, commonPropertiesMarked, component);
120             }
121             else
122             {
123                 if(component.getId()!=null && !component.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
124                 {
125                     span = true;
126     
127                     writer.startElement(HTML.SPAN_ELEM, component);
128     
129                     HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
130     
131                     HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.COMMON_PASSTROUGH_ATTRIBUTES);
132     
133                 }
134                 else
135                 {
136                     span = HtmlRendererUtils.renderHTMLAttributesWithOptionalStartElement(writer,component,
137                             HTML.SPAN_ELEM,HTML.COMMON_PASSTROUGH_ATTRIBUTES);
138                 }
139             }
140 
141             if (escape)
142             {
143                 if (log.isLoggable(Level.FINE))
144                 {
145                     log.fine("renderOutputText writing '" + text + "'");
146                 }
147                 writer.writeText(text, org.apache.myfaces.shared.renderkit.JSFAttr.VALUE_ATTR);
148             }
149             else
150             {
151                 writer.write(text);
152             }
153 
154             if(span)
155             {
156                 writer.endElement(org.apache.myfaces.shared.renderkit.html.HTML.SPAN_ELEM);
157             }
158         }
159     }
160 
161     private String getOutputFormatText(FacesContext facesContext, UIComponent htmlOutputFormat)
162     {
163         String pattern = RendererUtils.getStringValue(facesContext, htmlOutputFormat);
164         Object[] args;
165         if (htmlOutputFormat.getChildCount() == 0)
166         {
167             args = EMPTY_ARGS;
168         }
169         else
170         {
171             List<Object> argsList = null;
172             
173             if (htmlOutputFormat.getChildCount() > 0)
174             {
175                 List<UIParameter> validParams = HtmlRendererUtils.getValidUIParameterChildren(
176                         facesContext, htmlOutputFormat.getChildren(), false, false, false);
177                 for (int i = 0, size = validParams.size(); i < size; i++)
178                 {
179                     UIParameter param = validParams.get(i);
180                     if (argsList == null)
181                     {
182                         argsList = new ArrayList<Object>();
183                     }
184                     argsList.add(param.getValue());
185                 }
186             }
187             
188             if (argsList != null)
189             {
190                 args = argsList.toArray(new Object[argsList.size()]);
191             }
192             else
193             {
194                 args = EMPTY_ARGS;
195             }
196         }
197 
198         MessageFormat format = new MessageFormat(pattern, facesContext.getViewRoot().getLocale());
199         try
200         {
201             return format.format(args);
202         }
203         catch (Exception e)
204         {
205             log.log(Level.SEVERE, "Error formatting message of component "
206                                   + htmlOutputFormat.getClientId(facesContext));
207             return "";
208         }
209     }
210 
211 }