Revision 34342
Added by Nikon Gasparis over 9 years ago
XMLCrisClassVocabularyRule.java | ||
---|---|---|
1 |
package eu.dnetlib.validator.service.impls.rules.xml; |
|
2 |
|
|
3 |
import java.util.Properties; |
|
4 |
|
|
5 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
6 |
import javax.xml.parsers.ParserConfigurationException; |
|
7 |
import javax.xml.xpath.XPath; |
|
8 |
import javax.xml.xpath.XPathConstants; |
|
9 |
import javax.xml.xpath.XPathExpressionException; |
|
10 |
import javax.xml.xpath.XPathFactory; |
|
11 |
|
|
12 |
import org.w3c.dom.Document; |
|
13 |
import org.w3c.dom.Element; |
|
14 |
import org.w3c.dom.Node; |
|
15 |
import org.w3c.dom.NodeList; |
|
16 |
import org.w3c.dom.ls.DOMImplementationLS; |
|
17 |
import org.w3c.dom.ls.LSSerializer; |
|
18 |
|
|
19 |
import eu.dnetlib.validator.engine.data.DataException; |
|
20 |
import eu.dnetlib.validator.engine.data.RuleException; |
|
21 |
import eu.dnetlib.validator.engine.execution.ValidationObject; |
|
22 |
import eu.dnetlib.validator.service.impls.rules.VocabularyRule; |
|
23 |
import eu.dnetlib.validator.service.impls.valobjs.XMLTextValidationObject; |
|
24 |
|
|
25 |
public class XMLCrisClassVocabularyRule extends VocabularyRule implements XMLRule { |
|
26 |
|
|
27 |
|
|
28 |
private static final long serialVersionUID = 6697956781771512326L; |
|
29 |
|
|
30 |
public static final String SCHEME_ID = "scheme_id"; |
|
31 |
|
|
32 |
public XMLCrisClassVocabularyRule(Properties pros, int id) { |
|
33 |
super(pros, id); |
|
34 |
} |
|
35 |
|
|
36 |
@Override |
|
37 |
public boolean apply(ValidationObject obj) throws RuleException { |
|
38 |
String[] aterms = this.pros.getProperty(TERMS).split(","); |
|
39 |
XMLTextValidationObject tobj = (XMLTextValidationObject) obj; |
|
40 |
NodeList nodes; |
|
41 |
try { |
|
42 |
nodes = tobj.getNodes(this.pros.getProperty(XPATH)); |
|
43 |
} catch (DataException e) { |
|
44 |
log.error("", e); |
|
45 |
return false; |
|
46 |
} |
|
47 |
log.debug("XML CRIS Class Vocabulary Rule. Record ID: " + this.getValObjId()); |
|
48 |
log.debug("XML CRIS Class Vocabulary Rule. Class Nodes Found: " + nodes.getLength()); |
|
49 |
log.debug("XML CRIS Class Vocabulary Rule. Scheme_id to match: " + this.pros.getProperty(SCHEME_ID)); |
|
50 |
int success = 0, all = 0; |
|
51 |
|
|
52 |
for(int i=0; i<nodes.getLength();i++) { |
|
53 |
try { |
|
54 |
CrisClass crisClass = this.getCrisClass(nodes.item(i)); |
|
55 |
log.debug("XML CRIS Class Vocabulary Rule. Internal Node Class ID Value: "+ crisClass.getClassId()); |
|
56 |
log.debug("XML CRIS Class Vocabulary Rule. Internal Node Class Scheme ID Value: "+ crisClass.getClassSchemeId()); |
|
57 |
if (crisClass.getClassSchemeId().equalsIgnoreCase(pros.getProperty(SCHEME_ID))) { |
|
58 |
all++; |
|
59 |
for(String term : aterms) { |
|
60 |
if(term.trim().equals(crisClass.getClassId().trim())) { |
|
61 |
log.debug("XML CRIS Class Vocabulary Rule. Node: "+crisClass.getClassId().trim()+" matches with "+term); |
|
62 |
success++; |
|
63 |
break; |
|
64 |
} |
|
65 |
} |
|
66 |
} |
|
67 |
} catch (DataException e) { |
|
68 |
log.error("error getting cris class"+ e); |
|
69 |
} |
|
70 |
|
|
71 |
} |
|
72 |
|
|
73 |
String successConditions = this.pros.getProperty(SUCCESS); |
|
74 |
return Utils.success(successConditions, success, all); |
|
75 |
} |
|
76 |
|
|
77 |
private CrisClass getCrisClass(Node node) throws DataException { |
|
78 |
|
|
79 |
CrisClass retClass = new CrisClass(); |
|
80 |
Document newXmlDocument; |
|
81 |
try { |
|
82 |
newXmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); |
|
83 |
Element root = newXmlDocument.createElement("root"); |
|
84 |
newXmlDocument.appendChild(root); |
|
85 |
Node copyNode = newXmlDocument.importNode(node, true); |
|
86 |
root.appendChild(copyNode); |
|
87 |
this.printXmlDocument(newXmlDocument); |
|
88 |
XPathFactory factory = XPathFactory.newInstance(); |
|
89 |
XPath xPath = factory.newXPath(); |
|
90 |
retClass.setClassId((String) xPath.evaluate("//cfClassId/text()",newXmlDocument ,XPathConstants.STRING)); |
|
91 |
retClass.setClassSchemeId((String) xPath.evaluate("//cfClassSchemeId/text()",newXmlDocument ,XPathConstants.STRING)); |
|
92 |
retClass.setStartDate((String) xPath.evaluate("//cfStartDate/text()",newXmlDocument ,XPathConstants.STRING)); |
|
93 |
retClass.setEndDate((String) xPath.evaluate("//cfEndDate/text()",newXmlDocument ,XPathConstants.STRING)); |
|
94 |
|
|
95 |
} catch (ParserConfigurationException e) { |
|
96 |
log.error("error getting cris class"+ e); |
|
97 |
} catch (XPathExpressionException e) { |
|
98 |
log.error("error getting cris class"+ e); |
|
99 |
} |
|
100 |
return retClass; |
|
101 |
} |
|
102 |
|
|
103 |
private void printXmlDocument(Document document) { |
|
104 |
DOMImplementationLS domImplementationLS = |
|
105 |
(DOMImplementationLS) document.getImplementation(); |
|
106 |
LSSerializer lsSerializer = |
|
107 |
domImplementationLS.createLSSerializer(); |
|
108 |
String string = lsSerializer.writeToString(document); |
|
109 |
log.debug(string); |
|
110 |
} |
|
111 |
|
|
112 |
} |
|
113 |
|
|
114 |
class CrisClass { |
|
115 |
String classId; |
|
116 |
String ClassSchemeId; |
|
117 |
String startDate; |
|
118 |
String endDate; |
|
119 |
|
|
120 |
|
|
121 |
public CrisClass() { |
|
122 |
} |
|
123 |
|
|
124 |
public String getClassId() { |
|
125 |
return classId; |
|
126 |
} |
|
127 |
public void setClassId(String classId) { |
|
128 |
this.classId = classId; |
|
129 |
} |
|
130 |
public String getClassSchemeId() { |
|
131 |
return ClassSchemeId; |
|
132 |
} |
|
133 |
public void setClassSchemeId(String classSchemeId) { |
|
134 |
ClassSchemeId = classSchemeId; |
|
135 |
} |
|
136 |
public String getStartDate() { |
|
137 |
return startDate; |
|
138 |
} |
|
139 |
public void setStartDate(String startDate) { |
|
140 |
this.startDate = startDate; |
|
141 |
} |
|
142 |
public String getEndDate() { |
|
143 |
return endDate; |
|
144 |
} |
|
145 |
public void setEndDate(String endDate) { |
|
146 |
this.endDate = endDate; |
|
147 |
} |
|
148 |
|
|
149 |
} |
Also available in: Unified diff
*new version of validator service
*it now includes the validator implementation(replaces uoa-validator)
*registry for jobs,rules and providers is now removed.
*supports and implements the new ValidatorService interface
*new way to submit a new job (a common method is used for tests,registrations,or cris)
*content and usage jobs are now merged to one.
*listeners updated to support job merging
*works as standalone or with IS (BETA)