1
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
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 }