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.
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.
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.
16 package net.sf.jabref.imports;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.InputStreamReader;
22 import java.util.ArrayList;
23 import java.util.List;
25 import javax.swing.JPanel;
27 import net.sf.jabref.BibtexEntry;
28 import net.sf.jabref.GUIGlobals;
29 import net.sf.jabref.OutputPrinter;
31 public class DBLPFetcher implements EntryFetcher {
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";
38 private volatile boolean shouldContinue = false;
40 private final DBLPHelper helper = new DBLPHelper();
44 public void stopFetching() {
45 shouldContinue = false;
49 public boolean processQuery(String query, ImportInspector inspector,
50 OutputPrinter status) {
55 shouldContinue = true;
59 String address = makeSearchURL();
60 //System.out.println(address);
61 URL url = new URL(address);
62 String page = readFromURL(url);
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);
78 for(String urlStr : bibtexUrlList) {
79 if( ! shouldContinue ) {
83 final URL bibUrl = new URL(urlStr);
84 String bibtexPage = readFromURL(bibUrl);
85 //System.out.println(bibtexPage);
87 List<BibtexEntry> bibtexList = helper.getBibTexFromPage(bibtexPage);
89 for(BibtexEntry bibtexEntry : bibtexList ) {
90 inspector.addEntry(bibtexEntry);
91 if( ! shouldContinue ) {
95 inspector.setProgress(count, bibtexUrlList.size());
99 // everything went smooth
102 } catch (IOException e) {
104 status.showMessage(e.getMessage());
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();
116 char[] cbuf = new char[256];
118 while( (read = ir.read(cbuf)) != -1 ) {
119 sbuf.append(cbuf, 0, read);
121 return sbuf.toString();
124 private String makeSearchURL() {
125 StringBuffer sb = new StringBuffer(URL_START).append(URL_PART1);
126 String cleanedQuery = helper.cleanDBLPQuery(query);
127 sb.append(cleanedQuery);
129 return sb.toString();
133 public String getTitle() {
138 public String getKeyName() {
143 public URL getIcon() {
144 return GUIGlobals.getIconUrl("www");
148 public String getHelpPage() {
153 public JPanel getOptionsPanel() {