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;
12
import java.net.URLConnection;
13
import java.net.URLEncoder;
14
import java.util.ArrayList;
15
import java.util.List;
16
import java.util.StringTokenizer;
17
import java.util.regex.Matcher;
18
import java.util.regex.Pattern;
21
public class JSTORFetcher2 implements EntryFetcher {
23
protected static int MAX_PAGES_TO_LOAD = 8;
24
protected static final String JSTOR_URL = "http://www.jstor.org";
25
protected static final String SEARCH_URL = JSTOR_URL+"/action/doBasicSearch?Query=";
26
protected static final String SEARCH_URL_END = "&x=0&y=0&wc=on";
27
protected static final String SINGLE_CIT_ENC =
28
"http://www.jstor.org/action/exportSingleCitation?singleCitation=true&suffix=";
29
//"http%3A%2F%2Fwww.jstor.org%2Faction%2FexportSingleCitation%3FsingleCitation"
30
//+"%3Dtrue%26suffix%3D";
32
protected static final Pattern idPattern = Pattern.compile(
33
"<a class=\"title\" href=\"/stable/(\\d+)\\?");
35
protected static final Pattern nextPagePattern = Pattern.compile(
36
"<a href=\"(.*)\">Next >");
38
protected static final String noAccessIndicator = "We do not recognize you as having access to JSTOR";
40
protected boolean stopFetching = false;
41
protected boolean noAccessFound = false;
43
public String getHelpPage() {
47
public URL getIcon() {
48
return GUIGlobals.getIconUrl("www");
51
public String getKeyName() {
52
return "Search JSTOR";
55
public JPanel getOptionsPanel() {
60
public String getTitle() {
61
return Globals.menuTitle("Search JSTOR");
64
public void stopFetching() {
66
noAccessFound = false;
69
public boolean processQuery(String query, ImportInspector dialog, OutputPrinter status) {
72
List<String> citations = getCitations(query);
73
if (citations == null)
75
if (citations.size() == 0){
77
status.showMessage(Globals.lang("No entries found for the search string '%0'",
79
Globals.lang("Search JSTOR"), JOptionPane.INFORMATION_MESSAGE);
81
status.showMessage(Globals.lang("No entries found. It looks like you do not have access to search JStor.",
83
Globals.lang("Search JSTOR"), JOptionPane.INFORMATION_MESSAGE);
89
for (String cit : citations) {
92
BibtexEntry entry = getSingleCitation(cit);
94
dialog.addEntry(entry);
95
dialog.setProgress(++i, citations.size());
100
} catch (IOException e) {
102
status.showMessage(Globals.lang("Error while fetching from JSTOR") + ": " + e.getMessage());
110
* The search term to query JStor for.
111
* @return a list of IDs
112
* @throws java.io.IOException
114
protected List<String> getCitations(String query) throws IOException {
116
ArrayList<String> ids = new ArrayList<String>();
118
urlQuery = SEARCH_URL + URLEncoder.encode(query, "UTF-8") + SEARCH_URL_END;
120
String nextPage = null;
121
while (((nextPage = getCitationsFromUrl(urlQuery, ids)) != null)
122
&& (count < MAX_PAGES_TO_LOAD)) {
127
} catch (UnsupportedEncodingException e) {
128
throw new RuntimeException(e);
132
protected String getCitationsFromUrl(String urlQuery, List<String> ids) throws IOException {
133
URL url = new URL(urlQuery);
134
URLDownload ud = new URLDownload(url);
137
String cont = ud.getStringContent();
138
String entirePage = cont;
140
Matcher m = idPattern.matcher(cont);
144
cont = cont.substring(m.end());
145
m = idPattern.matcher(cont);
148
else if (entirePage.indexOf(noAccessIndicator) >= 0) {
149
noAccessFound = true;
155
m = nextPagePattern.matcher(entirePage);
157
String newQuery = JSTOR_URL+m.group(1);
164
protected BibtexEntry getSingleCitation(String cit) {
165
return BibsonomyScraper.getEntry(SINGLE_CIT_ENC+cit);
b'\\ No newline at end of file'