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 java.io.IOException;
22  import java.io.InputStream;
23  import java.util.List;
24  
25  import javax.faces.context.ExternalContext;
26  
27  import org.apache.commons.digester.Digester;
28  import org.apache.myfaces.config.FacesConfigUnmarshaller;
29  import org.apache.myfaces.config.impl.FacesConfigEntityResolver;
30  import org.apache.myfaces.config.impl.digester.elements.Application;
31  import org.apache.myfaces.config.impl.digester.elements.Attribute;
32  import org.apache.myfaces.config.impl.digester.elements.Converter;
33  import org.apache.myfaces.config.impl.digester.elements.FacesConfig;
34  import org.apache.myfaces.config.impl.digester.elements.Factory;
35  import org.apache.myfaces.config.impl.digester.elements.ListEntries;
36  import org.apache.myfaces.config.impl.digester.elements.LocaleConfig;
37  import org.apache.myfaces.config.impl.digester.elements.ManagedBean;
38  import org.apache.myfaces.config.impl.digester.elements.ManagedProperty;
39  import org.apache.myfaces.config.impl.digester.elements.MapEntries;
40  import org.apache.myfaces.config.impl.digester.elements.NavigationCase;
41  import org.apache.myfaces.config.impl.digester.elements.NavigationRule;
42  import org.apache.myfaces.config.impl.digester.elements.Property;
43  import org.apache.myfaces.config.impl.digester.elements.RenderKit;
44  import org.apache.myfaces.config.impl.digester.elements.Renderer;
45  import org.apache.myfaces.config.impl.digester.elements.ResourceBundle;
46  import org.apache.myfaces.shared_impl.util.ClassUtils;
47  import org.xml.sax.InputSource;
48  import org.xml.sax.SAXException;
49  
50  
51  /**
52   * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller</a>
53   */
54  public class DigesterFacesConfigUnmarshallerImpl implements FacesConfigUnmarshaller<FacesConfig>
55  {
56  
57      private Digester digester;
58  
59  
60      public DigesterFacesConfigUnmarshallerImpl(ExternalContext externalContext)
61      {
62          digester = new Digester();
63          // TODO: validation set to false during implementation of 1.2
64          digester.setValidating(false);
65          digester.setNamespaceAware(true);
66          digester.setEntityResolver(new FacesConfigEntityResolver(externalContext));
67          //digester.setUseContextClassLoader(true);
68          digester.setClassLoader(ClassUtils.getContextClassLoader());
69  
70          digester.addObjectCreate("faces-config", FacesConfig.class);
71          digester.addObjectCreate("faces-config/application", Application.class);
72          digester.addSetNext("faces-config/application", "addApplication");
73          digester.addCallMethod("faces-config/application/action-listener", "addActionListener", 0);
74          digester.addCallMethod("faces-config/application/default-render-kit-id", "addDefaultRenderkitId", 0);
75          digester.addCallMethod("faces-config/application/message-bundle", "addMessageBundle", 0);
76          digester.addCallMethod("faces-config/application/navigation-handler", "addNavigationHandler", 0);
77          digester.addCallMethod("faces-config/application/view-handler", "addViewHandler", 0);
78          digester.addCallMethod("faces-config/application/state-manager", "addStateManager", 0);
79          digester.addCallMethod("faces-config/application/property-resolver", "addPropertyResolver", 0);
80          digester.addCallMethod("faces-config/application/variable-resolver", "addVariableResolver", 0);
81          digester.addObjectCreate("faces-config/application/locale-config", LocaleConfig.class);
82          digester.addSetNext("faces-config/application/locale-config", "addLocaleConfig");
83          digester.addCallMethod("faces-config/application/locale-config/default-locale", "setDefaultLocale", 0);
84          digester.addCallMethod("faces-config/application/locale-config/supported-locale", "addSupportedLocale", 0);
85          
86          // 1.2 specific start
87          digester.addCallMethod("faces-config/application/el-resolver", "addElResolver", 0);
88          digester.addObjectCreate("faces-config/application/resource-bundle", ResourceBundle.class);
89          digester.addSetNext("faces-config/application/resource-bundle", "addResourceBundle");
90          digester.addCallMethod("faces-config/application/resource-bundle/base-name", "setBaseName", 0);
91          digester.addCallMethod("faces-config/application/resource-bundle/var", "setVar", 0);
92          digester.addCallMethod("faces-config/application/resource-bundle/display-name", "setDisplayName", 0);
93          // 1.2 specific end
94          
95          digester.addObjectCreate("faces-config/factory", Factory.class);
96          digester.addSetNext("faces-config/factory", "addFactory");
97          digester.addCallMethod("faces-config/factory/application-factory", "addApplicationFactory", 0);
98          digester.addCallMethod("faces-config/factory/faces-context-factory", "addFacesContextFactory", 0);
99          digester.addCallMethod("faces-config/factory/lifecycle-factory", "addLifecycleFactory", 0);
100         digester.addCallMethod("faces-config/factory/render-kit-factory", "addRenderkitFactory", 0);
101 
102         digester.addCallMethod("faces-config/component", "addComponent", 2);
103         digester.addCallParam("faces-config/component/component-type", 0);
104         digester.addCallParam("faces-config/component/component-class", 1);
105 
106         digester.addObjectCreate("faces-config/converter", Converter.class);
107         digester.addSetNext("faces-config/converter", "addConverter");
108         digester.addCallMethod("faces-config/converter/converter-id", "setConverterId", 0);
109         digester.addCallMethod("faces-config/converter/converter-for-class", "setForClass", 0);
110         digester.addCallMethod("faces-config/converter/converter-class", "setConverterClass", 0);
111         digester.addObjectCreate("faces-config/converter/attribute",Attribute.class);
112         digester.addSetNext("faces-config/converter/attribute","addAttribute");
113         digester.addCallMethod("faces-config/converter/attribute/description", "addDescription", 0);
114         digester.addCallMethod("faces-config/converter/attribute/display-name", "addDisplayName", 0);
115         digester.addCallMethod("faces-config/converter/attribute/icon", "addIcon", 0);
116         digester.addCallMethod("faces-config/converter/attribute/attribute-name", "setAttributeName", 0);
117         digester.addCallMethod("faces-config/converter/attribute/attribute-class", "setAttributeClass", 0);
118         digester.addCallMethod("faces-config/converter/attribute/default-value", "setDefaultValue", 0);
119         digester.addCallMethod("faces-config/converter/attribute/suggested-value", "setSuggestedValue", 0);
120         digester.addCallMethod("faces-config/converter/attribute/attribute-extension", "addAttributeExtension", 0);
121         digester.addObjectCreate("faces-config/converter/property",Property.class);
122         digester.addSetNext("faces-config/converter/property","addProperty");
123         digester.addCallMethod("faces-config/converter/property/description", "addDescription", 0);
124         digester.addCallMethod("faces-config/converter/property/display-name", "addDisplayName", 0);
125         digester.addCallMethod("faces-config/converter/property/icon", "addIcon", 0);
126         digester.addCallMethod("faces-config/converter/property/property-name", "setPropertyName", 0);
127         digester.addCallMethod("faces-config/converter/property/property-class", "setPropertyClass", 0);
128         digester.addCallMethod("faces-config/converter/property/default-value", "setDefaultValue", 0);
129         digester.addCallMethod("faces-config/converter/property/suggested-value", "setSuggestedValue", 0);
130         digester.addCallMethod("faces-config/converter/property/property-extension", "addPropertyExtension", 0);
131 
132         digester.addObjectCreate("faces-config/managed-bean", ManagedBean.class);
133         digester.addSetNext("faces-config/managed-bean", "addManagedBean");
134         digester.addCallMethod("faces-config/managed-bean/description", "setDescription", 0);
135         digester.addCallMethod("faces-config/managed-bean/managed-bean-name", "setName", 0);
136         digester.addCallMethod("faces-config/managed-bean/managed-bean-class", "setBeanClass", 0);
137         digester.addCallMethod("faces-config/managed-bean/managed-bean-scope", "setScope", 0);
138         digester.addObjectCreate("faces-config/managed-bean/managed-property", ManagedProperty.class);
139         digester.addSetNext("faces-config/managed-bean/managed-property", "addProperty");
140         digester.addCallMethod("faces-config/managed-bean/managed-property/property-name", "setPropertyName", 0);
141         digester.addCallMethod("faces-config/managed-bean/managed-property/property-class", "setPropertyClass", 0);
142         digester.addCallMethod("faces-config/managed-bean/managed-property/null-value", "setNullValue");
143         digester.addCallMethod("faces-config/managed-bean/managed-property/value", "setValue", 0);
144         digester.addObjectCreate("faces-config/managed-bean/managed-property/map-entries", MapEntries.class);
145         digester.addSetNext("faces-config/managed-bean/managed-property/map-entries", "setMapEntries");
146         digester.addCallMethod("faces-config/managed-bean/managed-property/map-entries/key-class", "setKeyClass", 0);
147         digester.addCallMethod("faces-config/managed-bean/managed-property/map-entries/value-class", "setValueClass", 0);
148         digester.addObjectCreate("faces-config/managed-bean/managed-property/map-entries/map-entry", MapEntries.Entry.class);
149         digester.addSetNext("faces-config/managed-bean/managed-property/map-entries/map-entry", "addEntry");
150         digester.addCallMethod("faces-config/managed-bean/managed-property/map-entries/map-entry/key", "setKey", 0);
151         digester.addCallMethod("faces-config/managed-bean/managed-property/map-entries/map-entry/null-value", "setNullValue");
152         digester.addCallMethod("faces-config/managed-bean/managed-property/map-entries/map-entry/value", "setValue", 0);
153         digester.addObjectCreate("faces-config/managed-bean/managed-property/list-entries", ListEntries.class);
154         digester.addSetNext("faces-config/managed-bean/managed-property/list-entries", "setListEntries");
155         digester.addCallMethod("faces-config/managed-bean/managed-property/list-entries/value-class", "setValueClass", 0);
156         digester.addObjectCreate("faces-config/managed-bean/managed-property/list-entries/null-value", ListEntries.Entry.class);
157         digester.addSetNext("faces-config/managed-bean/managed-property/list-entries/null-value", "addEntry");
158         digester.addCallMethod("faces-config/managed-bean/managed-property/list-entries/null-value", "setNullValue");
159         digester.addObjectCreate("faces-config/managed-bean/managed-property/list-entries/value", ListEntries.Entry.class);
160         digester.addSetNext("faces-config/managed-bean/managed-property/list-entries/value", "addEntry");
161         digester.addCallMethod("faces-config/managed-bean/managed-property/list-entries/value", "setValue", 0);
162         digester.addObjectCreate("faces-config/managed-bean/map-entries", MapEntries.class);
163         digester.addSetNext("faces-config/managed-bean/map-entries", "setMapEntries");
164         digester.addCallMethod("faces-config/managed-bean/map-entries/key-class", "setKeyClass", 0);
165         digester.addCallMethod("faces-config/managed-bean/map-entries/value-class", "setValueClass", 0);
166         digester.addObjectCreate("faces-config/managed-bean/map-entries/map-entry", MapEntries.Entry.class);
167         digester.addSetNext("faces-config/managed-bean/map-entries/map-entry", "addEntry");
168         digester.addCallMethod("faces-config/managed-bean/map-entries/map-entry/key", "setKey", 0);
169         digester.addCallMethod("faces-config/managed-bean/map-entries/map-entry/null-value", "setNullValue");
170         digester.addCallMethod("faces-config/managed-bean/map-entries/map-entry/value", "setValue", 0);
171         digester.addObjectCreate("faces-config/managed-bean/list-entries", ListEntries.class);
172         digester.addSetNext("faces-config/managed-bean/list-entries", "setListEntries");
173         digester.addCallMethod("faces-config/managed-bean/list-entries/value-class", "setValueClass", 0);
174         digester.addObjectCreate("faces-config/managed-bean/list-entries/null-value", ListEntries.Entry.class);
175         digester.addSetNext("faces-config/managed-bean/list-entries/null-value", "addEntry");
176         digester.addCallMethod("faces-config/managed-bean/list-entries/null-value", "setNullValue");
177         digester.addObjectCreate("faces-config/managed-bean/list-entries/value", ListEntries.Entry.class);
178         digester.addSetNext("faces-config/managed-bean/list-entries/value", "addEntry");
179         digester.addCallMethod("faces-config/managed-bean/list-entries/value", "setValue", 0);
180 
181         digester.addObjectCreate("faces-config/navigation-rule", NavigationRule.class);
182         digester.addSetNext("faces-config/navigation-rule", "addNavigationRule");
183         digester.addCallMethod("faces-config/navigation-rule/from-view-id", "setFromViewId", 0);
184         digester.addObjectCreate("faces-config/navigation-rule/navigation-case", NavigationCase.class);
185         digester.addSetNext("faces-config/navigation-rule/navigation-case", "addNavigationCase");
186         digester.addCallMethod("faces-config/navigation-rule/navigation-case/from-action", "setFromAction", 0);
187         digester.addCallMethod("faces-config/navigation-rule/navigation-case/from-outcome", "setFromOutcome", 0);
188         digester.addCallMethod("faces-config/navigation-rule/navigation-case/to-view-id", "setToViewId", 0);
189         digester.addCallMethod("faces-config/navigation-rule/navigation-case/redirect", "setRedirect");
190 
191         digester.addObjectCreate("faces-config/render-kit", RenderKit.class);
192         digester.addSetNext("faces-config/render-kit", "addRenderKit");
193         digester.addCallMethod("faces-config/render-kit/render-kit-id", "setId", 0);
194         digester.addCallMethod("faces-config/render-kit/render-kit-class", "setRenderKitClass", 0);
195         digester.addObjectCreate("faces-config/render-kit/renderer", Renderer.class);
196         digester.addSetNext("faces-config/render-kit/renderer", "addRenderer");
197         digester.addCallMethod("faces-config/render-kit/renderer/component-family", "setComponentFamily", 0);
198         digester.addCallMethod("faces-config/render-kit/renderer/renderer-type", "setRendererType", 0);
199         digester.addCallMethod("faces-config/render-kit/renderer/renderer-class", "setRendererClass", 0);
200 
201         digester.addCallMethod("faces-config/lifecycle/phase-listener", "addLifecyclePhaseListener", 0);
202 
203         digester.addCallMethod("faces-config/validator", "addValidator", 2);
204         digester.addCallParam("faces-config/validator/validator-id", 0);
205         digester.addCallParam("faces-config/validator/validator-class", 1);
206     }
207 
208 
209     public FacesConfig getFacesConfig(InputStream in, String systemId) throws IOException, SAXException
210     {
211         InputSource is = new InputSource(in);
212         is.setSystemId(systemId);
213 
214         //Fix for http://issues.apache.org/jira/browse/MYFACES-236
215         FacesConfig config = (FacesConfig) digester.parse(is);
216 
217         List li =config.getApplications();
218 
219         for (int i = 0; i < li.size(); i++)
220         {
221             Application application = (Application) li.get(i);
222             List localeList = application.getLocaleConfig();
223 
224             for (int j = 0; j < localeList.size(); j++)
225             {
226                 LocaleConfig localeConfig = (LocaleConfig) localeList.get(j);
227 
228                 if(!localeConfig.getSupportedLocales().contains(localeConfig.getDefaultLocale()))
229                     localeConfig.getSupportedLocales().add(localeConfig.getDefaultLocale());
230             }
231         }
232 
233         return config;
234     }
235 
236 
237 }