Search in sources :

Example 26 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.

the class PackageJsonValidatorExtension method getModuleFilterInformation.

/**
 * Validates the structure of the given {@code value} as module filter specifier and returns the information that
 * can be extracted from it.
 *
 * Returns the module filter specifier information of {@code value} in terms of a
 * {@link ValidationModuleFilterSpecifier}.
 *
 * Returns {@code null} if the given {@code value} is not a valid representation of a module filter specifier.
 *
 * Similar to {@link PackageJsonUtils#asModuleFilterSpecifierOrNull(JSONValue)} but also validates the structure
 * along the way.
 */
private ValidationModuleFilterSpecifier getModuleFilterInformation(JSONValue value, ModuleFilterType type) {
    // 1st variant:
    if (value instanceof JSONStringLiteral) {
        return new ValidationModuleFilterSpecifier(((JSONStringLiteral) value).getValue(), null, type, value);
    }
    // 2nd variant:
    if (value instanceof JSONObject) {
        final List<NameValuePair> pairs = ((JSONObject) value).getNameValuePairs();
        final NameValuePair sourceContainerPair = pairs.stream().filter(p -> NV_SOURCE_CONTAINER.name.equals(p.getName())).findFirst().orElse(null);
        final NameValuePair moduleFilterPair = pairs.stream().filter(p -> NV_MODULE.name.equals(p.getName())).findFirst().orElse(null);
        // make sure the pairs are of correct type (or null in case of sourceContainerPair)
        if ((sourceContainerPair == null || checkIsType(sourceContainerPair.getValue(), JSONPackage.Literals.JSON_STRING_LITERAL)) && (moduleFilterPair != null && checkIsType(moduleFilterPair.getValue(), JSONPackage.Literals.JSON_STRING_LITERAL))) {
            final String sourceContainer = sourceContainerPair != null ? ((JSONStringLiteral) sourceContainerPair.getValue()).getValue() : null;
            final String moduleFilter = ((JSONStringLiteral) moduleFilterPair.getValue()).getValue();
            return new ValidationModuleFilterSpecifier(moduleFilter, sourceContainer, type, value);
        }
    }
    // otherwise 'value' does not represent a valid module filter specifier
    addIssue(IssueCodes.getMessageForPKGJ_INVALID_MODULE_FILTER_SPECIFIER(), value, IssueCodes.PKGJ_INVALID_MODULE_FILTER_SPECIFIER);
    return null;
}
Also used : NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

Example 27 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair 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);
    }
}
Also used : REQUIRED_RUNTIME_LIBRARIES(org.eclipse.n4js.packagejson.PackageJsonProperties.REQUIRED_RUNTIME_LIBRARIES) IssueCodes(org.eclipse.n4js.validation.IssueCodes) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral) Inject(com.google.inject.Inject) NV_SOURCE_CONTAINER(org.eclipse.n4js.packagejson.PackageJsonProperties.NV_SOURCE_CONTAINER) NAME(org.eclipse.n4js.packagejson.PackageJsonProperties.NAME) FileUtils(org.eclipse.n4js.utils.io.FileUtils) ProjectNameInfo(org.eclipse.n4js.utils.ProjectDescriptionUtils.ProjectNameInfo) URLVersionRequirement(org.eclipse.n4js.semver.Semver.URLVersionRequirement) VENDOR_NAME(org.eclipse.n4js.packagejson.PackageJsonProperties.VENDOR_NAME) HashMultimap(com.google.common.collect.HashMultimap) PackageJsonUtils(org.eclipse.n4js.packagejson.PackageJsonUtils) DEPENDENCIES(org.eclipse.n4js.packagejson.PackageJsonProperties.DEPENDENCIES) ModuleFilterType(org.eclipse.n4js.packagejson.projectDescription.ModuleFilterType) Optional(com.google.common.base.Optional) Map(java.util.Map) SourceContainerType(org.eclipse.n4js.packagejson.projectDescription.SourceContainerType) VersionRangeSetRequirement(org.eclipse.n4js.semver.Semver.VersionRangeSetRequirement) INode(org.eclipse.xtext.nodemodel.INode) Check(org.eclipse.xtext.validation.Check) Path(java.nio.file.Path) PackageJsonProperties(org.eclipse.n4js.packagejson.PackageJsonProperties) JSONPackage(org.eclipse.n4js.json.JSON.JSONPackage) ProjectType(org.eclipse.n4js.packagejson.projectDescription.ProjectType) IParseResult(org.eclipse.xtext.parser.IParseResult) FileURI(org.eclipse.n4js.workspace.locations.FileURI) DEFINES_PACKAGE(org.eclipse.n4js.packagejson.PackageJsonProperties.DEFINES_PACKAGE) N4JS(org.eclipse.n4js.packagejson.PackageJsonProperties.N4JS) Collection(java.util.Collection) IMPLEMENTATION_ID(org.eclipse.n4js.packagejson.PackageJsonProperties.IMPLEMENTATION_ID) Set(java.util.Set) EObject(org.eclipse.emf.ecore.EObject) TagVersionRequirement(org.eclipse.n4js.semver.Semver.TagVersionRequirement) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Collectors(java.util.stream.Collectors) GitHubVersionRequirement(org.eclipse.n4js.semver.Semver.GitHubVersionRequirement) JSONArray(org.eclipse.n4js.json.JSON.JSONArray) List(java.util.List) Stream(java.util.stream.Stream) NPMVersionRequirement(org.eclipse.n4js.semver.Semver.NPMVersionRequirement) SimpleVersion(org.eclipse.n4js.semver.Semver.SimpleVersion) GENERATOR_REWRITE_MODULE_SPECIFIERS(org.eclipse.n4js.packagejson.PackageJsonProperties.GENERATOR_REWRITE_MODULE_SPECIFIERS) IMPLEMENTED_PROJECTS(org.eclipse.n4js.packagejson.PackageJsonProperties.IMPLEMENTED_PROJECTS) Entry(java.util.Map.Entry) Resource(org.eclipse.emf.ecore.resource.Resource) ProjectDescription(org.eclipse.n4js.packagejson.projectDescription.ProjectDescription) Singleton(com.google.inject.Singleton) DEV_DEPENDENCIES(org.eclipse.n4js.packagejson.PackageJsonProperties.DEV_DEPENDENCIES) URI(org.eclipse.emf.common.util.URI) VersionRangeConstraint(org.eclipse.n4js.semver.Semver.VersionRangeConstraint) JSONValue(org.eclipse.n4js.json.JSON.JSONValue) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) Multimap(com.google.common.collect.Multimap) VERSION(org.eclipse.n4js.packagejson.PackageJsonProperties.VERSION) NodeModelUtils(org.eclipse.xtext.nodemodel.util.NodeModelUtils) PROVIDED_RUNTIME_LIBRARIES(org.eclipse.n4js.packagejson.PackageJsonProperties.PROVIDED_RUNTIME_LIBRARIES) TESTED_PROJECTS(org.eclipse.n4js.packagejson.PackageJsonProperties.TESTED_PROJECTS) VENDOR_ID(org.eclipse.n4js.packagejson.PackageJsonProperties.VENDOR_ID) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) InvalidPathException(java.nio.file.InvalidPathException) EClass(org.eclipse.emf.ecore.EClass) N4JSGlobals(org.eclipse.n4js.N4JSGlobals) WorkspaceAccess(org.eclipse.n4js.workspace.WorkspaceAccess) PROJECT_TYPE(org.eclipse.n4js.packagejson.PackageJsonProperties.PROJECT_TYPE) JSONDocument(org.eclipse.n4js.json.JSON.JSONDocument) MAIN_MODULE(org.eclipse.n4js.packagejson.PackageJsonProperties.MAIN_MODULE) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) JSONModelUtils.asNonEmptyStringOrNull(org.eclipse.n4js.json.model.utils.JSONModelUtils.asNonEmptyStringOrNull) ProjectDescriptionUtils(org.eclipse.n4js.utils.ProjectDescriptionUtils) MODULE_FILTERS(org.eclipse.n4js.packagejson.PackageJsonProperties.MODULE_FILTERS) NV_MODULE(org.eclipse.n4js.packagejson.PackageJsonProperties.NV_MODULE) OUTPUT(org.eclipse.n4js.packagejson.PackageJsonProperties.OUTPUT) Iterator(java.util.Iterator) EXTENDED_RUNTIME_ENVIRONMENT(org.eclipse.n4js.packagejson.PackageJsonProperties.EXTENDED_RUNTIME_ENVIRONMENT) SOURCES(org.eclipse.n4js.packagejson.PackageJsonProperties.SOURCES) File(java.io.File) LocalPathVersionRequirement(org.eclipse.n4js.semver.Semver.LocalPathVersionRequirement) SemverHelper(org.eclipse.n4js.semver.SemverHelper) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) Issue(org.eclipse.xtext.validation.Issue) Paths(java.nio.file.Paths) SemverSerializer(org.eclipse.n4js.semver.model.SemverSerializer) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONArray(org.eclipse.n4js.json.JSON.JSONArray) ModuleFilterType(org.eclipse.n4js.packagejson.projectDescription.ModuleFilterType) JSONValue(org.eclipse.n4js.json.JSON.JSONValue) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 28 with NameValuePair

use of org.eclipse.n4js.json.JSON.NameValuePair 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;
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) JSONArray(org.eclipse.n4js.json.JSON.JSONArray) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) SourceContainerType(org.eclipse.n4js.packagejson.projectDescription.SourceContainerType) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

Aggregations

NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)28 JSONObject (org.eclipse.n4js.json.JSON.JSONObject)17 JSONValue (org.eclipse.n4js.json.JSON.JSONValue)17 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)7 EObject (org.eclipse.emf.ecore.EObject)6 JSONArray (org.eclipse.n4js.json.JSON.JSONArray)4 JSONDocument (org.eclipse.n4js.json.JSON.JSONDocument)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 URI (org.eclipse.emf.common.util.URI)2 JSONBooleanLiteral (org.eclipse.n4js.json.JSON.JSONBooleanLiteral)2 JSONNumericLiteral (org.eclipse.n4js.json.JSON.JSONNumericLiteral)2 SourceContainerType (org.eclipse.n4js.packagejson.projectDescription.SourceContainerType)2 NPMVersionRequirement (org.eclipse.n4js.semver.Semver.NPMVersionRequirement)2 INode (org.eclipse.xtext.nodemodel.INode)2 Check (org.eclipse.xtext.validation.Check)2 Optional (com.google.common.base.Optional)1