1
|
package eu.dnetlib.msro.openaireplus.workflows.nodes.datacite;
|
2
|
|
3
|
import java.io.ByteArrayInputStream;
|
4
|
import java.io.InputStream;
|
5
|
import java.io.StringWriter;
|
6
|
import java.nio.charset.StandardCharsets;
|
7
|
import java.util.ArrayList;
|
8
|
import java.util.List;
|
9
|
import java.util.Map;
|
10
|
import java.util.Queue;
|
11
|
import javax.xml.XMLConstants;
|
12
|
import javax.xml.parsers.DocumentBuilderFactory;
|
13
|
import javax.xml.transform.Transformer;
|
14
|
import javax.xml.transform.TransformerFactory;
|
15
|
import javax.xml.transform.dom.DOMSource;
|
16
|
import javax.xml.transform.stream.StreamResult;
|
17
|
import javax.xml.xpath.XPath;
|
18
|
import javax.xml.xpath.XPathConstants;
|
19
|
import javax.xml.xpath.XPathFactory;
|
20
|
|
21
|
import com.google.common.collect.Maps;
|
22
|
import org.apache.commons.lang.StringUtils;
|
23
|
import org.apache.commons.logging.Log;
|
24
|
import org.apache.commons.logging.LogFactory;
|
25
|
import org.w3c.dom.*;
|
26
|
|
27
|
/**
|
28
|
* The Class SplitterDatasetsIterator.
|
29
|
*/
|
30
|
public class SplitterDatasetsIterator {
|
31
|
|
32
|
/**
|
33
|
* The Constant log.
|
34
|
*/
|
35
|
private static final Log log = LogFactory.getLog(SplitterDatasetsIterator.class);
|
36
|
|
37
|
/**
|
38
|
* The end queue.
|
39
|
*/
|
40
|
public static String END_QUEUE = "END_QUEUE";
|
41
|
|
42
|
/**
|
43
|
* The publications.
|
44
|
*/
|
45
|
private Queue<String> publications;
|
46
|
|
47
|
/**
|
48
|
* The input epr.
|
49
|
*/
|
50
|
private Iterable<String> inputEPR;
|
51
|
|
52
|
/**
|
53
|
* The root name.
|
54
|
*/
|
55
|
private String rootName;
|
56
|
|
57
|
/**
|
58
|
* Instantiates a new splitter datasets iterator.
|
59
|
*
|
60
|
* @param datasetsQueue the datasets queue
|
61
|
* @param publicationsQueue the publications queue
|
62
|
* @param inputEPR the input epr
|
63
|
* @param rootName the root name
|
64
|
*/
|
65
|
public SplitterDatasetsIterator(final Queue<String> publicationsQueue, final Iterable<String> inputEPR, final String rootName) {
|
66
|
this.publications = publicationsQueue;
|
67
|
this.inputEPR = inputEPR;
|
68
|
this.rootName = rootName;
|
69
|
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Populate queues.
|
74
|
*/
|
75
|
public void populateQueues() {
|
76
|
|
77
|
if (this.inputEPR == null) return;
|
78
|
for (String inputXML : inputEPR) {
|
79
|
|
80
|
List<String> publication_extracted = extractByTag(new ByteArrayInputStream(inputXML.getBytes(StandardCharsets.UTF_8)), "publication", inputXML);
|
81
|
|
82
|
if (publication_extracted != null) {
|
83
|
publications.addAll(publication_extracted);
|
84
|
|
85
|
}
|
86
|
}
|
87
|
publications.add(END_QUEUE);
|
88
|
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* Extract by tag.
|
93
|
*
|
94
|
* @param inputXML the input xml
|
95
|
* @param tag the tag
|
96
|
* @return the list
|
97
|
*/
|
98
|
private List<String> extractByTag(final InputStream inputXML, final String tag, final String input) {
|
99
|
try {
|
100
|
|
101
|
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
102
|
Document doc = dbf.newDocumentBuilder().parse(inputXML);
|
103
|
|
104
|
XPath xpath = XPathFactory.newInstance().newXPath();
|
105
|
|
106
|
Node rootNode = (Node) xpath.evaluate("//*[local-name()='" + this.rootName + "']", doc, XPathConstants.NODE);
|
107
|
|
108
|
NamedNodeMap attributes = rootNode.getAttributes();
|
109
|
|
110
|
Map<String, String> nameSpaces = Maps.newHashMap();
|
111
|
|
112
|
for (int i = 0; i < attributes.getLength(); i++) {
|
113
|
Node node = attributes.item(i);
|
114
|
String name = node.getNodeName();
|
115
|
if (name.startsWith("xmlns:")) {
|
116
|
nameSpaces.put(StringUtils.substringAfter(name, "xmlns:"), node.getNodeValue());
|
117
|
}
|
118
|
|
119
|
}
|
120
|
xpath = XPathFactory.newInstance().newXPath();
|
121
|
NodeList nodes = (NodeList) xpath.evaluate("//*[local-name()='" + tag + "']/*[local-name()='record']", doc, XPathConstants.NODESET);
|
122
|
|
123
|
if ((nodes != null) && (nodes.getLength() > 0)) {
|
124
|
List<String> result = new ArrayList<String>();
|
125
|
for (int i = 0; i < nodes.getLength(); i++) {
|
126
|
Document currentDoc = dbf.newDocumentBuilder().newDocument();
|
127
|
Node imported = currentDoc.importNode(nodes.item(i), true);
|
128
|
for (String key : nameSpaces.keySet()) {
|
129
|
Element element = (Element) imported;
|
130
|
element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + key, nameSpaces.get(key));
|
131
|
}
|
132
|
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
133
|
DOMSource mydoc = new DOMSource(imported);
|
134
|
StringWriter writer = new StringWriter();
|
135
|
transformer.transform(mydoc, new StreamResult(writer));
|
136
|
String record = writer.toString();
|
137
|
result.add(record);
|
138
|
}
|
139
|
return result;
|
140
|
}
|
141
|
} catch (Exception e) {
|
142
|
log.error("Error on extracting " + tag, e);
|
143
|
return null;
|
144
|
}
|
145
|
return null;
|
146
|
}
|
147
|
}
|