2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.validator.faces;
19  
20  import java.io.IOException;
21  import java.util.Locale;
22  
23  import javax.faces.FacesException;
24  import javax.faces.FactoryFinder;
25  import javax.faces.application.ViewHandler;
26  import javax.faces.component.UIViewRoot;
27  import javax.faces.context.FacesContext;
28  import javax.faces.render.RenderKit;
29  import javax.faces.render.RenderKitFactory;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.apache.shale.util.Messages;
34  
35  /***
36   * <p>{@link ValidatorViewHandler} is a custom implementation of <code>ViewHandler</code>
37   * that adds support for setting up a decorated RenderKit.</p>
38   */
39  
40  public class ValidatorViewHandler extends ViewHandler {
41  
42  
43      // ------------------------------------------------------------- Constructor
44  
45  
46      /***
47       * <p>Create a {@link ValidatorViewHandler} instance that decorates the
48       * specified <code>ViewHandler</code> provided by the JSF runtime
49       * implementation.</p>
50       *
51       * @param original Original <code>ViewHandler</code> to be decorated
52       */
53      public ValidatorViewHandler(ViewHandler original) {
54          this.original = original;
55      }
56  
57  
58      // -------------------------------------------------------- Static Variables
59  
60  
61      /***
62       * <p>Log instance for this class.</p>
63       */
64      private static final Log log = LogFactory.getLog(ValidatorViewHandler.class);
65  
66  
67      /***
68       * <p>Message resources for this class.</p>
69       */
70      private static Messages messages =
71        new Messages("org.apache.shale.validator.resources.Bundle",
72                     ValidatorViewHandler.class.getClassLoader());
73  
74  
75      // ------------------------------------------------------ Instance Variables
76  
77  
78      /***
79       * <p>The <code>ViewHandler</code> instance we are decorating.  All requests
80       * are delegated to this instance, before or after any special handling that
81       * is required.</p>
82       */
83      private ViewHandler original = null;
84  
85  
86      // ----------------------------------------------------- ViewHandler Methods
87  
88  
89      /*** {@inheritDoc} */
90      public Locale calculateLocale(FacesContext context) {
91          return original.calculateLocale(context);
92      }
93  
94  
95      /*** {@inheritDoc} */
96      public String calculateRenderKitId(FacesContext context) {
97          return original.calculateRenderKitId(context);
98      }
99  
100 
101     /***
102      * <p>After delegating to our original <code>ViewHandler</code>,
103      * ensure that our decorator RenderKit has been initialized.</p>
104      *
105      * @param context <code>FacesContext</code> for the current request
106      * @param viewId View identifier of the view to be created
107      */
108     public UIViewRoot createView(FacesContext context, String viewId) {
109         UIViewRoot view = original.createView(context, viewId);
110         setupRenderKit(context, view);
111         return view;
112     }
113 
114 
115     /*** {@inheritDoc} */
116     public String getActionURL(FacesContext context, String viewId) {
117         return original.getActionURL(context, viewId);
118     }
119 
120 
121     /*** {@inheritDoc} */
122     public String getResourceURL(FacesContext context, String path) {
123         return original.getResourceURL(context, path);
124     }
125 
126 
127     /*** {@inheritDoc} */
128     public void renderView(FacesContext context, UIViewRoot view)
129       throws IOException, FacesException {
130         original.renderView(context, view);
131     }
132 
133 
134     /*** {@inheritDoc} */
135     public UIViewRoot restoreView(FacesContext context, String viewId) {
136         return original.restoreView(context, viewId);
137     }
138 
139 
140     /*** {@inheritDoc} */
141     public void writeState(FacesContext context) throws IOException {
142         original.writeState(context);
143     }
144 
145 
146     // --------------------------------------------------------- Private Methods
147 
148 
149     /***
150      * <p>The default RenderKit is decorated with {@link ValidatorRenderKit}.
151      * This wrapper intercepts component renderer's decorating them.</p>
152      *
153      * @param context <code>FacesContext</code> for the current request
154      * @param view <code>UIViewRoot</code> for the current component tree
155      */
156     private void setupRenderKit(FacesContext context, UIViewRoot view) {
157 
158         RenderKitFactory factory =
159           (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
160         RenderKit defaultRenderKit =
161           factory.getRenderKit(context, view.getRenderKitId());
162         if (!(defaultRenderKit instanceof ValidatorRenderKit)) {
163             factory.addRenderKit(view.getRenderKitId(),
164                                  new ValidatorRenderKit(defaultRenderKit));
165         }
166 
167     }
168 
169 
170 }