1 |
26600
|
sandro.lab
|
package eu.dnetlib.data.mdstore.modular;
|
2 |
|
|
|
3 |
|
|
import java.io.ByteArrayInputStream;
|
4 |
|
|
import java.util.HashMap;
|
5 |
|
|
import java.util.Map;
|
6 |
|
|
import java.util.Stack;
|
7 |
|
|
|
8 |
|
|
import javax.xml.stream.XMLInputFactory;
|
9 |
|
|
import javax.xml.stream.XMLStreamConstants;
|
10 |
|
|
import javax.xml.stream.XMLStreamException;
|
11 |
|
|
import javax.xml.stream.XMLStreamReader;
|
12 |
|
|
|
13 |
55328
|
alessia.ba
|
import org.apache.commons.logging.Log;
|
14 |
|
|
import org.apache.commons.logging.LogFactory;
|
15 |
|
|
|
16 |
52044
|
claudio.at
|
import static eu.dnetlib.data.mdstore.modular.MDStoreConstants.*;
|
17 |
|
|
|
18 |
26600
|
sandro.lab
|
/**
|
19 |
|
|
* This method outperforms SimpleRecordParser by a vast amount, especially since we are just getting stuff in the
|
20 |
|
|
* header.
|
21 |
|
|
*
|
22 |
|
|
* @author marko
|
23 |
|
|
*
|
24 |
|
|
*/
|
25 |
|
|
public class StreamingRecordParser implements RecordParser {
|
26 |
|
|
|
27 |
55328
|
alessia.ba
|
private static final Log log = LogFactory.getLog(StreamingRecordParser.class);
|
28 |
52042
|
claudio.at
|
private long ts;
|
29 |
|
|
|
30 |
26600
|
sandro.lab
|
@Override
|
31 |
|
|
public Map<String, String> parseRecord(String record) {
|
32 |
|
|
|
33 |
|
|
try {
|
34 |
|
|
XMLInputFactory factory = XMLInputFactory.newInstance();
|
35 |
|
|
XMLStreamReader parser = factory.createXMLStreamReader(new ByteArrayInputStream(record.getBytes()));
|
36 |
|
|
|
37 |
|
|
HashMap<String, String> res = new HashMap<String, String>();
|
38 |
52044
|
claudio.at
|
res.put(TIMESTAMP, String.valueOf(getTimestamp()));
|
39 |
26600
|
sandro.lab
|
|
40 |
|
|
Stack<String> elementStack = new Stack<String>();
|
41 |
|
|
elementStack.push("/");
|
42 |
|
|
|
43 |
|
|
while (parser.hasNext()) {
|
44 |
|
|
int event = parser.next();
|
45 |
|
|
|
46 |
|
|
if (event == XMLStreamConstants.END_ELEMENT) {
|
47 |
|
|
elementStack.pop();
|
48 |
|
|
} else if (event == XMLStreamConstants.START_ELEMENT) {
|
49 |
|
|
final String localName = parser.getLocalName();
|
50 |
|
|
elementStack.push(localName);
|
51 |
|
|
|
52 |
52044
|
claudio.at
|
if (OBJIDENTIFIER.equals(localName)) {
|
53 |
26600
|
sandro.lab
|
parser.next();
|
54 |
|
|
|
55 |
52044
|
claudio.at
|
res.put(ID, parser.getText().trim());
|
56 |
26600
|
sandro.lab
|
|
57 |
|
|
} else if ("identifier".equals(localName) && "efgEntity".equals(grandParent(elementStack))) {
|
58 |
|
|
if (!res.containsKey("originalId")) {
|
59 |
|
|
parser.next();
|
60 |
|
|
// log.info("ZZZZZZ OK: found identifier at right depth " + elementStack);
|
61 |
|
|
res.put("originalId", parser.getText().trim());
|
62 |
|
|
}
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
else if ("identifier".equals(localName)) {
|
66 |
|
|
|
67 |
|
|
// log.info("ZZZZZZ: found identifier not at right depth " + elementStack + " grand parent " + grandParent(elementStack));
|
68 |
|
|
}
|
69 |
|
|
|
70 |
52044
|
claudio.at
|
if (res.containsKey(ID) && res.containsKey("originalId"))
|
71 |
26600
|
sandro.lab
|
return res;
|
72 |
|
|
}
|
73 |
|
|
}
|
74 |
|
|
return res;
|
75 |
|
|
} catch (XMLStreamException e) {
|
76 |
|
|
throw new IllegalStateException(e);
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
private String grandParent(Stack<String> elementStack) {
|
82 |
|
|
if (elementStack.size() <= 3)
|
83 |
|
|
return "";
|
84 |
|
|
return elementStack.get(elementStack.size() - 3);
|
85 |
|
|
}
|
86 |
|
|
|
87 |
52042
|
claudio.at
|
@Override
|
88 |
|
|
public void setTimestamp(final long ts) {
|
89 |
|
|
this.ts = ts;
|
90 |
55328
|
alessia.ba
|
log.debug("RecordParser date set to "+ts);
|
91 |
52042
|
claudio.at
|
}
|
92 |
|
|
|
93 |
|
|
@Override
|
94 |
|
|
public long getTimestamp() {
|
95 |
|
|
return ts;
|
96 |
|
|
}
|
97 |
|
|
|
98 |
26600
|
sandro.lab
|
}
|