1105df54097ced61acdfabf0d3eb2cf5022440c5
[gofetch.git] / src / be / nikiroo / gofetch / support / BasicSupport.java
1 package be.nikiroo.gofetch.support;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.net.URLConnection;
7 import java.util.List;
8 import java.util.zip.GZIPInputStream;
9
10 import be.nikiroo.gofetch.data.Comment;
11 import be.nikiroo.gofetch.data.Story;
12
13 public abstract class BasicSupport {
14 public enum Type {
15 SLASHDOT,
16 }
17
18 static private String preselector;
19
20 private Type type;
21
22 abstract public List<Story> list() throws IOException;
23
24 abstract public List<Comment> getComments(Story story) throws IOException;
25
26 abstract public String getDescription();
27
28 public String getSelector() {
29 return getSelector(type);
30 }
31
32 public Type getType() {
33 return type;
34 }
35
36 protected void setType(Type type) {
37 this.type = type;
38 }
39
40 /**
41 * @param preselector
42 * the preselector to set
43 */
44 static public void setPreselector(String preselector) {
45 BasicSupport.preselector = preselector;
46 }
47
48 static public BasicSupport getSupport(Type type) {
49 BasicSupport support = null;
50
51 if (type != null) {
52 switch (type) {
53 case SLASHDOT:
54 support = new Slashdot();
55 break;
56 }
57
58 if (support != null) {
59 support.setType(type);
60 }
61 }
62
63 return support;
64 }
65
66 static public String getSelector(Type type) {
67 return preselector + "/" + type + "/";
68 }
69
70 // TODO: check Downloader.java?
71 static protected InputStream open(URL url) throws IOException {
72 URLConnection conn = url.openConnection();
73 conn.connect();
74 InputStream in = conn.getInputStream();
75 if ("gzip".equals(conn.getContentEncoding())) {
76 in = new GZIPInputStream(in);
77 }
78
79 return in;
80 }
81 }