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

« back to all changes in this revision

Viewing changes to src/java/net/sf/jabref/imports/ScienceDirectFetcher.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.IOException;
 
11
import java.io.UnsupportedEncodingException;
 
12
import java.net.URL;
 
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;
 
18
 
 
19
 
 
20
public class ScienceDirectFetcher implements EntryFetcher {
 
21
 
 
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=";
 
25
 
 
26
    protected static final String linkPrefix = "http://www.sciencedirect.com/science?_ob=ArticleURL&" ;
 
27
    protected static final Pattern linkPattern = Pattern.compile(
 
28
            "<a href=\""+
 
29
            linkPrefix.replaceAll("\\?", "\\\\?")+
 
30
            "([^\"]+)\"\"");
 
31
 
 
32
    protected static final Pattern nextPagePattern = Pattern.compile(
 
33
            "<a href=\"(.*)\">Next &gt;");
 
34
 
 
35
 
 
36
    protected boolean stopFetching = false;
 
37
    protected boolean noAccessFound = false;
 
38
 
 
39
    public String getHelpPage() {
 
40
        return "ScienceDirect.html";
 
41
    }
 
42
 
 
43
    public URL getIcon() {
 
44
        return GUIGlobals.getIconUrl("www");
 
45
    }
 
46
 
 
47
    public String getKeyName() {
 
48
        return "Search ScienceDirect";
 
49
    }
 
50
 
 
51
    public JPanel getOptionsPanel() {
 
52
        // No Options panel
 
53
        return null;
 
54
    }
 
55
 
 
56
    public String getTitle() {
 
57
        return Globals.menuTitle("Search ScienceDirect");
 
58
    }
 
59
 
 
60
    public void stopFetching() {
 
61
        stopFetching = true;
 
62
        noAccessFound = false;
 
63
    }
 
64
 
 
65
    public boolean processQuery(String query, ImportInspector dialog, OutputPrinter status) {
 
66
        stopFetching = false;
 
67
        try {
 
68
            List<String> citations = getCitations(query);
 
69
            if (citations == null)
 
70
                return false;
 
71
            if (citations.size() == 0){
 
72
                status.showMessage(Globals.lang("No entries found for the search string '%0'",
 
73
                    query),
 
74
                    Globals.lang("Search ScienceDirect"), JOptionPane.INFORMATION_MESSAGE);
 
75
                return false;
 
76
            }
 
77
 
 
78
            int i=0;
 
79
            for (String cit : citations) {
 
80
                if (stopFetching)
 
81
                    break;
 
82
                BibtexEntry entry = BibsonomyScraper.getEntry(cit);
 
83
                if (entry != null)
 
84
                    dialog.addEntry(entry);
 
85
                dialog.setProgress(++i, citations.size());
 
86
            }
 
87
 
 
88
            return true;
 
89
 
 
90
        } catch (IOException e) {
 
91
            e.printStackTrace();
 
92
            status.showMessage(Globals.lang("Error while fetching from ScienceDirect") + ": " + e.getMessage());
 
93
        }
 
94
        return false;
 
95
    }
 
96
 
 
97
    /**
 
98
     *
 
99
     * @param query
 
100
     *            The search term to query JStor for.
 
101
     * @return a list of IDs
 
102
     * @throws java.io.IOException
 
103
     */
 
104
    protected List<String> getCitations(String query) throws IOException {
 
105
        String urlQuery;
 
106
        ArrayList<String> ids = new ArrayList<String>();
 
107
        try {
 
108
            urlQuery = SEARCH_URL + URLEncoder.encode(query, "UTF-8");
 
109
            int count = 1;
 
110
            String nextPage = null;
 
111
            while (((nextPage = getCitationsFromUrl(urlQuery, ids)) != null)
 
112
                    && (count < MAX_PAGES_TO_LOAD)) {
 
113
                urlQuery = nextPage;
 
114
                count++;
 
115
            }
 
116
            return ids;
 
117
        } catch (UnsupportedEncodingException e) {
 
118
            throw new RuntimeException(e);
 
119
        }
 
120
    }
 
121
 
 
122
    protected String getCitationsFromUrl(String urlQuery, List<String> ids) throws IOException {
 
123
        URL url = new URL(urlQuery);
 
124
        URLDownload ud = new URLDownload(url);
 
125
        ud.download();
 
126
 
 
127
        String cont = ud.getStringContent();
 
128
        //String entirePage = cont;
 
129
        Matcher m = linkPattern.matcher(cont);
 
130
        if (m.find()) {
 
131
            while (m.find()) {
 
132
                ids.add(linkPrefix+m.group(1));
 
133
                cont = cont.substring(m.end());
 
134
                m = linkPattern.matcher(cont);
 
135
            }
 
136
        }
 
137
 
 
138
        else {
 
139
            return null;
 
140
        }
 
141
        /*m = nextPagePattern.matcher(entirePage);
 
142
        if (m.find()) {
 
143
            String newQuery = WEBSITE_URL +m.group(1);
 
144
            return newQuery;
 
145
        }
 
146
        else*/
 
147
            return null;
 
148
    }
 
149
 
 
150
 
 
151
}
 
 
b'\\ No newline at end of file'