@Override
  public List<Violation> check(
      ConfigurationServiceImpl configuration, RuleDTO rootRule, RuleDTO currentRule) {
    violations.clear();
    fromMappings = getAllClasspathsOfModule(currentRule.moduleFrom, currentRule.violationTypeKeys);
    toMappings = getAllClasspathsOfModule(currentRule.moduleTo, currentRule.violationTypeKeys);

    // Create HashMap with all allowed to-classes (including the from-classes)
    HashMap<String, Mapping> fromMap = new HashMap<String, Mapping>();
    for (Mapping from : fromMappings) {
      fromMap.put(from.getPhysicalPath(), from);
    }
    for (Mapping to : toMappings) {
      fromMap.put(to.getPhysicalPath(), to);
    }

    // Create a HashMap with all allowed from-to combinations, based on the exception rules.
    HashSet<String> allExceptionFromTos = getAllExceptionFromTos(currentRule);

    for (Mapping classPathTo : toMappings) {
      // Get all dependencies with matching dependency.classPathTo
      DependencyDTO[] dependenciesTo =
          analyseService.getDependenciesFromTo("", classPathTo.getPhysicalPath());
      for (DependencyDTO dependency : dependenciesTo) {
        String fromToCombi = dependency.from + "|" + classPathTo.getPhysicalPath();
        if (fromMap.containsKey(dependency.from) || allExceptionFromTos.contains(fromToCombi)) {
          // Do nothing
        } else {
          Mapping classPathFrom = new Mapping(dependency.from, classPathTo.getViolationTypes());
          Violation violation =
              createViolation(rootRule, classPathFrom, classPathTo, dependency, configuration);

          // Get logicalModuleFrom based on dependency.from and add it to the violation
          ModuleDTO moduleFrom =
              ServiceProvider.getInstance()
                  .getDefineService()
                  .getModule_BasedOnSoftwareUnitName(dependency.from);
          if (moduleFrom != null) {
            // Add moduleFrom to violation.logicalModules, so that graphics can include these
            // violations in architecture diagrams
            LogicalModules logicalModulesOld = violation.getLogicalModules();
            LogicalModule logicalModuleTo = logicalModulesOld.getLogicalModuleTo();
            LogicalModule logicalModuleFrom =
                new LogicalModule(moduleFrom.logicalPath, moduleFrom.type);
            LogicalModules logicalModules = new LogicalModules(logicalModuleFrom, logicalModuleTo);
            violation.setLogicalModules(logicalModules);
          }

          violations.add(violation);
        }
      }
    }
    return violations;
  }
Example #2
0
  private ViolationDTO createViolationDTO(Violation violation)
      throws RuleInstantionException, LanguageNotFoundException, RuleTypeNotFoundException {
    try {
      RuleTypeDTO rule = createRuleTypeDTO(violation);
      ViolationTypeDTO violationtype = rule.getViolationTypes()[0];

      final String classPathFrom = violation.getClassPathFrom();
      final String classPathTo = violation.getClassPathTo();
      final String logicalModuleFromPath =
          violation.getLogicalModules().getLogicalModuleFrom().getLogicalModulePath();
      final String logicalModuleToPath =
          violation.getLogicalModules().getLogicalModuleTo().getLogicalModulePath();
      final String message = messagebuilder.createMessage(violation.getMessage());
      final int linenumber = violation.getLinenumber();

      if (violation.getSeverity() != null) {
        final Severity severity = violation.getSeverity();
        final Color color = severity.getColor();
        final String userDefinedName = severity.getUserName();
        final String systemDefinedName = severity.getDefaultName();
        final int severityValue = configuration.getSeverityValue(violation.getSeverity());

        return new ViolationDTO(
            classPathFrom,
            classPathTo,
            logicalModuleFromPath,
            logicalModuleToPath,
            violationtype,
            rule,
            message,
            linenumber,
            color,
            userDefinedName,
            systemDefinedName,
            severityValue);
      } else {
        return new ViolationDTO(
            classPathFrom,
            classPathTo,
            logicalModuleFromPath,
            logicalModuleToPath,
            violationtype,
            rule,
            message,
            linenumber,
            Color.BLACK,
            "",
            "",
            0);
      }
    } catch (ViolationTypeNotFoundException e) {
      throw new ViolationTypeNotFoundException();
    }
  }
Example #3
0
  private RuleTypeDTO createRuleTypeDTO(Violation violation)
      throws RuleInstantionException, LanguageNotFoundException, RuleTypeNotFoundException {
    try {
      if (violationtypeFactory == null) {
        throw new LanguageNotFoundException();
      }
      ViolationType violationtype =
          violationtypeFactory.createViolationType(violation.getViolationtypeKey());
      RuleType rule = ruleFactory.generateRuleType(violation.getRuletypeKey());

      RuleTypeDTO ruleDTO = ruleAssembler.createRuleTypeDTO(rule, violationtype);
      return ruleDTO;
    } catch (ViolationTypeNotFoundException e) {
      throw new ViolationTypeNotFoundException();
    }
  }
Example #4
0
  public List<ViolationDTO> createViolationDTO(List<Violation> violations) {
    List<ViolationDTO> violationDTOList = new ArrayList<ViolationDTO>();

    for (Violation violation : violations) {
      try {
        ViolationDTO violationDTO = createViolationDTO(violation);
        violationDTOList.add(violationDTO);
      } catch (ViolationTypeNotFoundException e) {
        logger.warn(
            String.format(
                "ViolationtypeKey: %s not found in violation", violation.getViolationtypeKey()));
      } catch (LanguageNotFoundException e) {
        logger.warn(e.getMessage());
      } catch (RuleTypeNotFoundException e) {
        logger.warn(e.getMessage());
      } catch (RuleInstantionException e) {
        logger.warn(e.getMessage());
      }
    }
    return violationDTOList;
  }
Example #5
0
  private void createTable() throws DocumentException {
    Phrase title = new Phrase();
    title.setFont(new Font(FontFamily.HELVETICA, 13F, Font.BOLD, BaseColor.BLUE));
    title.add("Violations");
    document.add(new Paragraph(title));
    document.add(new Paragraph(" "));

    PdfPTable pdfTable = new PdfPTable(report.getLocaleColumnHeaders().length);
    pdfTable.setWidths(new int[] {3, 4, 1, 2, 2, 1});
    pdfTable.setWidthPercentage(100);

    for (String columnHeader : report.getLocaleColumnHeaders()) {
      addCellToTable(pdfTable, columnHeader, BaseColor.GRAY, true);
    }

    for (Violation violation : report.getViolations().getValue()) {
      // Source
      if (violation.getClassPathFrom() != null && !violation.getClassPathFrom().trim().equals("")) {
        addCellToTable(pdfTable, violation.getClassPathFrom(), BaseColor.WHITE, false);
      } else {
        addCellToTable(pdfTable, "", BaseColor.WHITE, false);
      }

      // Rule
      if (violation.getMessage() != null) {
        String message = new Messagebuilder().createMessage(violation.getMessage(), violation);
        addCellToTable(pdfTable, message, BaseColor.WHITE, false);
      } else {
        addCellToTable(pdfTable, "", BaseColor.WHITE, false);
      }

      // LineNumber
      if (!(violation.getLinenumber() == 0)) {
        addCellToTable(pdfTable, "" + violation.getLinenumber(), BaseColor.WHITE, false);
      } else {
        addCellToTable(pdfTable, "", BaseColor.WHITE, false);
      }

      // DependencyKind
      if (violation.getViolationTypeKey() != null) {
        addCellToTable(
            pdfTable,
            getDependencyKindValue(violation.getViolationTypeKey(), violation.isIndirect()),
            BaseColor.WHITE,
            false);
      } else {
        addCellToTable(pdfTable, "", BaseColor.WHITE, false);
      }
      // Target
      if (violation.getClassPathFrom() != null && !violation.getClassPathFrom().trim().equals("")) {
        addCellToTable(pdfTable, violation.getClassPathTo(), BaseColor.WHITE, false);
      } else {
        addCellToTable(pdfTable, "", BaseColor.WHITE, false);
      }

      // Severity
      if (violation.getSeverity() != null) {
        addCellToTable(
            pdfTable, "" + violation.getSeverity().getSeverityName(), BaseColor.WHITE, false);
      } else {
        addCellToTable(pdfTable, "", BaseColor.WHITE, false);
      }
    }

    document.add(pdfTable);
  }