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.util.ArrayList;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.LinkedHashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import javax.faces.render.RenderKitFactory;
30  
31  import org.apache.myfaces.config.FacesConfigDispenser;
32  import org.apache.myfaces.config.impl.digester.elements.Application;
33  import org.apache.myfaces.config.impl.digester.elements.Converter;
34  import org.apache.myfaces.config.impl.digester.elements.FacesConfig;
35  import org.apache.myfaces.config.impl.digester.elements.Factory;
36  import org.apache.myfaces.config.impl.digester.elements.LocaleConfig;
37  import org.apache.myfaces.config.impl.digester.elements.RenderKit;
38  
39  
40  /**
41   * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller</a>
42   */
43  public class DigesterFacesConfigDispenserImpl implements FacesConfigDispenser
44  {
45  
46      private List configs = new ArrayList();
47      private List applicationFactories = new ArrayList();
48      private List facesContextFactories = new ArrayList();
49      private List lifecycleFactories = new ArrayList();
50      private List renderKitFactories = new ArrayList();
51      private Map components = new HashMap();
52      private Map validators = new HashMap();
53      private String defaultRenderKitId;
54      private LocaleConfig localeConfig;
55      private List actionListeners = new ArrayList();
56      private List lifecyclePhaseListeners = new ArrayList();
57      private String messageBundle;
58      private List navigationHandlers = new ArrayList();
59      private List viewHandlers = new ArrayList();
60      private List stateManagers = new ArrayList();
61      private List propertyResolver = new ArrayList();
62      private List variableResolver = new ArrayList();
63      private Map converterById = new HashMap();
64      private Map converterByClass = new HashMap();
65      private Map converterConfigurationByClassName = new HashMap();
66      private Map renderKits = new LinkedHashMap();
67      private List managedBeans = new ArrayList();
68      private List navigationRules = new ArrayList();
69  
70  
71      /**
72       * Add another unmarshalled faces config object.
73       *
74       * @param facesConfig unmarshalled faces config object
75       */
76      public void feed(Object facesConfig)
77      {
78          FacesConfig config = (FacesConfig) facesConfig;
79          configs.add(config);
80          for (Iterator iterator = config.getFactories().iterator(); iterator.hasNext();)
81          {
82              Factory factory = (Factory) iterator.next();
83              applicationFactories.addAll(factory.getApplicationFactory());
84              facesContextFactories.addAll(factory.getFacesContextFactory());
85              lifecycleFactories.addAll(factory.getLifecycleFactory());
86              renderKitFactories.addAll(factory.getRenderkitFactory());
87          }
88          components.putAll(config.getComponents());
89          validators.putAll(config.getValidators());
90  
91          for (Iterator iterator = config.getApplications().iterator(); iterator.hasNext();)
92          {
93              Application application = (Application) iterator.next();
94              if (!application.getDefaultRenderkitId().isEmpty())
95              {
96                  defaultRenderKitId = (String) application.getDefaultRenderkitId().get(application.getDefaultRenderkitId().size() - 1);
97              }
98              if (!application.getMessageBundle().isEmpty())
99              {
100                 messageBundle = (String) application.getMessageBundle().get(application.getMessageBundle().size() - 1);
101             }
102             if (!application.getLocaleConfig().isEmpty())
103             {
104                 localeConfig = (LocaleConfig) application.getLocaleConfig().get(application.getLocaleConfig().size() - 1);
105             }
106             actionListeners.addAll(application.getActionListener());
107             navigationHandlers.addAll(application.getNavigationHandler());
108             viewHandlers.addAll(application.getViewHandler());
109             stateManagers.addAll(application.getStateManager());
110             propertyResolver.addAll(application.getPropertyResolver());
111             variableResolver.addAll(application.getVariableResolver());
112         }
113         for (Iterator iterator = config.getConverters().iterator(); iterator.hasNext();)
114         {
115             Converter converter = (Converter) iterator.next();
116 
117             if (converter.getConverterId() != null)
118             {
119                 converterById.put(converter.getConverterId(), converter.getConverterClass());
120             }
121             else
122             {
123                 converterByClass.put(converter.getForClass(), converter.getConverterClass());
124             }
125 
126             converterConfigurationByClassName.put(converter.getConverterClass(),converter);
127         }
128 
129         for (Iterator iterator = config.getRenderKits().iterator(); iterator.hasNext();)
130         {
131             RenderKit renderKit = (RenderKit) iterator.next();
132             String renderKitId = renderKit.getId();
133 
134             if (renderKitId == null) {
135                 renderKitId = RenderKitFactory.HTML_BASIC_RENDER_KIT;
136             }
137 
138             RenderKit existing = (RenderKit) renderKits.get(renderKitId);
139 
140             if (existing == null) {
141                 renderKits.put(renderKit.getId(), renderKit);
142             } else {
143                 existing.merge(renderKit);
144             }
145         }
146         lifecyclePhaseListeners.addAll(config.getLifecyclePhaseListener());
147         managedBeans.addAll(config.getManagedBeans());
148         navigationRules.addAll(config.getNavigationRules());
149     }
150 
151 
152     /**
153      * Add another ApplicationFactory class name
154      *
155      * @param factoryClassName a class name
156      */
157     public void feedApplicationFactory(String factoryClassName)
158     {
159         applicationFactories.add(factoryClassName);
160     }
161 
162 
163     /**
164      * Add another FacesContextFactory class name
165      *
166      * @param factoryClassName a class name
167      */
168     public void feedFacesContextFactory(String factoryClassName)
169     {
170         facesContextFactories.add(factoryClassName);
171     }
172 
173 
174     /**
175      * Add another LifecycleFactory class name
176      *
177      * @param factoryClassName a class name
178      */
179     public void feedLifecycleFactory(String factoryClassName)
180     {
181         lifecycleFactories.add(factoryClassName);
182     }
183 
184 
185     /**
186      * Add another RenderKitFactory class name
187      *
188      * @param factoryClassName a class name
189      */
190     public void feedRenderKitFactory(String factoryClassName)
191     {
192         renderKitFactories.add(factoryClassName);
193     }
194 
195 
196     /**
197      * @return Iterator over ApplicationFactory class names
198      */
199     public Iterator getApplicationFactoryIterator()
200     {
201         return applicationFactories.iterator();
202     }
203 
204 
205     /**
206      * @return Iterator over FacesContextFactory class names
207      */
208     public Iterator getFacesContextFactoryIterator()
209     {
210         return facesContextFactories.iterator();
211     }
212 
213 
214     /**
215      * @return Iterator over LifecycleFactory class names
216      */
217     public Iterator getLifecycleFactoryIterator()
218     {
219         return lifecycleFactories.iterator();
220     }
221 
222 
223     /**
224      * @return Iterator over RenderKit factory class names
225      */
226     public Iterator getRenderKitFactoryIterator()
227     {
228         return renderKitFactories.iterator();
229     }
230 
231 
232     /**
233      * @return Iterator over ActionListener class names
234      */
235     public Iterator getActionListenerIterator()
236     {
237         List listeners = new ArrayList(actionListeners);
238         return listeners.iterator();
239     }
240 
241 
242     /**
243      * @return the default render kit id
244      */
245     public String getDefaultRenderKitId()
246     {
247         return defaultRenderKitId;
248     }
249 
250 
251     /**
252      * @return Iterator over message bundle names
253      */
254     public String getMessageBundle()
255     {
256         return messageBundle;
257     }
258 
259 
260     /**
261      * @return Iterator over NavigationHandler class names
262      */
263     public Iterator getNavigationHandlerIterator()
264     {
265         List handlers = new ArrayList(navigationHandlers);
266         return handlers.iterator();
267     }
268 
269 
270     /**
271      * @return Iterator over ViewHandler class names
272      */
273     public Iterator getViewHandlerIterator()
274     {
275         List handlers = new ArrayList(viewHandlers);
276         return handlers.iterator();
277     }
278 
279 
280     /**
281      * @return Iterator over StateManager class names
282      */
283     public Iterator getStateManagerIterator()
284     {
285         List managers = new ArrayList(stateManagers);
286         return managers.iterator();
287     }
288 
289 
290     /**
291      * @return Iterator over PropertyResolver class names
292      */
293     public Iterator getPropertyResolverIterator()
294     {
295         List resolver = new ArrayList(propertyResolver);
296         return resolver.iterator();
297     }
298 
299 
300     /**
301      * @return Iterator over VariableResolver class names
302      */
303     public Iterator getVariableResolverIterator()
304     {
305         List resolver = new ArrayList(variableResolver);
306 
307         return resolver.iterator();
308     }
309 
310 
311     /**
312      * @return the default locale name
313      */
314     public String getDefaultLocale()
315     {
316         if (localeConfig != null)
317         {
318             return localeConfig.getDefaultLocale();
319         }
320         return null;
321     }
322 
323 
324     /**
325      * @return Iterator over supported locale names
326      */
327     public Iterator getSupportedLocalesIterator()
328     {
329         if (localeConfig != null)
330         {
331             return localeConfig.getSupportedLocales().iterator();
332         }
333         return Collections.EMPTY_LIST.iterator();
334     }
335 
336 
337     /**
338      * @return Iterator over all defined component types
339      */
340     public Iterator getComponentTypes()
341     {
342         return components.keySet().iterator();
343     }
344 
345 
346     /**
347      * @return component class that belongs to the given component type
348      */
349     public String getComponentClass(String componentType)
350     {
351         return (String) components.get(componentType);
352     }
353 
354 
355     /**
356      * @return Iterator over all defined converter ids
357      */
358     public Iterator getConverterIds()
359     {
360         return converterById.keySet().iterator();
361     }
362 
363 
364     /**
365      * @return Iterator over all classes with an associated converter
366      */
367     public Iterator getConverterClasses()
368     {
369         return converterByClass.keySet().iterator();
370     }
371 
372     public Iterator getConverterConfigurationByClassName()
373     {
374         return converterConfigurationByClassName.keySet().iterator();
375     }
376 
377     public Converter getConverterConfiguration(String converterClassName)
378     {
379         return (Converter) converterConfigurationByClassName.get(converterClassName);
380     }
381 
382 
383     /**
384      * @return converter class that belongs to the given converter id
385      */
386     public String getConverterClassById(String converterId)
387     {
388         return (String) converterById.get(converterId);
389     }
390 
391 
392     /**
393      * @return converter class that is associated with the given class name
394      */
395     public String getConverterClassByClass(String className)
396     {
397         return (String) converterByClass.get(className);
398     }
399 
400 
401     /**
402      * @return Iterator over all defined validator ids
403      */
404     public Iterator getValidatorIds()
405     {
406         return validators.keySet().iterator();
407     }
408 
409 
410     /**
411      * @return validator class name that belongs to the given validator id
412      */
413     public String getValidatorClass(String validatorId)
414     {
415         return (String) validators.get(validatorId);
416     }
417 
418 
419     /**
420      * @return Iterator over {@link org.apache.myfaces.config.element.ManagedBean ManagedBean}s
421      */
422     public Iterator getManagedBeans()
423     {
424         return managedBeans.iterator();
425     }
426 
427 
428     /**
429      * @return Iterator over {@link org.apache.myfaces.config.element.NavigationRule NavigationRule}s
430      */
431     public Iterator getNavigationRules()
432     {
433         return navigationRules.iterator();
434     }
435 
436 
437     /**
438      * @return Iterator over all defined renderkit ids
439      */
440     public Iterator getRenderKitIds()
441     {
442         return renderKits.keySet().iterator();
443     }
444 
445 
446     /**
447      * @return renderkit class name for given renderkit id
448      */
449     public String getRenderKitClass(String renderKitId)
450     {
451         RenderKit renderKit = (RenderKit) renderKits.get(renderKitId);
452         return renderKit.getRenderKitClass();
453     }
454 
455 
456     /**
457      * @return Iterator over {@link org.apache.myfaces.config.element.Renderer Renderer}s for the given renderKitId
458      */
459     public Iterator getRenderers(String renderKitId)
460     {
461         RenderKit renderKit = (RenderKit) renderKits.get(renderKitId);
462         return renderKit.getRenderer().iterator();
463     }
464 
465 
466     /**
467      * @return Iterator over {@link javax.faces.event.PhaseListener} implementation class names
468      */
469     public Iterator getLifecyclePhaseListeners()
470     {
471         return lifecyclePhaseListeners.iterator();
472     }
473 
474 }