Skip to content

Commit c916ff9

Browse files
committed
Simplify the web searcher plugins
We can eliminate the AbstractWebSearcher layer (it is actually less code without it), as well as the ImageJForumSearchResult subclass. The WikiSearcher is also renamed ImageJWikiSearcher, and its priority set higher than the ImageJForumSearcher.
1 parent 5b4f446 commit c916ff9

File tree

6 files changed

+103
-242
lines changed

6 files changed

+103
-242
lines changed

src/main/java/org/scijava/search/web/AbstractWebSearcher.java

Lines changed: 0 additions & 78 deletions
This file was deleted.

src/main/java/org/scijava/search/web/BISESearcher.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.IOException;
3333
import java.net.URL;
3434
import java.net.URLEncoder;
35+
import java.util.ArrayList;
3536
import java.util.List;
3637

3738
import javax.xml.parsers.DocumentBuilder;
@@ -41,6 +42,7 @@
4142
import org.scijava.log.LogService;
4243
import org.scijava.plugin.Parameter;
4344
import org.scijava.search.SearchResult;
45+
import org.scijava.search.Searcher;
4446
import org.w3c.dom.Document;
4547
import org.w3c.dom.Node;
4648
import org.w3c.dom.NodeList;
@@ -52,20 +54,28 @@
5254
*
5355
* @author Robert Haase (MPI-CBG)
5456
*/
55-
//@Plugin(type = Searcher.class, name = "BISE", enabled = false)
56-
public class BISESearcher extends AbstractWebSearcher {
57+
//@Plugin(type = Searcher.class, enabled = false)
58+
public class BISESearcher implements Searcher {
59+
60+
private final ArrayList<SearchResult> searchResults = new ArrayList<>();
61+
62+
private String currentHeading;
63+
private String currentLink;
64+
private String currentContent;
5765

5866
@Parameter
5967
private LogService log;
6068

61-
public BISESearcher() {
62-
super("BISE");
69+
@Override
70+
public String title() {
71+
return "BISE";
6372
}
6473

6574
@Override
6675
public List<SearchResult> search(final String text, final boolean fuzzy) {
76+
searchResults.clear();
77+
6778
try {
68-
// URL url = new URL("file:///c:/structure/temp/biii.eu_search2.html");
6979
final URL url = new URL("http://biii.eu/search?search_api_fulltext=" +
7080
URLEncoder.encode(text, "utf-8") + "&source=imagej");
7181

@@ -85,16 +95,12 @@ public List<SearchResult> search(final String text, final boolean fuzzy) {
8595
catch (final SAXException e) {
8696
log.debug(e);
8797
}
88-
return getSearchResults();
98+
return searchResults;
8999
}
90100

91-
String currentHeading;
92-
String currentLink;
93-
94101
private void parseHeading(final Node node) {
95-
96-
if (node.getTextContent() != null && node.getTextContent().trim()
97-
.length() > 0)
102+
if (node.getTextContent() != null && //
103+
node.getTextContent().trim().length() > 0)
98104
{
99105
currentHeading = node.getTextContent();
100106
}
@@ -108,13 +114,10 @@ private void parseHeading(final Node node) {
108114
final NodeList nodeList = node.getChildNodes();
109115
for (int i = 0; i < nodeList.getLength(); i++) {
110116
final Node childNode = nodeList.item(i);
111-
112117
parseHeading(childNode);
113118
}
114119
}
115120

116-
String currentContent;
117-
118121
private void parseContent(final Node node) {
119122
if (node.getTextContent() != null) {
120123
currentContent = node.getTextContent();
@@ -123,16 +126,14 @@ private void parseContent(final Node node) {
123126
final NodeList nodeList = node.getChildNodes();
124127
for (int i = 0; i < nodeList.getLength(); i++) {
125128
final Node childNode = nodeList.item(i);
126-
127129
parse(childNode);
128130
}
129131
}
130132

131133
private void saveLastItem() {
132134
if (currentHeading != null && currentHeading.length() > 0) {
133-
134-
addResult(currentHeading, "", currentLink, currentContent);
135-
135+
searchResults.add(new WebSearchResult(currentHeading, //
136+
currentLink, currentContent));
136137
}
137138
currentHeading = "";
138139
currentLink = "";
@@ -141,8 +142,8 @@ private void saveLastItem() {
141142

142143
private void parse(final Node node) {
143144
if (node.getNodeName().equals("div")) {
144-
final Node item = node.getAttributes() == null ? null : node
145-
.getAttributes().getNamedItem("class");
145+
final Node item = node.getAttributes() == null ? //
146+
null : node.getAttributes().getNamedItem("class");
146147
if (item != null && item.getNodeValue().equals(
147148
"views-field views-field-title"))
148149
{
@@ -151,7 +152,6 @@ private void parse(final Node node) {
151152
saveLastItem();
152153
}
153154
parseHeading(node);
154-
155155
return;
156156
}
157157
if (item != null && item.getNodeValue().equals(
@@ -165,10 +165,7 @@ private void parse(final Node node) {
165165
final NodeList nodeList = node.getChildNodes();
166166
for (int i = 0; i < nodeList.getLength(); i++) {
167167
final Node childNode = nodeList.item(i);
168-
169168
parse(childNode);
170169
}
171-
172170
}
173-
174171
}

src/main/java/org/scijava/search/web/ImageJForumSearchResult.java

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/main/java/org/scijava/search/web/ImageJForumSearcher.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@
3232
import java.io.IOException;
3333
import java.net.URL;
3434
import java.net.URLEncoder;
35+
import java.time.Instant;
36+
import java.time.LocalDateTime;
37+
import java.time.ZoneId;
38+
import java.time.ZoneOffset;
39+
import java.time.format.DateTimeFormatter;
40+
import java.util.ArrayList;
3541
import java.util.HashMap;
42+
import java.util.LinkedHashMap;
3643
import java.util.List;
44+
import java.util.Map;
3745
import java.util.Scanner;
3846

3947
import org.scijava.log.LogService;
@@ -47,28 +55,31 @@
4755
*
4856
* @author Robert Haase (MPI-CBG)
4957
*/
50-
@Plugin(type = Searcher.class, name = "ImageJ Forum", enabled = false)
51-
public class ImageJForumSearcher extends AbstractWebSearcher {
58+
@Plugin(type = Searcher.class, enabled = false)
59+
public class ImageJForumSearcher implements Searcher {
5260

5361
@Parameter
5462
private LogService log;
5563

56-
public ImageJForumSearcher() {
57-
super("ImageJ Forum");
64+
@Override
65+
public String title() {
66+
return "ImageJ Forum";
5867
}
5968

6069
@Override
6170
public List<SearchResult> search(final String text, final boolean fuzzy) {
71+
final ArrayList<SearchResult> searchResults = new ArrayList<>();
72+
6273
try {
6374
final URL url = new URL("http://forum.imagej.net/search?q=" + //
6475
URLEncoder.encode(text, "utf-8") + "&source=imagej");
6576
String webSearchContent;
6677
try (final Scanner s = new Scanner(url.openStream())) {
6778
s.useDelimiter("\"topics\":");
6879

69-
if (!s.hasNext()) return getSearchResults();
80+
if (!s.hasNext()) return searchResults;
7081
s.next();
71-
if (!s.hasNext()) return getSearchResults();
82+
if (!s.hasNext()) return searchResults;
7283
webSearchContent = s.next();
7384
}
7485
webSearchContent = webSearchContent.substring(webSearchContent.indexOf(
@@ -85,32 +96,21 @@ public List<SearchResult> search(final String text, final boolean fuzzy) {
8596
"Created: " + metaInfo.get("created_at") + "<br />" +
8697
"Last posted: " + metaInfo.get("last_posted_at");
8798

88-
addResult(metaInfo.get("title"), "", forumPostUrl, metaInfo, details);
99+
final Map<String, String> extraProps = new LinkedHashMap<>();
100+
extraProps.put("Tags", metaInfo.get("tags"));
101+
extraProps.put("Created", formatDate(metaInfo.get("created_at")));
102+
extraProps.put("Last posted", formatDate(metaInfo.get("last_posted_at")));
103+
searchResults.add(new WebSearchResult(metaInfo.get("title"), //
104+
forumPostUrl, details, null, extraProps));
89105
}
90106
}
91107
catch (final IOException e) {
92108
log.debug(e);
93109
}
94-
return getSearchResults();
110+
return searchResults;
95111
}
96112

97-
/**
98-
* @param name Resulting website title / name
99-
* @param iconPath path to an image representing the results
100-
* @param url URL of the found website
101-
* @param metaInfo
102-
* @param details some text from the website representing its content
103-
*/
104-
protected void addResult(final String name, final String iconPath,
105-
final String url, final HashMap<String, String> metaInfo,
106-
final String details)
107-
{
108-
getSearchResults().add(new ImageJForumSearchResult(name, //
109-
iconPath == null || iconPath.isEmpty() ? "/icons/search/world_link.png"
110-
: iconPath, url, metaInfo, details));
111-
}
112-
113-
HashMap<String, String> parseForumSearchResult(String content) {
113+
private HashMap<String, String> parseForumSearchResult(String content) {
114114
content = content + ",";
115115
final HashMap<String, String> map = new HashMap<>();
116116
String currentKey = "";
@@ -162,4 +162,10 @@ HashMap<String, String> parseForumSearchResult(String content) {
162162
return map;
163163
}
164164

165+
private String formatDate(final String datestr) {
166+
final Instant instant = Instant.parse(datestr);
167+
final LocalDateTime result = LocalDateTime.ofInstant(instant, ZoneId.of(
168+
ZoneOffset.UTC.getId()));
169+
return result.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
170+
}
165171
}

0 commit comments

Comments
 (0)