Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
ValidatorViewHandler |
|
| 1.1;1.1 |
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 | 0 | public ValidatorViewHandler(ViewHandler original) { |
54 | 0 | this.original = original; |
55 | 0 | } |
56 | ||
57 | ||
58 | // -------------------------------------------------------- Static Variables |
|
59 | ||
60 | ||
61 | /** |
|
62 | * <p>Log instance for this class.</p> |
|
63 | */ |
|
64 | 0 | private static final Log log = LogFactory.getLog(ValidatorViewHandler.class); |
65 | ||
66 | ||
67 | /** |
|
68 | * <p>Message resources for this class.</p> |
|
69 | */ |
|
70 | 0 | 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 | 0 | private ViewHandler original = null; |
84 | ||
85 | ||
86 | // ----------------------------------------------------- ViewHandler Methods |
|
87 | ||
88 | ||
89 | /** {@inheritDoc} */ |
|
90 | public Locale calculateLocale(FacesContext context) { |
|
91 | 0 | return original.calculateLocale(context); |
92 | } |
|
93 | ||
94 | ||
95 | /** {@inheritDoc} */ |
|
96 | public String calculateRenderKitId(FacesContext context) { |
|
97 | 0 | 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 | 0 | UIViewRoot view = original.createView(context, viewId); |
110 | 0 | setupRenderKit(context, view); |
111 | 0 | return view; |
112 | } |
|
113 | ||
114 | ||
115 | /** {@inheritDoc} */ |
|
116 | public String getActionURL(FacesContext context, String viewId) { |
|
117 | 0 | return original.getActionURL(context, viewId); |
118 | } |
|
119 | ||
120 | ||
121 | /** {@inheritDoc} */ |
|
122 | public String getResourceURL(FacesContext context, String path) { |
|
123 | 0 | return original.getResourceURL(context, path); |
124 | } |
|
125 | ||
126 | ||
127 | /** {@inheritDoc} */ |
|
128 | public void renderView(FacesContext context, UIViewRoot view) |
|
129 | throws IOException, FacesException { |
|
130 | 0 | original.renderView(context, view); |
131 | 0 | } |
132 | ||
133 | ||
134 | /** {@inheritDoc} */ |
|
135 | public UIViewRoot restoreView(FacesContext context, String viewId) { |
|
136 | 0 | return original.restoreView(context, viewId); |
137 | } |
|
138 | ||
139 | ||
140 | /** {@inheritDoc} */ |
|
141 | public void writeState(FacesContext context) throws IOException { |
|
142 | 0 | original.writeState(context); |
143 | 0 | } |
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 | 0 | RenderKitFactory factory = |
159 | (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); |
|
160 | 0 | RenderKit defaultRenderKit = |
161 | factory.getRenderKit(context, view.getRenderKitId()); |
|
162 | 0 | if (!(defaultRenderKit instanceof ValidatorRenderKit)) { |
163 | 0 | factory.addRenderKit(view.getRenderKitId(), |
164 | new ValidatorRenderKit(defaultRenderKit)); |
|
165 | } |
|
166 | ||
167 | 0 | } |
168 | ||
169 | ||
170 | } |