1
package net.sf.jabref.imports;
3
import net.sf.jabref.BibtexEntry;
4
import net.sf.jabref.GUIGlobals;
5
import net.sf.jabref.Globals;
6
import net.sf.jabref.OutputPrinter;
7
import net.sf.jabref.net.URLDownload;
10
import java.io.IOException;
11
import java.io.UnsupportedEncodingException;
13
import java.net.URLEncoder;
14
import java.util.ArrayList;
15
import java.util.List;
16
import java.util.regex.Matcher;
17
import java.util.regex.Pattern;
20
public class ScienceDirectFetcher implements EntryFetcher {
22
protected static int MAX_PAGES_TO_LOAD = 8;
23
protected static final String WEBSITE_URL = "http://www.sciencedirect.com";
24
protected static final String SEARCH_URL = WEBSITE_URL +"/science/quicksearch?query=";
26
protected static final String linkPrefix = "http://www.sciencedirect.com/science?_ob=ArticleURL&" ;
27
protected static final Pattern linkPattern = Pattern.compile(
29
linkPrefix.replaceAll("\\?", "\\\\?")+
32
protected static final Pattern nextPagePattern = Pattern.compile(
33
"<a href=\"(.*)\">Next >");
36
protected boolean stopFetching = false;
37
protected boolean noAccessFound = false;
39
public String getHelpPage() {
40
return "ScienceDirect.html";
43
public URL getIcon() {
44
return GUIGlobals.getIconUrl("www");
47
public String getKeyName() {
48
return "Search ScienceDirect";
51
public JPanel getOptionsPanel() {
56
public String getTitle() {
57
return Globals.menuTitle("Search ScienceDirect");
60
public void stopFetching() {
62
noAccessFound = false;
65
public boolean processQuery(String query, ImportInspector dialog, OutputPrinter status) {
68
List<String> citations = getCitations(query);
69
if (citations == null)
71
if (citations.size() == 0){
72
status.showMessage(Globals.lang("No entries found for the search string '%0'",
74
Globals.lang("Search ScienceDirect"), JOptionPane.INFORMATION_MESSAGE);
79
for (String cit : citations) {
82
BibtexEntry entry = BibsonomyScraper.getEntry(cit);
84
dialog.addEntry(entry);
85
dialog.setProgress(++i, citations.size());
90
} catch (IOException e) {
92
status.showMessage(Globals.lang("Error while fetching from ScienceDirect") + ": " + e.getMessage());
100
* The search term to query JStor for.
101
* @return a list of IDs
102
* @throws java.io.IOException
104
protected List<String> getCitations(String query) throws IOException {
106
ArrayList<String> ids = new ArrayList<String>();
108
urlQuery = SEARCH_URL + URLEncoder.encode(query, "UTF-8");
110
String nextPage = null;
111
while (((nextPage = getCitationsFromUrl(urlQuery, ids)) != null)
112
&& (count < MAX_PAGES_TO_LOAD)) {
117
} catch (UnsupportedEncodingException e) {
118
throw new RuntimeException(e);
122
protected String getCitationsFromUrl(String urlQuery, List<String> ids) throws IOException {
123
URL url = new URL(urlQuery);
124
URLDownload ud = new URLDownload(url);
127
String cont = ud.getStringContent();
128
//String entirePage = cont;
129
Matcher m = linkPattern.matcher(cont);
132
ids.add(linkPrefix+m.group(1));
133
cont = cont.substring(m.end());
134
m = linkPattern.matcher(cont);
141
/*m = nextPagePattern.matcher(entirePage);
143
String newQuery = WEBSITE_URL +m.group(1);
b'\\ No newline at end of file'