You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ja...@apache.org on 2010/01/21 21:30:24 UTC

svn commit: r901847 - in /myfaces/core/trunk: api/src/main/java/javax/faces/application/ impl/src/main/java/org/apache/myfaces/application/ impl/src/main/java/org/apache/myfaces/lifecycle/ impl/src/main/java/org/apache/myfaces/view/

Author: jakobk
Date: Thu Jan 21 20:30:23 2010
New Revision: 901847

URL: http://svn.apache.org/viewvc?rev=901847&view=rev
Log:
MYFACES-2468 MyFaces needs to support adding a <view-handler> in faces-config.xml

Modified:
    myfaces/core/trunk/api/src/main/java/javax/faces/application/ViewHandler.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ViewHandlerImpl.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/lifecycle/RestoreViewExecutor.java
    myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/ViewDeclarationLanguageFactoryImpl.java

Modified: myfaces/core/trunk/api/src/main/java/javax/faces/application/ViewHandler.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/api/src/main/java/javax/faces/application/ViewHandler.java?rev=901847&r1=901846&r2=901847&view=diff
==============================================================================
--- myfaces/core/trunk/api/src/main/java/javax/faces/application/ViewHandler.java (original)
+++ myfaces/core/trunk/api/src/main/java/javax/faces/application/ViewHandler.java Thu Jan 21 20:30:23 2010
@@ -25,20 +25,13 @@
 import java.util.Map;
 
 import javax.faces.FacesException;
-import javax.faces.FactoryFinder;
-import javax.faces.component.UIComponent;
 import javax.faces.component.UIViewRoot;
 import javax.faces.context.ExternalContext;
 import javax.faces.context.FacesContext;
-import javax.faces.view.AttachedObjectHandler;
 import javax.faces.view.ViewDeclarationLanguage;
-import javax.faces.view.ViewDeclarationLanguageFactory;
 
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
 
-import java.io.UnsupportedEncodingException;
-import java.util.Locale;
-
 /**
  * A ViewHandler manages the component-tree-creation and component-tree-rendering parts of a request lifecycle (ie
  * "create view", "restore view" and "render response").
@@ -207,20 +200,23 @@
     /**
      * Return the ViewDeclarationLanguage instance used for this ViewHandler  instance.
      * <P>
-     * The default implementation must use ViewDeclarationLanguageFactory.getViewDeclarationLanguage(java.lang.String) to obtain the appropriate ViewDeclarationLanguage implementation for the argument viewId. Any exceptions thrown as a result of invoking that method must not be swallowed.
-     * <P>
      * The default implementation of this method returns null.
      * 
      * @param context
      * @param viewId
      * @return
+     * 
+     * @since 2.0
      */
     public ViewDeclarationLanguage getViewDeclarationLanguage(FacesContext context, String viewId)
     {
-        // FIXME: Notify EG - The JavaDoc mention 2 default implementation, how lovely, using the most useful one.
         // TODO: In some places like RestoreViewExecutor, we are calling deriveViewId after call restoreViewSupport.calculateViewId
         // Maybe this method should be called from here, because it is supposed that calculateViewId "calculates the view id!"
-        return _getViewDeclarationLanguageFactory().getViewDeclarationLanguage(viewId);
+        
+        // here we return null to support pre jsf 2.0 ViewHandlers (e.g. com.sun.facelets.FaceletViewHandler),
+        // because they don't provide any ViewDeclarationLanguage,
+        // but in the default implementation (ViewHandlerImpl) we return vdlFactory.getViewDeclarationLanguage(viewId)
+        return null;
     }
 
     /**
@@ -231,6 +227,8 @@
      * @param parameters
      * @param includeViewParams
      * @return
+     * 
+     * @since 2.0
      */
     public String getRedirectURL(FacesContext context, String viewId, Map<String, List<String>> parameters,
                                  boolean includeViewParams)
@@ -289,8 +287,4 @@
      */
     public abstract void writeState(FacesContext context) throws IOException;
 
-    private ViewDeclarationLanguageFactory _getViewDeclarationLanguageFactory()
-    {
-        return (ViewDeclarationLanguageFactory)FactoryFinder.getFactory(FactoryFinder.VIEW_DECLARATION_LANGUAGE_FACTORY);
-    }
 }

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ViewHandlerImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ViewHandlerImpl.java?rev=901847&r1=901846&r2=901847&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ViewHandlerImpl.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/application/ViewHandlerImpl.java Thu Jan 21 20:30:23 2010
@@ -1,376 +1,382 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.application;
-
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Locale;
-import java.util.Map;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import javax.faces.FacesException;
-import javax.faces.FactoryFinder;
-import javax.faces.application.Application;
-import javax.faces.application.StateManager;
-import javax.faces.application.ViewHandler;
-import javax.faces.component.UIViewParameter;
-import javax.faces.component.UIViewRoot;
-import javax.faces.context.ExternalContext;
-import javax.faces.context.FacesContext;
-import javax.faces.render.RenderKitFactory;
-import javax.faces.render.ResponseStateManager;
-import javax.faces.view.ViewDeclarationLanguage;
-import javax.faces.view.ViewDeclarationLanguageFactory;
-import javax.faces.view.ViewMetadata;
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.myfaces.shared_impl.application.DefaultViewHandlerSupport;
-import org.apache.myfaces.shared_impl.application.InvalidViewIdException;
-import org.apache.myfaces.shared_impl.application.ViewHandlerSupport;
-import org.apache.myfaces.shared_impl.config.MyfacesConfig;
-import org.apache.myfaces.shared_impl.renderkit.html.util.JavascriptUtils;
-import org.apache.myfaces.view.facelets.StateWriter;
-
-public class ViewHandlerImpl extends ViewHandler
-{
-    //private static final Log log = LogFactory.getLog(ViewHandlerImpl.class);
-    private static final Logger log = Logger.getLogger(ViewHandlerImpl.class.getName());
-    public static final String FORM_STATE_MARKER = "<!-...@-->";
-    private ViewHandlerSupport _viewHandlerSupport;
-    private ViewDeclarationLanguageFactory _vdlFactory;
-
-    public ViewHandlerImpl()
-    {
-        _vdlFactory = (ViewDeclarationLanguageFactory)FactoryFinder.getFactory(FactoryFinder.VIEW_DECLARATION_LANGUAGE_FACTORY);
-        if (log.isLoggable(Level.FINEST))
-            log.finest("New ViewHandler instance created");
-    }
-
-    @Override
-    public String deriveViewId(FacesContext context, String input)
-    {
-        if(input != null){
-            try
-            {
-                //TODO: JSF 2.0 - need to make sure calculateViewId follows the new algorithm from 7.5.2 
-                return getViewHandlerSupport().calculateAndCheckViewId(context, input);
-            }
-            catch (InvalidViewIdException e)
-            {
-                sendSourceNotFound(context, e.getMessage());
-            }
-        }
-        return input;   // If the argument input is null, return null.
-    }
-
-    @Override
-    public String getBookmarkableURL(FacesContext context, String viewId,
-            Map<String, List<String>> parameters, boolean includeViewParams)
-    {
-        Map<String, List<String>> viewParameters;
-        ExternalContext externalContext = context.getExternalContext();
-        if (includeViewParams)
-        {
-            viewParameters = getViewParameterList(context, viewId, parameters);
-        }
-        else
-        {
-            viewParameters = parameters;
-        }
-        
-        String actionEncodedViewId = getActionURL(context, viewId);
-        String bookmarkEncodedURL = externalContext.encodeBookmarkableURL(actionEncodedViewId, viewParameters);
-        return externalContext.encodeActionURL(bookmarkEncodedURL);
-    }
-
-    @Override
-    public String getRedirectURL(FacesContext context, String viewId,
-            Map<String, List<String>> parameters, boolean includeViewParams)
-    {
-        Map<String, List<String>> viewParameters;
-        ExternalContext externalContext = context.getExternalContext();
-        if (includeViewParams)
-        {
-            viewParameters = getViewParameterList(context, viewId, parameters);
-        }
-        else
-        {
-            viewParameters = parameters;
-        }
-        
-        String actionEncodedViewId = getActionURL(context, viewId);
-        String redirectEncodedURL = externalContext.encodeRedirectURL(actionEncodedViewId, viewParameters);
-        return externalContext.encodeActionURL(redirectEncodedURL);
-    }
-
-    @Override
-    public ViewDeclarationLanguage getViewDeclarationLanguage(
-            FacesContext context, String viewId)
-    {
-        return _vdlFactory.getViewDeclarationLanguage(viewId);
-    }
-
-    @Override
-    public void initView(FacesContext context) throws FacesException
-    {
-        if(context.getExternalContext().getRequestCharacterEncoding() == null)
-        {
-            super.initView(context);    
-        }        
-    }
-
-    /**
-     * Get the locales specified as acceptable by the original request, compare them to the
-     * locales supported by this Application and return the best match.
-     */
-    @Override
-    public Locale calculateLocale(FacesContext facesContext)
-    {
-        Application application = facesContext.getApplication();
-        for (Iterator<Locale> requestLocales = facesContext.getExternalContext().getRequestLocales(); requestLocales
-                .hasNext();)
-        {
-            Locale requestLocale = requestLocales.next();
-            for (Iterator<Locale> supportedLocales = application.getSupportedLocales(); supportedLocales.hasNext();)
-            {
-                Locale supportedLocale = supportedLocales.next();
-                // higher priority to a language match over an exact match
-                // that occurs further down (see JSTL Reference 1.0 8.3.1)
-                if (requestLocale.getLanguage().equals(supportedLocale.getLanguage())
-                        && (supportedLocale.getCountry() == null || supportedLocale.getCountry().length() == 0))
-                {
-                    return supportedLocale;
-                }
-                else if (supportedLocale.equals(requestLocale))
-                {
-                    return supportedLocale;
-                }
-            }
-        }
-
-        Locale defaultLocale = application.getDefaultLocale();
-        return defaultLocale != null ? defaultLocale : Locale.getDefault();
-    }
-
-    @Override
-    public String calculateRenderKitId(FacesContext facesContext)
-    {
-        Object renderKitId = facesContext.getExternalContext().getRequestMap().get(
-                ResponseStateManager.RENDER_KIT_ID_PARAM);
-        if (renderKitId == null)
-        {
-            renderKitId = facesContext.getApplication().getDefaultRenderKitId();
-        }
-        if (renderKitId == null)
-        {
-            renderKitId = RenderKitFactory.HTML_BASIC_RENDER_KIT;
-        }
-        return renderKitId.toString();
-    }
-    
-    @Override
-    public UIViewRoot createView(FacesContext context, String viewId)
-    {
-       checkNull(context, "facesContext");
-       String calculatedViewId = getViewHandlerSupport().calculateViewId(context, viewId);
-       return getViewDeclarationLanguage(context,calculatedViewId).createView(context,calculatedViewId);
-    }
-
-    @Override
-    public String getActionURL(FacesContext context, String viewId)
-    {
-        return getViewHandlerSupport().calculateActionURL(context, viewId);
-    }
-
-    @Override
-    public String getResourceURL(FacesContext facesContext, String path)
-    {
-        if (path.length() > 0 && path.charAt(0) == '/')
-        {
-            return facesContext.getExternalContext().getRequestContextPath() + path;
-        }
-
-        return path;
-
-    }
-
-    @Override
-    public void renderView(FacesContext context, UIViewRoot viewToRender)
-            throws IOException, FacesException
-    {
-
-        checkNull(context, "context");
-        checkNull(viewToRender, "viewToRender");
-
-        getViewDeclarationLanguage(context,viewToRender.getViewId()).renderView(context, viewToRender);
-    }
-
-    @Override
-    public UIViewRoot restoreView(FacesContext context, String viewId)
-    {
-        checkNull(context, "context");
-    
-        String calculatedViewId = getViewHandlerSupport().calculateViewId(context, viewId);
-        return getViewDeclarationLanguage(context,calculatedViewId).restoreView(context, calculatedViewId); 
-    }
-    
-    @Override
-    public void writeState(FacesContext context) throws IOException
-    {
-        checkNull(context, "context");
-
-        if(context.getPartialViewContext().isAjaxRequest())
-            return;
-
-        // Facelets specific hack:
-        // Tell the StateWriter that we're about to write state
-        StateWriter stateWriter = StateWriter.getCurrentInstance();
-        if (stateWriter != null)
-        {
-            // Write the STATE_KEY out. Unfortunately, this will
-            // be wasteful for pure server-side state managers where nothing
-            // is actually written into the output, but this cannot
-            // programatically be discovered
-            stateWriter.writingState();
-        }
-
-        StateManager stateManager = context.getApplication().getStateManager();
-        if (stateManager.isSavingStateInClient(context))
-        {
-            // Only write state marker if javascript view state is disabled
-            ExternalContext extContext = context.getExternalContext();
-            if (!(JavascriptUtils.isJavascriptAllowed(extContext) && MyfacesConfig.getCurrentInstance(extContext).isViewStateJavascript())) {
-                context.getResponseWriter().write(FORM_STATE_MARKER);
-            }
-        }
-        else
-        {
-            stateManager.writeState(context, new Object[2]);
-        }
-    }
-    
-    private Map<String, List<String>> getViewParameterList(FacesContext context,
-            String viewId, Map<String, List<String>> parametersFromArg)
-    {
-
-        Map<String, List<String>> viewParameters;
-        UIViewRoot viewRoot = context.getViewRoot();
-        String currentViewId = viewRoot.getViewId();
-        Collection<UIViewParameter> toViewParams;
-        Collection<UIViewParameter> currentViewParams = ViewMetadata.getViewParameters(viewRoot);
-
-        if (currentViewId.equals(viewId))
-        {
-            toViewParams = currentViewParams;
-        }
-        else
-        {
-            String calculatedViewId = getViewHandlerSupport().calculateViewId(context, viewId);            
-            ViewDeclarationLanguage vdl = getViewDeclarationLanguage(context,calculatedViewId);
-            ViewMetadata viewMetadata = vdl.getViewMetadata(context, viewId);
-            UIViewRoot viewFromMetaData = viewMetadata.createMetadataView(context);
-            toViewParams = ViewMetadata.getViewParameters(viewFromMetaData);
-        }
-
-        if (toViewParams.isEmpty())
-        {
-            return parametersFromArg;
-        }
-
-        for (UIViewParameter viewParameter : toViewParams)
-        {
-            if (!parametersFromArg.containsKey(viewParameter.getName()))
-            {
-                String parameterValue = viewParameter.getStringValueFromModel(context);
-                if (parameterValue == null)
-                {
-                    if(currentViewId.equals(viewId))
-                    {
-                        parameterValue = viewParameter.getStringValue(context);
-                    }
-                    else
-                    {
-                        boolean found = false;
-                        for (UIViewParameter curParam : currentViewParams) {
-                            if (curParam.getName() != null && viewParameter.getName() != null &&
-                                    curParam.getName().equals(viewParameter.getName())) 
-                            {
-                                parameterValue = curParam.getStringValue(context);
-                                found = true;
-                            }
-                            if (found)
-                                break;
-                        }
-                    }
-                }
-                if (parameterValue != null)
-                {
-                    List<String> parameterValueList = parametersFromArg.get(viewParameter.getName());
-                    if (parameterValueList == null)
-                    {
-                        parameterValueList = new ArrayList<String>();
-                    }
-                    parameterValueList.add(parameterValue);
-                    parametersFromArg.put(viewParameter.getName(),parameterValueList);
-                }
-            }
-        }        
-        return parametersFromArg;
-    }
-    
-    private void checkNull(final Object o, final String param)
-    {
-        if (o == null)
-        {
-            throw new NullPointerException(param + " can not be null.");
-        }
-    }
-    
-    private void sendSourceNotFound(FacesContext context, String message)
-    {
-        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
-        try
-        {
-            context.responseComplete();
-            response.sendError(HttpServletResponse.SC_NOT_FOUND, message);
-        }
-        catch (IOException ioe)
-        {
-            throw new FacesException(ioe);
-        }
-    }
-    
-    public void setViewHandlerSupport(ViewHandlerSupport viewHandlerSupport)
-    {
-        _viewHandlerSupport = viewHandlerSupport;
-    }    
-    
-    protected ViewHandlerSupport getViewHandlerSupport()
-    {
-        if (_viewHandlerSupport == null)
-        {
-            _viewHandlerSupport = new DefaultViewHandlerSupport();
-        }
-        return _viewHandlerSupport;
-    }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.application;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.faces.FacesException;
+import javax.faces.FactoryFinder;
+import javax.faces.application.Application;
+import javax.faces.application.StateManager;
+import javax.faces.application.ViewHandler;
+import javax.faces.component.UIViewParameter;
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+import javax.faces.render.RenderKitFactory;
+import javax.faces.render.ResponseStateManager;
+import javax.faces.view.ViewDeclarationLanguage;
+import javax.faces.view.ViewDeclarationLanguageFactory;
+import javax.faces.view.ViewMetadata;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.myfaces.shared_impl.application.DefaultViewHandlerSupport;
+import org.apache.myfaces.shared_impl.application.InvalidViewIdException;
+import org.apache.myfaces.shared_impl.application.ViewHandlerSupport;
+import org.apache.myfaces.shared_impl.config.MyfacesConfig;
+import org.apache.myfaces.shared_impl.renderkit.html.util.JavascriptUtils;
+import org.apache.myfaces.view.facelets.StateWriter;
+
+/**
+ * JSF 2.0 ViewHandler implementation 
+ *
+ * @since 2.0
+ */
+public class ViewHandlerImpl extends ViewHandler
+{
+    //private static final Log log = LogFactory.getLog(ViewHandlerImpl.class);
+    private static final Logger log = Logger.getLogger(ViewHandlerImpl.class.getName());
+    public static final String FORM_STATE_MARKER = "<!-...@-->";
+    private ViewHandlerSupport _viewHandlerSupport;
+    private ViewDeclarationLanguageFactory _vdlFactory;
+
+    public ViewHandlerImpl()
+    {
+        _vdlFactory = (ViewDeclarationLanguageFactory)FactoryFinder.getFactory(FactoryFinder.VIEW_DECLARATION_LANGUAGE_FACTORY);
+        if (log.isLoggable(Level.FINEST))
+            log.finest("New ViewHandler instance created");
+    }
+
+    @Override
+    public String deriveViewId(FacesContext context, String input)
+    {
+        if(input != null){
+            try
+            {
+                //TODO: JSF 2.0 - need to make sure calculateViewId follows the new algorithm from 7.5.2 
+                return getViewHandlerSupport().calculateAndCheckViewId(context, input);
+            }
+            catch (InvalidViewIdException e)
+            {
+                sendSourceNotFound(context, e.getMessage());
+            }
+        }
+        return input;   // If the argument input is null, return null.
+    }
+
+    @Override
+    public String getBookmarkableURL(FacesContext context, String viewId,
+            Map<String, List<String>> parameters, boolean includeViewParams)
+    {
+        Map<String, List<String>> viewParameters;
+        ExternalContext externalContext = context.getExternalContext();
+        if (includeViewParams)
+        {
+            viewParameters = getViewParameterList(context, viewId, parameters);
+        }
+        else
+        {
+            viewParameters = parameters;
+        }
+        
+        String actionEncodedViewId = getActionURL(context, viewId);
+        String bookmarkEncodedURL = externalContext.encodeBookmarkableURL(actionEncodedViewId, viewParameters);
+        return externalContext.encodeActionURL(bookmarkEncodedURL);
+    }
+
+    @Override
+    public String getRedirectURL(FacesContext context, String viewId,
+            Map<String, List<String>> parameters, boolean includeViewParams)
+    {
+        Map<String, List<String>> viewParameters;
+        ExternalContext externalContext = context.getExternalContext();
+        if (includeViewParams)
+        {
+            viewParameters = getViewParameterList(context, viewId, parameters);
+        }
+        else
+        {
+            viewParameters = parameters;
+        }
+        
+        String actionEncodedViewId = getActionURL(context, viewId);
+        String redirectEncodedURL = externalContext.encodeRedirectURL(actionEncodedViewId, viewParameters);
+        return externalContext.encodeActionURL(redirectEncodedURL);
+    }
+
+    @Override
+    public ViewDeclarationLanguage getViewDeclarationLanguage(
+            FacesContext context, String viewId)
+    {
+        // return a suitable ViewDeclarationLanguage implementation for the given viewId
+        return _vdlFactory.getViewDeclarationLanguage(viewId);
+    }
+
+    @Override
+    public void initView(FacesContext context) throws FacesException
+    {
+        if(context.getExternalContext().getRequestCharacterEncoding() == null)
+        {
+            super.initView(context);    
+        }        
+    }
+
+    /**
+     * Get the locales specified as acceptable by the original request, compare them to the
+     * locales supported by this Application and return the best match.
+     */
+    @Override
+    public Locale calculateLocale(FacesContext facesContext)
+    {
+        Application application = facesContext.getApplication();
+        for (Iterator<Locale> requestLocales = facesContext.getExternalContext().getRequestLocales(); requestLocales
+                .hasNext();)
+        {
+            Locale requestLocale = requestLocales.next();
+            for (Iterator<Locale> supportedLocales = application.getSupportedLocales(); supportedLocales.hasNext();)
+            {
+                Locale supportedLocale = supportedLocales.next();
+                // higher priority to a language match over an exact match
+                // that occurs further down (see JSTL Reference 1.0 8.3.1)
+                if (requestLocale.getLanguage().equals(supportedLocale.getLanguage())
+                        && (supportedLocale.getCountry() == null || supportedLocale.getCountry().length() == 0))
+                {
+                    return supportedLocale;
+                }
+                else if (supportedLocale.equals(requestLocale))
+                {
+                    return supportedLocale;
+                }
+            }
+        }
+
+        Locale defaultLocale = application.getDefaultLocale();
+        return defaultLocale != null ? defaultLocale : Locale.getDefault();
+    }
+
+    @Override
+    public String calculateRenderKitId(FacesContext facesContext)
+    {
+        Object renderKitId = facesContext.getExternalContext().getRequestMap().get(
+                ResponseStateManager.RENDER_KIT_ID_PARAM);
+        if (renderKitId == null)
+        {
+            renderKitId = facesContext.getApplication().getDefaultRenderKitId();
+        }
+        if (renderKitId == null)
+        {
+            renderKitId = RenderKitFactory.HTML_BASIC_RENDER_KIT;
+        }
+        return renderKitId.toString();
+    }
+    
+    @Override
+    public UIViewRoot createView(FacesContext context, String viewId)
+    {
+       checkNull(context, "facesContext");
+       String calculatedViewId = getViewHandlerSupport().calculateViewId(context, viewId);
+       return getViewDeclarationLanguage(context,calculatedViewId).createView(context,calculatedViewId);
+    }
+
+    @Override
+    public String getActionURL(FacesContext context, String viewId)
+    {
+        return getViewHandlerSupport().calculateActionURL(context, viewId);
+    }
+
+    @Override
+    public String getResourceURL(FacesContext facesContext, String path)
+    {
+        if (path.length() > 0 && path.charAt(0) == '/')
+        {
+            return facesContext.getExternalContext().getRequestContextPath() + path;
+        }
+
+        return path;
+
+    }
+
+    @Override
+    public void renderView(FacesContext context, UIViewRoot viewToRender)
+            throws IOException, FacesException
+    {
+
+        checkNull(context, "context");
+        checkNull(viewToRender, "viewToRender");
+
+        getViewDeclarationLanguage(context,viewToRender.getViewId()).renderView(context, viewToRender);
+    }
+
+    @Override
+    public UIViewRoot restoreView(FacesContext context, String viewId)
+    {
+        checkNull(context, "context");
+    
+        String calculatedViewId = getViewHandlerSupport().calculateViewId(context, viewId);
+        return getViewDeclarationLanguage(context,calculatedViewId).restoreView(context, calculatedViewId); 
+    }
+    
+    @Override
+    public void writeState(FacesContext context) throws IOException
+    {
+        checkNull(context, "context");
+
+        if(context.getPartialViewContext().isAjaxRequest())
+            return;
+
+        // Facelets specific hack:
+        // Tell the StateWriter that we're about to write state
+        StateWriter stateWriter = StateWriter.getCurrentInstance();
+        if (stateWriter != null)
+        {
+            // Write the STATE_KEY out. Unfortunately, this will
+            // be wasteful for pure server-side state managers where nothing
+            // is actually written into the output, but this cannot
+            // programatically be discovered
+            stateWriter.writingState();
+        }
+
+        StateManager stateManager = context.getApplication().getStateManager();
+        if (stateManager.isSavingStateInClient(context))
+        {
+            // Only write state marker if javascript view state is disabled
+            ExternalContext extContext = context.getExternalContext();
+            if (!(JavascriptUtils.isJavascriptAllowed(extContext) && MyfacesConfig.getCurrentInstance(extContext).isViewStateJavascript())) {
+                context.getResponseWriter().write(FORM_STATE_MARKER);
+            }
+        }
+        else
+        {
+            stateManager.writeState(context, new Object[2]);
+        }
+    }
+    
+    private Map<String, List<String>> getViewParameterList(FacesContext context,
+            String viewId, Map<String, List<String>> parametersFromArg)
+    {
+
+        Map<String, List<String>> viewParameters;
+        UIViewRoot viewRoot = context.getViewRoot();
+        String currentViewId = viewRoot.getViewId();
+        Collection<UIViewParameter> toViewParams;
+        Collection<UIViewParameter> currentViewParams = ViewMetadata.getViewParameters(viewRoot);
+
+        if (currentViewId.equals(viewId))
+        {
+            toViewParams = currentViewParams;
+        }
+        else
+        {
+            String calculatedViewId = getViewHandlerSupport().calculateViewId(context, viewId);            
+            ViewDeclarationLanguage vdl = getViewDeclarationLanguage(context,calculatedViewId);
+            ViewMetadata viewMetadata = vdl.getViewMetadata(context, viewId);
+            UIViewRoot viewFromMetaData = viewMetadata.createMetadataView(context);
+            toViewParams = ViewMetadata.getViewParameters(viewFromMetaData);
+        }
+
+        if (toViewParams.isEmpty())
+        {
+            return parametersFromArg;
+        }
+
+        for (UIViewParameter viewParameter : toViewParams)
+        {
+            if (!parametersFromArg.containsKey(viewParameter.getName()))
+            {
+                String parameterValue = viewParameter.getStringValueFromModel(context);
+                if (parameterValue == null)
+                {
+                    if(currentViewId.equals(viewId))
+                    {
+                        parameterValue = viewParameter.getStringValue(context);
+                    }
+                    else
+                    {
+                        boolean found = false;
+                        for (UIViewParameter curParam : currentViewParams) {
+                            if (curParam.getName() != null && viewParameter.getName() != null &&
+                                    curParam.getName().equals(viewParameter.getName())) 
+                            {
+                                parameterValue = curParam.getStringValue(context);
+                                found = true;
+                            }
+                            if (found)
+                                break;
+                        }
+                    }
+                }
+                if (parameterValue != null)
+                {
+                    List<String> parameterValueList = parametersFromArg.get(viewParameter.getName());
+                    if (parameterValueList == null)
+                    {
+                        parameterValueList = new ArrayList<String>();
+                    }
+                    parameterValueList.add(parameterValue);
+                    parametersFromArg.put(viewParameter.getName(),parameterValueList);
+                }
+            }
+        }        
+        return parametersFromArg;
+    }
+    
+    private void checkNull(final Object o, final String param)
+    {
+        if (o == null)
+        {
+            throw new NullPointerException(param + " can not be null.");
+        }
+    }
+    
+    private void sendSourceNotFound(FacesContext context, String message)
+    {
+        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
+        try
+        {
+            context.responseComplete();
+            response.sendError(HttpServletResponse.SC_NOT_FOUND, message);
+        }
+        catch (IOException ioe)
+        {
+            throw new FacesException(ioe);
+        }
+    }
+    
+    public void setViewHandlerSupport(ViewHandlerSupport viewHandlerSupport)
+    {
+        _viewHandlerSupport = viewHandlerSupport;
+    }    
+    
+    protected ViewHandlerSupport getViewHandlerSupport()
+    {
+        if (_viewHandlerSupport == null)
+        {
+            _viewHandlerSupport = new DefaultViewHandlerSupport();
+        }
+        return _viewHandlerSupport;
+    }
+}

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/lifecycle/RestoreViewExecutor.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/lifecycle/RestoreViewExecutor.java?rev=901847&r1=901846&r2=901847&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/lifecycle/RestoreViewExecutor.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/lifecycle/RestoreViewExecutor.java Thu Jan 21 20:30:23 2010
@@ -153,37 +153,40 @@
                     }
                 }
     
-                // call ViewHandler.createView(), passing the FacesContext instance for the current request and 
-                // the view identifier
-                if (viewRoot == null)
-                {
-                    viewRoot = viewHandler.createView(facesContext, viewId);
-                }
-                
-                // Subscribe the newly created UIViewRoot instance to the AfterAddToParent event, passing the 
-                // UIViewRoot instance itself as the listener.
-                // -= Leonardo Uribe =- This line it is not necessary because it was
-                // removed from jsf 2.0 section 2.2.1 when pass from EDR2 to Public Review 
-                // viewRoot.subscribeToEvent(PostAddToViewEvent.class, viewRoot);
-                
-                // Store the new UIViewRoot instance in the FacesContext.
-                facesContext.setViewRoot(viewRoot);
-    
                 // If viewParameters is not an empty collection DO NOT call renderResponse
                 if ( !(viewParameters != null && !viewParameters.isEmpty()) )
                 {
                     // Call renderResponse() on the FacesContext.
                     facesContext.renderResponse();
                 }
-                
-                // Publish an AfterAddToParent event with the created UIViewRoot as the event source.
-                application.publishEvent(facesContext, PostAddToViewEvent.class, viewRoot);
             }
             else
             {
-                //Call renderResponse but do not publish event because no viewRoot is created
+                // Call renderResponse
                 facesContext.renderResponse();
             }
+            
+            // viewRoot can be null here, if ...
+            //   - we don't have a ViewDeclarationLanguage (e.g. when using facelets-1.x)
+            //   - there is no view metadata or metadata.createMetadataView() returned null
+            if (viewRoot == null)
+            {
+                // call ViewHandler.createView(), passing the FacesContext instance for the current request and 
+                // the view identifier
+                viewRoot = viewHandler.createView(facesContext, viewId);
+            }
+            
+            // Subscribe the newly created UIViewRoot instance to the AfterAddToParent event, passing the 
+            // UIViewRoot instance itself as the listener.
+            // -= Leonardo Uribe =- This line it is not necessary because it was
+            // removed from jsf 2.0 section 2.2.1 when pass from EDR2 to Public Review 
+            // viewRoot.subscribeToEvent(PostAddToViewEvent.class, viewRoot);
+            
+            // Store the new UIViewRoot instance in the FacesContext.
+            facesContext.setViewRoot(viewRoot);
+            
+            // Publish an AfterAddToParent event with the created UIViewRoot as the event source.
+            application.publishEvent(facesContext, PostAddToViewEvent.class, viewRoot);
         }
 
         // add the ErrorPageBean to the view map to fully support 

Modified: myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/ViewDeclarationLanguageFactoryImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/ViewDeclarationLanguageFactoryImpl.java?rev=901847&r1=901846&r2=901847&view=diff
==============================================================================
--- myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/ViewDeclarationLanguageFactoryImpl.java (original)
+++ myfaces/core/trunk/impl/src/main/java/org/apache/myfaces/view/ViewDeclarationLanguageFactoryImpl.java Thu Jan 21 20:30:23 2010
@@ -1,129 +1,132 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.view;
-
-import javax.faces.FacesException;
-import javax.faces.context.FacesContext;
-import javax.faces.view.ViewDeclarationLanguage;
-import javax.faces.view.ViewDeclarationLanguageFactory;
-
-import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
-import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguageStrategy;
-import org.apache.myfaces.view.jsp.JspViewDeclarationLanguageStrategy;
-
-/**
- * This is the default PDL factory used as of JSF 2.0, it tries to use Facelet PDL whenever possible, 
- * but fallback on JSP if required.
- * 
- * @author Simon Lessard (latest modification by $Author: slessard $)
- * @version $Revision: 696523 $ $Date: 2009-03-21 14:55:54 -0400 (mer., 17 sept. 2008) $
- *
- * @since 2.0
- */
-public class ViewDeclarationLanguageFactoryImpl extends ViewDeclarationLanguageFactory
-{
-    /**
-     * Disable facelets VDL from the current application project. 
-     */
-    @JSFWebConfigParam(since="2.0", defaultValue="false", expectedValues="true,false")
-    public static final String PARAM_DISABLE_JSF_FACELET = "javax.faces.DISABLE_FACELET_JSF_VIEWHANDLER";
-    
-    private boolean _initialized;
-    private ViewDeclarationLanguageStrategy[] _supportedLanguages;
-    
-    /**
-     * 
-     */
-    public ViewDeclarationLanguageFactoryImpl()
-    {
-        _initialized = false;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public ViewDeclarationLanguage getViewDeclarationLanguage(String viewId)
-    {
-        //if (viewId == null)
-        //{
-        //    throw new NullPointerException("viewId");
-        //}
-        
-        // TODO: It would be nice to be able to preinitialize the factory. However, since it requires 
-        //       access to the ExternalContext it may not be possible, depending on the loading order 
-        //       in the FactoryFinder. Could use ideas here. -= SL =-
-        initialize();
-        
-        for (ViewDeclarationLanguageStrategy strategy : _supportedLanguages)
-        {
-            if (strategy.handles(viewId))
-            {
-                return strategy.getViewDeclarationLanguage();
-            }
-        }
-        
-        throw new FacesException("Cannot find a valid PDL for view id " + viewId);
-    }
-    
-    /**
-     * Initialize the supported view declaration languages.
-     */
-    private void initialize()
-    {
-        if (!_initialized)
-        {
-            if (isFaceletsEnabled())
-            {
-                _supportedLanguages = new ViewDeclarationLanguageStrategy[2];
-                _supportedLanguages[0] = new FaceletViewDeclarationLanguageStrategy();
-                _supportedLanguages[1] = new JspViewDeclarationLanguageStrategy();
-            }
-            else
-            {
-                // Support JSP only
-                _supportedLanguages = new ViewDeclarationLanguageStrategy[1];
-                _supportedLanguages[0] = new JspViewDeclarationLanguageStrategy();
-            }
-
-            _initialized = true;
-        }
-    }
-    
-    /**
-     * Determines if the current application uses Facelets.
-     * 
-     * @return <code>true</code> if the current application uses Facelets, <code>false</code> 
-     *         otherwise.
-     */
-    private boolean isFaceletsEnabled()
-    {
-        FacesContext context = FacesContext.getCurrentInstance();
-        String param = context.getExternalContext().getInitParameter(PARAM_DISABLE_JSF_FACELET);
-        if (param == null)
-        {
-            // Facelets is supported by default
-            return true;
-        }
-        else
-        {
-            return !Boolean.parseBoolean(param.toLowerCase());
-        }
-    }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.view;
+
+import javax.faces.FacesException;
+import javax.faces.context.FacesContext;
+import javax.faces.view.ViewDeclarationLanguage;
+import javax.faces.view.ViewDeclarationLanguageFactory;
+
+import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
+import org.apache.myfaces.config.RuntimeConfig;
+import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguageStrategy;
+import org.apache.myfaces.view.jsp.JspViewDeclarationLanguageStrategy;
+
+/**
+ * This is the default PDL factory used as of JSF 2.0, it tries to use Facelet PDL whenever possible, 
+ * but fallback on JSP if required.
+ * 
+ * @author Simon Lessard (latest modification by $Author: slessard $)
+ * @version $Revision: 696523 $ $Date: 2009-03-21 14:55:54 -0400 (mer., 17 sept. 2008) $
+ *
+ * @since 2.0
+ */
+public class ViewDeclarationLanguageFactoryImpl extends ViewDeclarationLanguageFactory
+{
+    /**
+     * Disable facelets VDL from the current application project. 
+     */
+    @JSFWebConfigParam(since="2.0", defaultValue="false", expectedValues="true,false")
+    public static final String PARAM_DISABLE_JSF_FACELET = "javax.faces.DISABLE_FACELET_JSF_VIEWHANDLER";
+    
+    private static final String FACELETS_1_VIEW_HANDLER = "com.sun.facelets.FaceletViewHandler";
+    
+    private boolean _initialized;
+    private ViewDeclarationLanguageStrategy[] _supportedLanguages;
+    
+    /**
+     * 
+     */
+    public ViewDeclarationLanguageFactoryImpl()
+    {
+        _initialized = false;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public ViewDeclarationLanguage getViewDeclarationLanguage(String viewId)
+    {
+        //if (viewId == null)
+        //{
+        //    throw new NullPointerException("viewId");
+        //}
+        
+        // TODO: It would be nice to be able to preinitialize the factory. However, since it requires 
+        //       access to the ExternalContext it may not be possible, depending on the loading order 
+        //       in the FactoryFinder. Could use ideas here. -= SL =-
+        initialize();
+        
+        for (ViewDeclarationLanguageStrategy strategy : _supportedLanguages)
+        {
+            if (strategy.handles(viewId))
+            {
+                return strategy.getViewDeclarationLanguage();
+            }
+        }
+        
+        throw new FacesException("Cannot find a valid PDL for view id " + viewId);
+    }
+    
+    /**
+     * Initialize the supported view declaration languages.
+     */
+    private void initialize()
+    {
+        if (!_initialized)
+        {
+            if (isFacelets2Enabled())
+            {
+                _supportedLanguages = new ViewDeclarationLanguageStrategy[2];
+                _supportedLanguages[0] = new FaceletViewDeclarationLanguageStrategy();
+                _supportedLanguages[1] = new JspViewDeclarationLanguageStrategy();
+            }
+            else
+            {
+                // Support JSP only
+                _supportedLanguages = new ViewDeclarationLanguageStrategy[1];
+                _supportedLanguages[0] = new JspViewDeclarationLanguageStrategy();
+            }
+
+            _initialized = true;
+        }
+    }
+    
+    /**
+     * Determines if the current application uses Facelets-2.
+     * To accomplish that it looks at the init param javax.faces.DISABLE_FACELET_JSF_VIEWHANDLER,
+     * at the version attribute of the faces-config
+     * and it also looks if the Facelets-1 ViewHandler com.sun.facelets.FaceletViewHandler is present. 
+     * 
+     * @return <code>true</code> if the current application uses the built in Facelets-2,
+     *         <code>false</code> otherwise (e.g. it uses Facelets-1 or only JSP).
+     */
+    private boolean isFacelets2Enabled()
+    {
+        FacesContext context = FacesContext.getCurrentInstance();
+        String param = context.getExternalContext().getInitParameter(PARAM_DISABLE_JSF_FACELET);
+        boolean facelets2ParamDisabled = (param != null && Boolean.parseBoolean(param.toLowerCase()));
+        boolean jsf20 = "2.0".equals(RuntimeConfig.getCurrentInstance(context.getExternalContext()).getFacesVersion());
+        boolean facelets1ViewHandlerPresent  = context.getApplication().getViewHandler()
+                                                   .getClass().getName().equals(FACELETS_1_VIEW_HANDLER);
+        
+        return !facelets2ParamDisabled && jsf20 && !facelets1ViewHandlerPresent;
+    }
+}