1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.view.facelets.compiler; |
20 | |
|
21 | |
import java.util.List; |
22 | |
import java.util.logging.Level; |
23 | |
import java.util.logging.Logger; |
24 | |
import javax.faces.application.ViewHandler; |
25 | |
import javax.faces.component.UIComponent; |
26 | |
import javax.faces.component.UIViewRoot; |
27 | |
import javax.faces.context.ExternalContext; |
28 | |
import javax.faces.context.FacesContext; |
29 | |
import javax.faces.context.FacesContextWrapper; |
30 | |
import javax.faces.view.facelets.TagDecorator; |
31 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam; |
32 | |
import org.apache.myfaces.config.RuntimeConfig; |
33 | |
import org.apache.myfaces.config.element.ComponentTagDeclaration; |
34 | |
import org.apache.myfaces.config.element.facelets.FaceletTagLibrary; |
35 | |
import org.apache.myfaces.shared.config.MyfacesConfig; |
36 | |
import org.apache.myfaces.shared.util.WebConfigParamUtils; |
37 | |
import org.apache.myfaces.view.facelets.tag.ComponentTagDeclarationLibrary; |
38 | |
import org.apache.myfaces.view.facelets.tag.TagLibrary; |
39 | |
import org.apache.myfaces.view.facelets.tag.composite.CompositeLibrary; |
40 | |
import org.apache.myfaces.view.facelets.tag.composite.CompositeResourceLibrary; |
41 | |
import org.apache.myfaces.view.facelets.tag.jsf.JsfLibrary; |
42 | |
import org.apache.myfaces.view.facelets.tag.jsf.PassThroughLibrary; |
43 | |
import org.apache.myfaces.view.facelets.tag.jsf.core.CoreLibrary; |
44 | |
import org.apache.myfaces.view.facelets.tag.jsf.html.HtmlLibrary; |
45 | |
import org.apache.myfaces.view.facelets.tag.jstl.core.JstlCoreLibrary; |
46 | |
import org.apache.myfaces.view.facelets.tag.jstl.core.LegacyJstlCoreLibrary; |
47 | |
import org.apache.myfaces.view.facelets.tag.jstl.fn.JstlFnLibrary; |
48 | |
import org.apache.myfaces.view.facelets.tag.ui.LegacyUILibrary; |
49 | |
import org.apache.myfaces.view.facelets.tag.ui.UILibrary; |
50 | |
import org.apache.myfaces.view.facelets.util.ReflectionUtil; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | 0 | public class FaceletsCompilerSupport |
58 | |
{ |
59 | 0 | private static final Logger log = Logger.getLogger(FaceletsCompilerSupport.class.getName()); |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
@JSFWebConfigParam(since = "2.0", deprecated = true) |
66 | |
private final static String PARAM_DECORATORS_DEPRECATED = "facelets.DECORATORS"; |
67 | |
|
68 | 0 | private final static String[] PARAMS_DECORATORS = {ViewHandler.FACELETS_DECORATORS_PARAM_NAME, |
69 | |
PARAM_DECORATORS_DEPRECATED}; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
@JSFWebConfigParam(since = "2.0", deprecated = true) |
75 | |
private final static String PARAM_SKIP_COMMENTS_DEPRECATED = "facelets.SKIP_COMMENTS"; |
76 | |
|
77 | 0 | private final static String[] PARAMS_SKIP_COMMENTS = {ViewHandler.FACELETS_SKIP_COMMENTS_PARAM_NAME, |
78 | |
PARAM_SKIP_COMMENTS_DEPRECATED}; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public void loadLibraries(FacesContext context, Compiler compiler) |
89 | |
{ |
90 | 0 | ExternalContext eContext = context.getExternalContext(); |
91 | 0 | MyfacesConfig config = MyfacesConfig.getCurrentInstance(eContext); |
92 | |
|
93 | |
|
94 | 0 | compiler.addTagLibrary(new CoreLibrary()); |
95 | 0 | compiler.addTagLibrary(new HtmlLibrary()); |
96 | 0 | if (config.isStrictJsf2FaceletsCompatibility()) |
97 | |
{ |
98 | 0 | compiler.addTagLibrary(new LegacyUILibrary()); |
99 | 0 | compiler.addTagLibrary(new LegacyJstlCoreLibrary()); |
100 | 0 | compiler.addTagLibrary(new LegacyJstlCoreLibrary(JstlCoreLibrary.ALTERNATIVE_NAMESPACE)); |
101 | |
} |
102 | |
else |
103 | |
{ |
104 | 0 | compiler.addTagLibrary(new UILibrary()); |
105 | 0 | compiler.addTagLibrary(new JstlCoreLibrary()); |
106 | 0 | compiler.addTagLibrary(new JstlCoreLibrary(JstlCoreLibrary.ALTERNATIVE_NAMESPACE)); |
107 | |
} |
108 | 0 | compiler.addTagLibrary(new JstlFnLibrary()); |
109 | 0 | compiler.addTagLibrary(new CompositeLibrary()); |
110 | 0 | compiler.addTagLibrary(new CompositeResourceLibrary(context, |
111 | |
CompositeResourceLibrary.NAMESPACE_PREFIX)); |
112 | 0 | compiler.addTagLibrary(new CompositeResourceLibrary(context, |
113 | |
CompositeResourceLibrary.ALIAS_NAMESPACE_PREFIX)); |
114 | 0 | compiler.addTagLibrary(new JsfLibrary()); |
115 | 0 | compiler.addTagLibrary(new PassThroughLibrary()); |
116 | |
|
117 | 0 | RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(eContext); |
118 | 0 | if (!runtimeConfig.getComponentTagDeclarations().isEmpty()) |
119 | |
{ |
120 | |
|
121 | 0 | ComponentTagDeclarationLibrary componentTagDeclarationLibrary = new ComponentTagDeclarationLibrary(); |
122 | |
|
123 | 0 | LoadComponentTagDeclarationFacesContextWrapper wrappedFacesContext = |
124 | |
new LoadComponentTagDeclarationFacesContextWrapper(context); |
125 | |
try |
126 | |
{ |
127 | 0 | wrappedFacesContext.setWrapperAsCurrentFacesContext(); |
128 | 0 | UIViewRoot root = new UIViewRoot(); |
129 | 0 | root.setRenderKitId("HTML_BASIC"); |
130 | 0 | wrappedFacesContext.setViewRoot(new UIViewRoot()); |
131 | |
|
132 | 0 | for (ComponentTagDeclaration declaration : runtimeConfig.getComponentTagDeclarations()) |
133 | |
{ |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | 0 | UIComponent component = context.getApplication().createComponent(declaration.getComponentType()); |
142 | 0 | componentTagDeclarationLibrary.addComponent(declaration.getNamespace(), |
143 | |
declaration.getTagName(), declaration.getComponentType(), component.getRendererType()); |
144 | 0 | } |
145 | |
} |
146 | |
finally |
147 | |
{ |
148 | 0 | wrappedFacesContext.restoreCurrentFacesContext(); |
149 | 0 | } |
150 | 0 | compiler.addTagLibrary(componentTagDeclarationLibrary); |
151 | |
} |
152 | |
|
153 | 0 | List<FaceletTagLibrary> faceletTagLibraries = runtimeConfig.getFaceletTagLibraries(); |
154 | 0 | for (FaceletTagLibrary faceletTagLibrary : faceletTagLibraries) |
155 | |
{ |
156 | |
|
157 | 0 | TagLibrary tl = TagLibraryConfig.create(context, faceletTagLibrary); |
158 | 0 | if (tl != null) |
159 | |
{ |
160 | 0 | compiler.addTagLibrary(tl); |
161 | |
} |
162 | 0 | } |
163 | 0 | } |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public void loadDecorators(FacesContext context, Compiler compiler) |
174 | |
{ |
175 | 0 | String param = WebConfigParamUtils.getStringInitParameter(context.getExternalContext(), PARAMS_DECORATORS); |
176 | 0 | if (param != null) |
177 | |
{ |
178 | 0 | for (String decorator : param.split(";")) |
179 | |
{ |
180 | |
try |
181 | |
{ |
182 | 0 | compiler.addTagDecorator((TagDecorator) ReflectionUtil.forName(decorator).newInstance()); |
183 | 0 | if (log.isLoggable(Level.FINE)) |
184 | |
{ |
185 | 0 | log.fine("Successfully loaded decorator: " + decorator); |
186 | |
} |
187 | |
} |
188 | 0 | catch (Exception e) |
189 | |
{ |
190 | 0 | log.log(Level.SEVERE, "Error Loading decorator: " + decorator, e); |
191 | 0 | } |
192 | |
} |
193 | |
} |
194 | 0 | } |
195 | |
|
196 | |
public void loadOptions(FacesContext context, Compiler compiler) |
197 | |
{ |
198 | 0 | ExternalContext eContext = context.getExternalContext(); |
199 | |
|
200 | |
|
201 | 0 | compiler.setTrimmingComments(WebConfigParamUtils.getBooleanInitParameter( |
202 | |
eContext, PARAMS_SKIP_COMMENTS, false)); |
203 | |
|
204 | 0 | compiler.setFaceletsProcessingConfigurations( |
205 | |
RuntimeConfig.getCurrentInstance( |
206 | |
context.getExternalContext()).getFaceletProcessingConfigurations()); |
207 | 0 | } |
208 | |
|
209 | 0 | private static class LoadComponentTagDeclarationFacesContextWrapper |
210 | |
extends FacesContextWrapper |
211 | |
{ |
212 | |
private FacesContext delegate; |
213 | |
private UIViewRoot root; |
214 | |
|
215 | |
public LoadComponentTagDeclarationFacesContextWrapper(FacesContext delegate) |
216 | 0 | { |
217 | 0 | this.delegate = delegate; |
218 | 0 | } |
219 | |
|
220 | |
@Override |
221 | |
public UIViewRoot getViewRoot() |
222 | |
{ |
223 | 0 | return root; |
224 | |
} |
225 | |
|
226 | |
@Override |
227 | |
public void setViewRoot(UIViewRoot root) |
228 | |
{ |
229 | 0 | this.root = root; |
230 | 0 | } |
231 | |
|
232 | |
@Override |
233 | |
public FacesContext getWrapped() |
234 | |
{ |
235 | 0 | return delegate; |
236 | |
} |
237 | |
|
238 | |
void setWrapperAsCurrentFacesContext() |
239 | |
{ |
240 | 0 | setCurrentInstance(this); |
241 | 0 | } |
242 | |
|
243 | |
void restoreCurrentFacesContext() |
244 | |
{ |
245 | 0 | setCurrentInstance(delegate); |
246 | 0 | } |
247 | |
} |
248 | |
|
249 | |
} |