View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.docstore.discovery.util;
17  
18  import com.thoughtworks.xstream.XStream;
19  import com.thoughtworks.xstream.converters.Converter;
20  import com.thoughtworks.xstream.converters.MarshallingContext;
21  import com.thoughtworks.xstream.converters.UnmarshallingContext;
22  import com.thoughtworks.xstream.io.HierarchicalStreamReader;
23  import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
24  import org.apache.commons.io.IOUtils;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import java.io.InputStream;
29  import java.net.URL;
30  import java.util.ArrayList;
31  import java.util.Collections;
32  import java.util.List;
33  
34  public class Languages {
35  
36      private List<Language> languages = new ArrayList<Language>();
37      private static Languages soLanguagesISO639_1_cc = null;
38      private static Languages soLanguagesISO639_3 = null;
39      protected final Logger LOG = LoggerFactory.getLogger(Languages.class);
40      public static final String ISO_639_1_CC = "ISO_639_1_CC";
41      public static final String ISO_639_3 = "ISO_639_3";
42  
43      private Languages(String encoding) {
44          try {
45              String file = null;
46              if (ISO_639_1_CC.equals(encoding)) {
47                  file = "languages-iso-639-1-cc.xml";
48              } else if (ISO_639_3.equals(encoding)) {
49                  file = "languages-iso-639-3.xml";
50              }
51              URL url = this.getClass().getClassLoader().getResource(file);
52              XStream xStream = new XStream();
53              xStream.alias("languages", Languages.class);
54              xStream.alias("lang", Language.class);
55              xStream.addImplicitCollection(Languages.class, "languages", Language.class);
56              xStream.registerConverter(new LanguageConverter());
57              Languages langs = (Languages) xStream.fromXML(IOUtils.toString((InputStream) url.getContent()));
58              this.setLanguages(Collections.unmodifiableList(langs.getLanguages()));
59          } catch (Exception e) {
60              LOG.error(e.getMessage(), e);
61          }
62      }
63  
64      /**
65       * Method to give out the Single Instance of Languages.
66       *
67       * @param encoding - Specify the encoding type of the languages expected.
68       * @return
69       */
70      public static Languages getInstance(String encoding) {
71          if (ISO_639_1_CC.equals(encoding)) {
72              if (soLanguagesISO639_1_cc == null) {
73                  soLanguagesISO639_1_cc = new Languages(encoding);
74              }
75              return soLanguagesISO639_1_cc;
76          } else if (ISO_639_3.equals(encoding)) {
77              if (soLanguagesISO639_3 == null) {
78                  soLanguagesISO639_3 = new Languages(encoding);
79              }
80              return soLanguagesISO639_3;
81          } else {
82              return null;
83          }
84      }
85  
86      /**
87       * Method to give language. If not a valid language gives null.
88       *
89       * @param code
90       * @return
91       */
92      public Language getLanguage(String code) {
93          int indx = getLanguages().indexOf(new Language(code));
94          if (indx != -1) {
95              return this.getLanguages().get(indx);
96          } else {
97              return null;
98          }
99      }
100 
101     /**
102      * Method to give language description. If not a valid language gives null.
103      *
104      * @param code
105      * @return
106      */
107     public String getLanguageDescription(String code) {
108         int indx = getLanguages().indexOf(new Language(code));
109         if (indx != -1) {
110             return this.getLanguages().get(indx).getValue();
111         } else {
112             return null;
113         }
114     }
115 
116     public List<Language> getLanguages() {
117         return languages;
118     }
119 
120     private void setLanguages(List<Language> languages) {
121         this.languages = languages;
122     }
123 
124     private void addLanguage(Language language) {
125         this.languages.add(language);
126     }
127 
128     /**
129      * Class Language.
130      *
131      * @author Rajesh Chowdary K
132      */
133     class Language {
134         public Language() {
135         }
136 
137         public Language(String code) {
138             this.code = code;
139         }
140 
141         private String code = null;
142         private String value = null;
143 
144         public String getCode() {
145             return code;
146         }
147 
148         public void setCode(String code) {
149             this.code = code;
150         }
151 
152         public String getValue() {
153             return value;
154         }
155 
156         public void setValue(String value) {
157             this.value = value;
158         }
159 
160         @Override
161         public boolean equals(Object obj) {
162             if (obj instanceof Language) {
163                 if (((Language) obj).getCode().equals(code)) {
164                     return true;
165                 } else {
166                     return false;
167                 }
168             } else {
169                 return false;
170             }
171         }
172     }
173 
174     private class LanguageConverter
175             implements Converter {
176         @Override
177         public void marshal(Object o, HierarchicalStreamWriter streamWriter, MarshallingContext marshallingContext) {
178         }
179 
180         @Override
181         public Object unmarshal(HierarchicalStreamReader streamReader, UnmarshallingContext unmarshallingContext) {
182             Language language = new Language();
183             language.setCode(streamReader.getAttribute("code"));
184             language.setValue(streamReader.getValue());
185             return language;
186         }
187 
188         @Override
189         public boolean canConvert(Class aClass) {
190             return aClass.equals(Language.class);
191         }
192     }
193 }