1
|
package eu.dnetlib.download.plugin;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.nio.file.Path;
|
5
|
import java.nio.file.Paths;
|
6
|
import java.util.List;
|
7
|
|
8
|
import com.google.common.collect.Iterables;
|
9
|
import com.google.gson.Gson;
|
10
|
import eu.dnetlib.data.download.rmi.AbstractDownloadPlugin;
|
11
|
import eu.dnetlib.data.download.rmi.DownloadItem;
|
12
|
import eu.dnetlib.data.download.rmi.DownloadPlugin;
|
13
|
import eu.dnetlib.data.download.rmi.DownloadPluginException;
|
14
|
import org.apache.commons.lang3.StringUtils;
|
15
|
import org.apache.commons.logging.Log;
|
16
|
import org.apache.commons.logging.LogFactory;
|
17
|
|
18
|
public class ArxivImportFromFile extends AbstractDownloadPlugin implements DownloadPlugin {
|
19
|
|
20
|
private static final Log log = LogFactory.getLog(ArxivImportFromFile.class); // NOPMD by marko on 11/24/08 5:02 PM
|
21
|
|
22
|
/**
|
23
|
* The base path.
|
24
|
*/
|
25
|
private String basePath;
|
26
|
|
27
|
@Override
|
28
|
public DownloadItem retrieveUrl(final DownloadItem item) {
|
29
|
if (checkOpenAccess(item) == null) return null;
|
30
|
final String baseURLs = item.getUrl();
|
31
|
final List<String> urlsList = new Gson().fromJson(baseURLs, List.class);
|
32
|
for (final String baseURL : urlsList) {
|
33
|
|
34
|
if (baseURL.isEmpty() == false && baseURL.trim().startsWith("http://") == true) {
|
35
|
final String name = StringUtils.substringAfter(baseURL, "abs/").trim();
|
36
|
if (name == null) {
|
37
|
item.setUrl(null);
|
38
|
return item;
|
39
|
}
|
40
|
|
41
|
final String fileURL = createPath(name);
|
42
|
if (StringUtils.isBlank(fileURL)) {
|
43
|
item.setUrl(null);
|
44
|
return item;
|
45
|
}
|
46
|
|
47
|
final File f = new File(fileURL);
|
48
|
if (f.exists()) {
|
49
|
if (log.isDebugEnabled()) {
|
50
|
log.debug("found path associated to " + item.getIdItemMetadata() + " with path : " + fileURL);
|
51
|
}
|
52
|
item.setUrl("file://" + fileURL);
|
53
|
} else {
|
54
|
if (log.isDebugEnabled()) {
|
55
|
log.debug("NOT found path associated to " + item.getIdItemMetadata()+": "+fileURL);
|
56
|
}
|
57
|
item.setUrl(null);
|
58
|
}
|
59
|
item.setOriginalUrl(baseURL);
|
60
|
return item;
|
61
|
|
62
|
} else {
|
63
|
item.setUrl(null);
|
64
|
}
|
65
|
|
66
|
}
|
67
|
return item;
|
68
|
}
|
69
|
|
70
|
public String createPath(final String name) {
|
71
|
final String regExp = "\\d+\\.\\d+";
|
72
|
if (name.matches(regExp)) {
|
73
|
final String[] values = name.split("\\.");
|
74
|
final Path bsPath = Paths.get(basePath);
|
75
|
final Path filePath = Paths.get(String.format("%s/%s.pdf", values[0], name));
|
76
|
|
77
|
final String fileURL = bsPath.resolve(filePath).toString();
|
78
|
return fileURL;
|
79
|
} else {
|
80
|
if (name.contains("/")) {
|
81
|
final String[] values = name.split("/");
|
82
|
if (values.length != 2) return null;
|
83
|
if (values[1].length() > 4) {
|
84
|
final String middle = values[1].substring(0, 4);
|
85
|
final Path bsPath = Paths.get(basePath);
|
86
|
final Path filePath = Paths.get(String.format("%s/%s.pdf", middle, name.replace("/", "")));
|
87
|
final String fileURL = bsPath.resolve(filePath).toString();
|
88
|
return fileURL;
|
89
|
}
|
90
|
}
|
91
|
}
|
92
|
return null;
|
93
|
}
|
94
|
|
95
|
@Override
|
96
|
public Iterable<DownloadItem> retrieveUrls(final Iterable<DownloadItem> items) {
|
97
|
return Iterables.transform(items, input -> retrieveUrl(input));
|
98
|
}
|
99
|
|
100
|
@Override
|
101
|
public String getPluginName() {
|
102
|
return "ArxivImportFromFile";
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* @return the basePath
|
107
|
*/
|
108
|
public String getBasePath() {
|
109
|
return basePath;
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* @param basePath the basePath to set
|
114
|
*/
|
115
|
@Override
|
116
|
public void setBasePath(final String basePath) {
|
117
|
this.basePath = basePath;
|
118
|
}
|
119
|
|
120
|
@Override
|
121
|
public String extractURL(String baseURL) throws DownloadPluginException {
|
122
|
return null;
|
123
|
}
|
124
|
}
|