KickJava   Java API By Example, From Geeks To Geeks.

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


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, minimumNumberOfDigits, 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.DecimalFormat JavaDoc;
36
37 public class NumberedComicsSystem extends ComicsSystem
38 {
39     public void update(String JavaDoc arguments[])
40     {
41         try
42         {
43             String JavaDoc
44                 prefix = arguments[1],
45                 minimumNumberOfDigits = "1",
46                 suffix = "";
47
48             String JavaDoc filetypes[] = new String JavaDoc[] {".gif", ".png", ".jpg"};
49
50             long endPos = -1;
51             int skipTolerance = 0;
52
53             if (arguments.length >= 3)minimumNumberOfDigits = arguments[2];
54             if (arguments.length >= 4)suffix = arguments[3].trim();
55             if (arguments.length >= 5)endPos = Long.parseLong(arguments[4]);
56             if (arguments.length >= 6)
57             {
58                 filetypes = getArguments(arguments[5], "\n\r ,");
59                 if (filetypes.length == 0)
60                     filetypes = new String JavaDoc[] {""};
61             }
62             if (arguments.length >= 7)skipTolerance = Integer.parseInt(arguments[6]);
63
64             // 1) get current position
65
// loop
66
// 2) extrapolate URL
67
// 3) check if URL exists, if it does, add it and update comic
68

69             long curr = Long.parseLong(comic.position)+1;
70
71             int count = 0;
72
73             // add one day, check if file exists, etc...
74

75             URL JavaDoc url;
76             DecimalFormat JavaDoc formatter = new DecimalFormat JavaDoc("#######################################################");
77             formatter.setDecimalSeparatorAlwaysShown(false);
78             //formatter.setGroupingSize(0xFFFFFFF); // no digit grouping
79
formatter.setMinimumIntegerDigits(Integer.parseInt(minimumNumberOfDigits));
80
81             for (int skip = 0; skip <= skipTolerance; curr++)
82             //for (boolean found = true; found; curr++)
83
{
84                 if (endPos != -1 && curr >= endPos)
85                     break;
86
87                 skip++;
88                 for (int i = 0; i < filetypes.length; i++)
89                 {
90                     url = new URL JavaDoc(prefix+formatter.format(curr)+suffix+filetypes[i]);
91
92                     if (URLExists(url))
93                     {
94                         if (!debug)
95                         {
96                             log("! ");
97
98                             cmgr.addStrip(new Strip(comic.ID, url.toString(), new java.sql.Date JavaDoc(curr), Long.toString(curr), Long.toString(curr)));
99                         }
100                         else
101                             logln(url.toString());
102
103                         comic = cmgr.getComic(comic.ID);
104                         comic.position = Long.toString(curr);
105                         if (!debug)
106                             cmgr.changeComic(comic);
107                         count++;
108                         skip = 0;
109                         break;
110                     }
111                     else
112                         log(". ");
113                 }
114             }
115             logln(count+" strips added in "+comic.name);
116         }
117         catch (Exception JavaDoc e)
118         {
119             e.printStackTrace(new java.io.PrintWriter JavaDoc(out));
120         }
121     }
122 }
Popular Tags