c9254ef1338a246a0563b449818daf79c1aa9810
[debian/jabref.git] / src / main / java / net / sf / jabref / imports / DBLPFetcher.java
1 /*  Copyright (C) 2011 Sascha Hunold.
2     This program is free software; you can redistribute it and/or modify
3     it under the terms of the GNU General Public License as published by
4     the Free Software Foundation; either version 2 of the License, or
5     (at your option) any later version.
6
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10     GNU General Public License for more details.
11
12     You should have received a copy of the GNU General Public License along
13     with this program; if not, write to the Free Software Foundation, Inc.,
14     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 */
16 package net.sf.jabref.imports;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import javax.swing.JPanel;
26
27 import net.sf.jabref.BibtexEntry;
28 import net.sf.jabref.GUIGlobals;
29 import net.sf.jabref.OutputPrinter;
30
31 public class DBLPFetcher implements EntryFetcher {
32
33
34     private final static String URL_START = "http://www.dblp.org/search/api/";
35     private final static String URL_PART1 = "?q=";
36     private final static String URL_END   = "&h=1000&c=4&f=0&format=json";
37
38         private volatile boolean shouldContinue = false;
39         private String query;
40         private final DBLPHelper helper = new DBLPHelper();
41
42
43         @Override
44         public void stopFetching() {
45             shouldContinue  = false;
46         }
47
48         @Override
49         public boolean processQuery(String query, ImportInspector inspector,
50                         OutputPrinter status) {
51
52                 boolean res = false;
53                 this.query = query;
54
55                 shouldContinue = true;
56
57                 try {
58
59                         String address = makeSearchURL();
60                         //System.out.println(address);
61                         URL url = new URL(address);
62                 String page = readFromURL(url);
63
64                 //System.out.println(page);
65                 String[] lines = page.split("\n");
66                 List<String> bibtexUrlList = new ArrayList<String>();
67                 for(String line : lines) {
68                         if( line.startsWith("\"url\"") ) {
69                                 String addr = line.replace("\"url\":\"", "");
70                                 addr = addr.substring(0, addr.length()-2);
71                                 //System.out.println("key address: " + addr);
72                                 bibtexUrlList.add(addr);
73                         }
74                 }
75
76
77                 int count = 1;
78                 for(String urlStr : bibtexUrlList) {
79                         if( ! shouldContinue ) {
80                                 break;
81                         }
82
83                         final URL bibUrl = new URL(urlStr);
84                         String bibtexPage = readFromURL(bibUrl);
85                         //System.out.println(bibtexPage);
86
87                         List<BibtexEntry> bibtexList = helper.getBibTexFromPage(bibtexPage);
88
89                         for(BibtexEntry bibtexEntry : bibtexList ) {
90                                 inspector.addEntry(bibtexEntry);
91                                 if( ! shouldContinue ) {
92                                         break;
93                                 }
94                         }
95                         inspector.setProgress(count, bibtexUrlList.size());
96                         count++;
97                 }
98
99                 // everything went smooth
100                 res = true;
101
102                 } catch (IOException e) {
103                         e.printStackTrace();
104                         status.showMessage(e.getMessage());
105                 }
106
107         return res;
108         }
109
110
111     private String readFromURL(final URL source) throws IOException {
112         final InputStream in = source.openStream();
113         final InputStreamReader ir = new InputStreamReader(in);
114         final StringBuffer sbuf = new StringBuffer();
115
116         char[] cbuf = new char[256];
117         int read;
118         while( (read = ir.read(cbuf)) != -1 ) {
119                 sbuf.append(cbuf, 0, read);
120         }
121         return sbuf.toString();
122     }
123
124         private String makeSearchURL() {
125         StringBuffer sb = new StringBuffer(URL_START).append(URL_PART1);
126         String cleanedQuery = helper.cleanDBLPQuery(query);
127         sb.append(cleanedQuery);
128         sb.append(URL_END);
129         return sb.toString();
130         }
131
132         @Override
133         public String getTitle() {
134                 return "DBLP";
135         }
136
137         @Override
138         public String getKeyName() {
139                 return "DBLP";
140         }
141
142         @Override
143         public URL getIcon() {
144             return GUIGlobals.getIconUrl("www");
145         }
146
147         @Override
148         public String getHelpPage() {
149                 return null;
150         }
151
152         @Override
153         public JPanel getOptionsPanel() {
154                 return null;
155         }
156
157 }