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
import eu.dnetlib.data.collector.plugins.oai.engine.XmlCleaner;
17

    
18
public class Re3DataRepositoriesIterator implements Iterator<String>, Iterable<String> {
19

    
20
	private static final Log log = LogFactory.getLog(Re3DataRepositoriesIterator.class); // NOPMD by marko on 11/24/08 5:02 PM
21

    
22
	private String baseURL;
23
	private XMLStreamReader reader;
24
	private int countedRepos = 0;
25
	private String currentRepoPath = null;
26

    
27
	private HttpConnector httpConnector;
28

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

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

    
38
		try {
39
			String repoInfo = getRepositoryInfo(currentRepoPath);
40
			return repoInfo;
41
		} finally {
42
			currentRepoPath = moveToNextRepo();
43
		}
44
	}
45

    
46
	@Override
47
	public void remove() {
48
		throw new UnsupportedOperationException();
49
	}
50

    
51
	@Override
52
	public Iterator<String> iterator() {
53
		return this;
54
	}
55

    
56
	public Re3DataRepositoriesIterator(final InputStream xmlInputStream, final String baseUrl, final HttpConnector httpConnector) throws CollectorServiceException {
57
		this.httpConnector = httpConnector;
58
		XMLInputFactory factory = XMLInputFactory.newInstance();
59
		try {
60
			reader = factory.createXMLStreamReader(xmlInputStream);
61
		} catch (XMLStreamException e) {
62
			throw new CollectorServiceException(e);
63
		}
64
		baseURL = baseUrl;
65

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

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

    
74
	private String moveToNextRepo() {
75
		try {
76
			while (reader.hasNext()) {
77
				int event = reader.next();
78
				if (event == XMLStreamConstants.START_ELEMENT) {
79
					String elementName = reader.getLocalName();
80
					if (elementName.equals("link")) {
81
						String repoPath = getNextRepositoryPath();
82
						log.debug(String.format("Found %s repositories. The last has link %s", ++countedRepos, repoPath));
83
						return repoPath;
84
					}
85
				}
86
			}
87
			log.info("Seems there are no more repository to iterate on. Total: " + countedRepos);
88
			return null;
89
		} catch (XMLStreamException e) {
90
			throw new CollectorServiceRuntimeException(e);
91
		}
92
	}
93

    
94
	private String getRepositoryInfo(final String repositoryPath) throws CollectorServiceRuntimeException {
95

    
96
		String targetURL = repositoryPath;
97
		if(!repositoryPath.startsWith(baseURL))
98
			targetURL = baseURL + repositoryPath;
99
		try {
100
			log.debug(targetURL);
101
			String inputSource = getHttpConnector().getInputSource(targetURL);
102

    
103
			return XmlCleaner.cleanAllEntities(inputSource);
104
		} catch (CollectorServiceException e) {
105
			throw new CollectorServiceRuntimeException("OOOPS something bad happen getting repo info from " + targetURL, e);
106
		}
107
	}
108

    
109
//	public String testAccess(){
110
//		return getRepositoryInfo("/api/v1/repository/r3d100012823");
111
//	}
112
	public String getBaseURL() {
113
		return baseURL;
114
	}
115

    
116
	public void setBaseURL(final String baseURL) {
117
		this.baseURL = baseURL;
118
	}
119

    
120
	public int getCountedRepos() {
121
		return countedRepos;
122
	}
123

    
124
	public void setCountedRepos(final int countedRepos) {
125
		this.countedRepos = countedRepos;
126
	}
127

    
128
	public XMLStreamReader getReader() {
129
		return reader;
130
	}
131

    
132
	public void setReader(final XMLStreamReader reader) {
133
		this.reader = reader;
134
	}
135

    
136
	public String getCurrentRepoPath() {
137
		return currentRepoPath;
138
	}
139

    
140
	public void setCurrentRepoPath(final String currentRepoPath) {
141
		this.currentRepoPath = currentRepoPath;
142
	}
143

    
144
	public HttpConnector getHttpConnector() {
145
		return httpConnector;
146
	}
147

    
148
	public void setHttpConnector(final HttpConnector httpConnector) {
149
		this.httpConnector = httpConnector;
150
	}
151
}
(2-2/2)