1   /**
2    * Copyright (c) 2000-2008 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.todayinchristianhistory.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.webcache.WebCacheException;
29  import com.liferay.portal.kernel.webcache.WebCacheItem;
30  import com.liferay.portlet.todayinchristianhistory.model.Event;
31  import com.liferay.util.Time;
32  
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  /**
37   * <a href="EventsWebCacheItem.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class EventsWebCacheItem implements WebCacheItem {
43  
44      public Object convert(String key) throws WebCacheException {
45          List<Event> events = new ArrayList<Event>();
46  
47          try {
48              String text = HttpUtil.URLtoString(
49                  "http://www.studylight.org/his/tich");
50  
51              int x = text.indexOf("<table cellpadding=3 cellspacing=3>");
52              x = text.indexOf("<tr>", x);
53  
54              int y = text.indexOf("<tr><td align=center", x);
55  
56              text = HtmlUtil.stripComments(text.substring(x, y)).trim();
57  
58              String[] array = StringUtil.split(text, "<tr>");
59  
60              for (int i = 0; i < array.length; i++) {
61                  x = array[i].indexOf("<b>");
62                  y = array[i].indexOf("</b>");
63  
64                  if (x != -1 && y != -1) {
65                      String year = array[i].substring(x + 3, y).trim();
66  
67                      String description = HtmlUtil.extractText(
68                          array[i].substring(y, array[i].length())).trim();
69  
70                      if (description.startsWith("-  ")) {
71                          description = description.substring(
72                              3, description.length());
73                      }
74  
75                      events.add(new Event(Integer.parseInt(year), description));
76                  }
77              }
78          }
79          catch (Exception e) {
80              throw new WebCacheException(e);
81          }
82  
83          return events;
84      }
85  
86      public long getRefreshTime() {
87          return _REFRESH_TIME;
88      }
89  
90      private static final long _REFRESH_TIME = Time.HOUR;
91  
92  }