Fix html (gopher links with selectors, useless BR)
[gofetch.git] / src / be / nikiroo / gofetch / support / BasicSupport.java
CommitLineData
73785268
NR
1package be.nikiroo.gofetch.support;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.net.URL;
6import java.net.URLConnection;
7import java.util.List;
8import java.util.zip.GZIPInputStream;
9
10import be.nikiroo.gofetch.data.Comment;
11import be.nikiroo.gofetch.data.Story;
12
13public abstract class BasicSupport {
14 public enum Type {
2d95a873 15 SLASHDOT, PIPEDOT,
73785268
NR
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();
2d95a873 27
73785268
NR
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;
2d95a873
NR
56 case PIPEDOT:
57 support = new Pipedot();
58 break;
73785268
NR
59 }
60
61 if (support != null) {
62 support.setType(type);
63 }
64 }
65
66 return support;
67 }
68
69 static public String getSelector(Type type) {
70 return preselector + "/" + type + "/";
71 }
72
73 // TODO: check Downloader.java?
74 static protected InputStream open(URL url) throws IOException {
75 URLConnection conn = url.openConnection();
76 conn.connect();
77 InputStream in = conn.getInputStream();
78 if ("gzip".equals(conn.getContentEncoding())) {
79 in = new GZIPInputStream(in);
80 }
81
82 return in;
83 }
84}