KickJava   Java API By Example, From Geeks To Geeks.

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


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 TangDateComicsSystem extends ComicsSystem
40 {
41     static String JavaDoc filetypes[] = new String JavaDoc[] {".gif", ".png", ".jpg"};
42     static String JavaDoc suffixes[] = new String JavaDoc[] {"", "a", "b", "c", "d", "e", "f", "g", "h", "i"};
43   static SimpleDateFormat JavaDoc currFormatter = new SimpleDateFormat JavaDoc("yyyy-MM-dd");
44
45     public void update(String JavaDoc arguments[])
46     {
47         try
48         {
49             String JavaDoc
50                 prefix = arguments[1],
51                 dateFormat = "",
52                 suffix = "";
53
54         if (arguments.length >= 3)dateFormat = arguments[2];
55             if (arguments.length >= 4)suffix = arguments[3];
56
57             SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(dateFormat);
58
59         // 1) get current position
60
// loop
61
// 2) extrapolate URL
62
// 3) check if URL exists, if it does, add it and update comic
63

64             Calendar JavaDoc c = Calendar.getInstance();
65             {
66                 // set todays time to exact start of the day
67
int
68                     y = c.get(Calendar.YEAR),
69                     m = c.get(Calendar.MONTH),
70                     d = c.get(Calendar.DATE);
71                 c.clear();
72                 c.add(Calendar.DATE, 1);
73                 c.set(y, m, d);
74             }
75             Date JavaDoc
76                 today = c.getTime(),
77                 curr;
78
79             curr = currFormatter.parse(comic.position);
80             c.setTime(curr);
81             c.add(Calendar.DATE, 1);
82             curr = c.getTime();
83
84             int count = 0;
85
86             // add one day, check if file exists, etc...
87
while (curr.getTime() < today.getTime())
88             {
89                 suffixes: for (int j = 0; j < suffixes.length; j++)
90                 {
91                     for (int i = 0; i < filetypes.length; i++)
92                     {
93                         URL JavaDoc url = new URL JavaDoc(prefix+formatter.format(curr)+suffix+suffixes[j]+filetypes[i]);
94
95                         if (URLExists(url))
96                         {
97                             if (!debug)
98                             {
99                                 log("! ");
100                                 cmgr.addStrip(new Strip(comic.ID, url.toString(), new java.sql.Date JavaDoc(curr.getTime()), formatter.format(curr)+suffixes[j], formatter.format(curr)+suffixes[j]));
101                             }
102                             else
103                                 logln(url.toString());
104
105                             count++;
106                             comic = cmgr.getComic(comic.ID);
107                             comic.position = currFormatter.format(curr);
108
109                             if (!debug)
110                                 cmgr.changeComic(comic);
111
112                             if (j == 0)
113                                 break suffixes;
114
115                             break;
116                         }
117                         else
118                         {
119                             log(". ");
120
121                             if (j == 1 && i == filetypes.length-1) // prefix "a" not found, then there won't be any "b"
122
break suffixes;
123                         }
124                     }
125                 }
126
127                 c.add(Calendar.DATE, 1);
128                 curr = c.getTime();
129             }
130
131             log(count+" strips added in "+comic.name);
132         }
133         catch (Exception JavaDoc e)
134         {
135             e.printStackTrace(new java.io.PrintWriter JavaDoc(out));
136         }
137     }
138 }
Popular Tags