KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > comics > DateComicsSystem


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 /**
27  * For strips with a simple date-based url.
28  * The arguments will be thisClass, prefix, dateFormat, suffix
29  */

30 package net.killingar.forum.comics;
31
32 import net.killingar.forum.internal.Strip;
33
34 import java.net.URL JavaDoc;
35 import java.text.SimpleDateFormat JavaDoc;
36 import java.util.Calendar JavaDoc;
37 import java.util.Date JavaDoc;
38
39 public class DateComicsSystem extends ComicsSystem
40 {
41     static String JavaDoc filetypes[] = new String JavaDoc[] {".gif", ".png", ".jpg"};
42   static SimpleDateFormat JavaDoc currFormatter = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
43
44     public void update(String JavaDoc arguments[])
45     {
46         try
47         {
48             String JavaDoc
49                 prefix = arguments[1],
50                 dateFormat = "",
51                 suffix = "";
52
53         if (arguments.length >= 3)dateFormat = arguments[2];
54             if (arguments.length >= 4)suffix = arguments[3];
55
56             SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(dateFormat);
57
58         // 1) get current position
59
// loop
60
// 2) extrapolate URL
61
// 3) check if URL exists, if it does, add it and update comic
62

63             Calendar JavaDoc c = Calendar.getInstance();
64             {
65                 // set todays time to exact start of the day
66
int
67                     y = c.get(Calendar.YEAR),
68                     m = c.get(Calendar.MONTH),
69                     d = c.get(Calendar.DATE);
70                 c.clear();
71                 c.add(Calendar.DATE, 1);
72                 c.set(y, m, d);
73             }
74             Date JavaDoc
75                 today = c.getTime(),
76                 curr;
77
78             curr = currFormatter.parse(comic.position);
79             c.setTime(curr);
80             c.add(Calendar.DATE, 1);
81             curr = c.getTime();
82
83             int count = 0;
84
85             // add one day, check if file exists, etc...
86

87             while (curr.before(today))
88             {
89                 for (int i = 0; i < filetypes.length; i++)
90                 {
91                     URL JavaDoc url = new URL JavaDoc(prefix+formatter.format(curr)+suffix+filetypes[i]);
92
93                     if (URLExists(url))
94                     {
95                         if (!debug)
96                         {
97                             log("! ");
98                             cmgr.addStrip(new Strip(comic.ID, url.toString(), new java.sql.Date JavaDoc(curr.getTime()), formatter.format(curr), formatter.format(curr)));
99                         }
100                         else
101                             logln(url.toString());
102
103                         count++;
104                         comic = cmgr.getComic(comic.ID);
105                         comic.position = currFormatter.format(curr);
106                         if (!debug)
107                             cmgr.changeComic(comic);
108                         break;
109                     }
110                     else
111                         log(". ");
112                 }
113
114
115                 c.add(Calendar.DATE, 1);
116                 curr = c.getTime();
117             }
118
119             log(count+" strips added in "+comic.name);
120         }
121         catch (Exception JavaDoc e)
122         {
123             e.printStackTrace(new java.io.PrintWriter JavaDoc(out));
124         }
125     }
126 }
Popular Tags