Project

General

Profile

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.TransformerConfigurationException;
10
import javax.xml.transform.TransformerException;
11
import javax.xml.transform.TransformerFactory;
12
import javax.xml.transform.TransformerFactoryConfigurationError;
13
import javax.xml.transform.dom.DOMSource;
14
import javax.xml.transform.stream.StreamResult;
15

    
16
import org.apache.log4j.Logger;
17
import org.apache.velocity.Template;
18
import org.apache.velocity.VelocityContext;
19
import org.apache.velocity.app.VelocityEngine;
20
import org.apache.velocity.exception.ParseErrorException;
21
import org.apache.velocity.exception.ResourceNotFoundException;
22
import org.apache.velocity.runtime.RuntimeConstants;
23
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
24
import org.w3c.dom.Node;
25
import org.w3c.dom.NodeList;
26

    
27
public class RecordXMLBuilder {
28

    
29
	private static Logger logger = Logger.getLogger(RecordXMLBuilder.class);
30
	
31
	private VelocityEngine ve = null;
32
	
33
	public void init() throws Exception {
34
		this.ve = new VelocityEngine();
35
		this.ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
36
		this.ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
37
		this.ve.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
38
		this.ve.init();	
39
	}
40
	
41
	public String buildXml(List<Map<String, String>> veloList, Object record, Map<String, String> recordValidationResult) {
42
		logger.debug("Building XML file for record");
43
		String xmlRetString = null;
44
		
45
		try {
46
        
47
			VelocityContext context = new VelocityContext();
48
			NodeList list = ((Node) record).getChildNodes();
49
	        Node recordNode = null;
50
	        for(int i = 0; i < list.getLength(); i++)
51
	        {
52
	            Node node = list.item(i);
53
	            if(node.getNodeName().equals("root"))
54
	            {
55
	            	logger.debug("root found");
56
	            	list = node.getChildNodes();
57
	            	for(i = 0; i < list.getLength(); i++)
58
			        {
59
			            node = list.item(i);
60
			            logger.debug("node name: " + node.getNodeName());
61
			            logger.debug("node local name: " + node.getLocalName());
62
			            if(node.getNodeName().contains("record"))
63
			            {
64
			            	recordNode = node;
65
		                	logger.debug("record found");
66
		                	break;
67
			            }
68
			        }
69
	            }
70
	        } 
71
	        Transformer transformer;
72
			transformer = TransformerFactory.newInstance().newTransformer();
73
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
74
			transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
75
//			transformer.setOutputProperty(OutputKeys.VERSION, "no");
76
			
77
			
78
			
79
			StreamResult result = new StreamResult(new StringWriter());
80
			DOMSource source = new DOMSource(recordNode);
81

    
82
			transformer.transform(source, result);
83
						
84
			String xmlString = result.getWriter().toString();
85
//			logger.debug("xmlstring: " + xmlString);
86
			context.put("recordFull", xmlString);
87
			context.put("ruleList", veloList);
88
			context.put("record", recordValidationResult);
89
			Template t = this.ve.getTemplate("/eu/dnetlib/validator/service/listeners/xml.vm");
90
			
91
			StringWriter writer = new StringWriter();
92
			
93
			t.merge(context, writer);
94
			xmlRetString = writer.toString();
95
			
96
		} catch (TransformerConfigurationException e) {
97
			logger.error("Error while building XML file", e);
98
		} catch (TransformerFactoryConfigurationError e) {
99
			logger.error("Error while building XML file", e);
100
		} catch (TransformerException e) {
101
			logger.error("Error while building XML file", e);
102
		} catch (ResourceNotFoundException e) {
103
			logger.error("Error while building XML file", e);
104
		} catch (ParseErrorException e) {
105
			logger.error("Error while building XML file", e);
106
		} catch (Exception e) {
107
			logger.error("Error while building XML file", e);
108
		}
109
		
110
        return xmlRetString;
111
	}
112
}
(5-5/7)