Coverage Report - org.apache.any23.validator.DefaultValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultValidator
0%
0/53
0%
0/14
2.364
 
 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.MissingOpenGraphNamespaceRule;
 24  
 import org.apache.any23.validator.rule.OpenGraphNamespaceFix;
 25  
 import org.w3c.dom.Document;
 26  
 
 27  
 import java.net.URI;
 28  
 import java.util.ArrayList;
 29  
 import java.util.Collections;
 30  
 import java.util.HashMap;
 31  
 import java.util.List;
 32  
 import java.util.Map;
 33  
 
 34  
 /**
 35  
  * Default implementation of {@link Validator}.
 36  
  *
 37  
  * @author Michele Mostarda (mostarda@fbk.eu)
 38  
  * @author Davide Palmisano (palmisano@fbk.eu)
 39  
  */
 40  
 public class DefaultValidator implements Validator {
 41  
 
 42  
     private Map<Class<? extends Rule>, List<Class<? extends Fix>>> rulesToFixes;
 43  
 
 44  
     private List<Class<? extends Rule>> rulesOrder;
 45  
 
 46  0
     public DefaultValidator() {
 47  0
         rulesToFixes = new HashMap<Class<? extends Rule>, List<Class<? extends Fix>>>();
 48  0
         rulesOrder   = new ArrayList<Class<? extends Rule>>();
 49  0
         loadDefaultRules();
 50  0
     }
 51  
 
 52  
     public ValidationReport validate(DOMDocument document, boolean applyFix)
 53  
     throws ValidatorException {
 54  0
         final ValidationReportBuilder validationReportBuilder = new DefaultValidationReportBuilder();
 55  0
         for(Class<? extends Rule> cRule : rulesOrder) {
 56  0
             Rule rule = newInstance(cRule);
 57  0
             final RuleContext ruleContext = new DefaultRuleContext();            
 58  
             boolean applyOn;
 59  
             try {
 60  0
                 applyOn = rule.applyOn(document, ruleContext, validationReportBuilder);
 61  0
             } catch (Exception e) {
 62  0
                 validationReportBuilder.reportRuleError(rule, e, "Error while processing rule.");
 63  0
                 continue;
 64  0
             }
 65  0
             if(applyFix && applyOn) {
 66  0
                 validationReportBuilder.traceRuleActivation(rule);
 67  0
                 List<Class<? extends Fix>> cFixes = getFixes(cRule);
 68  0
                 for(Class<? extends Fix> cFix : cFixes) {
 69  0
                     Fix fix = newInstance(cFix);
 70  
                     try {
 71  0
                         fix.execute(rule, ruleContext, document);
 72  0
                     } catch (Exception e) {
 73  0
                         validationReportBuilder.reportFixError(fix, e, "Error while processing fix.");
 74  0
                     }
 75  0
                 }
 76  
             }
 77  0
         }
 78  0
         return validationReportBuilder.getReport();
 79  
     }
 80  
 
 81  
     public ValidationReport validate(URI documentURI, Document document, boolean applyFix)
 82  
     throws ValidatorException {
 83  0
         return validate( new DefaultDOMDocument(documentURI, document), applyFix );
 84  
     }
 85  
 
 86  
     public synchronized void addRule(Class<? extends Rule> rule, Class<? extends Fix> fix) {
 87  0
         List<Class<? extends Fix>> fixes = rulesToFixes.get(rule);
 88  0
         if(fixes == null) {
 89  0
             fixes = new ArrayList<Class<? extends Fix>>();
 90  
         }
 91  0
         rulesOrder.add(rule);
 92  0
         rulesToFixes.put(rule, fixes);
 93  0
         if(fix != null)  {
 94  0
             fixes.add(fix);
 95  
         }
 96  0
     }
 97  
 
 98  
     public void addRule(Class<? extends Rule> rule) {
 99  0
         addRule(rule, null);
 100  0
     }
 101  
 
 102  
     public synchronized void removeRule(Class<? extends Rule> rule) {
 103  0
         rulesOrder.remove(rule);
 104  0
         rulesToFixes.remove(rule);
 105  0
     }
 106  
 
 107  
     public List<Class<? extends Rule>> getAllRules() {
 108  0
         return Collections.unmodifiableList(rulesOrder);
 109  
     }
 110  
 
 111  
     public List<Class<? extends Fix>> getFixes(Class<? extends Rule> rule) {
 112  0
         List<Class<? extends Fix>> fixes = rulesToFixes.get(rule);
 113  0
         return  fixes == null
 114  
                 ?
 115  
                 Collections.<Class<? extends Fix>>emptyList()
 116  
                 :
 117  
                 Collections.unmodifiableList( rulesToFixes.get(rule) );
 118  
     }
 119  
 
 120  
     private void loadDefaultRules() {
 121  0
         addRule(MetaNameMisuseRule.class, MetaNameMisuseFix.class);
 122  0
         addRule(MissingOpenGraphNamespaceRule.class, OpenGraphNamespaceFix.class);
 123  0
         addRule(AboutNotURIRule.class);
 124  0
     }
 125  
 
 126  
     private Fix newInstance(Class<? extends Fix> cFix) throws ValidatorException {
 127  
         try {
 128  0
             return cFix.newInstance();
 129  0
         } catch (Exception e) {
 130  0
             throw new ValidatorException("An error occurred while instantiating a fix.", e);
 131  
         }
 132  
     }
 133  
 
 134  
     private Rule newInstance(Class<? extends Rule> cRule) throws ValidatorException {
 135  
         try {
 136  0
             return cRule.newInstance();
 137  0
         } catch (Exception e) {
 138  0
             throw new ValidatorException("An error occurred while instantiating a rule.", e);
 139  
         }
 140  
     }
 141  
 
 142  
 }