1
|
package eu.dnetlib.data.transform.xml2;
|
2
|
|
3
|
import java.nio.charset.Charset;
|
4
|
import java.util.Collection;
|
5
|
import java.util.Map;
|
6
|
import java.util.stream.Collectors;
|
7
|
import java.util.stream.Stream;
|
8
|
|
9
|
import com.google.common.collect.Maps;
|
10
|
import com.google.protobuf.Descriptors;
|
11
|
import com.google.protobuf.InvalidProtocolBufferException;
|
12
|
import com.google.protobuf.Message;
|
13
|
import eu.dnetlib.data.proto.FieldTypeProtos.*;
|
14
|
import eu.dnetlib.data.proto.OafProtos.OafRel;
|
15
|
import eu.dnetlib.data.proto.ResultProtos.Result.Context;
|
16
|
import eu.dnetlib.data.proto.ResultProtos.Result.ExternalReference;
|
17
|
import eu.dnetlib.data.transform.xml.AbstractDNetXsltFunctions;
|
18
|
import org.apache.commons.lang3.StringUtils;
|
19
|
import org.apache.commons.lang3.math.NumberUtils;
|
20
|
|
21
|
import static eu.dnetlib.data.transform.xml2.VtdUtilityParser.xpath;
|
22
|
|
23
|
public class Utils {
|
24
|
|
25
|
public static final String URL_REGEX = "^(http|https|ftp)\\://.*";
|
26
|
|
27
|
public static final String ID_SEPARATOR = "::";
|
28
|
|
29
|
public static final String TITLE_TYPE = "titleType";
|
30
|
public static final String DATE_TYPE = "dateType";
|
31
|
public static final String KEYWORD = "keyword";
|
32
|
|
33
|
public static final String DNET_EXT_REF_TYPOLOGIES = "dnet:externalReference_typologies";
|
34
|
public static final String DNET_TITLE_TYPOLOGIES = "dnet:dataCite_title";
|
35
|
public static final String DNET_SUBJECT_TYPOLOGIES = "dnet:subject_classification_typologies";
|
36
|
public static final String DNET_RESULT_TYPOLOGIES = "dnet:result_typologies";
|
37
|
public static final String DNET_PUBLICATION_RESOURCE = "dnet:publication_resource";
|
38
|
public static final String DNET_DATA_CITE_RESOURCE = "dnet:dataCite_resource";
|
39
|
public static final String DNET_ACCESS_MODES = "dnet:access_modes";
|
40
|
public static final String DNET_LANGUAGES = "dnet:languages";
|
41
|
public static final String DNET_PID_TYPES = "dnet:pid_types";
|
42
|
|
43
|
public static final String IDENTIFIER_TYPE = "identifierType";
|
44
|
public static final String ALTERNATE_IDENTIFIER_TYPE = "alternateIdentifierType";
|
45
|
public static final String DNET_PROVENANCE_ACTIONS = "dnet:provenanceActions";
|
46
|
|
47
|
public static final String CLASSID = "classid";
|
48
|
public static final String CLASSNAME = "classname";
|
49
|
public static final String SCHEMEID = "schemeid";
|
50
|
public static final String SCHEMENAME = "schemename";
|
51
|
|
52
|
public static final String RELATION_TYPE = "relationType";
|
53
|
public static final String RELATED_IDENTIFIER_TYPE = "relatedIdentifierType";
|
54
|
public static final String RIGHTS_URI = "rightsURI";
|
55
|
|
56
|
public static final String UTF_8 = "UTF-8";
|
57
|
|
58
|
// publication
|
59
|
public static final String PROJECTID = "projectid";
|
60
|
public static final String RELATED_DATASET = "relatedDataSet";
|
61
|
public static final String RELATED_PUBLICATION = "relatedPublication";
|
62
|
public static final String RELATED_IDENTIFIER = "relatedIdentifier";
|
63
|
|
64
|
protected static Map<String, String> mappingAccess = Maps.newHashMap();
|
65
|
|
66
|
static {
|
67
|
mappingAccess.put("info:eu-repo/semantics/openAccess", "OPEN");
|
68
|
mappingAccess.put("info:eu-repo/semantics/closedAccess", "CLOSED");
|
69
|
mappingAccess.put("info:eu-repo/semantics/restrictedAccess", "RESTRICTED");
|
70
|
mappingAccess.put("info:eu-repo/semantics/embargoedAccess", "EMBARGO");
|
71
|
|
72
|
// Transformator now maps the access rights into proper values, not sure if it does for all datasets.
|
73
|
mappingAccess.put("OPEN", "OPEN");
|
74
|
mappingAccess.put("CLOSED", "CLOSED");
|
75
|
mappingAccess.put("RESTRICTED", "RESTRICTED");
|
76
|
mappingAccess.put("EMBARGO", "EMBARGO");
|
77
|
}
|
78
|
|
79
|
public static String getValue(final Node node, final String defaultValue) {
|
80
|
return (node != null && StringUtils.isNotBlank(node.getTextValue())) ? node.getTextValue() : defaultValue;
|
81
|
}
|
82
|
|
83
|
public static String getValue(final String value, final String defaultValue) {
|
84
|
return StringUtils.isNotBlank(value) ? value : defaultValue;
|
85
|
}
|
86
|
|
87
|
public static KeyValue getKV(final String id, final String name) {
|
88
|
return KeyValue.newBuilder().setKey(id).setValue(name).build();
|
89
|
}
|
90
|
|
91
|
public static Qualifier getSimpleQualifier(final String classname, final String schemename) {
|
92
|
return getQualifier(classname, classname, schemename, schemename);
|
93
|
}
|
94
|
|
95
|
public static Qualifier getQualifier(final String classid, final String classname, final String schemeid, final String schemename) {
|
96
|
return Qualifier.newBuilder().setClassid(classid).setClassname(classname).setSchemeid(schemeid).setSchemename(schemename).build();
|
97
|
}
|
98
|
|
99
|
public static StructuredProperty getStructuredProperty(final String value,
|
100
|
final String classid,
|
101
|
final String classname,
|
102
|
final String schemeid,
|
103
|
final String schemename) {
|
104
|
if ((value == null) || value.isEmpty()) return null;
|
105
|
return StructuredProperty.newBuilder().setValue(value).setQualifier(getQualifier(classid, classname, schemeid, schemename)).build();
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* Gets the classname of the given class code
|
110
|
*
|
111
|
* @param code class code.
|
112
|
* @return the class name, if the code is a key of the map. The code itself otherwise.
|
113
|
*/
|
114
|
public static String getClassName(final String code) {
|
115
|
final String classname = AbstractDNetXsltFunctions.code2name.get(code);
|
116
|
if (StringUtils.isBlank(classname)) return code;
|
117
|
return classname;
|
118
|
}
|
119
|
|
120
|
public static String metadataXpath(final String otherValues) {
|
121
|
return xpath("record", "metadata", otherValues);
|
122
|
}
|
123
|
|
124
|
public static void addField(final Message.Builder builder, final Descriptors.FieldDescriptor descriptor, final Object value) {
|
125
|
|
126
|
if (value == null) return;
|
127
|
|
128
|
if (value instanceof Stream) {
|
129
|
addField(builder, descriptor, ((Stream) value).collect(Collectors.toList()));
|
130
|
} else if (value instanceof Collection<?>) {
|
131
|
for (final Object o : (Collection<Object>) value) {
|
132
|
addField(builder, descriptor, o);
|
133
|
}
|
134
|
} else {
|
135
|
Object v = value;
|
136
|
switch (descriptor.getType()) {
|
137
|
case BOOL:
|
138
|
v = Boolean.valueOf(value.toString());
|
139
|
break;
|
140
|
case BYTES:
|
141
|
v = value.toString().getBytes(Charset.forName(UTF_8));
|
142
|
break;
|
143
|
case DOUBLE:
|
144
|
v = Double.valueOf(value.toString());
|
145
|
break;
|
146
|
case FLOAT:
|
147
|
v = Float.valueOf(value.toString());
|
148
|
break;
|
149
|
case INT32:
|
150
|
case INT64:
|
151
|
case SINT32:
|
152
|
case SINT64:
|
153
|
v = Integer.valueOf(value.toString());
|
154
|
break;
|
155
|
case MESSAGE:
|
156
|
final Message.Builder q = builder.newBuilderForField(descriptor);
|
157
|
|
158
|
if (value instanceof Message.Builder) {
|
159
|
v = ((Message.Builder) value).build();
|
160
|
final byte[] b = ((Message) v).toByteArray();
|
161
|
try {
|
162
|
q.mergeFrom(b);
|
163
|
} catch (final InvalidProtocolBufferException e) {
|
164
|
throw new IllegalArgumentException("Unable to merge value: " + v + " with builder: " + q.getDescriptorForType().getName());
|
165
|
}
|
166
|
} else if (Qualifier.getDescriptor().getName().equals(q.getDescriptorForType().getName())) {
|
167
|
if (value instanceof Qualifier) {
|
168
|
q.mergeFrom((Qualifier) v);
|
169
|
}
|
170
|
} else if (StructuredProperty.getDescriptor().getName().equals(q.getDescriptorForType().getName())) {
|
171
|
if (value instanceof StructuredProperty) {
|
172
|
q.mergeFrom((StructuredProperty) v);
|
173
|
}
|
174
|
} else if (KeyValue.getDescriptor().getName().equals(q.getDescriptorForType().getName())) {
|
175
|
if (value instanceof KeyValue) {
|
176
|
q.mergeFrom((KeyValue) v);
|
177
|
}
|
178
|
} else if (Journal.getDescriptor().getName().equals(q.getDescriptorForType().getName())) {
|
179
|
if (value instanceof Journal) {
|
180
|
q.mergeFrom((Journal) v);
|
181
|
}
|
182
|
} else if (Context.getDescriptor().getName().equals(q.getDescriptorForType().getName())) {
|
183
|
if (value instanceof Context) {
|
184
|
q.mergeFrom((Context) v);
|
185
|
}
|
186
|
} else if (Author.getDescriptor().getName().equals(q.getDescriptorForType().getName())) {
|
187
|
if (value instanceof Author) {
|
188
|
q.mergeFrom((Author) v);
|
189
|
}
|
190
|
} else if (ExternalReference.getDescriptor().getName().equals(q.getDescriptorForType().getName())) {
|
191
|
if (value instanceof ExternalReference) {
|
192
|
q.mergeFrom((ExternalReference) v);
|
193
|
}
|
194
|
} else if (OafRel.getDescriptor().getName().equals(q.getDescriptorForType().getName())) {
|
195
|
if (value instanceof OafRel) {
|
196
|
q.mergeFrom((OafRel) v);
|
197
|
}
|
198
|
} else if (StringField.getDescriptor().getName().equals(q.getDescriptorForType().getName())) {
|
199
|
if (value instanceof StringField) {
|
200
|
q.mergeFrom((StringField) v);
|
201
|
} else {
|
202
|
q.setField(StringField.getDescriptor().findFieldByName("value"), v);
|
203
|
}
|
204
|
} else if (BoolField.getDescriptor().getName().equals(q.getDescriptorForType().getName())) {
|
205
|
if (value instanceof BoolField) {
|
206
|
q.mergeFrom((BoolField) v);
|
207
|
} else if (value instanceof String) {
|
208
|
q.setField(BoolField.getDescriptor().findFieldByName("value"), Boolean.valueOf((String) v));
|
209
|
} else {
|
210
|
q.setField(BoolField.getDescriptor().findFieldByName("value"), v);
|
211
|
}
|
212
|
} else if (IntField.getDescriptor().getName().equals(q.getDescriptorForType().getName())) {
|
213
|
if (value instanceof IntField) {
|
214
|
q.mergeFrom((IntField) v);
|
215
|
} else if (value instanceof String) {
|
216
|
q.setField(IntField.getDescriptor().findFieldByName("value"), NumberUtils.toInt((String) v));
|
217
|
} else {
|
218
|
q.setField(IntField.getDescriptor().findFieldByName("value"), v);
|
219
|
}
|
220
|
}
|
221
|
|
222
|
v = q.buildPartial();
|
223
|
break;
|
224
|
default:
|
225
|
break;
|
226
|
}
|
227
|
|
228
|
doAddField(builder, descriptor, v);
|
229
|
}
|
230
|
}
|
231
|
|
232
|
private static void doAddField(final Message.Builder builder, final Descriptors.FieldDescriptor fd, final Object value) {
|
233
|
if (value != null) {
|
234
|
if (fd.isRepeated()) {
|
235
|
builder.addRepeatedField(fd, value);
|
236
|
} else if (fd.isOptional() || fd.isRequired()) {
|
237
|
builder.setField(fd, value);
|
238
|
}
|
239
|
}
|
240
|
}
|
241
|
|
242
|
}
|