1 /***
2 * Created by IntelliJ IDEA.
3 * User: Lennart
4 * Date: 22-nov-2003
5 * Time: 15:48:57
6 */
7 package comics.core;
8
9 import java.net.URL;
10 import java.net.MalformedURLException;
11 import java.util.Calendar;
12 import java.util.Date;
13 import java.util.GregorianCalendar;
14 import java.util.Properties;
15 import java.text.SimpleDateFormat;
16 import java.io.InputStream;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19
20 /*
21 * CVS information:
22 *
23 * $Revision: 1.2 $
24 * $Date: 2003/12/11 12:08:31 $
25 */
26
27 /***
28 * This class creates URLs based on the specified comic provider, comic name and date.
29 *
30 * @author Lennart Martens
31 */
32 public class ComicURLFactory {
33
34 /***
35 * The comic provider '<a href="http://www.ucomics.com">UComics</a>'.
36 */
37 public static final String UCOMICS = "UCOMICS";
38
39 /***
40 * UComics date formatter.
41 */
42 private static final SimpleDateFormat iUComicsSDF = new SimpleDateFormat("yyMMdd");
43 /***
44 * UComics archive main URL.
45 */
46 private static String iUComicsSource = null;
47
48
49 private static final String UCOMICS_PROPERTIES = "ucomics.properties";
50
51 /***
52 * Read the URL from the properties file.
53 */
54 static{
55 try {
56 InputStream is = ComicURLFactory.class.getClassLoader().getResourceAsStream(UCOMICS_PROPERTIES);
57 if(is == null) {
58 throw new FileNotFoundException("You need to have a file called '" + UCOMICS_PROPERTIES + "' in the classpath!");
59 } else {
60 Properties props = new Properties();
61 props.load(is);
62 String url = props.getProperty("url");
63 if(url == null || url.trim().equals("")) {
64 throw new IOException("The '" + UCOMICS_PROPERTIES + "' file did not contain the 'url' key, or that key did not have a value!");
65 } else {
66 iUComicsSource = url;
67 }
68 }
69 } catch(Exception e) {
70 System.err.println("\n\nUnable to load UComics comics URL from the '" + UCOMICS_PROPERTIES + "' file:\n\n\t" + e.getMessage() +"\n\n");
71 System.exit(1);
72 }
73 }
74
75
76 /***
77 * This factory method creates a comic retrieval URL, based on the specified comic provider
78 * (eg. UComics), comic name (eg. 'ga' for Garfield at UComics) and date.
79 *
80 * @param aComicProvider String with the comic provider. Please restrict this parameter to
81 * the constants defined on this class. They constitute the only supported
82 * comic providers.
83 * @param aComicName String with the name of the comic, this name is specific for the provider
84 * @param aDate Calendar with the date for which the specified comic should be retrieved.
85 * @return URL pointing to the desired comic image.
86 */
87 public static URL createURL(String aComicProvider, String aComicName, Calendar aDate) {
88 URL url = null;
89 // See which url we should be generating.
90 if(aComicProvider.equals(UCOMICS)) {
91 // UComics URL.
92 url = createUComicsURL(aComicName, aDate);
93 } else {
94 throw new IllegalArgumentException("Unknown provider '" + aComicProvider + "'. Please restrict yourself to the constants defined on this class!");
95 }
96
97 return url;
98 }
99
100 /***
101 * This factory method creates a comic retrieval URL, based on the specified comic provider
102 * (eg. UComics), comic name (eg. 'ga' for Garfield at UComics) and date (Date class is converted
103 * to a Calendar class through a GregorianCalendar).
104 *
105 * @param aComicProvider String with the comic provider. Please restrict this parameter to
106 * the constants defined on this class. They constitute the only supported
107 * comic providers.
108 * @param aComicName String with the name of the comic, this name is specific for the provider
109 * @param aDate Date with the date for which the specified comic should be retrieved. This Date will be
110 * converted to a GregorianCalendar via the setTime method on GregorianCalendar.
111 * @return URL pointing to the desired comic image.
112 */
113 public static URL createURL(String aComicProvider, String aComicName, Date aDate) {
114 GregorianCalendar gc = new GregorianCalendar();
115 gc.setTime(aDate);
116 return createURL(aComicProvider, aComicName, gc);
117 }
118
119 /***
120 * This method creates a URL for comics provided by UComics.
121 *
122 * @param aComicName String with the name of the comic to retrieve, for UComics
123 * this is typically a short code, like 'ga' for Garfield.
124 * @param aDate Calendar with the date for which to retrieve the specified comic.
125 * @return URL pointing to the desired comic image.
126 */
127 private static URL createUComicsURL(String aComicName, Calendar aDate) {
128 URL url = null;
129 try {
130 url = new URL(iUComicsSource + aComicName + "/" + aDate.get(Calendar.YEAR) + "/" + aComicName + iUComicsSDF.format(aDate.getTime()) + ".gif");
131 } catch(MalformedURLException mfue) {
132 // Should only be thrown when the protocol is unknown, yet since the iUComicsSource
133 // is hardcoded and starts with 'http://' no problems are to be expected.
134 mfue.printStackTrace();
135 }
136 return url;
137 }
138 }
This page was automatically generated by Maven