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