KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > icefaces > samples > showcase > components > autocomplete > AutoCompleteDictionary


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

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 JavaDoc;
42 import java.beans.XMLDecoder JavaDoc;
43 import java.io.BufferedInputStream JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Collections JavaDoc;
47 import java.util.Comparator JavaDoc;
48 import java.util.List JavaDoc;
49 import java.util.zip.ZipEntry JavaDoc;
50 import java.util.zip.ZipFile JavaDoc;
51
52
53 /**
54  * Application-scope bean used to store static lookup information for
55  * AutoComplete (selectInputText) example. Statically referenced by
56  * AutoCompleteBean as the dictionary is rather large.
57  *
58  * @see AutoCompleteBean
59  */

60 public class AutoCompleteDictionary {
61
62     private static Log log = LogFactory.getLog(AutoCompleteDictionary.class);
63
64     // list of cities.
65
private static List JavaDoc dictionary;
66
67     public AutoCompleteDictionary() {
68         // initialize the ditionary
69
try {
70             log.info("initializing dictionary");
71             init();
72         } catch (Exception JavaDoc e) {
73             if (log.isErrorEnabled()) {
74                 log.error("Error initializtin sorting list");
75             }
76         }
77     }
78
79     /**
80      * Comparator utility for sorting city names.
81      */

82     public static final Comparator JavaDoc LABEL_COMPARATOR = new Comparator JavaDoc() {
83         String JavaDoc s1;
84         String JavaDoc s2;
85
86         // compare method for city entries.
87
public int compare(Object JavaDoc o1, Object JavaDoc 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             // compare ingnoring case, give the user a more automated feel when typing
101
return s1.compareToIgnoreCase(s2);
102         }
103     };
104
105     /**
106      * Gets the dictionary of cities.
107      *
108      * @return dictionary list in sorted by city name, ascending.
109      */

110     public List JavaDoc getDictionary() {
111         return dictionary;
112     }
113
114     private static void init() {
115         // Raw list of xml cities.
116
List JavaDoc cityList = null;
117
118         // load the city dictionary from the compressed xml file.
119

120         // get the path of the compressed file
121
HttpSession JavaDoc session = (HttpSession JavaDoc) FacesContext.getCurrentInstance().
122                 getExternalContext().getSession(true);
123         String JavaDoc basePath =
124                 session.getServletContext().getRealPath("/WEB-INF/resources");
125         basePath += "/city.xml.zip";
126
127         // extract the file
128
ZipEntry JavaDoc zipEntry;
129         ZipFile JavaDoc zipFile;
130         try {
131             zipFile = new ZipFile JavaDoc(basePath);
132             zipEntry = zipFile.getEntry("city.xml");
133         }
134         catch (Exception JavaDoc e) {
135             log.error("Error retrieving records", e);
136             return;
137         }
138
139         // get the xml stream and decode it.
140
if (zipFile.size() > 0 && zipEntry != null) {
141             try {
142                 BufferedInputStream JavaDoc dictionaryStream =
143                         new BufferedInputStream JavaDoc(
144                                 zipFile.getInputStream(zipEntry));
145                 XMLDecoder JavaDoc xDecoder = new XMLDecoder JavaDoc(dictionaryStream);
146                 // get the city list.
147
cityList = (List JavaDoc) xDecoder.readObject();
148                 dictionaryStream.close();
149                 zipFile.close();
150                 xDecoder.close();
151             } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
152                 log.error("Error getting city list, not city objects", e);
153                 return;
154             } catch (IOException JavaDoc e) {
155                 log.error("Error getting city list", e);
156                 return;
157             }
158         }
159
160         // Finally load the object from the xml file.
161
if (cityList != null) {
162             dictionary = new ArrayList JavaDoc(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             // finally sort the list
172
Collections.sort(dictionary, LABEL_COMPARATOR);
173         }
174
175     }
176 }
Popular Tags