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.w3c.dom.Node;
    21
    22  import java.util.ArrayList;
    23  import java.util.Collections;
    24  import java.util.List;
    25
    26  /**
    27   * Default implementation of {@link ValidationReportBuilder}.
    28   *
    29   * @author Michele Mostarda (mostarda@fbk.eu)
    30   * @author Davide Palmisano (palmisano@fbk.eu)
    31   */
    32  public class DefaultValidationReportBuilder implements ValidationReportBuilder {
    33
    34      private List<ValidationReport.Issue> issues;
    35      private List<ValidationReport.RuleActivation> ruleActivations;
    36      private List<ValidationReport.Error> errors;
    37
    38      public DefaultValidationReportBuilder() {
    39          // default constructor
    40      }
    41
    42      public ValidationReport getReport() {
    43          return new DefaultValidationReport(issues == null ? Collections.<ValidationReport.Issue> emptyList() : issues,
    44                  ruleActivations == null ? Collections.<ValidationReport.RuleActivation> emptyList() : ruleActivations,
    45                  errors == null ? Collections.<ValidationReport.Error> emptyList() : errors);
    46      }
    47
    48      public void reportIssue(ValidationReport.IssueLevel issueLevel, String message, Node n) {
    49          if (issues == null) {
    50              issues = new ArrayList<>();
    51          }
    52          issues.add(new ValidationReport.Issue(issueLevel, message, n));
    53      }
    54
    55      public void reportIssue(ValidationReport.IssueLevel issueLevel, String message) {
    56          reportIssue(issueLevel, message, null);
    57      }
    58
    59      public void traceRuleActivation(Rule r) {
    60          if (ruleActivations == null) {
    61              ruleActivations = new ArrayList<>();
    62          }
    63          ruleActivations.add(new ValidationReport.RuleActivation(r));
    64      }
    65
    66      public void reportRuleError(Rule r, Exception e, String msg) {
    67          if (errors == null) {
    68              errors = new ArrayList<>();
    69          }
    70          errors.add(new ValidationReport.RuleError(r, e, msg));
    71      }
    72
    73      public void reportFixError(Fix f, Exception e, String msg) {
    74          if (errors == null) {
    75              errors = new ArrayList<>();
    76          }
    77          errors.add(new ValidationReport.FixError(f, e, msg));
    78
    79      }
    80
    81      @Override
    82      public String toString() {
    83          final StringBuilder sb = new StringBuilder();
    84          if (ruleActivations != null) {
    85              sb.append("Rules {\n");
    86              for (ValidationReport.RuleActivation ra : ruleActivations) {
    87                  sb.append(ra).append('\n');
    88              }
    89              sb.append("}\n");
    90          }
    91          if (issues != null) {
    92              sb.append("Issues {\n");
    93              for (ValidationReport.Issue issue : issues) {
    94                  sb.append(issue.toString()).append('\n');
    95              }
    96              sb.append("}\n");
    97          }
    98          if (errors != null) {
    99              sb.append("Errors {\n");
    100             for (ValidationReport.Error error : errors) {
    101                 sb.append(error.toString()).append('\n');
    102             }
    103             sb.append("}\n");
    104         }
    105         return sb.toString();
    106     }
    107
    108 }