1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.randombibleverse.util;
24  
25  import com.liferay.portal.kernel.util.HtmlUtil;
26  import com.liferay.portal.kernel.util.HttpUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Time;
29  import com.liferay.portal.kernel.webcache.WebCacheException;
30  import com.liferay.portal.kernel.webcache.WebCacheItem;
31  import com.liferay.portlet.randombibleverse.model.Verse;
32  
33  /**
34   * <a href="VerseWebCacheItem.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class VerseWebCacheItem implements WebCacheItem {
40  
41      public VerseWebCacheItem(String location, String versionId) {
42          _location = location;
43          _versionId = versionId;
44      }
45  
46      public Object convert(String key) throws WebCacheException {
47          Verse verse = null;
48  
49          try {
50              String url =
51                  "http://www.biblegateway.com/passage/?search=" +
52                      HttpUtil.encodeURL(_location) + "&version=" + _versionId;
53  
54              String text = HttpUtil.URLtoString(url);
55  
56              int x = text.indexOf("result-text-style");
57              x = text.indexOf(">", x);
58  
59              int y = text.indexOf("</div>", x);
60  
61              text = text.substring(x + 1, y);
62  
63              y = text.indexOf("Footnotes:");
64  
65              if (y != -1) {
66                  text = text.substring(0, y);
67              }
68  
69              // Strip everything between <span> and </span>
70  
71              text = HtmlUtil.stripBetween(text, "span");
72  
73              // Strip everything between <sup> and </sup>
74  
75              text = HtmlUtil.stripBetween(text, "sup");
76  
77              // Strip everything between <h4> and </h4>
78  
79              text = HtmlUtil.stripBetween(text, "h4");
80  
81              // Strip everything between <h5> and </h5>
82  
83              text = HtmlUtil.stripBetween(text, "h5");
84  
85              // Strip HTML
86  
87              text = HtmlUtil.stripHtml(text).trim();
88  
89              // Strip &nbsp;
90  
91              text = StringUtil.replace(text, "&nbsp;", "");
92  
93              // Strip carriage returns
94  
95              text = StringUtil.replace(text, "\n", "");
96  
97              // Strip double spaces
98  
99              while (text.indexOf("  ") != -1) {
100                 text = StringUtil.replace(text, "  ", " ");
101             }
102 
103             // Replace " with &quot;
104 
105             text = StringUtil.replace(text, "\"", "&quot;");
106 
107             // Trim
108 
109             text = text.trim();
110 
111             verse = new Verse(_location, text);
112         }
113         catch (Exception e) {
114             throw new WebCacheException(
115                 _location + " " + _versionId + " " + e.toString());
116         }
117 
118         return verse;
119     }
120 
121     public long getRefreshTime() {
122         return _REFRESH_TIME;
123     }
124 
125     private static final long _REFRESH_TIME = Time.WEEK * 52;
126 
127     private String _location;
128     private String _versionId;
129 
130 }