KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > config > LanguagesConfigElement


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.config;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.alfresco.config.ConfigElement;
25 import org.alfresco.config.ConfigException;
26 import org.alfresco.config.element.ConfigElementAdapter;
27
28 /**
29  * Custom config element that represents config values for languages
30  *
31  * @author Gavin Cornwell
32  */

33 public class LanguagesConfigElement extends ConfigElementAdapter
34 {
35    public static final String JavaDoc CONFIG_ELEMENT_ID = "languages";
36          
37    private Map JavaDoc<String JavaDoc, String JavaDoc> localeMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
38    private List JavaDoc<String JavaDoc> languages = new ArrayList JavaDoc<String JavaDoc>(8);
39    
40    /**
41     * Default Constructor
42     */

43    public LanguagesConfigElement()
44    {
45       super(CONFIG_ELEMENT_ID);
46    }
47    
48    /**
49     * Constructor
50     *
51     * @param name Name of the element this config element represents
52     */

53    public LanguagesConfigElement(String JavaDoc name)
54    {
55       super(name);
56    }
57    
58    /**
59     * @see org.alfresco.config.element.ConfigElementAdapter#getChildren()
60     */

61    @Override JavaDoc
62    public List JavaDoc<ConfigElement> getChildren()
63    {
64       throw new ConfigException("Reading the languages config via the generic interfaces is not supported");
65    }
66
67    /**
68     * @see org.alfresco.config.element.ConfigElementAdapter#combine(org.alfresco.config.ConfigElement)
69     */

70    public ConfigElement combine(ConfigElement configElement)
71    {
72       LanguagesConfigElement existingElement = (LanguagesConfigElement)configElement;
73       LanguagesConfigElement newElement = new LanguagesConfigElement();
74
75       // add the languages from this config element
76
for (String JavaDoc locale : this.languages)
77       {
78          newElement.addLanguage(locale, this.localeMap.get(locale));
79       }
80       
81       // now add the languages from the one to be combined (but
82
// only if they are not already in the list)
83
List JavaDoc<String JavaDoc> languages = existingElement.getLanguages();
84       for (String JavaDoc locale : languages)
85       {
86          if (newElement.getLabelForLanguage(locale) == null)
87          {
88             String JavaDoc label = existingElement.getLabelForLanguage(locale);
89             newElement.addLanguage(locale, label);
90          }
91       }
92       
93       return newElement;
94    }
95
96    /**
97     * Add a language locale and display label to the list.
98     *
99     * @param locale Locale code
100     * @param label Display label
101     */

102    /*package*/ void addLanguage(String JavaDoc locale, String JavaDoc label)
103    {
104       this.localeMap.put(locale, label);
105       this.languages.add(locale);
106    }
107    
108    /**
109     * @return List of supported language locale strings in config file order
110     */

111    public List JavaDoc<String JavaDoc> getLanguages()
112    {
113       return this.languages;
114    }
115    
116    /**
117     * @param locale The locale string to lookup language label for
118     *
119     * @return the language label for specified locale string, or null if not found
120     */

121    public String JavaDoc getLabelForLanguage(String JavaDoc locale)
122    {
123       return this.localeMap.get(locale);
124    }
125 }
126
Popular Tags