Coverage Report - org.seasar.teeda.core.render.html.HtmlOutputFormatRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlOutputFormatRenderer
98%
39/40
86%
12/14
3
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 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,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.teeda.core.render.html;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.text.MessageFormat;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 import java.util.Locale;
 24  
 
 25  
 import javax.faces.component.UIComponent;
 26  
 import javax.faces.component.UIParameter;
 27  
 import javax.faces.component.html.HtmlOutputFormat;
 28  
 import javax.faces.context.FacesContext;
 29  
 import javax.faces.context.ResponseWriter;
 30  
 import javax.faces.internal.IgnoreAttribute;
 31  
 
 32  
 import org.seasar.teeda.core.JsfConstants;
 33  
 import org.seasar.teeda.core.render.AbstractRenderer;
 34  
 import org.seasar.teeda.core.util.RendererUtil;
 35  
 import org.seasar.teeda.core.util.ValueHolderUtil;
 36  
 
 37  
 /**
 38  
  * @author manhole
 39  
  */
 40  38
 public class HtmlOutputFormatRenderer extends AbstractRenderer {
 41  
 
 42  
     public static final String COMPONENT_FAMILY = "javax.faces.Output";
 43  
 
 44  
     public static final String RENDERER_TYPE = "javax.faces.Format";
 45  
 
 46  38
     private final IgnoreAttribute ignoreComponent = new IgnoreAttribute();
 47  
     {
 48  38
         ignoreComponent.addAttributeName(JsfConstants.ID_ATTR);
 49  38
         ignoreComponent.addAttributeName(JsfConstants.VALUE_ATTR);
 50  38
         ignoreComponent.addAttributeName(JsfConstants.ESCAPE_ATTR);
 51  38
     }
 52  
 
 53  
     public void encodeEnd(FacesContext context, UIComponent component)
 54  
             throws IOException {
 55  13
         assertNotNull(context, component);
 56  11
         if (!component.isRendered()) {
 57  1
             return;
 58  
         }
 59  10
         encodeHtmlOutputFormatEnd(context, (HtmlOutputFormat) component);
 60  10
     }
 61  
 
 62  
     protected void encodeHtmlOutputFormatEnd(FacesContext context,
 63  
             HtmlOutputFormat htmlOutputFormat) throws IOException {
 64  10
         ResponseWriter writer = context.getResponseWriter();
 65  10
         boolean startSpan = false;
 66  10
         if (containsAttributeForRender(htmlOutputFormat, ignoreComponent)) {
 67  3
             writer.startElement(JsfConstants.SPAN_ELEM, htmlOutputFormat);
 68  3
             startSpan = true;
 69  3
             RendererUtil.renderIdAttributeIfNecessary(writer, htmlOutputFormat,
 70  
                     getIdForRender(context, htmlOutputFormat));
 71  3
             renderRemainAttributes(htmlOutputFormat, writer, ignoreComponent);
 72  
         }
 73  10
         String value = getFormattedValue(context, htmlOutputFormat);
 74  10
         if (htmlOutputFormat.isEscape()) {
 75  9
             writer.writeText(value, null);
 76  
         } else {
 77  1
             writer.write(value);
 78  
         }
 79  10
         if (startSpan) {
 80  3
             writer.endElement(JsfConstants.SPAN_ELEM);
 81  
         }
 82  10
     }
 83  
 
 84  
     protected String getFormattedValue(FacesContext context,
 85  
             HtmlOutputFormat htmlOutputFormat) {
 86  10
         List args = new ArrayList();
 87  10
         for (Iterator it = htmlOutputFormat.getChildren().iterator(); it
 88  13
                 .hasNext();) {
 89  3
             UIComponent child = (UIComponent) it.next();
 90  3
             if (child instanceof UIParameter) {
 91  3
                 UIParameter parameter = (UIParameter) child;
 92  3
                 args.add(parameter.getValue());
 93  
             }
 94  
         }
 95  10
         String pattern = ValueHolderUtil.getValueForRender(context,
 96  
                 htmlOutputFormat);
 97  10
         MessageFormat format = new MessageFormat(pattern, getLocale(context));
 98  10
         String value = format.format((Object[]) args.toArray(new Object[args
 99  
                 .size()]));
 100  10
         return value;
 101  
     }
 102  
 
 103  
     protected Locale getLocale(FacesContext context) {
 104  14
         Locale locale = context.getViewRoot().getLocale();
 105  14
         if (locale == null) {
 106  0
             locale = Locale.getDefault();
 107  
         }
 108  14
         return locale;
 109  
     }
 110  
 
 111  
 }