1 |
38977
|
alessia.ba
|
package eu.dnetlib.data.collector.plugins.datasources;
|
2 |
|
|
|
3 |
|
|
import java.io.InputStream;
|
4 |
|
|
import java.util.Iterator;
|
5 |
39005
|
claudio.at
|
import java.util.NoSuchElementException;
|
6 |
38977
|
alessia.ba
|
import javax.xml.stream.XMLInputFactory;
|
7 |
|
|
import javax.xml.stream.XMLStreamConstants;
|
8 |
|
|
import javax.xml.stream.XMLStreamException;
|
9 |
|
|
import javax.xml.stream.XMLStreamReader;
|
10 |
|
|
|
11 |
49643
|
alessia.ba
|
import eu.dnetlib.data.collector.plugins.HttpConnector;
|
12 |
49638
|
alessia.ba
|
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
|
13 |
|
|
import eu.dnetlib.data.collector.rmi.CollectorServiceRuntimeException;
|
14 |
38977
|
alessia.ba
|
import org.apache.commons.logging.Log;
|
15 |
|
|
import org.apache.commons.logging.LogFactory;
|
16 |
|
|
|
17 |
|
|
public class Re3DataRepositoriesIterator implements Iterator<String>, Iterable<String> {
|
18 |
|
|
|
19 |
|
|
private static final Log log = LogFactory.getLog(Re3DataRepositoriesIterator.class); // NOPMD by marko on 11/24/08 5:02 PM
|
20 |
|
|
|
21 |
|
|
private String baseURL;
|
22 |
|
|
private XMLStreamReader reader;
|
23 |
38995
|
alessia.ba
|
private int countedRepos = 0;
|
24 |
39005
|
claudio.at
|
private String currentRepoPath = null;
|
25 |
38977
|
alessia.ba
|
|
26 |
49638
|
alessia.ba
|
private HttpConnector httpConnector;
|
27 |
|
|
|
28 |
38977
|
alessia.ba
|
@Override
|
29 |
|
|
public boolean hasNext() {
|
30 |
39005
|
claudio.at
|
return currentRepoPath != null;
|
31 |
38977
|
alessia.ba
|
}
|
32 |
|
|
|
33 |
|
|
@Override
|
34 |
|
|
public String next() {
|
35 |
39005
|
claudio.at
|
if (currentRepoPath == null) throw new NoSuchElementException();
|
36 |
|
|
|
37 |
38977
|
alessia.ba
|
try {
|
38 |
39005
|
claudio.at
|
String repoInfo = getRepositoryInfo(currentRepoPath);
|
39 |
38985
|
alessia.ba
|
return repoInfo;
|
40 |
39005
|
claudio.at
|
} finally {
|
41 |
|
|
currentRepoPath = moveToNextRepo();
|
42 |
38977
|
alessia.ba
|
}
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
@Override
|
46 |
|
|
public void remove() {
|
47 |
|
|
throw new UnsupportedOperationException();
|
48 |
|
|
}
|
49 |
|
|
|
50 |
|
|
@Override
|
51 |
|
|
public Iterator<String> iterator() {
|
52 |
|
|
return this;
|
53 |
|
|
}
|
54 |
|
|
|
55 |
49638
|
alessia.ba
|
public Re3DataRepositoriesIterator(final InputStream xmlInputStream, final String baseUrl, final HttpConnector httpConnector) throws CollectorServiceException {
|
56 |
|
|
this.httpConnector = httpConnector;
|
57 |
38977
|
alessia.ba
|
XMLInputFactory factory = XMLInputFactory.newInstance();
|
58 |
39005
|
claudio.at
|
try {
|
59 |
|
|
reader = factory.createXMLStreamReader(xmlInputStream);
|
60 |
|
|
} catch (XMLStreamException e) {
|
61 |
|
|
throw new CollectorServiceException(e);
|
62 |
|
|
}
|
63 |
38977
|
alessia.ba
|
baseURL = baseUrl;
|
64 |
39005
|
claudio.at
|
|
65 |
|
|
// try to fetch the 1st
|
66 |
|
|
currentRepoPath = moveToNextRepo();
|
67 |
38977
|
alessia.ba
|
}
|
68 |
|
|
|
69 |
|
|
private String getNextRepositoryPath() {
|
70 |
|
|
return reader.getAttributeValue(null, "href");
|
71 |
|
|
}
|
72 |
|
|
|
73 |
39005
|
claudio.at
|
private String moveToNextRepo() {
|
74 |
|
|
try {
|
75 |
|
|
while (reader.hasNext()) {
|
76 |
|
|
int event = reader.next();
|
77 |
|
|
if (event == XMLStreamConstants.START_ELEMENT) {
|
78 |
|
|
String elementName = reader.getLocalName();
|
79 |
|
|
if (elementName.equals("link")) {
|
80 |
|
|
String repoPath = getNextRepositoryPath();
|
81 |
|
|
log.debug(String.format("Found %s repositories. The last has link %s", ++countedRepos, repoPath));
|
82 |
|
|
return repoPath;
|
83 |
|
|
}
|
84 |
38996
|
alessia.ba
|
}
|
85 |
38977
|
alessia.ba
|
}
|
86 |
39005
|
claudio.at
|
log.info("Seems there are no more repository to iterate on. Total: " + countedRepos);
|
87 |
|
|
return null;
|
88 |
|
|
} catch (XMLStreamException e) {
|
89 |
|
|
throw new CollectorServiceRuntimeException(e);
|
90 |
38977
|
alessia.ba
|
}
|
91 |
|
|
}
|
92 |
|
|
|
93 |
39005
|
claudio.at
|
private String getRepositoryInfo(final String repositoryPath) throws CollectorServiceRuntimeException {
|
94 |
43991
|
alessia.ba
|
String targetURL = repositoryPath;
|
95 |
|
|
if(!repositoryPath.startsWith(baseURL))
|
96 |
|
|
targetURL = baseURL + repositoryPath;
|
97 |
38985
|
alessia.ba
|
try {
|
98 |
49638
|
alessia.ba
|
return getHttpConnector().getInputSource(targetURL);
|
99 |
|
|
} catch (CollectorServiceException e) {
|
100 |
39005
|
claudio.at
|
throw new CollectorServiceRuntimeException("OOOPS something bad happen getting repo info from " + targetURL, e);
|
101 |
38977
|
alessia.ba
|
}
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
public String getBaseURL() {
|
105 |
|
|
return baseURL;
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
public void setBaseURL(final String baseURL) {
|
109 |
|
|
this.baseURL = baseURL;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
38995
|
alessia.ba
|
public int getCountedRepos() {
|
113 |
|
|
return countedRepos;
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
public void setCountedRepos(final int countedRepos) {
|
117 |
|
|
this.countedRepos = countedRepos;
|
118 |
|
|
}
|
119 |
|
|
|
120 |
49638
|
alessia.ba
|
public XMLStreamReader getReader() {
|
121 |
|
|
return reader;
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
public void setReader(final XMLStreamReader reader) {
|
125 |
|
|
this.reader = reader;
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
public String getCurrentRepoPath() {
|
129 |
|
|
return currentRepoPath;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
public void setCurrentRepoPath(final String currentRepoPath) {
|
133 |
|
|
this.currentRepoPath = currentRepoPath;
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
public HttpConnector getHttpConnector() {
|
137 |
|
|
return httpConnector;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
public void setHttpConnector(final HttpConnector httpConnector) {
|
141 |
|
|
this.httpConnector = httpConnector;
|
142 |
|
|
}
|
143 |
38977
|
alessia.ba
|
}
|