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.frontend.xeditor.MCRBinding;
25  import org.w3c.dom.NamedNodeMap;
26  import org.w3c.dom.Node;
27  
28  public abstract class MCRValidator {
29  
30      private Node ruleElement;
31  
32      protected String xPath;
33  
34      public void init(String baseXPath, Node ruleElement) {
35          Node relativeXPath = ruleElement.getAttributes().getNamedItem("xpath");
36          this.xPath = relativeXPath != null ? relativeXPath.getNodeValue() : baseXPath;
37          this.ruleElement = ruleElement;
38  
39          if (hasRequiredAttributes()) {
40              configure();
41          }
42      }
43  
44      public abstract boolean hasRequiredAttributes();
45  
46      /** If validator uses properties to configure its behavior, override this */
47      public void configure() {
48      }
49  
50      public Node getRuleElement() {
51          return ruleElement;
52      }
53  
54      public String getAttributeValue(String name) {
55          NamedNodeMap attributes = ruleElement.getAttributes();
56          Node attribute = attributes.getNamedItem(name);
57          return attribute == null ? null : attribute.getNodeValue();
58      }
59  
60      public boolean hasAttributeValue(String name) {
61          return (getAttributeValue(name) != null);
62      }
63  
64      public boolean validate(MCRValidationResults results, MCRBinding root) throws JaxenException, JDOMException {
65          MCRBindingRBinding.html#MCRBinding">MCRBinding binding = new MCRBinding(xPath, false, root);
66          boolean isValid = validateBinding(results, binding);
67          binding.detach();
68          return isValid;
69      }
70  
71      public boolean validateBinding(MCRValidationResults results, MCRBinding binding) {
72          boolean isValid = true; // all nodes must validate
73          for (Object node : binding.getBoundNodes()) {
74              String absPath = MCRXPathBuilder.buildXPath(node);
75              if (results.hasError(absPath)) {
76                  continue;
77              }
78  
79              String value = MCRBinding.getValue(node);
80              if (value.isEmpty()) {
81                  continue;
82              }
83  
84              boolean result = isValid(value);
85              results.mark(absPath, result, this);
86              isValid = isValid && result;
87          }
88          return isValid;
89      }
90  
91      protected boolean isValid(String value) {
92          return true;
93      }
94  }