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  
20  package org.apache.myfaces.tobago.internal.config;
21  
22  import org.apache.myfaces.tobago.context.Theme;
23  import org.apache.myfaces.tobago.context.ThemeImpl;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  public class ThemeBuilder {
34  
35    private static final Logger LOG = LoggerFactory.getLogger(ThemeBuilder.class);
36  
37    private List<ThemeImpl> availableThemes = new ArrayList<ThemeImpl>();
38    private TobagoConfigImpl tobagoConfig;
39  
40    public ThemeBuilder(final TobagoConfigImpl tobagoConfig) {
41      this.tobagoConfig = tobagoConfig;
42    }
43  
44    public void resolveThemes() {
45      final Map<String, ThemeImpl> map = new HashMap<String, ThemeImpl>();
46      for (final ThemeImpl theme : availableThemes) {
47        if (LOG.isDebugEnabled()) {
48          LOG.debug("Theme from XML files: {} ", theme.getName());
49        }
50        map.put(theme.getName(), theme);
51      }
52      for (final ThemeImpl theme : availableThemes) {
53        final String fallbackName = theme.getFallbackName();
54        final ThemeImpl fallback = map.get(fallbackName);
55        theme.setFallback(fallback);
56      }
57      for (final ThemeImpl theme : availableThemes) {
58        theme.resolveFallbacks();
59      }
60      for (final ThemeImpl theme : availableThemes) {
61        theme.resolveRendererConfig(tobagoConfig.getRenderersConfig());
62      }
63      final Map<String, Theme> result = new HashMap<String, Theme>();
64      for (final ThemeImpl theme : availableThemes) {
65        result.put(theme.getName(), theme);
66      }
67      for (final ThemeImpl theme : availableThemes) {
68        theme.resolveResources();
69      }
70      for (final ThemeImpl theme : availableThemes) {
71        theme.init();
72      }
73      tobagoConfig.setAvailableThemes(Collections.unmodifiableMap(result));
74    }
75  
76    public void addTheme(final ThemeImpl theme) {
77      availableThemes.add(theme);
78    }
79  }