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.reverendfun.util;
24  
25  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
26  import com.liferay.portal.kernel.util.StringComparator;
27  import com.liferay.portal.util.WebCacheable;
28  import com.liferay.util.ConverterException;
29  import com.liferay.util.Http;
30  import com.liferay.util.Time;
31  
32  import java.text.DateFormat;
33  import java.text.SimpleDateFormat;
34  
35  import java.util.Calendar;
36  import java.util.Set;
37  import java.util.TreeSet;
38  
39  /**
40   * <a href="ReverendFunConverter.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class ReverendFunConverter implements WebCacheable {
46  
47      public ReverendFunConverter(String date) {
48          _date = date;
49      }
50  
51      public Object convert(String id) throws ConverterException {
52          Set dates = new TreeSet(new StringComparator(false, true));
53  
54          try {
55              DateFormat dateFormatYMD = new SimpleDateFormat("yyyyMMdd");
56              DateFormat dateFormatYM = new SimpleDateFormat("yyyyMM");
57  
58              Calendar cal = CalendarFactoryUtil.getCalendar();
59  
60              cal.setTime(dateFormatYMD.parse(_date));
61              cal.set(Calendar.DATE, 1);
62  
63              Calendar now = CalendarFactoryUtil.getCalendar();
64  
65              String url = "http://www.reverendfun.com/artchives/?search=";
66  
67              while (cal.before(now)) {
68                  String text = Http.URLtoString(
69                      url + dateFormatYM.format(cal.getTime()));
70  
71                  int x = text.indexOf("date=");
72                  int y = text.indexOf("\"", x);
73  
74                  while (x != -1 && y != -1) {
75                      String fromDateString = text.substring(x + 5, y);
76  
77                      dates.add(fromDateString);
78  
79                      x = text.indexOf("date=", y);
80                      y = text.indexOf("\"", x);
81                  }
82  
83                  cal.add(Calendar.MONTH, 1);
84              }
85          }
86          catch (Exception e) {
87              throw new ConverterException(_date + " " + e.toString());
88          }
89  
90          return dates;
91      }
92  
93      public long getRefreshTime() {
94          return _REFRESH_TIME;
95      }
96  
97      private static final long _REFRESH_TIME = Time.DAY;
98  
99      private String _date;
100 
101 }