KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
20
21 import org.alfresco.config.ConfigElement;
22 import org.alfresco.config.ConfigException;
23 import org.alfresco.config.xml.elementreader.ConfigElementReader;
24 import org.dom4j.Element;
25
26 /**
27  * Custom element reader to parse config for languages
28  *
29  * @author Gavin Cornwell
30  */

31 public class LanguagesElementReader implements ConfigElementReader
32 {
33    public static final String JavaDoc ELEMENT_LANGUAGE = "language";
34    public static final String JavaDoc ATTRIBUTE_LOCALE = "locale";
35    
36    /**
37     * @see org.alfresco.config.xml.elementreader.ConfigElementReader#parse(org.dom4j.Element)
38     */

39    @SuppressWarnings JavaDoc("unchecked")
40    public ConfigElement parse(Element element)
41    {
42       LanguagesConfigElement configElement = null;
43       
44       if (element != null)
45       {
46          String JavaDoc name = element.getName();
47          if (name.equals(LanguagesConfigElement.CONFIG_ELEMENT_ID) == false)
48          {
49             throw new ConfigException("LanguagesElementReader can only parse " +
50                   LanguagesConfigElement.CONFIG_ELEMENT_ID + " elements, the element passed was '" +
51                   name + "'");
52          }
53          
54          configElement = new LanguagesConfigElement();
55          
56          Iterator JavaDoc<Element> langsItr = element.elementIterator(ELEMENT_LANGUAGE);
57          while (langsItr.hasNext())
58          {
59             Element language = langsItr.next();
60             String JavaDoc localeCode = language.attributeValue(ATTRIBUTE_LOCALE);
61             String JavaDoc label = language.getTextTrim();
62             
63             if (localeCode != null && localeCode.length() != 0 &&
64                 label != null && label.length() != 0)
65             {
66                // store the language code against the display label
67
configElement.addLanguage(localeCode, label);
68             }
69          }
70       }
71       
72       return configElement;
73    }
74 }
75
Popular Tags