001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.cszsearch.util;
022:
023: import com.liferay.portal.kernel.util.StringPool;
024: import com.liferay.portal.kernel.webcache.WebCacheException;
025: import com.liferay.portal.kernel.webcache.WebCacheItem;
026: import com.liferay.portlet.cszsearch.model.CSZAddress;
027: import com.liferay.util.Html;
028: import com.liferay.util.Http;
029: import com.liferay.util.TextFormatter;
030: import com.liferay.util.Time;
031:
032: import java.io.BufferedReader;
033: import java.io.StringReader;
034:
035: import java.util.ArrayList;
036: import java.util.List;
037:
038: /**
039: * <a href="ZipWebCacheItem.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Brian Wing Shun Chan
042: *
043: */
044: public class ZipWebCacheItem implements WebCacheItem {
045:
046: public ZipWebCacheItem(String zip) {
047: _zip = zip;
048: }
049:
050: public Object convert(String key) throws WebCacheException {
051: List list = new ArrayList();
052:
053: String city = null;
054: String state = null;
055: String zip = _zip;
056:
057: try {
058: String text = Http
059: .URLtoString("http://zip4.usps.com/zip4/zcl_3_results.jsp?zip5="
060: + zip);
061:
062: BufferedReader br = new BufferedReader(new StringReader(
063: text));
064:
065: String line = null;
066:
067: while ((line = br.readLine()) != null) {
068: line = line.trim();
069:
070: if (!line.equals("")) {
071: if (line
072: .indexOf("<h2 style=\"color:#CC0000;\">Not Acceptable") > -1) {
073:
074: break;
075: } else if (line
076: .indexOf("<td valign=\"top\" class=\"main\"") > -1) {
077:
078: String cityAndState = Html.stripHtml(line)
079: .trim();
080:
081: int pos = cityAndState
082: .indexOf(StringPool.COMMA);
083:
084: city = cityAndState.substring(0, pos);
085: state = cityAndState.substring(pos + 1,
086: cityAndState.length()).trim();
087:
088: if (city != null && state != null) {
089: list.add(new CSZAddress(null, TextFormatter
090: .formatName(city), state, zip));
091:
092: city = null;
093: state = null;
094: }
095: }
096: }
097: }
098:
099: br.close();
100: } catch (Exception e) {
101: throw new WebCacheException(e);
102: }
103:
104: return list;
105: }
106:
107: public long getRefreshTime() {
108: return _REFRESH_TIME;
109: }
110:
111: private static final long _REFRESH_TIME = Time.DAY * 90;
112:
113: private String _zip;
114:
115: }
|