1
19
20 package com.liferay.portlet.randombibleverse.util;
21
22 import com.liferay.portal.kernel.util.HtmlUtil;
23 import com.liferay.portal.kernel.util.HttpUtil;
24 import com.liferay.portal.kernel.util.StringUtil;
25 import com.liferay.portal.kernel.util.Time;
26 import com.liferay.portal.kernel.webcache.WebCacheException;
27 import com.liferay.portal.kernel.webcache.WebCacheItem;
28 import com.liferay.portlet.randombibleverse.model.Verse;
29
30
36 public class VerseWebCacheItem implements WebCacheItem {
37
38 public VerseWebCacheItem(String location, String versionId) {
39 _location = location;
40 _versionId = versionId;
41 }
42
43 public Object convert(String key) throws WebCacheException {
44 Verse verse = null;
45
46 try {
47 String url =
48 "http://www.biblegateway.com/passage/?search=" +
49 HttpUtil.encodeURL(_location) + "&version=" + _versionId;
50
51 String text = HttpUtil.URLtoString(url);
52
53 int x = text.indexOf("result-text-style");
54 x = text.indexOf(">", x);
55
56 int y = text.indexOf("</div>", x);
57
58 text = text.substring(x + 1, y);
59
60 y = text.indexOf("Footnotes:");
61
62 if (y != -1) {
63 text = text.substring(0, y);
64 }
65 else {
66 y = text.indexOf("Cross references:");
67
68 if (y != -1) {
69 text = text.substring(0, y);
70 }
71 }
72
73
75 text = HtmlUtil.stripBetween(text, "span");
76
77
79 text = HtmlUtil.stripBetween(text, "sup");
80
81
83 text = HtmlUtil.stripBetween(text, "h4");
84
85
87 text = HtmlUtil.stripBetween(text, "h5");
88
89
91 text = HtmlUtil.stripHtml(text).trim();
92
93
95 text = StringUtil.replace(text, " ", "");
96
97
99 text = StringUtil.replace(text, "\n", "");
100
101
103 while (text.indexOf(" ") != -1) {
104 text = StringUtil.replace(text, " ", " ");
105 }
106
107
109 text = StringUtil.replace(text, "\"", """);
110
111
113 text = text.trim();
114
115 verse = new Verse(_location, text);
116 }
117 catch (Exception e) {
118 throw new WebCacheException(
119 _location + " " + _versionId + " " + e.toString());
120 }
121
122 return verse;
123 }
124
125 public long getRefreshTime() {
126 return _REFRESH_TIME;
127 }
128
129 private static final long _REFRESH_TIME = Time.WEEK * 52;
130
131 private String _location;
132 private String _versionId;
133
134 }