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.config.impl.digester;
20  
21  import org.apache.commons.digester.Digester;
22  import org.apache.myfaces.config.FacesConfigUnmarshaller;
23  import org.apache.myfaces.config.impl.FacesConfigEntityResolver;
24  import org.apache.myfaces.config.impl.digester.elements.*;
25  import org.apache.myfaces.shared.util.ClassUtils;
26  import org.xml.sax.InputSource;
27  import org.xml.sax.SAXException;
28  
29  import javax.faces.context.ExternalContext;
30  import java.io.IOException;
31  import java.io.InputStream;
32  
33  /**
34   * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller</a>
35   */
36  public class DigesterFacesConfigUnmarshallerImpl implements FacesConfigUnmarshaller<FacesConfig>
37  {
38      private Digester digester;
39  
40      public DigesterFacesConfigUnmarshallerImpl(ExternalContext externalContext)
41      {
42          digester = new Digester();
43          // TODO: validation set to false during implementation of 1.2
44          digester.setValidating(false);
45          digester.setNamespaceAware(true);
46          digester.setEntityResolver(new FacesConfigEntityResolver(externalContext));
47          //digester.setUseContextClassLoader(true);
48          digester.setClassLoader(ClassUtils.getContextClassLoader());
49  
50          digester.addObjectCreate("faces-config", FacesConfig.class);
51          // 2.0 specific start
52          digester.addSetProperties("faces-config", "metadata-complete", "metadataComplete");
53          digester.addSetProperties("faces-config", "version", "version");
54          // 2.0 specific end
55          // 2.0 config ordering name start
56          digester.addCallMethod("faces-config/name", "setName", 0);
57          digester.addObjectCreate("faces-config/ordering", Ordering.class);
58          digester.addSetNext("faces-config/ordering", "setOrdering");
59          digester.addObjectCreate("faces-config/ordering/before/name", FacesConfigNameSlot.class);
60          digester.addSetNext("faces-config/ordering/before/name", "addBeforeSlot");
61          digester.addCallMethod("faces-config/ordering/before/name", "setName",0);        
62          digester.addObjectCreate("faces-config/ordering/before/others", ConfigOthersSlot.class);
63          digester.addSetNext("faces-config/ordering/before/others", "addBeforeSlot");
64          
65          digester.addObjectCreate("faces-config/ordering/after/name", FacesConfigNameSlot.class);
66          digester.addSetNext("faces-config/ordering/after/name", "addAfterSlot");
67          digester.addCallMethod("faces-config/ordering/after/name", "setName",0);        
68          digester.addObjectCreate("faces-config/ordering/after/others", ConfigOthersSlot.class);
69          digester.addSetNext("faces-config/ordering/after/others", "addAfterSlot");        
70          
71          digester.addObjectCreate("faces-config/absolute-ordering", AbsoluteOrdering.class);
72          digester.addSetNext("faces-config/absolute-ordering", "setAbsoluteOrdering");
73          digester.addObjectCreate("faces-config/absolute-ordering/name", FacesConfigNameSlot.class);
74          digester.addSetNext("faces-config/absolute-ordering/name", "addOrderSlot");
75          digester.addCallMethod("faces-config/absolute-ordering/name", "setName",0);        
76          digester.addObjectCreate("faces-config/absolute-ordering/others", ConfigOthersSlot.class);
77          digester.addSetNext("faces-config/absolute-ordering/others", "addOrderSlot");
78          // 2.0 config ordering name end
79          
80          digester.addObjectCreate("faces-config/application", Application.class);
81          digester.addSetNext("faces-config/application", "addApplication");
82          digester.addCallMethod("faces-config/application/action-listener", "addActionListener", 0);
83          digester.addCallMethod("faces-config/application/default-render-kit-id", "addDefaultRenderkitId", 0);
84          digester.addCallMethod("faces-config/application/default-validators", "setDefaultValidatorsPresent");
85          digester.addCallMethod("faces-config/application/default-validators/validator-id", "addDefaultValidatorId", 0);
86          digester.addCallMethod("faces-config/application/message-bundle", "addMessageBundle", 0);
87          digester.addCallMethod("faces-config/application/navigation-handler", "addNavigationHandler", 0);
88          digester.addCallMethod("faces-config/application/partial-traversal", "addPartialTraversal", 0);
89          digester.addCallMethod("faces-config/application/view-handler", "addViewHandler", 0);
90          digester.addCallMethod("faces-config/application/state-manager", "addStateManager", 0);
91          digester.addCallMethod("faces-config/application/property-resolver", "addPropertyResolver", 0);
92          digester.addCallMethod("faces-config/application/variable-resolver", "addVariableResolver", 0);
93          digester.addObjectCreate("faces-config/application/locale-config", LocaleConfig.class);
94          digester.addSetNext("faces-config/application/locale-config", "addLocaleConfig");
95          digester.addCallMethod("faces-config/application/locale-config/default-locale", "setDefaultLocale", 0);
96          digester.addCallMethod("faces-config/application/locale-config/supported-locale", "addSupportedLocale", 0);
97  
98          // 1.2 specific start
99          digester.addCallMethod("faces-config/application/el-resolver", "addElResolver", 0);
100         digester.addObjectCreate("faces-config/application/resource-bundle", ResourceBundle.class);
101         digester.addSetNext("faces-config/application/resource-bundle", "addResourceBundle");
102         digester.addCallMethod("faces-config/application/resource-bundle/base-name", "setBaseName", 0);
103         digester.addCallMethod("faces-config/application/resource-bundle/var", "setVar", 0);
104         digester.addCallMethod("faces-config/application/resource-bundle/display-name", "setDisplayName", 0);
105         // 1.2 specific end
106 
107         // 2.0 specific start
108         digester.addObjectCreate("faces-config/application/system-event-listener", SystemEventListener.class);
109         digester.addSetNext("faces-config/application/system-event-listener", "addSystemEventListener");
110         digester.addCallMethod("faces-config/application/system-event-listener/system-event-listener-class",
111                                "setSystemEventListenerClass",0);
112         digester.addCallMethod("faces-config/application/system-event-listener/system-event-class",
113                                "setSystemEventClass",0);
114         digester.addCallMethod("faces-config/application/system-event-listener/source-class", "setSourceClass",0);
115         digester.addCallMethod("faces-config/application/resource-handler", "addResourceHandler", 0);
116         digester.addCallMethod("faces-config/factory/exception-handler-factory", "addExceptionHandlerFactory", 0);
117         digester.addCallMethod("faces-config/factory/external-context-factory", "addExternalContextFactory", 0);
118         digester.addCallMethod("faces-config/factory/view-declaration-language-factory",
119                                "addViewDeclarationLanguageFactory", 0);
120         digester.addCallMethod("faces-config/factory/partial-view-context-factory", "addPartialViewContextFactory", 0);
121         digester.addCallMethod("faces-config/factory/tag-handler-delegate-factory", "addTagHandlerDelegateFactory", 0);
122         digester.addCallMethod("faces-config/factory/visit-context-factory", "addVisitContextFactory", 0);
123         // 2.0 specific end
124         
125         // 2.1 specific start
126         digester.addCallMethod("faces-config/factory/facelet-cache-factory", "addFaceletCacheFactory", 0);
127         // 2.1 specific end
128 
129         digester.addObjectCreate("faces-config/factory", Factory.class);
130         digester.addSetNext("faces-config/factory", "addFactory");
131         digester.addCallMethod("faces-config/factory/application-factory", "addApplicationFactory", 0);
132         digester.addCallMethod("faces-config/factory/faces-context-factory", "addFacesContextFactory", 0);
133         digester.addCallMethod("faces-config/factory/lifecycle-factory", "addLifecycleFactory", 0);
134         digester.addCallMethod("faces-config/factory/render-kit-factory", "addRenderkitFactory", 0);
135 
136         digester.addCallMethod("faces-config/component", "addComponent", 2);
137         digester.addCallParam("faces-config/component/component-type", 0);
138         digester.addCallParam("faces-config/component/component-class", 1);
139 
140         digester.addObjectCreate("faces-config/converter", Converter.class);
141         digester.addSetNext("faces-config/converter", "addConverter");
142         digester.addCallMethod("faces-config/converter/converter-id", "setConverterId", 0);
143         digester.addCallMethod("faces-config/converter/converter-for-class", "setForClass", 0);
144         digester.addCallMethod("faces-config/converter/converter-class", "setConverterClass", 0);
145         digester.addObjectCreate("faces-config/converter/attribute", Attribute.class);
146         digester.addSetNext("faces-config/converter/attribute", "addAttribute");
147         digester.addCallMethod("faces-config/converter/attribute/description", "addDescription", 0);
148         digester.addCallMethod("faces-config/converter/attribute/display-name", "addDisplayName", 0);
149         digester.addCallMethod("faces-config/converter/attribute/icon", "addIcon", 0);
150         digester.addCallMethod("faces-config/converter/attribute/attribute-name", "setAttributeName", 0);
151         digester.addCallMethod("faces-config/converter/attribute/attribute-class", "setAttributeClass", 0);
152         digester.addCallMethod("faces-config/converter/attribute/default-value", "setDefaultValue", 0);
153         digester.addCallMethod("faces-config/converter/attribute/suggested-value", "setSuggestedValue", 0);
154         digester.addCallMethod("faces-config/converter/attribute/attribute-extension", "addAttributeExtension", 0);
155         digester.addObjectCreate("faces-config/converter/property", Property.class);
156         digester.addSetNext("faces-config/converter/property", "addProperty");
157         digester.addCallMethod("faces-config/converter/property/description", "addDescription", 0);
158         digester.addCallMethod("faces-config/converter/property/display-name", "addDisplayName", 0);
159         digester.addCallMethod("faces-config/converter/property/icon", "addIcon", 0);
160         digester.addCallMethod("faces-config/converter/property/property-name", "setPropertyName", 0);
161         digester.addCallMethod("faces-config/converter/property/property-class", "setPropertyClass", 0);
162         digester.addCallMethod("faces-config/converter/property/default-value", "setDefaultValue", 0);
163         digester.addCallMethod("faces-config/converter/property/suggested-value", "setSuggestedValue", 0);
164         digester.addCallMethod("faces-config/converter/property/property-extension", "addPropertyExtension", 0);
165 
166         digester.addObjectCreate("faces-config/managed-bean", ManagedBean.class);
167         digester.addSetProperties("faces-config/managed-bean", "eager", "eager");
168         digester.addSetNext("faces-config/managed-bean", "addManagedBean");
169         digester.addCallMethod("faces-config/managed-bean/description", "setDescription", 0);
170         digester.addCallMethod("faces-config/managed-bean/managed-bean-name", "setName", 0);
171         digester.addCallMethod("faces-config/managed-bean/managed-bean-class", "setBeanClass", 0);
172         digester.addCallMethod("faces-config/managed-bean/managed-bean-scope", "setScope", 0);
173         digester.addObjectCreate("faces-config/managed-bean/managed-property", ManagedProperty.class);
174         digester.addSetNext("faces-config/managed-bean/managed-property", "addProperty");
175         digester.addCallMethod("faces-config/managed-bean/managed-property/property-name", "setPropertyName", 0);
176         digester.addCallMethod("faces-config/managed-bean/managed-property/property-class", "setPropertyClass", 0);
177         digester.addCallMethod("faces-config/managed-bean/managed-property/null-value", "setNullValue");
178         digester.addCallMethod("faces-config/managed-bean/managed-property/value", "setValue", 0);
179         digester.addObjectCreate("faces-config/managed-bean/managed-property/map-entries", MapEntries.class);
180         digester.addSetNext("faces-config/managed-bean/managed-property/map-entries", "setMapEntries");
181         digester.addCallMethod("faces-config/managed-bean/managed-property/map-entries/key-class", "setKeyClass", 0);
182         digester.addCallMethod("faces-config/managed-bean/managed-property/map-entries/value-class",
183                                "setValueClass", 0);
184         digester.addObjectCreate("faces-config/managed-bean/managed-property/map-entries/map-entry",
185                                  MapEntries.Entry.class);
186         digester.addSetNext("faces-config/managed-bean/managed-property/map-entries/map-entry", "addEntry");
187         digester.addCallMethod("faces-config/managed-bean/managed-property/map-entries/map-entry/key", "setKey", 0);
188         digester.addCallMethod("faces-config/managed-bean/managed-property/map-entries/map-entry/null-value",
189                                "setNullValue");
190         digester.addCallMethod("faces-config/managed-bean/managed-property/map-entries/map-entry/value", "setValue", 0);
191         digester.addObjectCreate("faces-config/managed-bean/managed-property/list-entries", ListEntries.class);
192         digester.addSetNext("faces-config/managed-bean/managed-property/list-entries", "setListEntries");
193         digester.addCallMethod("faces-config/managed-bean/managed-property/list-entries/value-class", "setValueClass",
194                                0);
195         digester.addObjectCreate("faces-config/managed-bean/managed-property/list-entries/null-value",
196                                  ListEntries.Entry.class);
197         digester.addSetNext("faces-config/managed-bean/managed-property/list-entries/null-value", "addEntry");
198         digester.addCallMethod("faces-config/managed-bean/managed-property/list-entries/null-value", "setNullValue");
199         digester.addObjectCreate("faces-config/managed-bean/managed-property/list-entries/value",
200                                  ListEntries.Entry.class);
201         digester.addSetNext("faces-config/managed-bean/managed-property/list-entries/value", "addEntry");
202         digester.addCallMethod("faces-config/managed-bean/managed-property/list-entries/value", "setValue", 0);
203         digester.addObjectCreate("faces-config/managed-bean/map-entries", MapEntries.class);
204         digester.addSetNext("faces-config/managed-bean/map-entries", "setMapEntries");
205         digester.addCallMethod("faces-config/managed-bean/map-entries/key-class", "setKeyClass", 0);
206         digester.addCallMethod("faces-config/managed-bean/map-entries/value-class", "setValueClass", 0);
207         digester.addObjectCreate("faces-config/managed-bean/map-entries/map-entry", MapEntries.Entry.class);
208         digester.addSetNext("faces-config/managed-bean/map-entries/map-entry", "addEntry");
209         digester.addCallMethod("faces-config/managed-bean/map-entries/map-entry/key", "setKey", 0);
210         digester.addCallMethod("faces-config/managed-bean/map-entries/map-entry/null-value", "setNullValue");
211         digester.addCallMethod("faces-config/managed-bean/map-entries/map-entry/value", "setValue", 0);
212         digester.addObjectCreate("faces-config/managed-bean/list-entries", ListEntries.class);
213         digester.addSetNext("faces-config/managed-bean/list-entries", "setListEntries");
214         digester.addCallMethod("faces-config/managed-bean/list-entries/value-class", "setValueClass", 0);
215         digester.addObjectCreate("faces-config/managed-bean/list-entries/null-value", ListEntries.Entry.class);
216         digester.addSetNext("faces-config/managed-bean/list-entries/null-value", "addEntry");
217         digester.addCallMethod("faces-config/managed-bean/list-entries/null-value", "setNullValue");
218         digester.addObjectCreate("faces-config/managed-bean/list-entries/value", ListEntries.Entry.class);
219         digester.addSetNext("faces-config/managed-bean/list-entries/value", "addEntry");
220         digester.addCallMethod("faces-config/managed-bean/list-entries/value", "setValue", 0);
221 
222         digester.addObjectCreate("faces-config/navigation-rule", NavigationRule.class);
223         digester.addSetNext("faces-config/navigation-rule", "addNavigationRule");
224         digester.addCallMethod("faces-config/navigation-rule/from-view-id", "setFromViewId", 0);
225         digester.addObjectCreate("faces-config/navigation-rule/navigation-case", NavigationCase.class);
226         digester.addSetNext("faces-config/navigation-rule/navigation-case", "addNavigationCase");
227         digester.addCallMethod("faces-config/navigation-rule/navigation-case/from-action", "setFromAction", 0);
228         digester.addCallMethod("faces-config/navigation-rule/navigation-case/from-outcome", "setFromOutcome", 0);
229         digester.addCallMethod("faces-config/navigation-rule/navigation-case/if", "setIf", 0);
230         digester.addCallMethod("faces-config/navigation-rule/navigation-case/to-view-id", "setToViewId", 0);
231         digester.addObjectCreate("faces-config/navigation-rule/navigation-case/redirect", Redirect.class);
232         digester.addSetProperties("faces-config/navigation-rule/navigation-case/redirect", "include-view-params",
233                                   "includeViewParams");
234         digester.addSetNext("faces-config/navigation-rule/navigation-case/redirect", "setRedirect");
235         digester.addObjectCreate("faces-config/navigation-rule/navigation-case/redirect/view-param", ViewParam.class);
236         digester.addSetNext("faces-config/navigation-rule/navigation-case/redirect/view-param", "addViewParam");
237         digester.addCallMethod("faces-config/navigation-rule/navigation-case/redirect/view-param/name", "setName",0);
238         digester.addCallMethod("faces-config/navigation-rule/navigation-case/redirect/view-param/value", "setValue",0);
239 
240         digester.addObjectCreate("faces-config/navigation-rule/navigation-case/redirect/redirect-param", 
241             ViewParam.class);
242         digester.addSetNext("faces-config/navigation-rule/navigation-case/redirect/redirect-param", 
243             "addViewParam");
244         digester.addCallMethod("faces-config/navigation-rule/navigation-case/redirect/redirect-param/name", 
245             "setName",0);
246         digester.addCallMethod("faces-config/navigation-rule/navigation-case/redirect/redirect-param/value", 
247             "setValue",0);
248 
249         digester.addObjectCreate("faces-config/render-kit", RenderKit.class);
250         digester.addSetNext("faces-config/render-kit", "addRenderKit");
251         digester.addCallMethod("faces-config/render-kit/render-kit-id", "setId", 0);
252         //digester.addCallMethod("faces-config/render-kit/render-kit-class", "setRenderKitClass", 0);
253         digester.addCallMethod("faces-config/render-kit/render-kit-class", "addRenderKitClass", 0);
254         digester.addObjectCreate("faces-config/render-kit/renderer", Renderer.class);
255         digester.addSetNext("faces-config/render-kit/renderer", "addRenderer");
256         digester.addCallMethod("faces-config/render-kit/renderer/component-family", "setComponentFamily", 0);
257         digester.addCallMethod("faces-config/render-kit/renderer/renderer-type", "setRendererType", 0);
258         digester.addCallMethod("faces-config/render-kit/renderer/renderer-class", "setRendererClass", 0);
259         digester.addObjectCreate("faces-config/render-kit/client-behavior-renderer", ClientBehaviorRenderer.class);
260         digester.addSetNext("faces-config/render-kit/client-behavior-renderer", "addClientBehaviorRenderer");
261         digester.addCallMethod("faces-config/render-kit/client-behavior-renderer/client-behavior-renderer-type",
262                                "setRendererType", 0);
263         digester.addCallMethod("faces-config/render-kit/client-behavior-renderer/client-behavior-renderer-class",
264                                "setRendererClass", 0);
265         
266         // 2.0 behavior start
267         digester.addObjectCreate("faces-config/behavior", Behavior.class);
268         digester.addSetNext("faces-config/behavior", "addBehavior");
269         digester.addCallMethod("faces-config/behavior/behavior-id", "setBehaviorId", 0);
270         digester.addCallMethod("faces-config/behavior/behavior-class", "setBehaviorClass", 0);
271         digester.addObjectCreate("faces-config/behavior/attribute", Attribute.class);
272         digester.addSetNext("faces-config/behavior/attribute", "addAttribute");
273         digester.addCallMethod("faces-config/behavior/attribute/description", "addDescription", 0);
274         digester.addCallMethod("faces-config/behavior/attribute/display-name", "addDisplayName", 0);
275         digester.addCallMethod("faces-config/behavior/attribute/icon", "addIcon", 0);
276         digester.addCallMethod("faces-config/behavior/attribute/attribute-name", "setAttributeName", 0);
277         digester.addCallMethod("faces-config/behavior/attribute/attribute-class", "setAttributeClass", 0);
278         digester.addCallMethod("faces-config/behavior/attribute/default-value", "setDefaultValue", 0);
279         digester.addCallMethod("faces-config/behavior/attribute/suggested-value", "setSuggestedValue", 0);
280         digester.addCallMethod("faces-config/behavior/attribute/attribute-extension", "addAttributeExtension", 0);
281         digester.addObjectCreate("faces-config/behavior/property", Property.class);
282         digester.addSetNext("faces-config/behavior/property", "addProperty");
283         digester.addCallMethod("faces-config/behavior/property/description", "addDescription", 0);
284         digester.addCallMethod("faces-config/behavior/property/display-name", "addDisplayName", 0);
285         digester.addCallMethod("faces-config/behavior/property/icon", "addIcon", 0);
286         digester.addCallMethod("faces-config/behavior/property/property-name", "setPropertyName", 0);
287         digester.addCallMethod("faces-config/behavior/property/property-class", "setPropertyClass", 0);
288         digester.addCallMethod("faces-config/behavior/property/default-value", "setDefaultValue", 0);
289         digester.addCallMethod("faces-config/behavior/property/suggested-value", "setSuggestedValue", 0);
290         digester.addCallMethod("faces-config/behavior/property/property-extension", "addPropertyExtension", 0);
291         // 2.0 behavior end
292         
293         digester.addCallMethod("faces-config/lifecycle/phase-listener", "addLifecyclePhaseListener", 0);
294 
295         digester.addCallMethod("faces-config/validator", "addValidator", 2);
296         digester.addCallParam("faces-config/validator/validator-id", 0);
297         digester.addCallParam("faces-config/validator/validator-class", 1);
298         
299         // 2.1 facelets-processing start
300         digester.addObjectCreate("faces-config/faces-config-extension", FacesConfigExtension.class);
301         digester.addSetNext("faces-config/faces-config-extension", "addFacesConfigExtension");
302         digester.addObjectCreate("faces-config/faces-config-extension/facelets-processing", FaceletsProcessing.class);
303         digester.addSetNext("faces-config/faces-config-extension/facelets-processing", "addFaceletsProcessing");
304         digester.addCallMethod("faces-config/faces-config-extension/facelets-processing/file-extension",
305                                "setFileExtension", 0);
306         digester.addCallMethod("faces-config/faces-config-extension/facelets-processing/process-as", "setProcessAs", 0);
307 
308         // 2.1 facelets-processing end
309         
310         //MyFaces specific facelets-processing instruction.
311         digester.addCallMethod("faces-config/faces-config-extension/facelets-processing/oam-compress-spaces", 
312                 "setOamCompressSpaces", 0);
313     }
314 
315     public FacesConfig getFacesConfig(InputStream in, String systemId) throws IOException, SAXException
316     {
317         InputSource is = new InputSource(in);
318         is.setSystemId(systemId);
319 
320         // Fix for http://issues.apache.org/jira/browse/MYFACES-236
321         FacesConfig config = (FacesConfig) digester.parse(is);
322 
323         for (org.apache.myfaces.config.element.Application application : config.getApplications())
324         {
325             for (org.apache.myfaces.config.element.LocaleConfig localeConfig : application.getLocaleConfig())
326             {
327                 if (!localeConfig.getSupportedLocales().contains(localeConfig.getDefaultLocale()))
328                 {
329                     localeConfig.getSupportedLocales().add(localeConfig.getDefaultLocale());
330                 }
331             }
332         }
333 
334         return config;
335     }
336 }