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