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: 12:43 PM 024 * To change this template use File | Settings | File Templates. 025 */ 026public class Places008 { 027 private List<Place> listOfPlaces = new ArrayList<Place>(); 028 private static Places008 placesObj = null; 029 protected final Logger LOG = LoggerFactory.getLogger(Places008.class); 030 031 public Places008() { 032 try { 033 URL url = this.getClass().getClassLoader().getResource("places008.xml"); 034 XStream xStream = new XStream(); 035 xStream.alias("places", Places008.class); 036 xStream.alias("place", Place.class); 037 xStream.addImplicitCollection(Places008.class, "listOfPlaces", Place.class); 038 xStream.registerConverter(new PlaceConverter()); 039 Places008 places008 = (Places008) xStream.fromXML(IOUtils.toString((InputStream) url.getContent())); 040 this.setListOfPlaces(Collections.unmodifiableList(places008.getListOfPlaces())); 041 } catch (Exception e) { 042 LOG.error(e.getMessage(), e); 043 } 044 } 045 046 public static Places008 getInstance() { 047 if (placesObj == null) { 048 placesObj = new Places008(); 049 } 050 return placesObj; 051 052 } 053 054 055 public Place getPlace() { 056 int indx = getListOfPlaces().indexOf(new Place()); 057 if (indx != -1) { 058 return this.getListOfPlaces().get(indx); 059 } else { 060 return null; 061 } 062 } 063 064 /** 065 * Method to give language description. If not a valid language gives null. 066 * 067 * @param code 068 * @return 069 */ 070 public List<String> getPlaceDescription(String code) { 071 List<String> pList = new ArrayList<>(); 072 for (Place place : listOfPlaces) { 073 if (code.equals("*")) { 074 pList.add(place.getValue()); 075 } else if (place.getValue().toLowerCase().indexOf(code)>0) { 076 pList.add(place.getValue()); 077 } 078 if(place.getCode().startsWith(code)){ 079 pList.add(place.getValue()); 080 } 081 } 082 return pList; 083 } 084 085 public List<Place> getListOfPlaces() { 086 return listOfPlaces; 087 } 088 089 private void setListOfPlaces(List<Place> listOfPlaces) { 090 this.listOfPlaces = listOfPlaces; 091 } 092 093 private void addLanguage(Place place) { 094 this.listOfPlaces.add(place); 095 } 096 097 /** 098 * Class Language. 099 * 100 * @author premkumarv 101 */ 102 public class Place { 103 public Place() { 104 } 105 106 public Place(String code) { 107 this.code = code; 108 } 109 110 private String code = null; 111 private String value = null; 112 113 public String getCode() { 114 return code; 115 } 116 117 public void setCode(String code) { 118 this.code = code; 119 } 120 121 public String getValue() { 122 return value; 123 } 124 125 public void setValue(String value) { 126 this.value = value; 127 } 128 129 @Override 130 public boolean equals(Object obj) { 131 if (obj instanceof Place) { 132 if (((Place) obj).getCode().equals(code)) { 133 return true; 134 } else { 135 return false; 136 } 137 } else { 138 return false; 139 } 140 } 141 } 142 143 private class PlaceConverter 144 implements Converter { 145 @Override 146 public void marshal(Object o, HierarchicalStreamWriter streamWriter, MarshallingContext marshallingContext) { 147 } 148 149 @Override 150 public Object unmarshal(HierarchicalStreamReader streamReader, UnmarshallingContext unmarshallingContext) { 151 Place place = new Place(); 152 place.setCode(streamReader.getAttribute("code")); 153 place.setValue(streamReader.getValue()); 154 return place; 155 } 156 157 @Override 158 public boolean canConvert(Class aClass) { 159 return aClass.equals(Place.class); 160 } 161 } 162 163}