001: /*
002: *
003: * Copyright (c) 2007, Sun Microsystems, Inc.
004: *
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * * Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * * Neither the name of Sun Microsystems nor the names of its contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032: package examples.cityguide;
033:
034: import java.io.IOException;
035: import java.io.InputStream;
036: import java.io.InputStreamReader;
037:
038: import java.util.Enumeration;
039: import java.util.Hashtable;
040: import java.util.NoSuchElementException;
041: import java.util.Vector;
042:
043: import javax.microedition.lcdui.Gauge;
044: import javax.microedition.lcdui.Image;
045: import javax.microedition.location.AddressInfo;
046: import javax.microedition.location.Landmark;
047: import javax.microedition.location.LandmarkStore;
048: import javax.microedition.location.QualifiedCoordinates;
049:
050: /**
051: * general utilities for CityGuide
052: */
053: public class Util {
054: /**
055: * This method reads landmarks from InputStream.
056: * Landmarks are stored in text file as values with delimiter ","
057: * Method updates progress bar.
058: *
059: * @param is character stream containing records of landmarks
060: * @param progressGauge if not null external progress indicator is updated.
061: */
062: public static final void readLandmarksFromStream(
063: LandmarkStore store, InputStream is, Gauge progressGauge)
064: throws IOException {
065: Landmark landmark;
066: int count = 0;
067: int nchars = -1;
068: char[] buffer = new char[10]; //slowdown reading
069: InputStreamReader isr = new InputStreamReader(is);
070: StringBuffer record = new StringBuffer();
071:
072: do {
073: nchars = isr.read(buffer);
074:
075: for (int i = 0; i < nchars; i++) {
076: if (buffer[i] == '\n') {
077: addLandmarkFromString(store, record.toString());
078:
079: if (progressGauge != null) {
080: progressGauge.setValue(count++);
081: }
082:
083: record = new StringBuffer();
084: } else {
085: record.append(buffer[i]);
086: }
087: }
088: } while (nchars == buffer.length);
089:
090: addLandmarkFromString(store, record.toString());
091: progressGauge.setValue(count++);
092: }
093:
094: /**
095: * This method creates landmark from values in String record.
096: * Values are separated by comma. Sequence of the values is firm.
097: * Latitude, Longitude, Altitude,Name, Description, Street & house number,
098: * City, Post Code, Phone Number, Category
099: *
100: * @return landmark
101: * @param record comma separated values
102: */
103: public static final void addLandmarkFromString(LandmarkStore store,
104: String record) throws IOException {
105: Landmark landmark = null;
106:
107: try {
108: Enumeration e = parseSVRecord(record, ',');
109:
110: if (!e.hasMoreElements()) {
111: return;
112: }
113:
114: double xCoord = Double
115: .parseDouble((String) e.nextElement());
116: double yCoord = Double
117: .parseDouble((String) e.nextElement());
118: float altitude = Float.parseFloat((String) e.nextElement());
119: String name = (String) e.nextElement();
120: String description = (String) e.nextElement();
121: String street = (String) e.nextElement();
122: String pcode = (String) e.nextElement();
123: String phone = (String) e.nextElement();
124: String city = (String) e.nextElement();
125: String category = (String) e.nextElement();
126:
127: //create coordinates
128: QualifiedCoordinates coords = new QualifiedCoordinates(
129: xCoord, yCoord, altitude, 0, 0);
130:
131: //create address
132: AddressInfo address = new AddressInfo();
133: address.setField(AddressInfo.STREET, street);
134: address.setField(AddressInfo.CITY, city);
135: address.setField(AddressInfo.POSTAL_CODE, pcode);
136: address.setField(AddressInfo.PHONE_NUMBER, phone);
137:
138: landmark = new Landmark(name, description, coords, address);
139:
140: Enumeration c = store.getCategories();
141: boolean exists;
142:
143: for (exists = false; c.hasMoreElements();)
144: if (category.equals((String) c.nextElement())) {
145: exists = true;
146:
147: break;
148: }
149:
150: if (!exists) {
151: store.addCategory(category);
152: }
153:
154: store.addLandmark(landmark, category);
155: } catch (NoSuchElementException e) {
156: throw new IOException(
157: "Incorrect number of fields in landmark record.");
158: } catch (Exception e) {
159: throw new IOException("Cannot add landmark.");
160: }
161: }
162:
163: /**
164: * This method parses separated values from record.
165: *
166: * @return String values from record as Enumeration
167: * @param str record containing separated values
168: * @param delimiter values delimiter char
169: */
170: public static final Enumeration parseSVRecord(String str,
171: char delimiter) {
172: int length = str.length();
173: int beginindex = 0;
174: Vector v = new Vector();
175:
176: for (int i = 0; i < length; i++) {
177: if (str.charAt(i) == delimiter) {
178: v.addElement(str.substring(beginindex, i).trim());
179: beginindex = i + 1;
180: }
181:
182: if (i == (length - 1)) {
183: v.addElement(str.substring(beginindex, length).trim());
184: }
185: }
186:
187: return v.elements();
188: }
189:
190: /**
191: *
192: */
193: public static final String[] asArray(Enumeration e) {
194: Vector v = new Vector();
195: v.copyInto(new String[v.size()]);
196:
197: for (; e.hasMoreElements();)
198: v.addElement(e.nextElement());
199:
200: String[] array = new String[v.size()];
201: Object o;
202: v.copyInto(array);
203:
204: return array;
205: }
206: }
|