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);
}
}
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;
}
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;
}
Aggregations