break;
}
- if (searchOn == null || search == null) {
+ if (searchOn == null) {
// TODO: do on reader!!!
for (SupportType type : SupportType.values()) {
if (BasicSearchable.getSearchable(type) != null) {
System.out.println(type);
}
}
- } else {
+ } else if (search != null) {
try {
BasicReader.getReader().search(searchOn, search, page,
item, true);
} catch (IOException e1) {
Instance.getTraceHandler().error(e1);
}
+ } else {
+ exitCode = 255;
}
break;
@Override
public void search(SupportType searchOn, String keywords, int page,
int item, boolean sync) throws IOException {
- // TODO
+ BasicSearchable search = BasicSearchable.getSearchable(searchOn);
+
+ if (page == 0) {
+ System.out.println(search.searchPages(keywords));
+ } else {
+ List<MetaData> metas = search.search(keywords, page);
+
+ if (item == 0) {
+ System.out.println("Page " + page + " of stories for: "
+ + keywords);
+ displayStories(metas);
+ } else {
+ // ! 1-based index !
+ if (item <= 0 | item > metas.size()) {
+ throw new IOException("Index out of bounds: " + item);
+ }
+
+ MetaData meta = metas.get(item - 1);
+ displayStory(meta);
+ }
+ }
}
@Override
if (item <= count) {
if (metas != null) {
MetaData meta = metas.get(item - 1);
- System.out.println(page + "/" + item + ": "
- + meta.getTitle());
- System.out.println();
- System.out.println(meta.getUrl());
- System.out.println();
- System.out.println("Tags: " + meta.getTags());
- System.out.println();
- for (Paragraph para : meta.getResume()) {
- System.out.println(para.getContent());
- System.out.println("");
- }
+ displayStory(meta);
} else {
SearchableTag subtag = subtags.get(item - 1);
// TODO i18n
System.out.println(String.format("Content of %s: ",
fqnTag));
- int i = 1;
- for (MetaData meta : metas) {
- System.out.println(i + ": " + meta.getTitle());
- i++;
- }
+ displayStories(metas);
} else {
// TODO i18n
System.out.println(String.format("Subtags of %s: ",
}
}
}
+
+ private void displayStory(MetaData meta) {
+ System.out.println(meta.getTitle());
+ System.out.println();
+ System.out.println(meta.getUrl());
+ System.out.println();
+ System.out.println("Tags: " + meta.getTags());
+ System.out.println();
+ for (Paragraph para : meta.getResume()) {
+ System.out.println(para.getContent());
+ System.out.println("");
+ }
+ }
+
+ private void displayStories(List<MetaData> metas) {
+ int i = 1;
+ for (MetaData meta : metas) {
+ System.out.println(i + ": " + meta.getTitle());
+ i++;
+ }
+ }
}
*/
abstract public void fillTag(SearchableTag tag) throws IOException;
+ /**
+ * Search for the given term and return the number of pages of results of
+ * stories satisfying this search term.
+ *
+ * @param search
+ * the term to search for
+ *
+ * @return a number of pages
+ *
+ * @throws IOException
+ * in case of I/O error
+ */
+ abstract public int searchPages(String search) throws IOException;
+
/**
* Search for the given term and return a list of stories satisfying this
* search term.
@Override
public List<MetaData> search(String search, int page) throws IOException {
String encoded = URLEncoder.encode(search.toLowerCase(), "utf-8");
- return getStories(BASE_URL + "search/?ready=1&type=story&keywords="
- + encoded + "&ppage=" + page, null, null);
+ String url = BASE_URL + "search/?ready=1&type=story&keywords="
+ + encoded + "&ppage=" + page;
+
+ return getStories(url, null, null);
+ }
+
+ @Override
+ public int searchPages(String search) throws IOException {
+ String encoded = URLEncoder.encode(search.toLowerCase(), "utf-8");
+ String url = BASE_URL + "search/?ready=1&type=story&keywords="
+ + encoded;
+
+ return getPages(load(url, false));
}
@Override