BasicSearchable
[fanfix.git] / src / be / nikiroo / fanfix / searchable / BasicSearchable.java
1 package be.nikiroo.fanfix.searchable;
2
3 import java.io.IOException;
4 import java.net.URL;
5 import java.util.List;
6
7 import org.jsoup.helper.DataUtil;
8 import org.jsoup.nodes.Document;
9
10 import be.nikiroo.fanfix.Instance;
11 import be.nikiroo.fanfix.data.MetaData;
12 import be.nikiroo.fanfix.supported.BasicSupport;
13 import be.nikiroo.fanfix.supported.SupportType;
14
15 /**
16 * This class supports browsing through stories on the supported websites. It
17 * will fetch some {@link MetaData} that satisfy a search query or some tags if
18 * supported.
19 *
20 * @author niki
21 */
22 public abstract class BasicSearchable {
23 private SupportType type;
24 private BasicSupport support;
25
26 /**
27 * Create a new {@link BasicSearchable} of the given type.
28 *
29 * @param type
30 * the type, must not be NULL
31 */
32 public BasicSearchable(SupportType type) {
33 setType(type);
34 support = BasicSupport.getSupport(getType(), null);
35 }
36
37 /**
38 * The support type.
39 *
40 * @return the type
41 */
42 public SupportType getType() {
43 return type;
44 }
45
46 /**
47 * The support type.
48 *
49 * @param type
50 * the new type
51 */
52 protected void setType(SupportType type) {
53 this.type = type;
54 }
55
56 /**
57 * The associated {@link BasicSupport}.
58 * <p>
59 * Mostly used to download content.
60 *
61 * @return the support
62 */
63 protected BasicSupport getSupport() {
64 return support;
65 }
66
67 /**
68 * Get a list of tags that can be browsed here.
69 *
70 * @return the list of tags
71 *
72 * @throws IOException
73 * in case of I/O error
74 */
75 abstract public List<SearchableTag> getTags() throws IOException;
76
77 /**
78 * Fill the tag (set it 'complete') with more information from the support.
79 *
80 * @param tag
81 * the tag to fill
82 *
83 * @throws IOException
84 * in case of I/O error
85 */
86 abstract protected void fillTag(SearchableTag tag) throws IOException;
87
88 /**
89 * Search for the given term and return a list of stories satisfying this
90 * search term.
91 * <p>
92 * Not that the returned stories will <b>NOT</b> be complete, but will only
93 * contain enough information to present them to the user and retrieve them.
94 * <p>
95 * URL is guaranteed to be usable, LUID will always be NULL.
96 *
97 * @param search
98 * the term to search for
99 *
100 * @return a list of stories that satisfy that search term
101 *
102 * @throws IOException
103 * in case of I/O error
104 */
105 abstract public List<MetaData> search(String search) throws IOException;
106
107 /**
108 * Search for the given tag and return a list of stories satisfying this
109 * tag.
110 * <p>
111 * Not that the returned stories will <b>NOT</b> be complete, but will only
112 * contain enough information to present them to the user and retrieve them.
113 * <p>
114 * URL is guaranteed to be usable, LUID will always be NULL.
115 *
116 * @param tagId
117 * the tag to search for
118 *
119 * @return a list of stories that satisfy that search term
120 *
121 * @throws IOException
122 * in case of I/O error
123 */
124 abstract public List<MetaData> search(SearchableTag tag) throws IOException;
125
126 /**
127 * Load a document from its url.
128 *
129 * @param url
130 * the URL to load
131 * @return the document
132 *
133 * @throws IOException
134 * in case of I/O error
135 */
136 protected Document load(String url) throws IOException {
137 return load(new URL(url));
138 }
139
140 /**
141 * Load a document from its url.
142 *
143 * @param url
144 * the URL to load
145 * @return the document
146 *
147 * @throws IOException
148 * in case of I/O error
149 */
150 protected Document load(URL url) throws IOException {
151 return DataUtil.load(Instance.getCache().open(url, support, false),
152 "UTF-8", url.toString());
153 }
154
155 /**
156 * Return a {@link BasicSearchable} implementation supporting the given
157 * type, or NULL if it does not exist.
158 *
159 * @param type
160 * the type, must not be NULL
161 *
162 * @return an implementation that supports it, or NULL
163 */
164 public static BasicSearchable getSearchable(SupportType type) {
165 BasicSearchable support = null;
166
167 switch (type) {
168 case FIMFICTION:
169 // TODO
170 break;
171 case FANFICTION:
172 support = new Fanfiction(type);
173 break;
174 case MANGAFOX:
175 // TODO
176 break;
177 case E621:
178 // TODO
179 break;
180 case YIFFSTAR:
181 // TODO
182 break;
183 case E_HENTAI:
184 // TODO
185 break;
186 case MANGA_LEL:
187 // TODO
188 break;
189 case CBZ:
190 case HTML:
191 case INFO_TEXT:
192 case TEXT:
193 case EPUB:
194 break;
195 }
196
197 return support;
198 }
199 }