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