Project

General

Profile

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