Coverage Report - org.apache.myfaces.shared_impl.renderkit.html.HtmlFormRendererBase
 
Classes in this File Line Coverage Branch Coverage Complexity
HtmlFormRendererBase
0%
0/82
0%
0/22
1.733
 
 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.shared_impl.renderkit.html;
 20  
 
 21  
 import org.apache.myfaces.shared_impl.config.MyfacesConfig;
 22  
 import org.apache.myfaces.shared_impl.renderkit.JSFAttr;
 23  
 
 24  
 import javax.faces.application.ViewHandler;
 25  
 import javax.faces.component.UIComponent;
 26  
 import javax.faces.component.UIForm;
 27  
 import javax.faces.component.html.HtmlForm;
 28  
 import javax.faces.context.FacesContext;
 29  
 import javax.faces.context.ResponseWriter;
 30  
 import java.io.IOException;
 31  
 import java.util.HashSet;
 32  
 import java.util.Map;
 33  
 import java.util.Set;
 34  
 
 35  
 /**
 36  
  * @author Manfred Geiler (latest modification by $Author: pmahoney $)
 37  
  * @author Thomas Spiegl
 38  
  * @author Anton Koinov
 39  
  * @version $Revision: 707057 $ $Date: 2008-10-22 07:41:52 -0500 (Wed, 22 Oct 2008) $
 40  
  */
 41  0
 public class HtmlFormRendererBase
 42  
         extends HtmlRenderer
 43  
 {
 44  
     //private static final Log log = LogFactory.getLog(HtmlFormRenderer.class);
 45  
 
 46  
     private static final String HIDDEN_SUBMIT_INPUT_SUFFIX = "_SUBMIT";
 47  
     private static final String HIDDEN_SUBMIT_INPUT_VALUE = "1";
 48  
 
 49  0
     private static final String HIDDEN_COMMAND_INPUTS_SET_ATTR
 50  
             = UIForm.class.getName() + ".org.apache.myfaces.HIDDEN_COMMAND_INPUTS_SET";
 51  
 
 52  
     private static final String SCROLL_HIDDEN_INPUT = "org.apache.myfaces.SCROLL_HIDDEN_INPUT";
 53  
 
 54  
     public void encodeBegin(FacesContext facesContext, UIComponent component)
 55  
             throws IOException
 56  
     {
 57  0
         org.apache.myfaces.shared_impl.renderkit.RendererUtils.checkParamValidity(facesContext, component, UIForm.class);
 58  
 
 59  0
         UIForm htmlForm = (UIForm)component;
 60  
 
 61  0
         ResponseWriter writer = facesContext.getResponseWriter();
 62  0
         String clientId = htmlForm.getClientId(facesContext);
 63  0
         String acceptCharset = getAcceptCharset(facesContext, htmlForm);
 64  0
         String actionURL = getActionUrl(facesContext, htmlForm);
 65  0
         String method = getMethod(facesContext, htmlForm);
 66  
 
 67  0
         writer.startElement(HTML.FORM_ELEM, htmlForm);
 68  0
         writer.writeAttribute(HTML.ID_ATTR, clientId, null);
 69  0
         writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
 70  0
         writer.writeAttribute(HTML.METHOD_ATTR, method, null);
 71  0
         if (acceptCharset != null) {
 72  0
             writer.writeAttribute(HTML.ACCEPT_CHARSET_ATTR, acceptCharset, null);
 73  
         }
 74  0
         writer.writeURIAttribute(HTML.ACTION_ATTR,
 75  
                                  facesContext.getExternalContext().encodeActionURL(actionURL),
 76  
                                  null);
 77  
 
 78  0
         HtmlRendererUtils.renderHTMLAttributes(writer, htmlForm, HTML.FORM_PASSTHROUGH_ATTRIBUTES);
 79  
 
 80  0
         writer.write(""); // force start element tag to be closed
 81  
 
 82  
         // not needed in this version as nothing is written to the form tag, but
 83  
         // included for backward compatibility to the 1.1.1 patch (JIRA MYFACES-1276)
 84  
         // However, might be needed in the future
 85  0
         beforeFormElementsStart(facesContext, component);
 86  0
         afterFormElementsStart(facesContext, component);
 87  0
     }
 88  
 
 89  
     protected String getActionUrl(FacesContext facesContext, UIForm form) {
 90  0
         return getActionUrl(facesContext);
 91  
     }
 92  
 
 93  
     protected String getMethod(FacesContext facesContext, UIForm form) {
 94  0
         return "post";
 95  
     }
 96  
 
 97  
     protected String getAcceptCharset(FacesContext facesContext, UIForm form ) {
 98  0
         return (String)form.getAttributes().get( JSFAttr.ACCEPTCHARSET_ATTR );
 99  
     }
 100  
 
 101  
     public void encodeEnd(FacesContext facesContext, UIComponent component)
 102  
             throws IOException
 103  
     {
 104  0
         ResponseWriter writer = facesContext.getResponseWriter();
 105  
 
 106  0
         beforeFormElementsEnd(facesContext, component);
 107  
 
 108  
         //render hidden command inputs
 109  0
         Set set =  (Set) facesContext.getExternalContext().getRequestMap().get(
 110  
                 getHiddenCommandInputsSetName(facesContext, component)); 
 111  0
         if (set != null && !set.isEmpty())
 112  
         {
 113  0
             HtmlRendererUtils.renderHiddenCommandFormParams(writer, set);
 114  
 
 115  
             String target;
 116  0
             if (component instanceof HtmlForm)
 117  
             {
 118  0
                 target = ((HtmlForm)component).getTarget();
 119  
             }
 120  
             else
 121  
             {
 122  0
                 target = (String)component.getAttributes().get(HTML.TARGET_ATTR);
 123  
             }
 124  0
             HtmlRendererUtils.renderClearHiddenCommandFormParamsFunction(writer,
 125  
                                                                          component.getClientId(facesContext),
 126  
                                                                          set,
 127  
                                                                          target);
 128  
         }
 129  
 
 130  
         //write hidden input to determine "submitted" value on decode
 131  0
         writer.startElement(HTML.INPUT_ELEM, component);
 132  0
         writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_HIDDEN, null);
 133  0
         writer.writeAttribute(HTML.NAME_ATTR, component.getClientId(facesContext) +
 134  
                                               HIDDEN_SUBMIT_INPUT_SUFFIX, null);
 135  0
         writer.writeAttribute(HTML.VALUE_ATTR, HIDDEN_SUBMIT_INPUT_VALUE, null);
 136  0
         writer.endElement(HTML.INPUT_ELEM);
 137  
 
 138  0
         renderScrollHiddenInputIfNecessary(component, facesContext, writer);
 139  
 
 140  
         //write state marker at the end of the form
 141  
         //Todo: this breaks client-side enabled AJAX components again which are searching for the state
 142  
         //we'll need to fix this
 143  0
         ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
 144  0
         viewHandler.writeState(facesContext);
 145  
 
 146  0
         afterFormElementsEnd(facesContext, component);
 147  0
         writer.endElement(HTML.FORM_ELEM);
 148  0
     }
 149  
 
 150  
     private static String getHiddenCommandInputsSetName(FacesContext facesContext, UIComponent form) {
 151  0
         StringBuffer buf = new StringBuffer();
 152  0
         buf.append(HIDDEN_COMMAND_INPUTS_SET_ATTR);
 153  0
         buf.append("_");
 154  0
         buf.append(form.getClientId(facesContext));
 155  0
         return buf.toString();
 156  
     }
 157  
 
 158  
     private static String getScrollHiddenInputName(FacesContext facesContext, UIComponent form) {
 159  0
         StringBuffer buf = new StringBuffer();
 160  0
         buf.append(SCROLL_HIDDEN_INPUT);
 161  0
         buf.append("_");
 162  0
         buf.append(form.getClientId(facesContext));
 163  0
         return buf.toString();
 164  
     }
 165  
 
 166  
 
 167  
     public void decode(FacesContext facesContext, UIComponent component)
 168  
     {
 169  0
         org.apache.myfaces.shared_impl.renderkit.RendererUtils.checkParamValidity(facesContext, component, UIForm.class);
 170  
 
 171  
         /*
 172  
         if (HTMLUtil.isDisabled(component))
 173  
         {
 174  
             return;
 175  
         }
 176  
         */
 177  
 
 178  0
         UIForm htmlForm = (UIForm)component;
 179  
 
 180  0
         Map paramMap = facesContext.getExternalContext().getRequestParameterMap();
 181  0
         String submittedValue = (String)paramMap.get(component.getClientId(facesContext) +
 182  
                                                      HIDDEN_SUBMIT_INPUT_SUFFIX);
 183  0
         if (submittedValue != null && submittedValue.equals(HIDDEN_SUBMIT_INPUT_VALUE))
 184  
         {
 185  0
             htmlForm.setSubmitted(true);
 186  
         }
 187  
         else
 188  
         {
 189  0
             htmlForm.setSubmitted(false);
 190  
         }
 191  0
     }
 192  
 
 193  
 
 194  
     public static void addHiddenCommandParameter(FacesContext facesContext, UIComponent form, String paramName)
 195  
     {
 196  0
         Set set = (Set) facesContext.getExternalContext().getRequestMap().get(getHiddenCommandInputsSetName(facesContext, form));
 197  0
         if (set == null)
 198  
         {
 199  0
             set = new HashSet();
 200  0
             facesContext.getExternalContext().getRequestMap().put(getHiddenCommandInputsSetName(facesContext, form), set);
 201  
         }
 202  0
         set.add(paramName);
 203  0
     }
 204  
 
 205  
     public static void renderScrollHiddenInputIfNecessary(UIComponent form, FacesContext facesContext, ResponseWriter writer)
 206  
         throws IOException {
 207  0
         if (form == null) {
 208  0
             return;
 209  
         }
 210  
 
 211  0
         if (facesContext.getExternalContext().getRequestMap().get(getScrollHiddenInputName(facesContext, form)) == null)
 212  
         {
 213  0
             if (MyfacesConfig.getCurrentInstance(facesContext.getExternalContext()).isAutoScroll()) {
 214  0
                 HtmlRendererUtils.renderAutoScrollHiddenInput(facesContext, writer);
 215  
             }
 216  0
             facesContext.getExternalContext().getRequestMap().put(getScrollHiddenInputName(facesContext, form), Boolean.TRUE);
 217  
         }
 218  0
     }
 219  
 
 220  
     private String getAcceptCharset(UIComponent uiComponent)
 221  
     {
 222  0
         if (uiComponent instanceof HtmlForm)
 223  
         {
 224  0
             return ((HtmlForm)uiComponent).getAcceptcharset();
 225  
         }
 226  0
         return (String)uiComponent.getAttributes().get(JSFAttr.ACCEPTCHARSET_ATTR);
 227  
     }
 228  
 
 229  
     /**
 230  
      * Called before the state and any elements are added to the form tag in the
 231  
      * encodeBegin method
 232  
      */
 233  
     protected void beforeFormElementsStart(FacesContext facesContext, UIComponent component)
 234  
             throws IOException
 235  0
     {}
 236  
 
 237  
     /**
 238  
      * Called after the state and any elements are added to the form tag in the
 239  
      * encodeBegin method
 240  
      */
 241  
     protected void afterFormElementsStart(FacesContext facesContext, UIComponent component)
 242  
             throws IOException
 243  0
     {}
 244  
 
 245  
     /**
 246  
      * Called before the state and any elements are added to the form tag in the
 247  
      * encodeEnd method
 248  
      */
 249  
     protected void beforeFormElementsEnd(FacesContext facesContext, UIComponent component)
 250  
             throws IOException
 251  0
     {}
 252  
 
 253  
     /**
 254  
      * Called after the state and any elements are added to the form tag in the
 255  
      * encodeEnd method
 256  
      */
 257  
     protected void afterFormElementsEnd(FacesContext facesContext, UIComponent component)
 258  
             throws IOException
 259  0
     {}
 260  
 }