~ubuntu-branches/ubuntu/natty/jabref/natty

« back to all changes in this revision

Viewing changes to src/java/net/sf/jabref/imports/JSTORFetcher2.java

  • Committer: Bazaar Package Importer
  • Author(s): gregor herrmann
  • Date: 2010-04-27 16:49:34 UTC
  • mfrom: (2.1.8 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100427164934-ozu2dvinslmo3444
Tags: 2.6+ds-2
debian/control: add "Recommends: xdg-utils"; thanks to Vincent Fourmond
for the bug report (closes: #579346). Change xpdf to xpdf-reader in
Suggests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package net.sf.jabref.imports;
 
2
 
 
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;
 
8
 
 
9
import javax.swing.*;
 
10
import java.io.*;
 
11
import java.net.URL;
 
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;
 
19
 
 
20
 
 
21
public class JSTORFetcher2 implements EntryFetcher {
 
22
 
 
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";
 
31
    
 
32
    protected static final Pattern idPattern = Pattern.compile(
 
33
            "<a class=\"title\" href=\"/stable/(\\d+)\\?");
 
34
 
 
35
    protected static final Pattern nextPagePattern = Pattern.compile(
 
36
            "<a href=\"(.*)\">Next &gt;");
 
37
 
 
38
    protected static final String noAccessIndicator = "We do not recognize you as having access to JSTOR";
 
39
 
 
40
    protected boolean stopFetching = false;
 
41
    protected boolean noAccessFound = false;
 
42
 
 
43
    public String getHelpPage() {
 
44
        return "JSTOR.html";
 
45
    }
 
46
 
 
47
    public URL getIcon() {
 
48
        return GUIGlobals.getIconUrl("www");
 
49
    }
 
50
 
 
51
    public String getKeyName() {
 
52
        return "Search JSTOR";
 
53
    }
 
54
 
 
55
    public JPanel getOptionsPanel() {
 
56
        // No Options panel
 
57
        return null;
 
58
    }
 
59
 
 
60
    public String getTitle() {
 
61
        return Globals.menuTitle("Search JSTOR");
 
62
    }
 
63
 
 
64
    public void stopFetching() {
 
65
        stopFetching = true;
 
66
        noAccessFound = false;
 
67
    }
 
68
 
 
69
    public boolean processQuery(String query, ImportInspector dialog, OutputPrinter status) {
 
70
        stopFetching = false;
 
71
        try {
 
72
            List<String> citations = getCitations(query);
 
73
            if (citations == null)
 
74
                return false;
 
75
            if (citations.size() == 0){
 
76
                if (!noAccessFound)
 
77
                    status.showMessage(Globals.lang("No entries found for the search string '%0'",
 
78
                        query),
 
79
                        Globals.lang("Search JSTOR"), JOptionPane.INFORMATION_MESSAGE);
 
80
                else {
 
81
                    status.showMessage(Globals.lang("No entries found. It looks like you do not have access to search JStor.",
 
82
                        query),
 
83
                        Globals.lang("Search JSTOR"), JOptionPane.INFORMATION_MESSAGE);
 
84
                }
 
85
                return false;
 
86
            }
 
87
 
 
88
            int i=0;
 
89
            for (String cit : citations) {
 
90
                if (stopFetching)
 
91
                    break;
 
92
                BibtexEntry entry = getSingleCitation(cit);
 
93
                if (entry != null)
 
94
                    dialog.addEntry(entry);
 
95
                dialog.setProgress(++i, citations.size());
 
96
            }
 
97
 
 
98
            return true;
 
99
            
 
100
        } catch (IOException e) {
 
101
            e.printStackTrace();
 
102
            status.showMessage(Globals.lang("Error while fetching from JSTOR") + ": " + e.getMessage());
 
103
        }
 
104
        return false;
 
105
    }
 
106
 
 
107
    /**
 
108
     *
 
109
     * @param query
 
110
     *            The search term to query JStor for.
 
111
     * @return a list of IDs
 
112
     * @throws java.io.IOException
 
113
     */
 
114
    protected List<String> getCitations(String query) throws IOException {
 
115
        String urlQuery;
 
116
        ArrayList<String> ids = new ArrayList<String>();
 
117
        try {
 
118
            urlQuery = SEARCH_URL + URLEncoder.encode(query, "UTF-8") + SEARCH_URL_END;
 
119
            int count = 1;
 
120
            String nextPage = null;
 
121
            while (((nextPage = getCitationsFromUrl(urlQuery, ids)) != null)
 
122
                    && (count < MAX_PAGES_TO_LOAD)) {
 
123
                urlQuery = nextPage;
 
124
                count++;
 
125
            }
 
126
            return ids;
 
127
        } catch (UnsupportedEncodingException e) {
 
128
            throw new RuntimeException(e);
 
129
        }
 
130
    }
 
131
 
 
132
    protected String getCitationsFromUrl(String urlQuery, List<String> ids) throws IOException {
 
133
        URL url = new URL(urlQuery);
 
134
        URLDownload ud = new URLDownload(url);
 
135
        ud.download();
 
136
 
 
137
        String cont = ud.getStringContent();
 
138
        String entirePage = cont;
 
139
 
 
140
        Matcher m = idPattern.matcher(cont);
 
141
        if (m.find()) {
 
142
            while (m.find()) {
 
143
                ids.add(m.group(1));
 
144
                cont = cont.substring(m.end());
 
145
                m = idPattern.matcher(cont);
 
146
            }
 
147
        }
 
148
        else if (entirePage.indexOf(noAccessIndicator) >= 0) {
 
149
            noAccessFound = true;
 
150
            return null;
 
151
        }
 
152
        else {
 
153
            return null;
 
154
        }
 
155
        m = nextPagePattern.matcher(entirePage);
 
156
        if (m.find()) {
 
157
            String newQuery = JSTOR_URL+m.group(1);
 
158
            return newQuery;
 
159
        }
 
160
        else
 
161
            return null;
 
162
    }
 
163
 
 
164
    protected BibtexEntry getSingleCitation(String cit) {
 
165
        return BibsonomyScraper.getEntry(SINGLE_CIT_ENC+cit);
 
166
    }
 
167
 
 
168
}
 
 
b'\\ No newline at end of file'