use of org.eclipse.n4js.json.JSON.JSONValue in project n4js by eclipse.
the class PackageJsonValidatorExtension method internalCheckModuleFilterEntry.
/**
* Checks whether the given {@code moduleFilterPair} represents a valid module-filter section entry (e.g. noValidate
* section).
*/
private void internalCheckModuleFilterEntry(NameValuePair moduleFilterPair) {
// obtain enum-representation of the validated module filter type
final ModuleFilterType filterType = PackageJsonUtils.parseModuleFilterType(moduleFilterPair.getName());
// make sure the module filter type could be parsed successfully
if (filterType == null) {
final String message = IssueCodes.getMessageForPKGJ_INVALID_MODULE_FILTER_TYPE(moduleFilterPair.getName(), "noValidate");
addIssue(message, moduleFilterPair, JSONPackage.Literals.NAME_VALUE_PAIR__NAME, IssueCodes.PKGJ_INVALID_MODULE_FILTER_TYPE);
}
// check type of RHS
if (!checkIsType(moduleFilterPair.getValue(), JSONPackage.Literals.JSON_ARRAY, "as module filter specifiers")) {
return;
}
// obtain a list of all declared filter specifiers
final List<JSONValue> specifierValues = ((JSONArray) moduleFilterPair.getValue()).getElements();
// obtain a list of all declared source container paths
final Set<String> sourceContainerPaths = getAllSourceContainerPaths();
// first, validate all declared filter specifiers
final List<ValidationModuleFilterSpecifier> declaredFilterSpecifiers = specifierValues.stream().map(v -> getModuleFilterInformation(v, filterType)).collect(Collectors.toList());
declaredFilterSpecifiers.forEach(specifier -> checkModuleFilterSpecifier(specifier));
// determine the groups of duplicate module filter specifiers (same source container and same filter)
final Map<String, List<ValidationModuleFilterSpecifier>> duplicateGroups = declaredFilterSpecifiers.stream().filter(i -> i != null).flatMap(i -> {
if (i.sourceContainerPath == null) {
// source container paths.
return sourceContainerPaths.stream().map(sourceContainer -> new ValidationModuleFilterSpecifier(i.filter, sourceContainer, i.filterType, i.astRepresentation));
} else {
// the module specifier filter only applies to the declared source container path
return Stream.of(i);
}
}).collect(Collectors.groupingBy(s -> s.filter + ":" + s.sourceContainerPath));
// compute set of all duplicate module filter specifiers
final Set<JSONValue> duplicateFilterSpecifiers = new HashSet<>();
// add an issue for all duplicate module filter specifiers
duplicateGroups.entrySet().stream().filter(e -> e.getValue().size() > 1).flatMap(group -> group.getValue().stream().skip(1)).forEach(duplicate -> duplicateFilterSpecifiers.add(duplicate.astRepresentation));
for (JSONValue duplicateFilterSpecifier : duplicateFilterSpecifiers) {
addIssue(IssueCodes.getMessageForPKGJ_DUPLICATE_MODULE_FILTER_SPECIFIER(), duplicateFilterSpecifier, IssueCodes.PKGJ_DUPLICATE_MODULE_FILTER_SPECIFIER);
}
}
use of org.eclipse.n4js.json.JSON.JSONValue in project n4js by eclipse.
the class PackageJsonValidatorExtension method doGetSourceContainers.
/**
* Validates the correct structure of a {@link PackageJsonProperties#SOURCES} section and returns a map between the
* declared source container types and corresponding {@link JSONStringLiteral}s that specify the various source
* container paths.
*/
private Multimap<SourceContainerType, List<JSONStringLiteral>> doGetSourceContainers() {
final Collection<JSONValue> sourcesValues = getDocumentValues(SOURCES);
// first check whether n4js.sources section has been defined at all
if (sourcesValues.isEmpty()) {
// return an empty map
return ImmutableMultimap.<SourceContainerType, List<JSONStringLiteral>>of();
}
// first check type of all occuring 'sources' sections
if (!checkIsType(sourcesValues, JSONPackage.Literals.JSON_OBJECT, "as source container section")) {
// return an empty map
return ImmutableMultimap.<SourceContainerType, List<JSONStringLiteral>>of();
}
// only consider the first n4js.sources section for further validation (in case of duplicates)
final JSONValue sourcesValue = sourcesValues.iterator().next();
final JSONObject sourceContainerObject = (JSONObject) sourcesValue;
final Multimap<SourceContainerType, List<JSONStringLiteral>> sourceContainerValues = HashMultimap.create();
for (NameValuePair pair : sourceContainerObject.getNameValuePairs()) {
final String sourceContainerType = pair.getName();
// compute type of source container sub-section
final SourceContainerType containerType = PackageJsonUtils.parseSourceContainerType(pair.getName());
// check that sourceContainerType represents a valid source container type
if (containerType == null) {
addIssue(IssueCodes.getMessageForPKGJ_INVALID_SOURCE_CONTAINER_TYPE(sourceContainerType), pair, JSONPackage.Literals.NAME_VALUE_PAIR__NAME, IssueCodes.PKGJ_INVALID_SOURCE_CONTAINER_TYPE);
continue;
}
// check type of RHS (list of source paths)
if (!checkIsType(pair.getValue(), JSONPackage.Literals.JSON_ARRAY, "as source container list")) {
continue;
}
final JSONArray sourceContainerSpecifiers = (JSONArray) pair.getValue();
// collect all source container paths in this list
final List<JSONStringLiteral> specifierLiterals = new ArrayList<>();
for (JSONValue specifier : sourceContainerSpecifiers.getElements()) {
if (!checkIsType(specifier, JSONPackage.Literals.JSON_STRING_LITERAL, "as source container specifier")) {
continue;
}
specifierLiterals.add((JSONStringLiteral) specifier);
}
// This may override a value in case of a duplicate containerType (e.g. two external sections).
// However, this will also issue an appropriate warning for a duplicate key and
// is therefore not handled here.
sourceContainerValues.put(containerType, specifierLiterals);
}
return sourceContainerValues;
}
Aggregations