View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *  http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.any23.validator;
19  
20  import org.apache.any23.validator.rule.AboutNotURIRule;
21  import org.apache.any23.validator.rule.MetaNameMisuseFix;
22  import org.apache.any23.validator.rule.MetaNameMisuseRule;
23  import org.apache.any23.validator.rule.MissingItemscopeAttributeValueFix;
24  import org.apache.any23.validator.rule.MissingItemscopeAttributeValueRule;
25  import org.apache.any23.validator.rule.MissingOpenGraphNamespaceRule;
26  import org.apache.any23.validator.rule.OpenGraphNamespaceFix;
27  import org.w3c.dom.Document;
28  
29  import java.net.URI;
30  import java.util.ArrayList;
31  import java.util.Collections;
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  /**
37   * Default implementation of {@link Validator}.
38   *
39   * @author Michele Mostarda (mostarda@fbk.eu)
40   * @author Davide Palmisano (palmisano@fbk.eu)
41   */
42  public class DefaultValidator implements Validator {
43  
44      private Map<Class<? extends Rule>, List<Class<? extends Fix>>> rulesToFixes;
45  
46      private List<Class<? extends Rule>> rulesOrder;
47  
48      public DefaultValidator() {
49          rulesToFixes = new HashMap<>();
50          rulesOrder   = new ArrayList<>();
51          loadDefaultRules();
52      }
53  
54      @Override
55      public ValidationReport validate(DOMDocument document, boolean applyFix)
56      throws ValidatorException {
57          final ValidationReportBuilder validationReportBuilder = new DefaultValidationReportBuilder();
58          for(Class<? extends Rule> cRule : rulesOrder) {
59              Rule rule = newRuleInstance(cRule);
60              @SuppressWarnings("rawtypes")
61              final RuleContext ruleContext = new DefaultRuleContext();
62              boolean applyOn;
63              try {
64                  applyOn = rule.applyOn(document, ruleContext, validationReportBuilder);
65              } catch (Exception e) {
66                  validationReportBuilder.reportRuleError(rule, e, "Error while processing rule.");
67                  continue;
68              }
69              if(applyFix && applyOn) {
70                  validationReportBuilder.traceRuleActivation(rule);
71                  List<Class<? extends Fix>> cFixes = getFixes(cRule);
72                  for(Class<? extends Fix> cFix : cFixes) {
73                      Fix fix = newFixInstance(cFix);
74                      try {
75                          fix.execute(rule, ruleContext, document);
76                      } catch (Exception e) {
77                          validationReportBuilder.reportFixError(fix, e, "Error while processing fix.");
78                      }
79                  }
80              }
81          }
82          return validationReportBuilder.getReport();
83      }
84  
85      @Override
86      public ValidationReport validate(URI documentIRI, Document document, boolean applyFix)
87      throws ValidatorException {
88          return validate( new DefaultDOMDocument(documentIRI, document), applyFix );
89      }
90  
91      @Override
92      public synchronized void addRule(Class<? extends Rule> rule, Class<? extends Fix> fix) {
93          List<Class<? extends Fix>> fixes = rulesToFixes.get(rule);
94          if(fixes == null) {
95              fixes = new ArrayList<>();
96          }
97          rulesOrder.add(rule);
98          rulesToFixes.put(rule, fixes);
99          if(fix != null)  {
100             fixes.add(fix);
101         }
102     }
103 
104     @Override
105     public void addRule(Class<? extends Rule> rule) {
106         addRule(rule, null);
107     }
108 
109     @Override
110     public synchronized void removeRule(Class<? extends Rule> rule) {
111         rulesOrder.remove(rule);
112         rulesToFixes.remove(rule);
113     }
114 
115     @Override
116     public List<Class<? extends Rule>> getAllRules() {
117         return Collections.unmodifiableList(rulesOrder);
118     }
119 
120     @Override
121     public List<Class<? extends Fix>> getFixes(Class<? extends Rule> rule) {
122         List<Class<? extends Fix>> fixes = rulesToFixes.get(rule);
123         return  fixes == null
124                 ?
125                 Collections.<Class<? extends Fix>>emptyList()
126                 :
127                 Collections.unmodifiableList( rulesToFixes.get(rule) );
128     }
129 
130     private void loadDefaultRules() {
131         addRule(MetaNameMisuseRule.class, MetaNameMisuseFix.class);
132         addRule(MissingOpenGraphNamespaceRule.class, OpenGraphNamespaceFix.class);
133         addRule(AboutNotURIRule.class);
134         addRule(MissingItemscopeAttributeValueRule.class, MissingItemscopeAttributeValueFix.class);
135     }
136 
137     private Fix newFixInstance(Class<? extends Fix> cFix) throws ValidatorException {
138         try {
139             return cFix.newInstance();
140         } catch (Exception e) {
141             throw new ValidatorException("An error occurred while instantiating a fix.", e);
142         }
143     }
144 
145     private Rule newRuleInstance(Class<? extends Rule> cRule) throws ValidatorException {
146         try {
147             return cRule.newInstance();
148         } catch (Exception e) {
149             throw new ValidatorException("An error occurred while instantiating a rule.", e);
150         }
151     }
152 
153 }