2b94725b28d15ef88cb96e75125a4636bfa7d878
1 package be
.nikiroo
.fanfix
.searchable
;
3 import java
.io
.IOException
;
7 import org
.jsoup
.helper
.DataUtil
;
8 import org
.jsoup
.nodes
.Document
;
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
;
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
22 public abstract class BasicSearchable
{
23 private SupportType type
;
24 private BasicSupport support
;
27 * Create a new {@link BasicSearchable} of the given type.
30 * the type, must not be NULL
32 public BasicSearchable(SupportType type
) {
34 support
= BasicSupport
.getSupport(getType(), null);
38 * Find the given tag by its hierarchical IDs.
40 * I.E., it will take the tag A, subtag B, subsubtag C...
45 * @return the appropriate tag fully filled, or NULL if not found
48 * in case of I/O error
50 public SearchableTag
getTag(Integer
... ids
) throws IOException
{
51 SearchableTag tag
= null;
52 List
<SearchableTag
> tags
= getTags();
54 for (Integer tagIndex
: ids
) {
56 if (tagIndex
== null || tags
== null || tagIndex
<= 0
57 || tagIndex
> tags
.size()) {
61 tag
= tags
.get(tagIndex
- 1);
63 tags
= tag
.getChildren();
74 public SupportType
getType() {
84 protected void setType(SupportType type
) {
89 * The associated {@link BasicSupport}.
91 * Mostly used to download content.
95 protected BasicSupport
getSupport() {
100 * Get a list of tags that can be browsed here.
102 * @return the list of tags
104 * @throws IOException
105 * in case of I/O error
107 abstract public List
<SearchableTag
> getTags() throws IOException
;
110 * Fill the tag (set it 'complete') with more information from the support.
115 * @throws IOException
116 * in case of I/O error
118 abstract public void fillTag(SearchableTag tag
) throws IOException
;
121 * Search for the given term and return the number of pages of results of
122 * stories satisfying this search term.
125 * the term to search for
127 * @return a number of pages
129 * @throws IOException
130 * in case of I/O error
132 abstract public int searchPages(String search
) throws IOException
;
135 * Search for the given tag and return the number of pages of results of
136 * stories satisfying this tag.
139 * the tag to search for
141 * @return a number of pages
143 * @throws IOException
144 * in case of I/O error
146 abstract public int searchPages(SearchableTag tag
) throws IOException
;
149 * Search for the given term and return a list of stories satisfying this
152 * Not that the returned stories will <b>NOT</b> be complete, but will only
153 * contain enough information to present them to the user and retrieve them.
155 * URL is guaranteed to be usable, LUID will always be NULL.
158 * the term to search for
160 * the page to use for result pagination, index is 1-based
162 * @return a list of stories that satisfy that search term
164 * @throws IOException
165 * in case of I/O error
167 abstract public List
<MetaData
> search(String search
, int page
)
171 * Search for the given tag and return a list of stories satisfying this
174 * Not that the returned stories will <b>NOT</b> be complete, but will only
175 * contain enough information to present them to the user and retrieve them.
177 * URL is guaranteed to be usable, LUID will always be NULL.
180 * the tag to search for
182 * the page to use for result pagination (see
183 * {@link SearchableTag#getPages()}, remember to check for -1),
186 * @return a list of stories that satisfy that search term
188 * @throws IOException
189 * in case of I/O error
191 abstract public List
<MetaData
> search(SearchableTag tag
, int page
)
195 * Load a document from its url.
200 * TRUE for more stable resources, FALSE when they often change
202 * @return the document
204 * @throws IOException
205 * in case of I/O error
207 protected Document
load(String url
, boolean stable
) throws IOException
{
208 return load(new URL(url
), stable
);
212 * Load a document from its url.
217 * TRUE for more stable resources, FALSE when they often change
219 * @return the document
221 * @throws IOException
222 * in case of I/O error
224 protected Document
load(URL url
, boolean stable
) throws IOException
{
225 return DataUtil
.load(Instance
.getCache().open(url
, support
, stable
),
226 "UTF-8", url
.toString());
230 * Return a {@link BasicSearchable} implementation supporting the given
231 * type, or NULL if it does not exist.
234 * the type, can be NULL (will just return NULL, since we do not
237 * @return an implementation that supports it, or NULL
239 static public BasicSearchable
getSearchable(SupportType type
) {
240 BasicSearchable support
= null;
248 support
= new Fanfiction(type
);
263 support
= new MangaLel();