1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.frontend.xeditor.validation;
20
21 import org.jaxen.JaxenException;
22 import org.jdom2.JDOMException;
23 import org.mycore.common.xml.MCRXPathBuilder;
24 import org.mycore.common.xml.MCRXPathEvaluator;
25 import org.mycore.frontend.xeditor.MCRBinding;
26 import org.w3c.dom.NamedNodeMap;
27 import org.w3c.dom.Node;
28
29 import java.util.List;
30
31 public abstract class MCRValidator {
32
33 private static final String ATTR_RELEVANT_IF = "relevant-if";
34
35 private Node ruleElement;
36
37 protected String xPath;
38 protected String relevantIfXPath;
39
40 public void init(String baseXPath, Node ruleElement) {
41 Node relativeXPath = ruleElement.getAttributes().getNamedItem("xpath");
42 this.xPath = relativeXPath != null ? relativeXPath.getNodeValue() : baseXPath;
43 this.ruleElement = ruleElement;
44
45 if (hasRequiredAttributes()) {
46 relevantIfXPath = getAttributeValue(ATTR_RELEVANT_IF);
47 configure();
48 }
49 }
50
51 public abstract boolean hasRequiredAttributes();
52
53
54 public void configure() {
55
56 }
57
58 public Node getRuleElement() {
59 return ruleElement;
60 }
61
62 public String getAttributeValue(String name) {
63 NamedNodeMap attributes = ruleElement.getAttributes();
64 Node attribute = attributes.getNamedItem(name);
65 return attribute == null ? null : attribute.getNodeValue();
66 }
67
68 public boolean hasAttributeValue(String name) {
69 return (getAttributeValue(name) != null);
70 }
71
72 public boolean validate(MCRValidationResults results, MCRBinding root) throws JaxenException, JDOMException {
73 MCRBinding binding = new MCRBinding(xPath, false, root);
74 boolean isValid = validateBinding(results, binding);
75 binding.detach();
76 return isValid;
77 }
78
79 public boolean validateBinding(MCRValidationResults results, MCRBinding binding) {
80 boolean isValid = true;
81
82 List<Object> boundNodes = binding.getBoundNodes();
83 for (int i = 0; i < boundNodes.size(); i++) {
84 Object node = boundNodes.get(i);
85
86 String absPath = MCRXPathBuilder.buildXPath(node);
87 if (results.hasError(absPath)) {
88 continue;
89 }
90
91 MCRBinding nodeBinding = new MCRBinding(i + 1, binding);
92 if (!isRelevant(nodeBinding)) {
93 continue;
94 }
95
96 String value = MCRBinding.getValue(node);
97 if (value.isEmpty()) {
98 continue;
99 }
100
101 boolean result = isValid(value);
102 results.mark(absPath, result, this);
103 isValid = isValid && result;
104 }
105 return isValid;
106 }
107
108 protected boolean isRelevant(MCRBinding binding) {
109 if (null == relevantIfXPath) {
110 return true;
111 } else {
112 MCRXPathEvaluator evaluator = binding.getXPathEvaluator();
113 boolean isRelevant = evaluator.test(relevantIfXPath);
114 binding.detach();
115 return isRelevant;
116 }
117 }
118
119 protected boolean isValid(String value) {
120 return true;
121 }
122 }