1
|
package eu.dnetlib.clients.index.utils;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.List;
|
5
|
import java.util.Map;
|
6
|
|
7
|
import com.google.common.collect.HashBasedTable;
|
8
|
import com.google.common.collect.Table;
|
9
|
import com.mycila.xmltool.CallBack;
|
10
|
import com.mycila.xmltool.XMLDoc;
|
11
|
import com.mycila.xmltool.XMLTag;
|
12
|
import eu.dnetlib.clients.index.client.IndexClientException;
|
13
|
import org.dom4j.Document;
|
14
|
import org.dom4j.DocumentException;
|
15
|
import org.dom4j.io.SAXReader;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
import org.xml.sax.InputSource;
|
18
|
|
19
|
;
|
20
|
|
21
|
public class MDFormatReader {
|
22
|
|
23
|
@Autowired
|
24
|
private ServiceTools serviceTools;
|
25
|
@Autowired
|
26
|
private MetadataReferenceFactory mdFactory;
|
27
|
|
28
|
public List<MetadataReference> listMDRefs() throws IndexClientException {
|
29
|
return serviceTools.listMDRefs();
|
30
|
}
|
31
|
|
32
|
public Document getFields(final MetadataReference mdRef) {
|
33
|
String fields = serviceTools.getIndexFields(mdRef);
|
34
|
return (fields != null) && !fields.isEmpty() ? XmlUtils.parse(fields) : null;
|
35
|
}
|
36
|
|
37
|
public Map<String, String> getAttributeMap(final MetadataReference mdRef, final String attribute) throws IndexClientException {
|
38
|
return getAttributeTable(mdRef, attribute).column(attribute);
|
39
|
}
|
40
|
|
41
|
public Table<String, String, String> getAttributeTable(final MetadataReference mdRef, final String... attributeList) throws IndexClientException {
|
42
|
|
43
|
final String fields = serviceTools.getIndexFields(mdRef);
|
44
|
if (fields.isEmpty()) throw new IndexClientException("No result getting index layout informations");
|
45
|
final Table<String, String, String> t = HashBasedTable.create();
|
46
|
XMLDoc.from(serviceTools.getIndexFields(mdRef), false).forEach("//FIELD", new CallBack() {
|
47
|
|
48
|
@Override
|
49
|
public void execute(final XMLTag field) {
|
50
|
|
51
|
for (String attribute : attributeList) {
|
52
|
String value = null;
|
53
|
|
54
|
if ("xpath".equals(attribute)) {
|
55
|
if (!(field.hasAttribute("xpath") || field.hasAttribute("value"))) return;
|
56
|
|
57
|
value = field.hasAttribute("xpath") ? field.getAttribute("xpath") : field.getAttribute("value");
|
58
|
}
|
59
|
|
60
|
if ("weight".equals(attribute)) {
|
61
|
if (!(field.hasAttribute(attribute))) return;
|
62
|
|
63
|
value = field.hasAttribute(attribute) ? field.getAttribute(attribute) : "";
|
64
|
}
|
65
|
|
66
|
if (value == null) {
|
67
|
value = field.getAttribute(attribute);
|
68
|
}
|
69
|
|
70
|
t.put(field.getAttribute("name").toLowerCase(), attribute, value);
|
71
|
}
|
72
|
}
|
73
|
});
|
74
|
return t;
|
75
|
}
|
76
|
|
77
|
static class XmlUtils {
|
78
|
|
79
|
/**
|
80
|
* helper method, parses a list of fields.
|
81
|
* <p>
|
82
|
* <p>
|
83
|
* the given fields
|
84
|
*
|
85
|
* @return the parsed fields
|
86
|
* if cannot parse the fields
|
87
|
*/
|
88
|
public static Document parse(final String xml) {
|
89
|
try {
|
90
|
return new SAXReader().read(new StringReader(xml));
|
91
|
} catch (DocumentException e) {
|
92
|
throw new IllegalArgumentException("cannot parse: " + xml);
|
93
|
}
|
94
|
}
|
95
|
|
96
|
public static InputSource asInputSource(final String input) throws DocumentException {
|
97
|
return new InputSource(new StringReader(parse(input).asXML()));
|
98
|
}
|
99
|
}
|
100
|
|
101
|
}
|