Coverage Report - org.seasar.teeda.core.render.html.HtmlFormRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlFormRenderer
99%
97/98
83%
20/24
1.929
 
 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  
 import java.util.LinkedHashMap;
 22  
 import java.util.Map;
 23  
 import java.util.Set;
 24  
 import java.util.Map.Entry;
 25  
 
 26  
 import javax.faces.application.ViewHandler;
 27  
 import javax.faces.component.UIComponent;
 28  
 import javax.faces.component.UIForm;
 29  
 import javax.faces.component.html.HtmlForm;
 30  
 import javax.faces.context.FacesContext;
 31  
 import javax.faces.context.ResponseWriter;
 32  
 import javax.faces.internal.IgnoreAttribute;
 33  
 import javax.faces.internal.WindowIdUtil;
 34  
 
 35  
 import org.seasar.teeda.core.JsfConstants;
 36  
 import org.seasar.teeda.core.render.AbstractRenderer;
 37  
 import org.seasar.teeda.core.render.html.support.UrlBuilder;
 38  
 import org.seasar.teeda.core.util.FacesContextUtil;
 39  
 import org.seasar.teeda.core.util.HtmlFormRendererUtil;
 40  
 import org.seasar.teeda.core.util.JavaScriptUtil;
 41  
 import org.seasar.teeda.core.util.RendererUtil;
 42  
 
 43  
 /**
 44  
  * @author manhole
 45  
  * @author shot
 46  
  * @author yone
 47  
  */
 48  57
 public class HtmlFormRenderer extends AbstractRenderer {
 49  
 
 50  
     //TODO need to avoid hidden parameter that is not used by commandlink.
 51  
     public static final String COMPONENT_FAMILY = "javax.faces.Form";
 52  
 
 53  
     public static final String RENDERER_TYPE = "javax.faces.Form";
 54  
 
 55  2
     private static final String HIDDEN_PARAMETER_KEY = HtmlFormRenderer.class
 56  
             .getName()
 57  
             + ".HIDDEN_PARAMETER_KEY";
 58  
 
 59  57
     private final IgnoreAttribute ignoreComponent = new IgnoreAttribute();
 60  
     {
 61  57
         ignoreComponent.addAttributeName(JsfConstants.ID_ATTR);
 62  57
         ignoreComponent.addAttributeName(JsfConstants.NAME_ATTR);
 63  57
         ignoreComponent.addAttributeName(JsfConstants.METHOD_ATTR);
 64  57
     }
 65  
 
 66  
     public void encodeBegin(FacesContext context, UIComponent component)
 67  
             throws IOException {
 68  15
         assertNotNull(context, component);
 69  13
         if (!component.isRendered()) {
 70  1
             return;
 71  
         }
 72  12
         encodeHtmlFormBegin(context, (HtmlForm) component);
 73  12
     }
 74  
 
 75  
     protected void encodeHtmlFormBegin(FacesContext context, HtmlForm htmlForm)
 76  
             throws IOException {
 77  12
         final ResponseWriter writer = context.getResponseWriter();
 78  12
         writer.startElement(JsfConstants.FORM_ELEM, htmlForm);
 79  12
         RendererUtil.renderIdAttributeIfNecessary(writer, htmlForm,
 80  
                 getIdForRender(context, htmlForm));
 81  12
         RendererUtil.renderAttribute(writer, JsfConstants.NAME_ATTR, htmlForm
 82  
                 .getClientId(context));
 83  
 
 84  12
         RendererUtil.renderAttribute(writer, JsfConstants.METHOD_ATTR,
 85  
                 JsfConstants.POST_VALUE);
 86  12
         renderRemainAttributes(htmlForm, writer, ignoreComponent);
 87  
 
 88  
         // action attribute
 89  12
         final ViewHandler viewHandler = FacesContextUtil
 90  
                 .getViewHandler(context);
 91  12
         final String viewId = context.getViewRoot().getViewId();
 92  12
         final String url = viewHandler.getActionURL(context, viewId);
 93  12
         if (url != null) {
 94  11
             UrlBuilder urlBuilder = new UrlBuilder();
 95  11
             urlBuilder.setBase(url);
 96  11
             final String encoding = writer.getCharacterEncoding();
 97  11
             if (WindowIdUtil.isNewWindowTarget(htmlForm.getTarget())) {
 98  2
                 urlBuilder.add(URLEncoder.encode(WindowIdUtil.NEWWINDOW,
 99  
                         encoding), URLEncoder.encode(JsfConstants.TRUE,
 100  
                         encoding));
 101  
             }
 102  11
             writer.writeURIAttribute(JsfConstants.ACTION_ATTR, context
 103  
                     .getExternalContext().encodeActionURL(urlBuilder.build()),
 104  
                     null);
 105  
         }
 106  12
     }
 107  
 
 108  
     public void encodeEnd(FacesContext context, UIComponent component)
 109  
             throws IOException {
 110  15
         assertNotNull(context, component);
 111  13
         if (!component.isRendered()) {
 112  1
             return;
 113  
         }
 114  12
         encodeHtmlFormEnd(context, (HtmlForm) component);
 115  12
     }
 116  
 
 117  
     protected void encodeHtmlFormEnd(FacesContext context, HtmlForm htmlForm)
 118  
             throws IOException {
 119  12
         context.getApplication().getViewHandler().writeState(context);
 120  12
         ResponseWriter writer = context.getResponseWriter();
 121  12
         renderFormSubmitMarker(context, htmlForm, writer);
 122  12
         renderForCommandLink(context, htmlForm, writer);
 123  12
         writer.endElement(JsfConstants.FORM_ELEM);
 124  12
     }
 125  
 
 126  
     protected void renderForCommandLink(FacesContext context,
 127  
             HtmlForm htmlForm, ResponseWriter writer) throws IOException {
 128  12
         final Map hiddenParameters = getCommandLinkHiddenParameters(htmlForm);
 129  12
         boolean hasCommandLink = false;
 130  12
         for (final Iterator it = hiddenParameters.entrySet().iterator(); it
 131  19
                 .hasNext();) {
 132  7
             hasCommandLink = true;
 133  7
             final Map.Entry entry = (Entry) it.next();
 134  7
             final String name = (String) entry.getKey();
 135  7
             final Object value = entry.getValue();
 136  7
             RendererUtil.renderHidden(htmlForm, writer, name, value);
 137  
         }
 138  12
         if (hasCommandLink) {
 139  4
             final String target = htmlForm.getTarget();
 140  4
             renderClearHiddenCommandFormParamsFunction(writer, getIdForRender(
 141  
                     context, htmlForm), hiddenParameters.entrySet(), target);
 142  
         }
 143  12
         htmlForm.getAttributes().remove(HIDDEN_PARAMETER_KEY);
 144  12
     }
 145  
 
 146  
     public static void setCommandLinkHiddenParameter(UIForm form, String key,
 147  
             Object value) {
 148  22
         Map map = getCommandLinkHiddenParameters(form);
 149  22
         map.put(key, value);
 150  22
     }
 151  
 
 152  
     public static Map getCommandLinkHiddenParameters(UIForm form) {
 153  35
         Map attributes = form.getAttributes();
 154  35
         Map map = (Map) attributes.get(HIDDEN_PARAMETER_KEY);
 155  35
         if (map == null) {
 156  24
             map = new LinkedHashMap();
 157  24
             attributes.put(HIDDEN_PARAMETER_KEY, map);
 158  
         }
 159  35
         return map;
 160  
     }
 161  
 
 162  
     public static void clearCommandLinkHiddenParameters(UIForm form, String key) {
 163  1
         Map map = getCommandLinkHiddenParameters(form);
 164  1
         map.remove(key);
 165  1
     }
 166  
 
 167  
     private void renderFormSubmitMarker(FacesContext context,
 168  
             HtmlForm htmlForm, ResponseWriter writer) throws IOException {
 169  12
         final String clientId = htmlForm.getClientId(context);
 170  12
         final String key = getFormSubmitKey(context, htmlForm);
 171  12
         RendererUtil.renderHidden(htmlForm, writer, key, clientId);
 172  12
     }
 173  
 
 174  
     public void decode(FacesContext context, UIComponent component) {
 175  4
         assertNotNull(context, component);
 176  2
         decodeHtmlForm(context, (HtmlForm) component);
 177  2
     }
 178  
 
 179  
     protected void decodeHtmlForm(FacesContext context, HtmlForm htmlForm) {
 180  2
         Map paramMap = context.getExternalContext().getRequestParameterMap();
 181  2
         String key = getFormSubmitKey(context, htmlForm);
 182  2
         if (paramMap.containsKey(key)) {
 183  1
             htmlForm.setSubmitted(true);
 184  
         } else {
 185  1
             htmlForm.setSubmitted(false);
 186  
         }
 187  2
     }
 188  
 
 189  
     private String getFormSubmitKey(FacesContext context, HtmlForm htmlForm) {
 190  14
         return HtmlFormRendererUtil.getFormSubmitKey(context, htmlForm);
 191  
     }
 192  
 
 193  
     protected void renderClearHiddenCommandFormParamsFunction(
 194  
             ResponseWriter writer, String formName, Set hiddenFormParams,
 195  
             String formTarget) throws IOException {
 196  4
         String functionName = JavaScriptUtil
 197  
                 .getClearHiddenCommandFormParamsFunctionName(formName);
 198  4
         StringBuffer sb = new StringBuffer(512);
 199  4
         sb.append("function ").append(functionName).append("(){");
 200  4
         if (hiddenFormParams != null) {
 201  4
             sb.append("var f = document.forms['").append(formName)
 202  
                     .append("'];");
 203  4
             for (Iterator it = hiddenFormParams.iterator(); it.hasNext();) {
 204  7
                 final Map.Entry entry = (Entry) it.next();
 205  7
                 final String name = (String) entry.getKey();
 206  7
                 sb.append(" f.elements['").append(name).append(
 207  
                         "'].value='null';");
 208  
             }
 209  
         }
 210  4
         sb.append(" f.target=");
 211  4
         if (formTarget == null || formTarget.length() == 0) {
 212  4
             sb.append("'';");
 213  
         } else {
 214  0
             sb.append("'").append(formTarget).append("';");
 215  
         }
 216  4
         sb.append("} ").append(functionName).append("();");
 217  4
         renderJavaScriptElement(writer, new String(sb));
 218  4
     }
 219  
 
 220  
     public void addIgnoreAttributeName(final String name) {
 221  28
         ignoreComponent.addAttributeName(name);
 222  28
     }
 223  
 
 224  
 }