1
|
package eu.dnetlib.goldoa.service.utils;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.Publication;
|
4
|
import org.springframework.stereotype.Component;
|
5
|
import org.w3c.dom.Document;
|
6
|
import org.xml.sax.SAXException;
|
7
|
|
8
|
import javax.xml.parsers.DocumentBuilder;
|
9
|
import javax.xml.parsers.DocumentBuilderFactory;
|
10
|
import javax.xml.parsers.ParserConfigurationException;
|
11
|
import javax.xml.xpath.XPath;
|
12
|
import javax.xml.xpath.XPathExpressionException;
|
13
|
import javax.xml.xpath.XPathFactory;
|
14
|
import java.io.IOException;
|
15
|
import java.io.InputStream;
|
16
|
import java.net.MalformedURLException;
|
17
|
import java.net.URL;
|
18
|
|
19
|
/**
|
20
|
* Created by antleb on 3/7/16.
|
21
|
*/
|
22
|
@Component(value = "openaireClient")
|
23
|
public class OpenAIREClient {
|
24
|
|
25
|
private DocumentBuilder documentBuilder;
|
26
|
private XPath xPath = XPathFactory.newInstance().newXPath();
|
27
|
|
28
|
public OpenAIREClient() throws ParserConfigurationException {
|
29
|
documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
30
|
}
|
31
|
|
32
|
public Publication findPublication(String title) {
|
33
|
Publication publication = new Publication();
|
34
|
|
35
|
InputStream is = null;
|
36
|
|
37
|
try {
|
38
|
is = new URL("http://api.openaire.eu/search/publications?title=" + title).openStream();
|
39
|
Document doc = documentBuilder.parse(is);
|
40
|
|
41
|
String docTitle = xPath.compile("/response//result//title").evaluate(doc);
|
42
|
|
43
|
} catch (MalformedURLException e) {
|
44
|
e.printStackTrace();
|
45
|
} catch (IOException e) {
|
46
|
e.printStackTrace();
|
47
|
} catch (SAXException e) {
|
48
|
e.printStackTrace();
|
49
|
} catch (XPathExpressionException e) {
|
50
|
e.printStackTrace();
|
51
|
} finally {
|
52
|
try {
|
53
|
if (is != null)
|
54
|
is.close();
|
55
|
} catch (Exception e) {
|
56
|
}
|
57
|
}
|
58
|
|
59
|
return publication;
|
60
|
}
|
61
|
}
|