Project

General

Profile

1
package eu.dnetlib.data.collector.plugins.datasources;
2

    
3
import java.io.InputStream;
4
import java.util.Iterator;
5
import java.util.NoSuchElementException;
6
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
import eu.dnetlib.data.collector.plugins.HttpConnector;
12
import eu.dnetlib.data.collector.rmi.CollectorServiceException;
13
import eu.dnetlib.data.collector.rmi.CollectorServiceRuntimeException;
14
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
	private int countedRepos = 0;
24
	private String currentRepoPath = null;
25

    
26
	private HttpConnector httpConnector;
27

    
28
	@Override
29
	public boolean hasNext() {
30
		return currentRepoPath != null;
31
	}
32

    
33
	@Override
34
	public String next() {
35
		if (currentRepoPath == null) throw new NoSuchElementException();
36

    
37
		try {
38
			String repoInfo = getRepositoryInfo(currentRepoPath);
39
			return repoInfo;
40
		} finally {
41
			currentRepoPath = moveToNextRepo();
42
		}
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
	public Re3DataRepositoriesIterator(final InputStream xmlInputStream, final String baseUrl, final HttpConnector httpConnector) throws CollectorServiceException {
56
		this.httpConnector = httpConnector;
57
		XMLInputFactory factory = XMLInputFactory.newInstance();
58
		try {
59
			reader = factory.createXMLStreamReader(xmlInputStream);
60
		} catch (XMLStreamException e) {
61
			throw new CollectorServiceException(e);
62
		}
63
		baseURL = baseUrl;
64

    
65
		// try to fetch the 1st
66
		currentRepoPath = moveToNextRepo();
67
	}
68

    
69
	private String getNextRepositoryPath() {
70
		return reader.getAttributeValue(null, "href");
71
	}
72

    
73
	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
				}
85
			}
86
			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
		}
91
	}
92

    
93
	private String getRepositoryInfo(final String repositoryPath) throws CollectorServiceRuntimeException {
94
		String targetURL = repositoryPath;
95
		if(!repositoryPath.startsWith(baseURL))
96
			targetURL = baseURL + repositoryPath;
97
		try {
98
			return getHttpConnector().getInputSource(targetURL);
99
		} catch (CollectorServiceException e) {
100
			throw new CollectorServiceRuntimeException("OOOPS something bad happen getting repo info from " + targetURL, e);
101
		}
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
	public int getCountedRepos() {
113
		return countedRepos;
114
	}
115

    
116
	public void setCountedRepos(final int countedRepos) {
117
		this.countedRepos = countedRepos;
118
	}
119

    
120
	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
}
(2-2/2)