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        if (LOG.isDebugEnabled()) {
49          LOG.debug("Theme from XML files: {} ", theme.getName());
50        }
51        map.put(theme.getName(), theme);
52      }
53      for (ThemeImpl theme : availableThemes) {
54        String fallbackName = theme.getFallbackName();
55        ThemeImpl fallback = map.get(fallbackName);
56        theme.setFallback(fallback);
57      }
58      for (ThemeImpl theme : availableThemes) {
59        theme.resolveFallbacks();
60      }
61      for (ThemeImpl theme : availableThemes) {
62        theme.resolveRendererConfig(tobagoConfig.getRenderersConfig());
63      }
64      Map<String, Theme> result = new HashMap<String, Theme>();
65      for (ThemeImpl theme : availableThemes) {
66        result.put(theme.getName(), theme);
67      }
68      for (ThemeImpl theme : availableThemes) {
69        theme.resolveResources();
70      }
71      for (ThemeImpl theme : availableThemes) {
72        theme.init();
73      }
74      tobagoConfig.setAvailableThemes(Collections.unmodifiableMap(result));
75    }
76  
77    public void addTheme(ThemeImpl theme) {
78      availableThemes.add(theme);
79    }
80  }