View Javadoc
1   package org.kuali.ole.util;
2   
3   import com.thoughtworks.xstream.XStream;
4   import com.thoughtworks.xstream.converters.Converter;
5   import com.thoughtworks.xstream.converters.MarshallingContext;
6   import com.thoughtworks.xstream.converters.UnmarshallingContext;
7   import com.thoughtworks.xstream.io.HierarchicalStreamReader;
8   import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
9   import org.apache.commons.io.IOUtils;
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  import java.io.InputStream;
14  import java.net.URL;
15  import java.util.ArrayList;
16  import java.util.Collections;
17  import java.util.List;
18  
19  /**
20   * Created with IntelliJ IDEA.
21   * User:premkumarv
22   * Date: 9/18/13
23   * Time: 6:08 PM
24   * To change this template use File | Settings | File Templates.
25   */
26  public class Languages008 {
27      private List<Language> listOfLanguages = new ArrayList<Language>();
28      private static Languages008 languagesObj = null;
29      protected final Logger LOG = LoggerFactory.getLogger(Places008.class);
30  
31      public Languages008() {
32          try {
33              URL url = this.getClass().getClassLoader().getResource("008languages.xml");
34              XStream xStream = new XStream();
35              xStream.alias("languages", Languages008.class);
36              xStream.alias("language", Language.class);
37              xStream.addImplicitCollection(Languages008.class, "listOfLanguages", Language.class);
38              xStream.registerConverter(new LanguageConverter());
39              Languages008 languages008 = (Languages008) xStream.fromXML(IOUtils.toString((InputStream) url.getContent()));
40              this.setListOfLanguages(Collections.unmodifiableList(languages008.getListOfLanguages()));
41          } catch (Exception e) {
42              LOG.error(e.getMessage(), e);
43          }
44      }
45  
46      public static Languages008 getInstance() {
47          if (languagesObj == null) {
48              languagesObj = new Languages008();
49          }
50          return languagesObj;
51  
52      }
53  
54      /**
55       * Method to give language description. If not a valid language gives null.
56       *
57       * @param code
58       * @return
59       */
60      public List<String> getLanguageDescription(String code) {
61          List<String> lList = new ArrayList<>();
62          for (Language language : listOfLanguages) {
63              if (code.equals("*")) {
64                  lList.add(language.getValue());
65              } else if (language.getValue().toLowerCase().indexOf(code)>0) {
66                  lList.add(language.getValue());
67              }
68  
69              if(language.getCode().startsWith(code)){
70                  lList.add(language.getValue());
71              }
72          }
73          return lList;
74      }
75  
76      public List<Language> getListOfLanguages() {
77          return listOfLanguages;
78      }
79  
80      private void setListOfLanguages(List<Language> listOfLanguages) {
81          this.listOfLanguages = listOfLanguages;
82      }
83  
84       /**
85       * Class Language.
86       *
87       * @author premkumarv
88       */
89      public class Language {
90          public Language() {
91          }
92  
93          public Language(String code) {
94              this.code = code;
95          }
96  
97          private String code = null;
98          private String value = null;
99  
100         public String getCode() {
101             return code;
102         }
103 
104         public void setCode(String code) {
105             this.code = code;
106         }
107 
108         public String getValue() {
109             return value;
110         }
111 
112         public void setValue(String value) {
113             this.value = value;
114         }
115 
116         @Override
117         public boolean equals(Object obj) {
118             if (obj instanceof Language) {
119                 if (((Language) obj).getCode().equals(code)) {
120                     return true;
121                 } else {
122                     return false;
123                 }
124             } else {
125                 return false;
126             }
127         }
128     }
129 
130     private class LanguageConverter
131             implements Converter {
132         @Override
133         public void marshal(Object o, HierarchicalStreamWriter streamWriter, MarshallingContext marshallingContext) {
134         }
135 
136         @Override
137         public Object unmarshal(HierarchicalStreamReader streamReader, UnmarshallingContext unmarshallingContext) {
138             Language language = new Language();
139             language.setCode(streamReader.getAttribute("code"));
140             language.setValue(streamReader.getValue());
141             return language;
142         }
143 
144         @Override
145         public boolean canConvert(Class aClass) {
146             return aClass.equals(Language.class);
147         }
148     }
149 
150 }