Project

General

Profile

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

    
3
import java.io.BufferedReader;
4
import java.io.FileReader;
5
import java.io.IOException;
6
import java.util.Iterator;
7

    
8
import com.google.gson.JsonElement;
9
import com.google.gson.JsonObject;
10
import com.google.gson.JsonParser;
11

    
12
public class MongoDumpIterator implements Iterator<String> {
13

    
14
	private final BufferedReader inputStream;
15
	private String currentLine = null;
16

    
17
	public MongoDumpIterator(final FileReader inputStream) {
18
		this.inputStream = new BufferedReader(inputStream);
19
		this.currentLine = getNextLine();
20
	}
21

    
22
	@Override
23
	public boolean hasNext() {
24
		return currentLine != null;
25

    
26
	}
27

    
28
	@Override
29
	public String next() {
30
		final String returnedString = this.currentLine;
31
		this.currentLine = getNextLine();
32
		return returnedString;
33
	}
34

    
35
	@Override
36
	public void remove() {
37
		// TODO Auto-generated method stub
38

    
39
	}
40

    
41
	private String getNextLine() {
42
		try {
43
			String input = inputStream.readLine();
44
			while (input != null) {
45
				JsonElement jElement = new JsonParser().parse(input);
46
				JsonObject jobject = jElement.getAsJsonObject();
47
				if (jobject.has("body")) { return jobject.get("body").getAsString(); }
48
				input = inputStream.readLine();
49
			}
50
			return null;
51

    
52
		} catch (IOException e) {
53
			return null;
54
		}
55
	}
56
}
(2-2/3)