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