1   /**
2    * Copyright (c) 2000-2007 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.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  /**
35   * <a href="VerseConverter.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
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              // Strip everything between <span> and </span>
71  
72              text = Html.stripBetween(text, "span");
73  
74              // Strip everything between <sup> and </sup>
75  
76              text = Html.stripBetween(text, "sup");
77  
78              // Strip everything between <h4> and </h4>
79  
80              text = Html.stripBetween(text, "h4");
81  
82              // Strip everything between <h5> and </h5>
83  
84              text = Html.stripBetween(text, "h5");
85  
86              // Strip HTML
87  
88              text = Html.stripHtml(text).trim();
89  
90              // Strip &nbsp;
91  
92              text = StringUtil.replace(text, "&nbsp;", "");
93  
94              // Strip carriage returns
95  
96              text = StringUtil.replace(text, "\n", "");
97  
98              // Strip double spaces
99  
100             while (text.indexOf("  ") != -1) {
101                 text = StringUtil.replace(text, "  ", " ");
102             }
103 
104             // Replace " with &quot;
105 
106             text = StringUtil.replace(text, "\"", "&quot;");
107 
108             // Trim
109 
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 }