1
22
23 package com.liferay.portlet.randombibleverse.util;
24
25 import com.liferay.portal.kernel.util.StringUtil;
26 import com.liferay.portal.util.WebCacheable;
27 import com.liferay.portlet.randombibleverse.model.Verse;
28 import com.liferay.util.ConverterException;
29 import com.liferay.util.Html;
30 import com.liferay.util.Http;
31 import com.liferay.util.HttpUtil;
32 import com.liferay.util.Time;
33
34
40 public class VerseConverter implements WebCacheable {
41
42 public VerseConverter(String location, String versionId) {
43 _location = location;
44 _versionId = versionId;
45 }
46
47 public Object convert(String id) throws ConverterException {
48 Verse verse = null;
49
50 try {
51 String url =
52 "http://www.biblegateway.com/passage/?search=" +
53 HttpUtil.encodeURL(_location) + "&version=" + _versionId;
54
55 String text = Http.URLtoString(url);
56
57 int x = text.indexOf("result-text-style-normal");
58 x = text.indexOf(">", x);
59
60 int y = text.indexOf("</div>", x);
61
62 text = text.substring(x + 1, y);
63
64 y = text.indexOf("Footnotes:");
65
66 if (y != -1) {
67 text = text.substring(0, y);
68 }
69
70
72 text = Html.stripBetween(text, "span");
73
74
76 text = Html.stripBetween(text, "sup");
77
78
80 text = Html.stripBetween(text, "h4");
81
82
84 text = Html.stripBetween(text, "h5");
85
86
88 text = Html.stripHtml(text).trim();
89
90
92 text = StringUtil.replace(text, " ", "");
93
94
96 text = StringUtil.replace(text, "\n", "");
97
98
100 while (text.indexOf(" ") != -1) {
101 text = StringUtil.replace(text, " ", " ");
102 }
103
104
106 text = StringUtil.replace(text, "\"", """);
107
108
110 text = text.trim();
111
112 verse = new Verse(_location, text);
113 }
114 catch (Exception e) {
115 throw new ConverterException(
116 _location + " " + _versionId + " " + e.toString());
117 }
118
119 return verse;
120 }
121
122 public long getRefreshTime() {
123 return _REFRESH_TIME;
124 }
125
126 private static final long _REFRESH_TIME = Time.WEEK * 52;
127
128 private String _location;
129 private String _versionId;
130
131 }