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.randombibleverse.util;
022:
023: import com.liferay.portal.kernel.util.StringUtil;
024: import com.liferay.portal.kernel.webcache.WebCacheException;
025: import com.liferay.portal.kernel.webcache.WebCacheItem;
026: import com.liferay.portlet.randombibleverse.model.Verse;
027: import com.liferay.util.Html;
028: import com.liferay.util.Http;
029: import com.liferay.util.HttpUtil;
030: import com.liferay.util.Time;
031:
032: /**
033: * <a href="VerseWebCacheItem.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Brian Wing Shun Chan
036: *
037: */
038: public class VerseWebCacheItem implements WebCacheItem {
039:
040: public VerseWebCacheItem(String location, String versionId) {
041: _location = location;
042: _versionId = versionId;
043: }
044:
045: public Object convert(String key) throws WebCacheException {
046: Verse verse = null;
047:
048: try {
049: String url = "http://www.biblegateway.com/passage/?search="
050: + HttpUtil.encodeURL(_location) + "&version="
051: + _versionId;
052:
053: String text = Http.URLtoString(url);
054:
055: int x = text.indexOf("result-text-style-normal");
056: x = text.indexOf(">", x);
057:
058: int y = text.indexOf("</div>", x);
059:
060: text = text.substring(x + 1, y);
061:
062: y = text.indexOf("Footnotes:");
063:
064: if (y != -1) {
065: text = text.substring(0, y);
066: }
067:
068: // Strip everything between <span> and </span>
069:
070: text = Html.stripBetween(text, "span");
071:
072: // Strip everything between <sup> and </sup>
073:
074: text = Html.stripBetween(text, "sup");
075:
076: // Strip everything between <h4> and </h4>
077:
078: text = Html.stripBetween(text, "h4");
079:
080: // Strip everything between <h5> and </h5>
081:
082: text = Html.stripBetween(text, "h5");
083:
084: // Strip HTML
085:
086: text = Html.stripHtml(text).trim();
087:
088: // Strip
089:
090: text = StringUtil.replace(text, " ", "");
091:
092: // Strip carriage returns
093:
094: text = StringUtil.replace(text, "\n", "");
095:
096: // Strip double spaces
097:
098: while (text.indexOf(" ") != -1) {
099: text = StringUtil.replace(text, " ", " ");
100: }
101:
102: // Replace " with "
103:
104: text = StringUtil.replace(text, "\"", """);
105:
106: // Trim
107:
108: text = text.trim();
109:
110: verse = new Verse(_location, text);
111: } catch (Exception e) {
112: throw new WebCacheException(_location + " " + _versionId
113: + " " + e.toString());
114: }
115:
116: return verse;
117: }
118:
119: public long getRefreshTime() {
120: return _REFRESH_TIME;
121: }
122:
123: private static final long _REFRESH_TIME = Time.WEEK * 52;
124:
125: private String _location;
126: private String _versionId;
127:
128: }
|