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              else {
69                  y = text.indexOf("Cross references:");
70  
71                  if (y != -1) {
72                      text = text.substring(0, y);
73                  }
74              }
75  
76              // Strip everything between <span> and </span>
77  
78              text = HtmlUtil.stripBetween(text, "span");
79  
80              // Strip everything between <sup> and </sup>
81  
82              text = HtmlUtil.stripBetween(text, "sup");
83  
84              // Strip everything between <h4> and </h4>
85  
86              text = HtmlUtil.stripBetween(text, "h4");
87  
88              // Strip everything between <h5> and </h5>
89  
90              text = HtmlUtil.stripBetween(text, "h5");
91  
92              // Strip HTML
93  
94              text = HtmlUtil.stripHtml(text).trim();
95  
96              // Strip &nbsp;
97  
98              text = StringUtil.replace(text, "&nbsp;", "");
99  
100             // Strip carriage returns
101 
102             text = StringUtil.replace(text, "\n", "");
103 
104             // Strip double spaces
105 
106             while (text.indexOf("  ") != -1) {
107                 text = StringUtil.replace(text, "  ", " ");
108             }
109 
110             // Replace " with &quot;
111 
112             text = StringUtil.replace(text, "\"", "&quot;");
113 
114             // Trim
115 
116             text = text.trim();
117 
118             verse = new Verse(_location, text);
119         }
120         catch (Exception e) {
121             throw new WebCacheException(
122                 _location + " " + _versionId + " " + e.toString());
123         }
124 
125         return verse;
126     }
127 
128     public long getRefreshTime() {
129         return _REFRESH_TIME;
130     }
131 
132     private static final long _REFRESH_TIME = Time.WEEK * 52;
133 
134     private String _location;
135     private String _versionId;
136 
137 }