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