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 java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  
25  import org.jaxen.JaxenException;
26  import org.jdom2.JDOMException;
27  import org.mycore.frontend.xeditor.MCRBinding;
28  import org.mycore.frontend.xeditor.MCREditorSession;
29  import org.w3c.dom.Node;
30  
31  /**
32   * @author Frank L\u00FCtzenkirchen
33   */
34  public class MCRXEditorValidator {
35  
36      public static final String XED_VALIDATION_FAILED = "xed-validation-failed";
37  
38      public static final String XED_VALIDATION_MARKER = "xed-validation-marker";
39  
40      private List<MCRValidator> validationRules = new ArrayList<>();
41  
42      private MCRValidationResults results = new MCRValidationResults();
43  
44      private MCREditorSession session;
45  
46      public MCRXEditorValidator(MCREditorSession session) {
47          this.session = session;
48      }
49  
50      public void addRule(String baseXPath, Node ruleElement) {
51          addIfConfigured(new MCRRequiredValidator(), baseXPath, ruleElement);
52          addIfConfigured(new MCRMinLengthValidator(), baseXPath, ruleElement);
53          addIfConfigured(new MCRMaxLengthValidator(), baseXPath, ruleElement);
54          addIfConfigured(new MCRMatchesValidator(), baseXPath, ruleElement);
55          addIfConfigured(new MCRXPathTestValidator(), baseXPath, ruleElement);
56          addIfConfigured(new MCRDateValidator(), baseXPath, ruleElement);
57          addIfConfigured(new MCRMinDateValidator(), baseXPath, ruleElement);
58          addIfConfigured(new MCRMaxDateValidator(), baseXPath, ruleElement);
59          addIfConfigured(new MCRIntegerValidator(), baseXPath, ruleElement);
60          addIfConfigured(new MCRMinIntegerValidator(), baseXPath, ruleElement);
61          addIfConfigured(new MCRMaxIntegerValidator(), baseXPath, ruleElement);
62          addIfConfigured(new MCRDecimalValidator(), baseXPath, ruleElement);
63          addIfConfigured(new MCRMinDecimalValidator(), baseXPath, ruleElement);
64          addIfConfigured(new MCRMaxDecimalValidator(), baseXPath, ruleElement);
65          addIfConfigured(new MCRMinStringValidator(), baseXPath, ruleElement);
66          addIfConfigured(new MCRMaxStringValidator(), baseXPath, ruleElement);
67          addIfConfigured(new MCRExternalValidator(), baseXPath, ruleElement);
68      }
69  
70      private void addIfConfigured(MCRValidator validator, String baseXPath, Node ruleElement) {
71          validator.init(baseXPath, ruleElement);
72          if (validator.hasRequiredAttributes()) {
73              validationRules.add(validator);
74          }
75      }
76  
77      public void clearRules() {
78          validationRules.clear();
79      }
80  
81      public boolean isValid() throws JDOMException, JaxenException {
82          MCRBinding root = session.getRootBinding();
83          for (MCRValidator rule : validationRules) {
84              rule.validate(results, root);
85          }
86  
87          session.getVariables().put(XED_VALIDATION_FAILED, String.valueOf(!results.isValid()));
88          return results.isValid();
89      }
90  
91      public boolean hasError(MCRBinding binding) {
92          return results.hasError(binding.getAbsoluteXPath());
93      }
94  
95      public MCRValidator getFailedRule(MCRBinding binding) {
96          return results.getFailedRule(binding.getAbsoluteXPath());
97      }
98  
99      public void setValidationMarker(MCRBinding binding) {
100         String xPath = binding.getAbsoluteXPath();
101         String marker = results.getValidationMarker(xPath);
102         session.getVariables().put(XED_VALIDATION_MARKER, marker);
103     }
104 
105     public Collection<MCRValidator> getFailedRules() {
106         return results.getFailedRules();
107     }
108 
109     public void clearValidationResults() {
110         results = new MCRValidationResults();
111         session.getVariables().remove(XED_VALIDATION_FAILED);
112     }
113 }