View Javadoc

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.view;
20  
21  import javax.faces.application.Application;
22  import javax.faces.application.ViewHandler;
23  import javax.faces.component.UIViewRoot;
24  import javax.faces.context.FacesContext;
25  import javax.faces.view.ViewDeclarationLanguage;
26  
27  import org.apache.myfaces.shared.application.InvalidViewIdException;
28  
29  /**
30   * @author Simon Lessard (latest modification by $Author: bommel $)
31   * @version $Revision: 1187701 $ $Date: 2011-10-22 07:21:54 -0500 (Sat, 22 Oct 2011) $
32   * 
33   * @since 2.0
34   */
35  public abstract class ViewDeclarationLanguageBase extends ViewDeclarationLanguage
36  {
37      
38      /**
39       * Process the specification required algorithm that is generic to all PDL.
40       * 
41       * @param context
42       * @param viewId
43       */
44      public UIViewRoot createView(FacesContext context, String viewId)
45      {
46          checkNull(context, "context");
47          //checkNull(viewId, "viewId");
48  
49          try
50          {
51              viewId = calculateViewId(context, viewId);
52              
53              Application application = context.getApplication();
54  
55              // Create a new UIViewRoot object instance using Application.createComponent(UIViewRoot.COMPONENT_TYPE).
56              UIViewRoot newViewRoot = (UIViewRoot) application.createComponent(UIViewRoot.COMPONENT_TYPE);
57              UIViewRoot oldViewRoot = context.getViewRoot();
58              if (oldViewRoot == null)
59              {
60                  // If not, this method must call calculateLocale() and calculateRenderKitId(), and store the results
61                  // as the values of the locale and renderKitId, proeprties, respectively, of the newly created
62                  // UIViewRoot.
63                  ViewHandler handler = application.getViewHandler();
64                  newViewRoot.setLocale(handler.calculateLocale(context));
65                  newViewRoot.setRenderKitId(handler.calculateRenderKitId(context));
66              }
67              else
68              {
69                  // If there is an existing UIViewRoot available on the FacesContext, this method must copy its locale
70                  // and renderKitId to this new view root
71                  newViewRoot.setLocale(oldViewRoot.getLocale());
72                  newViewRoot.setRenderKitId(oldViewRoot.getRenderKitId());
73              }
74              
75              // TODO: VALIDATE - The spec is silent on the following line, but I feel bad if I don't set it
76              newViewRoot.setViewId(viewId);
77  
78              return newViewRoot;
79          }
80          catch (InvalidViewIdException e)
81          {
82              // If no viewId could be identified, or the viewId is exactly equal to the servlet mapping, 
83              // send the response error code SC_NOT_FOUND with a suitable message to the client.
84              sendSourceNotFound(context, e.getMessage());
85              
86              // TODO: VALIDATE - Spec is silent on the return value when an error was sent
87              return null;
88          }
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public UIViewRoot restoreView(FacesContext context, String viewId)
96      {
97          checkNull(context, "context");
98          //checkNull(viewId, "viewId");
99  
100         Application application = context.getApplication();
101         
102         ViewHandler applicationViewHandler = application.getViewHandler();
103         
104         String renderKitId = applicationViewHandler.calculateRenderKitId(context);
105 
106         UIViewRoot viewRoot = application.getStateManager().restoreView(context, viewId, renderKitId);
107 
108         return viewRoot;
109     }
110 
111     /**
112      * Calculates the effective view identifier for the specified raw view identifier.
113      * 
114      * @param context le current FacesContext
115      * @param viewId the raw view identifier
116      * 
117      * @return the effective view identifier
118      */
119     protected abstract String calculateViewId(FacesContext context, String viewId);
120     
121     /**
122      * Send a source not found to the client. Although it can be considered ok in JSP mode,
123      * I think it's pretty lame to have this kind of requirement at VDL level considering VDL 
124      * represents the page --> JSF tree link, not the transport layer required to send a 
125      * SC_NOT_FOUND.
126      * 
127      * @param context le current FacesContext
128      * @param message the message associated with the error
129      */
130     protected abstract void sendSourceNotFound(FacesContext context, String message);
131     
132     /**
133      * Check if the specified value of a param is <code>null</code>.
134      * 
135      * @param o the parameter's value
136      * @param param the parameter's name
137      * 
138      * @throws NullPointerException if the value is <code>null</code>
139      */
140     protected void checkNull(final Object o, final String param)
141     {
142         if (o == null)
143         {
144             throw new NullPointerException(param + " can not be null.");
145         }
146     }
147 }