1
|
package eu.dnetlib.validator.service.impls.valobjs;
|
2
|
|
3
|
import java.io.StringWriter;
|
4
|
|
5
|
import javax.xml.transform.Result;
|
6
|
import javax.xml.transform.Source;
|
7
|
import javax.xml.transform.Transformer;
|
8
|
import javax.xml.transform.TransformerConfigurationException;
|
9
|
import javax.xml.transform.TransformerException;
|
10
|
import javax.xml.transform.TransformerFactory;
|
11
|
import javax.xml.transform.TransformerFactoryConfigurationError;
|
12
|
import javax.xml.transform.dom.DOMSource;
|
13
|
import javax.xml.transform.stream.StreamResult;
|
14
|
import javax.xml.xpath.XPath;
|
15
|
import javax.xml.xpath.XPathConstants;
|
16
|
import javax.xml.xpath.XPathExpression;
|
17
|
import javax.xml.xpath.XPathExpressionException;
|
18
|
import javax.xml.xpath.XPathFactory;
|
19
|
|
20
|
import org.apache.log4j.Logger;
|
21
|
import org.w3c.dom.Document;
|
22
|
import org.w3c.dom.NodeList;
|
23
|
|
24
|
import eu.dnetlib.validator.engine.data.DataException;
|
25
|
/**
|
26
|
* Indicates that a Validation Object is actually an xml text.
|
27
|
* @author Manos Karvounis
|
28
|
*/
|
29
|
public class XMLTextValidationObject implements TextValidationObject {
|
30
|
|
31
|
private transient Logger log = Logger.getLogger(XMLTextValidationObject.class);
|
32
|
|
33
|
private String id = "";
|
34
|
private String status;
|
35
|
private final Document doc;
|
36
|
private XPathFactory factory = XPathFactory.newInstance();
|
37
|
|
38
|
public XMLTextValidationObject(Document doc) {
|
39
|
super();
|
40
|
this.doc = doc;
|
41
|
}
|
42
|
|
43
|
@Override
|
44
|
public String getId() {
|
45
|
return this.id;
|
46
|
}
|
47
|
|
48
|
@Override
|
49
|
public void setId(String id) {
|
50
|
this.id = id;
|
51
|
}
|
52
|
|
53
|
public String getStatus() {
|
54
|
return status;
|
55
|
}
|
56
|
|
57
|
public void setStatus(String status) {
|
58
|
this.status = status;
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
public Object getContentAsObject() {
|
63
|
return this.doc;
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
public String getContentAsText() {
|
68
|
StringWriter sw = new StringWriter();
|
69
|
Transformer transformer = null;
|
70
|
try {
|
71
|
transformer = TransformerFactory.newInstance().newTransformer();
|
72
|
Source source = new DOMSource(doc);
|
73
|
Result output = new StreamResult(sw);
|
74
|
transformer.transform(source, output);
|
75
|
} catch (TransformerConfigurationException e) {
|
76
|
log.error("", e);
|
77
|
} catch (TransformerFactoryConfigurationError e) {
|
78
|
log.error("", e);
|
79
|
} catch (TransformerException e) {
|
80
|
log.error("", e);
|
81
|
}
|
82
|
return sw.toString();
|
83
|
}
|
84
|
|
85
|
public Document getDocument() {
|
86
|
return this.doc;
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Get the nodes retrieved by issuing the given XPATH query.
|
91
|
* @param xpath
|
92
|
* @return
|
93
|
* @throws DataException
|
94
|
*/
|
95
|
public NodeList getNodes(String xpath) throws DataException {
|
96
|
log.debug("xpath: "+xpath);
|
97
|
try {
|
98
|
XPath xxpath = factory.newXPath();
|
99
|
XPathExpression expr = xxpath.compile(xpath);
|
100
|
return (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
|
101
|
} catch (XPathExpressionException e) {
|
102
|
throw new DataException();
|
103
|
}
|
104
|
}
|
105
|
|
106
|
}
|