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