1
|
package eu.dnetlib.validator.service.impls.listeners;
|
2
|
|
3
|
import java.io.StringWriter;
|
4
|
import java.util.List;
|
5
|
import java.util.Map;
|
6
|
|
7
|
import javax.xml.transform.OutputKeys;
|
8
|
import javax.xml.transform.Transformer;
|
9
|
import javax.xml.transform.TransformerFactory;
|
10
|
import javax.xml.transform.dom.DOMSource;
|
11
|
import javax.xml.transform.stream.StreamResult;
|
12
|
|
13
|
import org.apache.log4j.Logger;
|
14
|
import org.apache.velocity.Template;
|
15
|
import org.apache.velocity.VelocityContext;
|
16
|
import org.apache.velocity.app.VelocityEngine;
|
17
|
import org.apache.velocity.runtime.RuntimeConstants;
|
18
|
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
|
19
|
import org.w3c.dom.Node;
|
20
|
import org.w3c.dom.NodeList;
|
21
|
|
22
|
public class RecordXMLBuilder {
|
23
|
|
24
|
private static Logger logger = Logger.getLogger(RecordXMLBuilder.class);
|
25
|
|
26
|
private VelocityEngine ve = null;
|
27
|
|
28
|
public void init() throws Exception {
|
29
|
this.ve = new VelocityEngine();
|
30
|
this.ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
|
31
|
this.ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
|
32
|
this.ve.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
|
33
|
this.ve.init();
|
34
|
}
|
35
|
|
36
|
public String buildXml(List<Map<String, String>> veloList, Object record, Map<String, String> recordValidationResult) {
|
37
|
logger.debug("Building XML file for record");
|
38
|
String xmlRetString = null;
|
39
|
|
40
|
try {
|
41
|
|
42
|
VelocityContext context = new VelocityContext();
|
43
|
NodeList list = ((Node) record).getChildNodes();
|
44
|
Node recordNode = null;
|
45
|
for(int i = 0; i < list.getLength(); i++)
|
46
|
{
|
47
|
Node node = list.item(i);
|
48
|
if(node.getNodeName().equals("root"))
|
49
|
{
|
50
|
logger.debug("root found");
|
51
|
list = node.getChildNodes();
|
52
|
for(i = 0; i < list.getLength(); i++)
|
53
|
{
|
54
|
node = list.item(i);
|
55
|
logger.debug("node name: " + node.getNodeName());
|
56
|
logger.debug("node local name: " + node.getLocalName());
|
57
|
if(node.getNodeName().contains("record"))
|
58
|
{
|
59
|
recordNode = node;
|
60
|
logger.debug("record found");
|
61
|
break;
|
62
|
}
|
63
|
}
|
64
|
}
|
65
|
}
|
66
|
Transformer transformer;
|
67
|
transformer = TransformerFactory.newInstance().newTransformer();
|
68
|
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
69
|
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
|
70
|
// transformer.setOutputProperty(OutputKeys.VERSION, "no");
|
71
|
|
72
|
|
73
|
StreamResult result = new StreamResult(new StringWriter());
|
74
|
DOMSource source = new DOMSource(recordNode);
|
75
|
|
76
|
transformer.transform(source, result);
|
77
|
|
78
|
String xmlString = result.getWriter().toString();
|
79
|
// logger.debug("xmlstring: " + xmlString);
|
80
|
context.put("recordFull", xmlString.substring(xmlString.indexOf("?>")+3));
|
81
|
context.put("ruleList", veloList);
|
82
|
context.put("record", recordValidationResult);
|
83
|
Template t = this.ve.getTemplate("/eu/dnetlib/validator/service/listeners/xml.vm");
|
84
|
|
85
|
StringWriter writer = new StringWriter();
|
86
|
|
87
|
t.merge(context, writer);
|
88
|
xmlRetString = writer.toString();
|
89
|
|
90
|
} catch (Exception e) {
|
91
|
logger.error("Error while building XML file", e);
|
92
|
}
|
93
|
|
94
|
return xmlRetString;
|
95
|
}
|
96
|
}
|