001package org.kuali.ole.util; 002 003import com.thoughtworks.xstream.XStream; 004import com.thoughtworks.xstream.converters.Converter; 005import com.thoughtworks.xstream.converters.MarshallingContext; 006import com.thoughtworks.xstream.converters.UnmarshallingContext; 007import com.thoughtworks.xstream.io.HierarchicalStreamReader; 008import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 009import org.apache.commons.io.IOUtils; 010import org.slf4j.Logger; 011import org.slf4j.LoggerFactory; 012 013import java.io.InputStream; 014import java.net.URL; 015import java.util.ArrayList; 016import java.util.Collections; 017import java.util.List; 018 019/** 020 * Created with IntelliJ IDEA. 021 * User:premkumarv 022 * Date: 9/18/13 023 * Time: 6:08 PM 024 * To change this template use File | Settings | File Templates. 025 */ 026public class Languages008 { 027 private List<Language> listOfLanguages = new ArrayList<Language>(); 028 private static Languages008 languagesObj = null; 029 protected final Logger LOG = LoggerFactory.getLogger(Places008.class); 030 031 public Languages008() { 032 try { 033 URL url = this.getClass().getClassLoader().getResource("008languages.xml"); 034 XStream xStream = new XStream(); 035 xStream.alias("languages", Languages008.class); 036 xStream.alias("language", Language.class); 037 xStream.addImplicitCollection(Languages008.class, "listOfLanguages", Language.class); 038 xStream.registerConverter(new LanguageConverter()); 039 Languages008 languages008 = (Languages008) xStream.fromXML(IOUtils.toString((InputStream) url.getContent())); 040 this.setListOfLanguages(Collections.unmodifiableList(languages008.getListOfLanguages())); 041 } catch (Exception e) { 042 LOG.error(e.getMessage(), e); 043 } 044 } 045 046 public static Languages008 getInstance() { 047 if (languagesObj == null) { 048 languagesObj = new Languages008(); 049 } 050 return languagesObj; 051 052 } 053 054 /** 055 * Method to give language description. If not a valid language gives null. 056 * 057 * @param code 058 * @return 059 */ 060 public List<String> getLanguageDescription(String code) { 061 List<String> lList = new ArrayList<>(); 062 for (Language language : listOfLanguages) { 063 if (code.equals("*")) { 064 lList.add(language.getValue()); 065 } else if (language.getValue().toLowerCase().indexOf(code)>0) { 066 lList.add(language.getValue()); 067 } 068 069 if(language.getCode().startsWith(code)){ 070 lList.add(language.getValue()); 071 } 072 } 073 return lList; 074 } 075 076 public List<Language> getListOfLanguages() { 077 return listOfLanguages; 078 } 079 080 private void setListOfLanguages(List<Language> listOfLanguages) { 081 this.listOfLanguages = listOfLanguages; 082 } 083 084 /** 085 * Class Language. 086 * 087 * @author premkumarv 088 */ 089 public class Language { 090 public Language() { 091 } 092 093 public Language(String code) { 094 this.code = code; 095 } 096 097 private String code = null; 098 private String value = null; 099 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}