1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
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  /**
31   * <a href="VerseWebCacheItem.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   *
35   */
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              // Strip everything between <span> and </span>
74  
75              text = HtmlUtil.stripBetween(text, "span");
76  
77              // Strip everything between <sup> and </sup>
78  
79              text = HtmlUtil.stripBetween(text, "sup");
80  
81              // Strip everything between <h4> and </h4>
82  
83              text = HtmlUtil.stripBetween(text, "h4");
84  
85              // Strip everything between <h5> and </h5>
86  
87              text = HtmlUtil.stripBetween(text, "h5");
88  
89              // Strip HTML
90  
91              text = HtmlUtil.stripHtml(text).trim();
92  
93              // Strip &nbsp;
94  
95              text = StringUtil.replace(text, "&nbsp;", "");
96  
97              // Strip carriage returns
98  
99              text = StringUtil.replace(text, "\n", "");
100 
101             // Strip double spaces
102 
103             while (text.indexOf("  ") != -1) {
104                 text = StringUtil.replace(text, "  ", " ");
105             }
106 
107             // Replace " with &quot;
108 
109             text = StringUtil.replace(text, "\"", "&quot;");
110 
111             // Trim
112 
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 }