| 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, PIPEDOT, |
| 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 | case PIPEDOT: |
| 57 | support = new Pipedot(); |
| 58 | break; |
| 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 | } |