a67d2f6022e47088824d7f61cbaaa341b6ce8a0c
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 term and return a list of stories satisfying this
138 * Not that the returned stories will <b>NOT</b> be complete, but will only
139 * contain enough information to present them to the user and retrieve them.
141 * URL is guaranteed to be usable, LUID will always be NULL.
144 * the term to search for
146 * the page to use for result pagination, index is 1-based
148 * @return a list of stories that satisfy that search term
150 * @throws IOException
151 * in case of I/O error
153 abstract public List
<MetaData
> search(String search
, int page
)
157 * Search for the given tag and return a list of stories satisfying this
160 * Not that the returned stories will <b>NOT</b> be complete, but will only
161 * contain enough information to present them to the user and retrieve them.
163 * URL is guaranteed to be usable, LUID will always be NULL.
166 * the tag to search for
168 * the page to use for result pagination (see
169 * {@link SearchableTag#getPages()}, remember to check for -1),
172 * @return a list of stories that satisfy that search term
174 * @throws IOException
175 * in case of I/O error
177 abstract public List
<MetaData
> search(SearchableTag tag
, int page
)
181 * Load a document from its url.
186 * TRUE for more stable resources, FALSE when they often change
188 * @return the document
190 * @throws IOException
191 * in case of I/O error
193 protected Document
load(String url
, boolean stable
) throws IOException
{
194 return load(new URL(url
), stable
);
198 * Load a document from its url.
203 * TRUE for more stable resources, FALSE when they often change
205 * @return the document
207 * @throws IOException
208 * in case of I/O error
210 protected Document
load(URL url
, boolean stable
) throws IOException
{
211 return DataUtil
.load(Instance
.getCache().open(url
, support
, stable
),
212 "UTF-8", url
.toString());
216 * Return a {@link BasicSearchable} implementation supporting the given
217 * type, or NULL if it does not exist.
220 * the type, can be NULL (will just return NULL, since we do not
223 * @return an implementation that supports it, or NULL
225 static public BasicSearchable
getSearchable(SupportType type
) {
226 BasicSearchable support
= null;
234 support
= new Fanfiction(type
);
249 support
= new MangaLel();