1 33 34 package com.icesoft.icefaces.samples.showcase.components.autocomplete; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 import javax.faces.context.FacesContext; 40 import javax.faces.model.SelectItem; 41 import javax.servlet.http.HttpSession ; 42 import java.beans.XMLDecoder ; 43 import java.io.BufferedInputStream ; 44 import java.io.IOException ; 45 import java.util.ArrayList ; 46 import java.util.Collections ; 47 import java.util.Comparator ; 48 import java.util.List ; 49 import java.util.zip.ZipEntry ; 50 import java.util.zip.ZipFile ; 51 52 53 60 public class AutoCompleteDictionary { 61 62 private static Log log = LogFactory.getLog(AutoCompleteDictionary.class); 63 64 private static List dictionary; 66 67 public AutoCompleteDictionary() { 68 try { 70 log.info("initializing dictionary"); 71 init(); 72 } catch (Exception e) { 73 if (log.isErrorEnabled()) { 74 log.error("Error initializtin sorting list"); 75 } 76 } 77 } 78 79 82 public static final Comparator LABEL_COMPARATOR = new Comparator () { 83 String s1; 84 String s2; 85 86 public int compare(Object o1, Object o2) { 88 89 if (o1 instanceof SelectItem) { 90 s1 = ((SelectItem) o1).getLabel(); 91 } else { 92 s1 = o1.toString(); 93 } 94 95 if (o2 instanceof SelectItem) { 96 s2 = ((SelectItem) o2).getLabel(); 97 } else { 98 s2 = o2.toString(); 99 } 100 return s1.compareToIgnoreCase(s2); 102 } 103 }; 104 105 110 public List getDictionary() { 111 return dictionary; 112 } 113 114 private static void init() { 115 List cityList = null; 117 118 120 HttpSession session = (HttpSession ) FacesContext.getCurrentInstance(). 122 getExternalContext().getSession(true); 123 String basePath = 124 session.getServletContext().getRealPath("/WEB-INF/resources"); 125 basePath += "/city.xml.zip"; 126 127 ZipEntry zipEntry; 129 ZipFile zipFile; 130 try { 131 zipFile = new ZipFile (basePath); 132 zipEntry = zipFile.getEntry("city.xml"); 133 } 134 catch (Exception e) { 135 log.error("Error retrieving records", e); 136 return; 137 } 138 139 if (zipFile.size() > 0 && zipEntry != null) { 141 try { 142 BufferedInputStream dictionaryStream = 143 new BufferedInputStream ( 144 zipFile.getInputStream(zipEntry)); 145 XMLDecoder xDecoder = new XMLDecoder (dictionaryStream); 146 cityList = (List ) xDecoder.readObject(); 148 dictionaryStream.close(); 149 zipFile.close(); 150 xDecoder.close(); 151 } catch (ArrayIndexOutOfBoundsException e) { 152 log.error("Error getting city list, not city objects", e); 153 return; 154 } catch (IOException e) { 155 log.error("Error getting city list", e); 156 return; 157 } 158 } 159 160 if (cityList != null) { 162 dictionary = new ArrayList (cityList.size()); 163 City tmpCity; 164 for (int i = 0, max = cityList.size(); i < max; i++) { 165 tmpCity = (City) cityList.get(i); 166 if (tmpCity != null && tmpCity.getCity() != null) { 167 dictionary.add(new SelectItem(tmpCity, tmpCity.getCity())); 168 } 169 } 170 cityList.clear(); 171 Collections.sort(dictionary, LABEL_COMPARATOR); 173 } 174 175 } 176 } | Popular Tags |