コード例 #1
0
 /**
  * Materialize the current active rule set for the profile
  *
  * @param activeRules
  * @param profileName
  * @return
  */
 protected PmdRuleset createRuleset(List<ActiveRule> activeRules, String profileName) {
   PmdRuleset ruleset = new PmdRuleset(profileName);
   for (ActiveRule activeRule : activeRules) {
     if (activeRule
         .getRule()
         .getPluginName()
         .equals(PhpCodeSnifferRuleRepository.PHPCS_REPOSITORY_KEY)) {
       String configKey = activeRule.getRule().getKey();
       PmdRule rule = new PmdRule(configKey, mapper.to(activeRule.getPriority()));
       List<PmdProperty> properties = null;
       List<ActiveRuleParam> activeRuleParams = activeRule.getActiveRuleParams();
       if (activeRuleParams != null && !activeRuleParams.isEmpty()) {
         properties = new ArrayList<PmdProperty>();
         for (ActiveRuleParam activeRuleParam : activeRuleParams) {
           properties.add(
               new PmdProperty(
                   activeRuleParam.getRuleParam().getKey(), activeRuleParam.getValue()));
         }
       }
       rule.setProperties(properties);
       ruleset.addRule(rule);
       processXPathRule(activeRule.getRuleKey(), rule);
     }
   }
   return ruleset;
 }
コード例 #2
0
  private List<ProfileDefinition> loadFromDeprecatedRepository(RulesRepository repository) {
    List<ProfileDefinition> result = new ArrayList<ProfileDefinition>();

    for (int index = 0; index < repository.getProvidedProfiles().size(); index++) {
      RulesProfile deprecated = (RulesProfile) repository.getProvidedProfiles().get(index);
      DefaultProfileDefinition providedProfile =
          DefaultProfileDefinition.create(deprecated.getName(), repository.getLanguage().getKey());
      for (ActiveRule deprecatedActiveRule : deprecated.getActiveRules(true)) {
        String repositoryKey = deprecatedActiveRule.getRepositoryKey();
        if (StringUtils.isBlank(repositoryKey)) {
          repositoryKey = getPluginKey(repository);
        }
        Rule rule = ruleFinder.findByKey(repositoryKey, deprecatedActiveRule.getRuleKey());
        if (rule != null) {
          ActiveRule activeRule =
              providedProfile.activateRule(rule, deprecatedActiveRule.getSeverity());
          for (ActiveRuleParam arp : deprecatedActiveRule.getActiveRuleParams()) {
            activeRule.setParameter(arp.getKey(), arp.getValue());
          }
        }
      }
      result.add(providedProfile);
    }
    return result;
  }
コード例 #3
0
 /**
  * Note: disabled rules are excluded.
  *
  * @return an active rule from a plugin key and a rule key if the rule is activated, null
  *     otherwise
  */
 @CheckForNull
 public ActiveRule getActiveRule(String repositoryKey, String ruleKey) {
   for (ActiveRule activeRule : activeRules) {
     if (StringUtils.equals(activeRule.getRepositoryKey(), repositoryKey)
         && StringUtils.equals(activeRule.getRuleKey(), ruleKey)
         && activeRule.isEnabled()) {
       return activeRule;
     }
   }
   return null;
 }
コード例 #4
0
 private void appendRule(ActiveRule activeRule, Writer writer) throws IOException {
   writer.append("<rule><repositoryKey>");
   writer.append(activeRule.getRepositoryKey());
   writer.append("</repositoryKey><key>");
   StringEscapeUtils.escapeXml(writer, activeRule.getRuleKey());
   writer.append("</key>");
   if (activeRule.getSeverity() != null) {
     writer.append("<priority>");
     writer.append(activeRule.getSeverity().name());
     writer.append("</priority>");
   }
   appendRuleParameters(activeRule, writer);
   writer.append("</rule>");
 }
コード例 #5
0
 private LintProfile createLintProfile(List<ActiveRule> activeRules) {
   LintProfile profile = new LintProfile();
   Map<String, RulePriority> activeKeys = new HashMap<>();
   List<LintIssue> issues = Lists.newArrayList();
   for (ActiveRule rule : activeRules) {
     activeKeys.put(rule.getRuleKey(), rule.getSeverity());
   }
   for (String ruleKey : ruleKeys) {
     issues.add(getLintIssue(ruleKey, activeKeys));
   }
   // ensure order of issues in output, sort by key.
   Collections.sort(issues, new IssueComparator());
   profile.issues = issues;
   return profile;
 }
コード例 #6
0
  /**
   * Matches to something like: (ATOM:ATOM/NUMBER)(WHITESPACES)(SOMETHING NOT
   * WHITESPACES)(:WHITESPACES)(NUMBER,NUMBER-NUMBER,NUMBER) like: game:start/0
   * /home/dev/project/erlang/game.erl: 4,1-5,12 means: 'application name':'function name'/'number
   * of parameters' 'URL to the file': 'starting row','starting col'-'ending row','ending col'
   */
  public ViolationReport refactorErl(Project project, RulesProfile profile) {
    ViolationReport report = new ViolationReport();
    List<ActiveRule> activeRules = profile.getActiveRulesByRepository("Erlang");

    /** Read refactorErl results */
    File basedir =
        new File(
            project.getFileSystem().getBasedir()
                + File.separator
                + ((Erlang) project.getLanguage()).getEunitFolder());
    LOG.debug("Parsing refactorErl reports from folder {}", basedir.getAbsolutePath());

    String refactorErlPattern = ((Erlang) project.getLanguage()).getRefactorErlFilenamePattern();

    String[] list = getFileNamesByPattern(basedir, refactorErlPattern);

    if (list.length == 0) {
      LOG.warn("no file matches to : ", refactorErlPattern);
      return report;
    }

    for (String file : list) {
      try {
        List<ViolationReportUnit> units = readRefactorErlReportUnits(basedir, file);
        for (ViolationReportUnit refactorErlReportUnit : units) {
          ActiveRule activeRule =
              ActiveRuleFilter.getActiveRuleByRuleName(
                  activeRules, refactorErlReportUnit.getMetricKey());
          if (activeRule != null
              && ViolationUtil.checkIsValid(activeRule, refactorErlReportUnit.getMetricValue())) {
            refactorErlReportUnit.setDescription(
                ViolationUtil.getMessageForMetric(
                    activeRule, refactorErlReportUnit.getMetricValue()));
            /** Replace key coming from activeProfile because it contains the name originaly */
            refactorErlReportUnit.setMetricKey(activeRule.getRuleKey());
            report.addUnit(refactorErlReportUnit);
          }
        }
      } catch (FileNotFoundException e) {
      } catch (IOException e) {
      }
    }
    return report;
  }