View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
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      /** If validator uses properties to configure its behavior, override this */
54      public void configure() {
55          // empty
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; // all nodes must validate
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 }