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.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import java.io.Serializable;
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  public class RenderersConfigImpl implements RenderersConfig, Serializable {
34  
35    private static final long serialVersionUID = 1L;
36  
37    private static final Logger LOG = LoggerFactory.getLogger(RenderersConfigImpl.class);
38  
39    private Map<String, RendererConfig> rendererMap = new HashMap<String, RendererConfig>();
40    private boolean merged = false;
41  
42    private boolean unmodifiable = false;
43  
44    private void checkLocked() throws IllegalStateException {
45      if (unmodifiable) {
46        throw new RuntimeException("The configuration must not be changed after initialization!");
47      }
48    }
49  
50    /**
51     * Lock the configuration, so it cannot be modified any more.
52     */
53    public void lock() {
54      unmodifiable = true;
55      rendererMap = Collections.unmodifiableMap(rendererMap);
56    }
57  
58    public boolean isMerged() {
59      return merged;
60    }
61  
62    public void setMerged(final boolean merged) {
63      checkLocked();
64      this.merged = merged;
65    }
66  
67    @Override
68    public List<RendererConfig> getRendererConfigs() {
69      final ArrayList<RendererConfig> result = new ArrayList<RendererConfig>();
70      result.addAll(rendererMap.values());
71      return result;
72    }
73  
74    public void addRenderer(final RendererConfig rendererConfig) {
75      checkLocked();
76      final String name = rendererConfig.getName();
77      if (rendererMap.containsKey(name)) {
78        rendererMap.get(name).merge(rendererConfig);
79      } else {
80        rendererMap.put(name, rendererConfig);
81      }
82    }
83  
84    @Override
85    public boolean isMarkupSupported(final String rendererName, final String markup) {
86      final RendererConfig rendererConfig = rendererMap.get(rendererName);
87      return rendererConfig != null && rendererConfig.contains(markup);
88    }
89  
90    public void merge(final RenderersConfig renderersConfig, final boolean override) {
91      checkLocked();
92      final Collection<RendererConfig> renderers = renderersConfig.getRendererConfigs();
93      for (final RendererConfig rendererConfig : renderers) {
94        addRenderer(rendererConfig);
95      }
96    }
97  
98    public String toString() {
99      return rendererMap.toString();
100   }
101 }