1
|
package eu.dnetlib.dli.resolver.model.serializer;
|
2
|
|
3
|
import java.io.ByteArrayOutputStream;
|
4
|
import java.io.StringWriter;
|
5
|
import java.time.LocalDateTime;
|
6
|
|
7
|
import com.ximpleware.AutoPilot;
|
8
|
import com.ximpleware.VTDGen;
|
9
|
import com.ximpleware.VTDNav;
|
10
|
import com.ximpleware.XMLModifier;
|
11
|
import eu.dnetlib.dli.resolver.model.DLIResolvedObject;
|
12
|
|
13
|
import eu.dnetlib.pid.resolver.model.ObjectType;
|
14
|
import org.antlr.stringtemplate.NoIndentWriter;
|
15
|
import org.antlr.stringtemplate.StringTemplate;
|
16
|
import org.apache.commons.logging.Log;
|
17
|
import org.apache.commons.logging.LogFactory;
|
18
|
import org.springframework.beans.factory.annotation.Required;
|
19
|
|
20
|
/**
|
21
|
* Created by sandro on 9/21/16.
|
22
|
*/
|
23
|
public class ResolverSerializer {
|
24
|
|
25
|
private static final Log log = LogFactory.getLog(ResolverSerializer.class);
|
26
|
|
27
|
private StringTemplate dmfTemplate;
|
28
|
|
29
|
private StringTemplate pmfTemplate;
|
30
|
|
31
|
private StringTemplate scholixTemplate;
|
32
|
|
33
|
|
34
|
public String serializeToXML(final DLIResolvedObject object) {
|
35
|
StringWriter writer = new StringWriter(16);
|
36
|
NoIndentWriter out = new NoIndentWriter(writer);
|
37
|
|
38
|
if (object.getType() == null)
|
39
|
object.setType(ObjectType.unknown);
|
40
|
|
41
|
switch (object.getType()) {
|
42
|
case publication: {
|
43
|
return serialize(pmfTemplate, object, writer, out);
|
44
|
}
|
45
|
case dataset: {
|
46
|
return serialize(dmfTemplate, object, writer, out);
|
47
|
}
|
48
|
case unknown: {
|
49
|
return serialize(scholixTemplate, object, writer, out);
|
50
|
}
|
51
|
}
|
52
|
return null;
|
53
|
}
|
54
|
|
55
|
protected String serialize(final StringTemplate template, final DLIResolvedObject object, final StringWriter writer, final NoIndentWriter out) {
|
56
|
try {
|
57
|
template.removeAttribute("object");
|
58
|
template.setAttribute("object", object);
|
59
|
template.write(out);
|
60
|
String result = writer.toString();
|
61
|
writer.close();
|
62
|
return result;
|
63
|
} catch (Throwable e) {
|
64
|
throw new RuntimeException(e);
|
65
|
}
|
66
|
}
|
67
|
|
68
|
|
69
|
public String serializeReplacingXML(final String xml, final DLIResolvedObject object) {
|
70
|
|
71
|
try {
|
72
|
VTDGen vg = new VTDGen(); // Instantiate VTDGen
|
73
|
XMLModifier xm = new XMLModifier(); //Instantiate XMLModifier
|
74
|
|
75
|
vg.setDoc(xml.getBytes());
|
76
|
|
77
|
vg.parse(true);
|
78
|
VTDNav vn = vg.getNav();
|
79
|
xm.bind(vn);
|
80
|
|
81
|
final AutoPilot ap = new AutoPilot(vn);
|
82
|
|
83
|
ap.selectXPath("//*[local-name()='objIdentifier']");
|
84
|
if (ap.evalXPath() != -1) {
|
85
|
LocalDateTime currentTime = LocalDateTime.now();
|
86
|
xm.insertAfterElement(String.format("\n<dri:resolvedDate>%s</dri:resolvedDate>", currentTime));
|
87
|
}
|
88
|
|
89
|
ap.selectXPath("//*[local-name()='about']");
|
90
|
if (ap.evalXPath() != -1) {
|
91
|
xm.remove();
|
92
|
}
|
93
|
ap.selectXPath("//*[local-name()='metadata']");
|
94
|
|
95
|
if (ap.evalXPath() != -1) {
|
96
|
xm.insertBeforeElement(serializeToXML(object));
|
97
|
xm.remove();
|
98
|
}
|
99
|
final ByteArrayOutputStream bas = new ByteArrayOutputStream();
|
100
|
xm.output(bas);
|
101
|
return new String(bas.toByteArray());
|
102
|
} catch (Throwable e) {
|
103
|
if (log.isDebugEnabled()) {
|
104
|
log.debug(String.format("Error on replacing xml: %s", xml));
|
105
|
}
|
106
|
return null;
|
107
|
}
|
108
|
}
|
109
|
|
110
|
public StringTemplate getDmfTemplate() {
|
111
|
return dmfTemplate;
|
112
|
}
|
113
|
|
114
|
@Required
|
115
|
public void setDmfTemplate(final StringTemplate dmfTemplate) {
|
116
|
this.dmfTemplate = dmfTemplate;
|
117
|
}
|
118
|
|
119
|
public StringTemplate getPmfTemplate() {
|
120
|
return pmfTemplate;
|
121
|
}
|
122
|
|
123
|
@Required
|
124
|
public void setPmfTemplate(final StringTemplate pmfTemplate) {
|
125
|
this.pmfTemplate = pmfTemplate;
|
126
|
}
|
127
|
|
128
|
public StringTemplate getScholixTemplate() {
|
129
|
return scholixTemplate;
|
130
|
}
|
131
|
|
132
|
@Required
|
133
|
public void setScholixTemplate(final StringTemplate scholixTemplate) {
|
134
|
this.scholixTemplate = scholixTemplate;
|
135
|
}
|
136
|
}
|