Search in sources :

Example 1 with ValidationProblem

use of ch.acanda.eclipse.pmd.ui.model.ValidationProblem in project eclipse-pmd by acanda.

the class AddRuleSetConfigurationModel method validateLocation.

/**
 * Validates the location of the rule set configuration and sets or resets the property {@link #rules} depending on
 * whether {@link #location} contains a valid rule set configuration location or not.
 */
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private void validateLocation(final String propertyName, final ValidationResult result) {
    final Builder<Rule> rules = ImmutableList.builder();
    String ruleSetName = null;
    if (!errorIfBlank(LOCATION, location, "Please enter the location of the rule set configuration", result)) {
        RuleSet ruleSet = null;
        try {
            final String referenceId;
            if (isRemoteTypeSelected) {
                referenceId = validateRemoteLocation(result);
            } else {
                referenceId = validateLocalLocation(result);
            }
            if (referenceId != null) {
                ruleSet = new RuleSetFactory().createRuleSet(referenceId);
                ruleSetName = ruleSet.getName();
                rules.addAll(ruleSet.getRules());
            }
        } catch (final RuleSetNotFoundException | RuntimeException e) {
        // the rule set location is invalid - the validation problem will be added below
        }
        if (ruleSet == null || ruleSet.getRules().isEmpty()) {
            result.add(new ValidationProblem(LOCATION, Severity.ERROR, "The rule set configuration at the given location is invalid"));
        }
    }
    if (LOCATION.equals(propertyName)) {
        setRules(rules.build());
        setName(ruleSetName == null ? "" : ruleSetName);
    }
}
Also used : RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSet(net.sourceforge.pmd.RuleSet) RuleSetNotFoundException(net.sourceforge.pmd.RuleSetNotFoundException) ValidationProblem(ch.acanda.eclipse.pmd.ui.model.ValidationProblem) Rule(net.sourceforge.pmd.Rule)

Example 2 with ValidationProblem

use of ch.acanda.eclipse.pmd.ui.model.ValidationProblem in project eclipse-pmd by acanda.

the class AddRuleSetConfigurationModel method validateRemoteLocation.

private String validateRemoteLocation(final ValidationResult result) {
    String referenceId = null;
    try {
        final URI uri = new URI(location);
        try (InputStream stream = uri.toURL().openStream()) {
            final Path tempFile = Files.createTempFile("eclipse-pmd-remote-", ".xml");
            Files.copy(stream, tempFile, StandardCopyOption.REPLACE_EXISTING);
            tempFile.toFile().deleteOnExit();
            referenceId = tempFile.toString();
        }
    } catch (final URISyntaxException e) {
        result.add(new ValidationProblem(LOCATION, Severity.ERROR, "The location is not a valid URI"));
    } catch (final IOException e) {
        result.add(new ValidationProblem(LOCATION, Severity.ERROR, "The resource at the given URI does not exist"));
    }
    return referenceId;
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) ValidationProblem(ch.acanda.eclipse.pmd.ui.model.ValidationProblem) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI)

Example 3 with ValidationProblem

use of ch.acanda.eclipse.pmd.ui.model.ValidationProblem in project eclipse-pmd by acanda.

the class AddRuleSetConfigurationModel method validateLocalLocation.

private String validateLocalLocation(final ValidationResult result) {
    String referenceId = null;
    final Optional<Path> absoluteLocation = getAbsoluteLocation();
    if (absoluteLocation.isPresent()) {
        if (Files.exists(absoluteLocation.get())) {
            referenceId = absoluteLocation.get().toString();
        } else {
            final String msg = "The location {0} which resolves to {1} does not point to an existing file";
            result.add(new ValidationProblem(LOCATION, Severity.ERROR, MessageFormat.format(msg, location, absoluteLocation)));
        }
    } else {
        final String msg = "The location {0} cannot be resolved";
        result.add(new ValidationProblem(LOCATION, Severity.ERROR, MessageFormat.format(msg, location)));
    }
    return referenceId;
}
Also used : Path(java.nio.file.Path) ValidationProblem(ch.acanda.eclipse.pmd.ui.model.ValidationProblem)

Aggregations

ValidationProblem (ch.acanda.eclipse.pmd.ui.model.ValidationProblem)3 Path (java.nio.file.Path)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Rule (net.sourceforge.pmd.Rule)1 RuleSet (net.sourceforge.pmd.RuleSet)1 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)1 RuleSetNotFoundException (net.sourceforge.pmd.RuleSetNotFoundException)1