You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ba...@apache.org on 2006/01/29 03:01:48 UTC

svn commit: r373272 - in /myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html: ./ util/

Author: baranda
Date: Sat Jan 28 18:01:37 2006
New Revision: 373272

URL: http://svn.apache.org/viewcvs?rev=373272&view=rev
Log:
Now the dummyForm system work a bit different. When a component needs to render a dummyForm, an object with the dummyForm params is instantiated and put in the request map. The AddResource class, called from the extensions filter, will render the dummyForm before the end of the body tag getting the information from the request. No more subclasses of HtmlResponseWriter needed to deal with the dummyForm. Also this should fix an issue where tobago couldn't work, due to a class cast to HtmlResponseWriterImpl (and tobago uses another implementation of the HtmlResponseWriter)

Added:
    myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/DummyFormRequestInfo.java   (with props)
Removed:
    myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/DummyFormResponseWriter.java
    myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/DummyFormResponseWriterWrapper.java
Modified:
    myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/HtmlButtonRendererBase.java
    myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/HtmlLinkRendererBase.java
    myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java
    myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/DummyFormUtils.java

Modified: myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/HtmlButtonRendererBase.java
URL: http://svn.apache.org/viewcvs/myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/HtmlButtonRendererBase.java?rev=373272&r1=373271&r2=373272&view=diff
==============================================================================
--- myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/HtmlButtonRendererBase.java (original)
+++ myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/HtmlButtonRendererBase.java Sat Jan 28 18:01:37 2006
@@ -16,7 +16,6 @@
 package org.apache.myfaces.renderkit.html;
 
 import java.io.IOException;
-import java.util.List;
 import java.util.Map;
 
 import javax.faces.component.UICommand;
@@ -32,7 +31,6 @@
 import org.apache.myfaces.config.MyfacesConfig;
 import org.apache.myfaces.renderkit.JSFAttr;
 import org.apache.myfaces.renderkit.RendererUtils;
-import org.apache.myfaces.renderkit.html.util.DummyFormResponseWriter;
 import org.apache.myfaces.renderkit.html.util.DummyFormUtils;
 import org.apache.myfaces.renderkit.html.util.JavascriptUtils;
 
@@ -151,20 +149,21 @@
 
         UIForm nestingForm = null;
         String formName;
-        DummyFormResponseWriter dummyFormResponseWriter;
+
         if (parent != null)
         {
             //link is nested inside a form
             nestingForm = (UIForm)parent;
             formName = nestingForm.getClientId(facesContext);
-            dummyFormResponseWriter = null;
+
         }
         else
         {
             //not nested in form, we must add a dummy form at the end of the document
             formName = DummyFormUtils.DUMMY_FORM_NAME;
-            dummyFormResponseWriter = DummyFormUtils.getDummyFormResponseWriter(facesContext);
-            dummyFormResponseWriter.setWriteDummyForm(true);
+            //dummyFormResponseWriter = DummyFormUtils.getDummyFormResponseWriter(facesContext);
+            //dummyFormResponseWriter.setWriteDummyForm(true);
+            DummyFormUtils.setWriteDummyForm(facesContext, true);
         }
         StringBuffer onClick = new StringBuffer();
         String commandOnClick = (String)uiComponent.getAttributes().get(HTML.ONCLICK_ATTR);
@@ -191,7 +190,7 @@
         }
         else
         {
-            dummyFormResponseWriter.addDummyFormParameter(hiddenFieldName);
+            DummyFormUtils.addDummyFormParameter(facesContext, hiddenFieldName);
         }
 
         return onClick;

Modified: myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/HtmlLinkRendererBase.java
URL: http://svn.apache.org/viewcvs/myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/HtmlLinkRendererBase.java?rev=373272&r1=373271&r2=373272&view=diff
==============================================================================
--- myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/HtmlLinkRendererBase.java (original)
+++ myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/HtmlLinkRendererBase.java Sat Jan 28 18:01:37 2006
@@ -35,7 +35,6 @@
 import org.apache.myfaces.config.MyfacesConfig;
 import org.apache.myfaces.renderkit.JSFAttr;
 import org.apache.myfaces.renderkit.RendererUtils;
-import org.apache.myfaces.renderkit.html.util.DummyFormResponseWriter;
 import org.apache.myfaces.renderkit.html.util.DummyFormUtils;
 import org.apache.myfaces.renderkit.html.util.HTMLEncoder;
 import org.apache.myfaces.renderkit.html.util.JavascriptUtils;
@@ -200,20 +199,20 @@
 
         UIForm nestingForm = null;
         String formName;
-        DummyFormResponseWriter dummyFormResponseWriter;
+
         if (parent != null)
         {
             //link is nested inside a form
             nestingForm = (UIForm)parent;
             formName = nestingForm.getClientId(facesContext);
-            dummyFormResponseWriter = null;
         }
         else
         {
             //not nested in form, we must add a dummy form at the end of the document
             formName = DummyFormUtils.DUMMY_FORM_NAME;
-            dummyFormResponseWriter = DummyFormUtils.getDummyFormResponseWriter(facesContext);
-            dummyFormResponseWriter.setWriteDummyForm(true);
+            //dummyFormResponseWriter = DummyFormUtils.getDummyFormResponseWriter(facesContext);
+            //dummyFormResponseWriter.setWriteDummyForm(true);
+            DummyFormUtils.setWriteDummyForm(facesContext, true);
         }
 
         StringBuffer onClick = new StringBuffer();
@@ -254,7 +253,7 @@
         }
         else
         {
-            dummyFormResponseWriter.addDummyFormParameter(hiddenFieldName);
+            DummyFormUtils.addDummyFormParameter(facesContext, hiddenFieldName);
         }
 
         //add child parameters
@@ -266,7 +265,7 @@
                 String name = ((UIParameter)child).getName();
                 Object value = ((UIParameter)child).getValue();
 
-                renderLinkParameter(dummyFormResponseWriter, name, value, onClick, jsForm, nestingForm);
+                renderLinkParameter(name, value, onClick, jsForm, nestingForm);
             }
         }
 
@@ -401,8 +400,7 @@
         writer.flush();
     }
 
-    private void renderLinkParameter(DummyFormResponseWriter dummyFormResponseWriter,
-                                     String name,
+    private void renderLinkParameter(String name,
                                      Object value,
                                      StringBuffer onClick,
                                      String jsForm,
@@ -425,7 +423,7 @@
         }
         else
         {
-            dummyFormResponseWriter.addDummyFormParameter(name);
+            DummyFormUtils.addDummyFormParameter(FacesContext.getCurrentInstance(), name);
         }
     }
 

Modified: myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java
URL: http://svn.apache.org/viewcvs/myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java?rev=373272&r1=373271&r2=373272&view=diff
==============================================================================
--- myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java (original)
+++ myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/HtmlResponseWriterImpl.java Sat Jan 28 18:01:37 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004 The Apache Software Foundation.
+ * Copyright 2004-2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.myfaces.renderkit.RendererUtils;
-import org.apache.myfaces.renderkit.html.util.DummyFormResponseWriter;
 import org.apache.myfaces.renderkit.html.util.DummyFormUtils;
 import org.apache.myfaces.renderkit.html.util.HTMLEncoder;
 import org.apache.myfaces.renderkit.html.util.UnicodeEncoder;
@@ -39,7 +38,6 @@
  */
 public class HtmlResponseWriterImpl
         extends ResponseWriter
-        implements DummyFormResponseWriter
 {
     private static final Log log = LogFactory.getLog(HtmlResponseWriterImpl.class);
 
@@ -577,36 +575,5 @@
         // Don't bother encoding anything if chosen character encoding is UTF-8
         if (UTF8.equals(_characterEncoding)) _writer.write(strValue);
         else _writer.write(UnicodeEncoder.encode(strValue) );
-    }
-
-    // DummyFormResponseWriter support
-
-    public boolean isWriteDummyForm()
-    {
-        return _writeDummyForm;
-    }
-
-    public void setWriteDummyForm(boolean writeDummyForm)
-    {
-        _writeDummyForm = writeDummyForm;
-    }
-
-    public String getDummyFormName()
-    {
-        return DummyFormUtils.DUMMY_FORM_NAME;
-    }
-
-    public void addDummyFormParameter(String paramName)
-    {
-        if (_dummyFormParams == null)
-        {
-            _dummyFormParams = new HashSet();
-        }
-        _dummyFormParams.add(paramName);
-    }
-
-    public Set getDummyFormParams()
-    {
-        return _dummyFormParams;
     }
 }

Added: myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/DummyFormRequestInfo.java
URL: http://svn.apache.org/viewcvs/myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/DummyFormRequestInfo.java?rev=373272&view=auto
==============================================================================
--- myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/DummyFormRequestInfo.java (added)
+++ myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/DummyFormRequestInfo.java Sat Jan 28 18:01:37 2006
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.renderkit.html.util;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Convenient class to store whether a dummyForm needs to be rendered and its params.
+ * This class will be stored in the request when a dummyForm is needed to be rendered in the page.
+ * AddResources will add it from a method called from the ExtensionsFilter.
+ * <p>
+ * All this replaces the old system based in a DummyFormResponseWriter
+ *
+ * @author Bruno Aranda (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class DummyFormRequestInfo
+{
+
+   private Set _dummyFormParams = null;
+
+   public String getDummyFormName()
+   {
+       return DummyFormUtils.DUMMY_FORM_NAME;
+   }
+
+   public void addDummyFormParameter(String paramName)
+   {
+       if (_dummyFormParams == null)
+       {
+           _dummyFormParams = new HashSet();
+       }
+       _dummyFormParams.add(paramName);
+   }
+
+   public Set getDummyFormParams()
+   {
+       return _dummyFormParams;
+   }
+}

Propchange: myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/DummyFormRequestInfo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/DummyFormRequestInfo.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/DummyFormUtils.java
URL: http://svn.apache.org/viewcvs/myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/DummyFormUtils.java?rev=373272&r1=373271&r2=373272&view=diff
==============================================================================
--- myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/DummyFormUtils.java (original)
+++ myfaces/commons/trunk/src/main/java/org/apache/myfaces/renderkit/html/util/DummyFormUtils.java Sat Jan 28 18:01:37 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004 The Apache Software Foundation.
+ * Copyright 2004-2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -30,17 +30,19 @@
 import java.io.IOException;
 import java.util.Map;
 import java.util.Set;
+import java.util.HashSet;
 
 /**
  * Many JSF components can be used without an enclosing h:form. In this
  * case, they need a dummy form to store data into and submit when
  * communication with the server is necessary. These components can use
- * methods on the DummyFormResponseWriter object accessable via this
+ * methods on the <code>DummyFormRequestInfo</code> object accessable via this
  * class to register parameters and get the name of a form to submit.
  * <p>
  * Only one dummy form will be rendered into the response.
  * 
  * @author Manfred Geiler (latest modification by $Author$)
+ * @author Bruno Aranda
  * @version $Revision$ $Date$
  */
 public class DummyFormUtils
@@ -54,58 +56,77 @@
     private static final String DUMMY_FORM_ID = "linkDummyForm";
 
     /**
-     * Returns a DummyFormResponseWriter.
-     * <p>
-     * Note that the default HTML renderkit's createResponseWriter method returns
-     * instances of HtmlResponseWriterImpl, which already implements the
-     * DummyFormResponseWriter interface. Calls to this method when using that
-     * renderkit therefore just return the standard responsewriter, typecast
-     * to the appropriate interface.
-     * <p>
-     * For other renderkits whose createResponseWriter method doesn't return
-     * an implementation of DummyFormResponseWriter this method creates an
-     * implementation which <i>wraps</i> the current ResponseWriter while
-     * providing the additional functionality needed. The FacesContext is
-     * updated to use the wrapper as its ResponseWriter.
-     * <p>
-     * When a wrapper is created, it is also stored into the request for
-     * some reason. This can have unexpected side-effects, as changing the
-     * current FacesContext's responseWriter will not cause the cached
-     * DummyFormResponseWriterWrapper's underlying response writer to change.
-     * TODO: document why caching is needed.
+     * Used to store the instance of <code>DummyFormRequestInfo</code> in the request map
+     */
+    public static final String DUMMY_FORM_INFO = DummyFormUtils.class.getName()+".DUMMY_FORM_INFO";
+
+
+    public static String getDummyFormName() {
+        return DUMMY_FORM_NAME;
+    }
+
+    /**
+     * When writeDummyForm is set to true, a <code>DummyFormRequestInfo</code> object will be instantiated
+     * and put in the request. Later, if this object is found, the dummyForm javascript will be rendered in the page
+     * before the end of the <code>body</code> tag
+     *
+     * @param facesContext
+     * @param writeDummyForm
+     */
+    public static void setWriteDummyForm(FacesContext facesContext, boolean writeDummyForm)
+    {
+        if (!writeDummyForm)
+        {
+            return;
+        }
+
+         if (!isWriteDummyForm(facesContext))
+         {
+             DummyFormRequestInfo dummyFormInfo = new DummyFormRequestInfo();
+             facesContext.getExternalContext().getRequestMap().put(DUMMY_FORM_INFO, dummyFormInfo);
+         }
+    }
+
+    /**
+     * Checks if the DummyFormRequestInfo is already in the request map.
+     * @param facesContext
+     * @return
+     */
+    public static boolean isWriteDummyForm(FacesContext facesContext)
+    {
+        return facesContext.getExternalContext().getRequestMap().containsKey(DUMMY_FORM_INFO);
+    }
+
+    /**
+     * Delegator method to add a parameter to the DummyFormRequestInfo object in the request
      * 
      * @param facesContext
-     * @return a DummyFormResponseWriter instance
+     * @param paramName
      */
-    public static DummyFormResponseWriter getDummyFormResponseWriter(FacesContext facesContext)
+    public static void addDummyFormParameter(FacesContext facesContext, String paramName)
     {
-        ResponseWriter currentWriter = facesContext.getResponseWriter();
-        if (currentWriter instanceof DummyFormResponseWriter)
+        if (isWriteDummyForm(facesContext))
         {
-            return (DummyFormResponseWriter)currentWriter;
+            DummyFormRequestInfo dummyFormInfo = (DummyFormRequestInfo) facesContext.getExternalContext().getRequestMap().get(DUMMY_FORM_INFO);
+            dummyFormInfo.addDummyFormParameter(paramName);
         }
         else
         {
-            // current ResponseWriter is not a DummyFormResponseWriter
-            // and therfore must be wrapped
-            Map requestMap = facesContext.getExternalContext().getRequestMap();
-            DummyFormResponseWriterWrapper writer
-                    = (DummyFormResponseWriterWrapper)requestMap.get(DUMMY_FORM_RESPONSE_WRITER_WRAPPER_REQ_PARAM);
-            if (writer != null)
+            if (log.isWarnEnabled())
             {
-                //There is already a wrapped response writer
-                return writer;
-            }
-            else
-            {
-                //We must wrap current writer and replace it in the FacesContext
-                writer = new DummyFormResponseWriterWrapper(currentWriter);
-                facesContext.setResponseWriter(writer);
-                requestMap.put(DUMMY_FORM_RESPONSE_WRITER_WRAPPER_REQ_PARAM, writer);
-                log.debug("DummyFormResponseWriterWrapper installed");
-                return writer;
+                log.warn("Dummy Form parameter was not added because dummy form is not written");
             }
         }
+    }
+
+    public static Set getDummyFormParameters(FacesContext facesContext) {
+        if (isWriteDummyForm(facesContext))
+        {
+            DummyFormRequestInfo dummyFormInfo = (DummyFormRequestInfo) facesContext.getExternalContext().getRequestMap().get(DUMMY_FORM_INFO);
+            dummyFormInfo.getDummyFormParams();
+        }
+
+        return new HashSet();
     }