1
|
package eu.dnetlib.data.mapreduce.util;
|
2
|
|
3
|
import java.util.Arrays;
|
4
|
import java.util.List;
|
5
|
import java.util.Map.Entry;
|
6
|
|
7
|
import com.google.protobuf.*;
|
8
|
import com.google.protobuf.Descriptors.EnumValueDescriptor;
|
9
|
import com.google.protobuf.Descriptors.FieldDescriptor;
|
10
|
|
11
|
import com.google.protobuf.GeneratedMessage.GeneratedExtension;
|
12
|
import eu.dnetlib.data.proto.DNGFProtos.DNGF;
|
13
|
import eu.dnetlib.data.proto.DNGFProtos.DNGFEntity;
|
14
|
import eu.dnetlib.data.proto.DNGFProtos.DNGFRel;
|
15
|
import eu.dnetlib.data.proto.FieldTypeProtos.Qualifier;
|
16
|
import eu.dnetlib.data.proto.KindProtos.Kind;
|
17
|
import eu.dnetlib.data.proto.RelTypeProtos.RelType;
|
18
|
|
19
|
/**
|
20
|
* Helper class, to be used as accessor helper over the DNGF structure.
|
21
|
*
|
22
|
* @author claudio
|
23
|
*
|
24
|
*/
|
25
|
public class DNGFDecoder {
|
26
|
|
27
|
/**
|
28
|
* DNGF object
|
29
|
*/
|
30
|
private DNGF dngf;
|
31
|
|
32
|
/**
|
33
|
* Cached sub decoder
|
34
|
*/
|
35
|
private DNGFEntityDecoder entityDecoder = null;
|
36
|
|
37
|
/**
|
38
|
* Cached sub decoder
|
39
|
*/
|
40
|
private DNGFRelDecoder relDecoder = null;
|
41
|
|
42
|
public static DNGFDecoder decode(final DNGF dngf) {
|
43
|
return new DNGFDecoder(dngf);
|
44
|
}
|
45
|
|
46
|
public static DNGFDecoder decode(final byte[] b) {
|
47
|
return new DNGFDecoder(b);
|
48
|
}
|
49
|
|
50
|
public static DNGFDecoder decode(final byte[] b, final GeneratedExtension... ge) {
|
51
|
return new DNGFDecoder(b, ge);
|
52
|
}
|
53
|
|
54
|
protected DNGFDecoder(final byte[] value) {
|
55
|
this(value, null);
|
56
|
}
|
57
|
|
58
|
protected DNGFDecoder(final byte[] value, final GeneratedExtension... ge) {
|
59
|
try {
|
60
|
final ExtensionRegistry registry = ExtensionRegistry.newInstance();
|
61
|
if (ge != null) {
|
62
|
for(GeneratedExtension e : ge) {
|
63
|
registry.add(e);
|
64
|
}
|
65
|
}
|
66
|
this.dngf = DNGF.parseFrom(value, registry);
|
67
|
|
68
|
} catch (InvalidProtocolBufferException e) {
|
69
|
throw new RuntimeException("unable to deserialize proto: " + new String(value));
|
70
|
}
|
71
|
}
|
72
|
|
73
|
private DNGFDecoder(final DNGF dngf) {
|
74
|
this.dngf = dngf;
|
75
|
}
|
76
|
|
77
|
public Kind getKind() {
|
78
|
return dngf.getKind();
|
79
|
}
|
80
|
|
81
|
public DNGF getDNGF() {
|
82
|
return dngf;
|
83
|
}
|
84
|
|
85
|
// / Entity
|
86
|
|
87
|
public GeneratedMessage getMetadata() {
|
88
|
return decodeEntity().getMetadata();
|
89
|
}
|
90
|
|
91
|
public GeneratedMessage getDNGFEntity() {
|
92
|
return decodeEntity().getEntity();
|
93
|
}
|
94
|
|
95
|
public String getEntityId() {
|
96
|
return decodeEntity().getId();
|
97
|
}
|
98
|
|
99
|
public DNGFEntity getEntity() {
|
100
|
return dngf.getEntity();
|
101
|
}
|
102
|
|
103
|
public DNGFEntityDecoder decodeEntity() {
|
104
|
if (entityDecoder == null) {
|
105
|
entityDecoder = DNGFEntityDecoder.decode(getEntity());
|
106
|
}
|
107
|
return entityDecoder;
|
108
|
}
|
109
|
|
110
|
// / Rel
|
111
|
|
112
|
public DNGFRel getDNGFRel() {
|
113
|
return dngf.getRel();
|
114
|
}
|
115
|
|
116
|
public GeneratedMessage getRel() {
|
117
|
return decodeRel().getRel();
|
118
|
}
|
119
|
|
120
|
public Qualifier relType() {
|
121
|
return decodeRel().getRelType();
|
122
|
}
|
123
|
|
124
|
public String relTypeName() {
|
125
|
return relType().toString();
|
126
|
}
|
127
|
|
128
|
public String relSourceId() {
|
129
|
return decodeRel().getRelSourceId();
|
130
|
}
|
131
|
|
132
|
public String relTargetId() {
|
133
|
return decodeRel().getRelTargetId();
|
134
|
}
|
135
|
|
136
|
private DNGFRelDecoder decodeRel() {
|
137
|
if (relDecoder == null) {
|
138
|
relDecoder = DNGFRelDecoder.decode(getDNGFRel());
|
139
|
}
|
140
|
return relDecoder;
|
141
|
}
|
142
|
|
143
|
public byte[] toByteArray() {
|
144
|
return dngf.toByteArray();
|
145
|
}
|
146
|
|
147
|
public String asXml() {
|
148
|
StringBuilder sb = new StringBuilder("<oaf>");
|
149
|
|
150
|
for (Entry<FieldDescriptor, Object> e : dngf.getAllFields().entrySet()) {
|
151
|
asXml(sb, e.getKey(), e.getValue());
|
152
|
}
|
153
|
sb.append("</oaf>");
|
154
|
return sb.toString();
|
155
|
}
|
156
|
|
157
|
@SuppressWarnings("unchecked")
|
158
|
private void asXml(final StringBuilder sb, final FieldDescriptor fd, final Object value) {
|
159
|
|
160
|
if (fd.isRepeated() && (value instanceof List<?>)) {
|
161
|
for (Object o : ((List<Object>) value)) {
|
162
|
asXml(sb, fd, o);
|
163
|
}
|
164
|
} else if (fd.getType().equals(FieldDescriptor.Type.MESSAGE)) {
|
165
|
sb.append("<" + fd.getName() + ">");
|
166
|
for (Entry<FieldDescriptor, Object> e : ((Message) value).getAllFields().entrySet()) {
|
167
|
asXml(sb, e.getKey(), e.getValue());
|
168
|
}
|
169
|
sb.append("</" + fd.getName() + ">");
|
170
|
} else if (fd.getType().equals(FieldDescriptor.Type.ENUM)) {
|
171
|
sb.append("<" + fd.getName() + ">");
|
172
|
sb.append(((EnumValueDescriptor) value).getName());
|
173
|
sb.append("</" + fd.getName() + ">");
|
174
|
} else {
|
175
|
sb.append("<" + fd.getName() + ">");
|
176
|
sb.append(escapeXml(value.toString()));
|
177
|
sb.append("</" + fd.getName() + ">");
|
178
|
}
|
179
|
}
|
180
|
|
181
|
private static String escapeXml(final String value) {
|
182
|
return value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
183
|
}
|
184
|
|
185
|
}
|