Coverage Report - org.seasar.teeda.core.render.html.HtmlOutputLinkRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlOutputLinkRenderer
98%
42/43
92%
11/12
2.125
 
 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.net.URLEncoder;
 20  
 import java.util.Iterator;
 21  
 
 22  
 import javax.faces.component.UIComponent;
 23  
 import javax.faces.component.UIParameter;
 24  
 import javax.faces.component.html.HtmlOutputLink;
 25  
 import javax.faces.context.FacesContext;
 26  
 import javax.faces.context.ResponseWriter;
 27  
 import javax.faces.internal.IgnoreAttribute;
 28  
 import javax.faces.internal.WindowIdUtil;
 29  
 
 30  
 import org.seasar.teeda.core.JsfConstants;
 31  
 import org.seasar.teeda.core.render.AbstractRenderer;
 32  
 import org.seasar.teeda.core.render.html.support.UrlBuilder;
 33  
 import org.seasar.teeda.core.util.RendererUtil;
 34  
 import org.seasar.teeda.core.util.ValueHolderUtil;
 35  
 
 36  
 /**
 37  
  * @author manhole
 38  
  */
 39  45
 public class HtmlOutputLinkRenderer extends AbstractRenderer {
 40  
 
 41  
     public static final String COMPONENT_FAMILY = "javax.faces.Output";
 42  
 
 43  
     public static final String RENDERER_TYPE = "javax.faces.Link";
 44  
 
 45  45
     private final IgnoreAttribute ignoreComponent = new IgnoreAttribute();
 46  
     {
 47  45
         ignoreComponent.addAttributeName(JsfConstants.ID_ATTR);
 48  45
         ignoreComponent.addAttributeName(JsfConstants.VALUE_ATTR);
 49  45
     }
 50  
 
 51  
     public void encodeBegin(FacesContext context, UIComponent component)
 52  
             throws IOException {
 53  21
         assertNotNull(context, component);
 54  19
         if (!component.isRendered()) {
 55  1
             return;
 56  
         }
 57  18
         encodeHtmlOutputLinkBegin(context, (HtmlOutputLink) component);
 58  18
     }
 59  
 
 60  
     protected void encodeHtmlOutputLinkBegin(FacesContext context,
 61  
             HtmlOutputLink htmlOutputLink) throws IOException {
 62  18
         ResponseWriter writer = context.getResponseWriter();
 63  18
         final String encoding = writer.getCharacterEncoding();
 64  18
         final String href = buildHref(context, htmlOutputLink, encoding);
 65  18
         writer.startElement(JsfConstants.ANCHOR_ELEM, htmlOutputLink);
 66  18
         RendererUtil.renderIdAttributeIfNecessary(writer, htmlOutputLink,
 67  
                 getIdForRender(context, htmlOutputLink));
 68  18
         renderHref(context, writer, href);
 69  18
         renderRemainAttributes(htmlOutputLink, writer, ignoreComponent);
 70  18
     }
 71  
 
 72  
     protected void renderHref(final FacesContext context,
 73  
             final ResponseWriter writer, final String href) throws IOException {
 74  18
         writer.writeURIAttribute(JsfConstants.HREF_ATTR, href, null);
 75  18
     }
 76  
 
 77  
     protected String buildHref(FacesContext context,
 78  
             HtmlOutputLink htmlOutputLink, String encoding) throws IOException {
 79  18
         UrlBuilder urlBuilder = new UrlBuilder();
 80  18
         urlBuilder.setBase(ValueHolderUtil.getValueForRender(context,
 81  
                 htmlOutputLink));
 82  18
         for (Iterator it = htmlOutputLink.getChildren().iterator(); it
 83  26
                 .hasNext();) {
 84  8
             UIComponent child = (UIComponent) it.next();
 85  8
             if (child instanceof UIParameter) {
 86  7
                 UIParameter parameter = (UIParameter) child;
 87  7
                 urlBuilder.add(
 88  
                         URLEncoder.encode(parameter.getName(), encoding),
 89  
                         URLEncoder.encode(toBlankIfNull(parameter.getValue()),
 90  
                                 encoding));
 91  
             }
 92  
         }
 93  18
         if (WindowIdUtil.isNewWindowTarget(htmlOutputLink.getTarget())) {
 94  2
             urlBuilder.add(WindowIdUtil.NEWWINDOW, JsfConstants.TRUE);
 95  
         }
 96  18
         return context.getExternalContext().encodeResourceURL(
 97  
                 urlBuilder.build());
 98  
     }
 99  
 
 100  
     protected String toBlankIfNull(final Object value) {
 101  7
         if (value == null) {
 102  0
             return "";
 103  
         }
 104  7
         return value.toString();
 105  
     }
 106  
 
 107  
     public void encodeEnd(FacesContext context, UIComponent component)
 108  
             throws IOException {
 109  21
         assertNotNull(context, component);
 110  19
         if (!component.isRendered()) {
 111  1
             return;
 112  
         }
 113  18
         encodeHtmlOutputLinkEnd(context, (HtmlOutputLink) component);
 114  18
     }
 115  
 
 116  
     protected void encodeHtmlOutputLinkEnd(FacesContext context,
 117  
             HtmlOutputLink link) throws IOException {
 118  18
         ResponseWriter writer = context.getResponseWriter();
 119  18
         writer.endElement(JsfConstants.ANCHOR_ELEM);
 120  18
     }
 121  
 
 122  
     public boolean getRendersChildren() {
 123  20
         return true;
 124  
     }
 125  
 
 126  
 }