Search in sources :

Example 21 with JSONValue

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

the class JSONModelUtils method setPath.

private static <V extends JSONValue> V setPath(JSONObject object, List<String> currentPath, List<String> fullPath, V value) {
    if (currentPath.size() == 0) {
        return null;
    }
    final String currentProperty = currentPath.get(0);
    final int pathLength = currentPath.size();
    // if we are at the end of the path
    if (pathLength == 1) {
        // set the value on 'object'
        setProperty(object, currentProperty, value);
        return value;
    }
    // obtain NameValuePair that matches the first segment in propertyPath
    final Optional<NameValuePair> pair = getNameValuePair(object, currentProperty);
    // if pair already exists
    if (pair.isPresent()) {
        final JSONValue pathValue = pair.get().getValue();
        // check whether the value is an object
        if (!(pathValue instanceof JSONObject)) {
            // if not, the property path is invalid
            throw new JSONPropertyPathException("Cannot resolve JSON property path further then " + fullPath.subList(0, fullPath.size() - pathLength).stream().collect(Collectors.joining(".")) + ". " + pathValue + " is not a JSONObject.", null);
        }
        // setPath recursively on the (object) value of the existing pair
        return setPath((JSONObject) pathValue, currentPath.subList(1, pathLength), fullPath, value);
    } else {
        // add new object name-value-pair for current property
        final JSONObject nextObject = addProperty(object, currentProperty, JSONFactory.eINSTANCE.createJSONObject());
        return setPath(nextObject, currentPath.subList(1, pathLength), fullPath, value);
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject)

Example 22 with JSONValue

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

the class JSONValidator method checkDuplicateKeys.

/**
 * Checks for duplicate keys in {@link JSONObject}s.
 */
@Check
public void checkDuplicateKeys(JSONObject object) {
    final Map<String, JSONValue> values = new HashMap<>();
    for (NameValuePair pair : object.getNameValuePairs()) {
        final JSONValue value = values.get(pair.getName());
        if (value != null) {
            final INode duplicatedNode = NodeModelUtils.findActualNodeFor(value);
            final int duplicatedLine = NodeModelUtils.getLineAndColumn(duplicatedNode, duplicatedNode.getOffset()).getLine();
            addIssue(JSONIssueCodes.getMessageForJSON_DUPLICATE_KEY(pair.getName(), duplicatedLine), pair, JSONPackage.Literals.NAME_VALUE_PAIR__NAME, JSONIssueCodes.JSON_DUPLICATE_KEY);
        }
        values.put(pair.getName(), pair.getValue());
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) INode(org.eclipse.xtext.nodemodel.INode) HashMap(java.util.HashMap) Check(org.eclipse.xtext.validation.Check)

Example 23 with JSONValue

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

the class PackageJsonUtils method findNameValuePairs.

/**
 * Traverses the given {@link Resource} and finds all {@link JSONValue}s that match the given
 * {@link PackageJsonProperties}. The list of results is filtered in case a {@link Class} is given.
 *
 * @return all {@link NameValuePair}s for the given {@link PackageJsonProperties} in the given json resource
 */
public static <T extends JSONValue> List<T> findNameValuePairs(Resource jsonResource, PackageJsonProperties prop, Class<T> clazz) {
    String[] pathElements = prop.getPathElements();
    List<T> nameValuePairs = new LinkedList<>();
    EList<EObject> contents = jsonResource.getContents();
    EObject rootElem = contents.get(0);
    if (rootElem instanceof JSONDocument) {
        JSONDocument jsonDocument = (JSONDocument) rootElem;
        JSONValue jsonContent = jsonDocument.getContent();
        if (jsonContent instanceof JSONObject) {
            JSONObject jsonObj = (JSONObject) jsonContent;
            for (NameValuePair child : jsonObj.getNameValuePairs()) {
                searchNameValuePair(child, pathElements, 0, clazz, nameValuePairs);
            }
        }
    }
    return nameValuePairs;
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) EObject(org.eclipse.emf.ecore.EObject) JSONDocument(org.eclipse.n4js.json.JSON.JSONDocument) LinkedList(java.util.LinkedList)

Example 24 with JSONValue

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

the class PackageJsonHelper method convertDependencies.

private void convertDependencies(ProjectDescriptionBuilder target, List<NameValuePair> depPairs, boolean avoidDuplicates, DependencyType type) {
    Objects.requireNonNull(type);
    Set<String> existingProjectNames = new HashSet<>();
    if (avoidDuplicates) {
        for (ProjectDependency pd : target.getDependencies()) {
            existingProjectNames.add(pd.getPackageName());
        }
    }
    for (NameValuePair pair : depPairs) {
        String projectName = pair.getName();
        boolean addProjectDependency = true;
        addProjectDependency &= projectName != null && !projectName.isEmpty();
        addProjectDependency &= !(avoidDuplicates && existingProjectNames.contains(projectName));
        existingProjectNames.add(projectName);
        if (addProjectDependency) {
            JSONValue value = pair.getValue();
            String valueStr = asStringOrNull(value);
            NPMVersionRequirement versionRequirement = valueStr != null ? semverHelper.parse(valueStr) : null;
            ProjectDependency dep = new ProjectDependency(projectName, type, valueStr, versionRequirement);
            target.addDependency(dep);
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) ProjectDependency(org.eclipse.n4js.packagejson.projectDescription.ProjectDependency) NPMVersionRequirement(org.eclipse.n4js.semver.Semver.NPMVersionRequirement) HashSet(java.util.HashSet)

Example 25 with JSONValue

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

the class PackageJsonValidatorExtension method checkImplementedProjects.

/**
 * Validates basic properties of the list of {@code n4js.implementedProjects}.
 */
@CheckProperty(property = IMPLEMENTED_PROJECTS)
public void checkImplementedProjects(JSONValue value) {
    // check for correct types of implementedProjects
    if (!checkIsType(value, JSONPackage.Literals.JSON_ARRAY, "as list of implemented projects")) {
        return;
    }
    // check for correct types of all implementedProjects elements (they represent project references)
    final List<JSONValue> implementedProjectValues = ((JSONArray) value).getElements();
    List<JSONStringLiteral> implementedProjectLiterals = implementedProjectValues.stream().map(v -> {
        if (!checkIsType(v, JSONPackage.Literals.JSON_STRING_LITERAL, "as implemented project reference")) {
            return null;
        }
        return ((JSONStringLiteral) v);
    }).filter(p -> p != null).collect(Collectors.toList());
    // obtain the declared project name (name property)
    final JSONStringLiteral declaredProjectNameValue = getSingleDocumentValue(NAME, JSONStringLiteral.class);
    // exit early if project name cannot be determined
    if (declaredProjectNameValue == null) {
        return;
    }
    for (JSONStringLiteral implementedProjectLiteral : implementedProjectLiterals) {
        if (implementedProjectLiteral.getValue().equals(declaredProjectNameValue.getValue())) {
            // reflexive implementation
            addIssue(IssueCodes.getMessageForPKGJ_APIIMPL_REFLEXIVE(), implementedProjectLiteral, IssueCodes.PKGJ_APIIMPL_REFLEXIVE);
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) 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) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

Aggregations

JSONValue (org.eclipse.n4js.json.JSON.JSONValue)27 NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)19 JSONObject (org.eclipse.n4js.json.JSON.JSONObject)17 EObject (org.eclipse.emf.ecore.EObject)7 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)7 JSONDocument (org.eclipse.n4js.json.JSON.JSONDocument)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 URI (org.eclipse.emf.common.util.URI)4 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)3 Multimap (com.google.common.collect.Multimap)3 Inject (com.google.inject.Inject)3 Collection (java.util.Collection)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 EClass (org.eclipse.emf.ecore.EClass)3 Resource (org.eclipse.emf.ecore.resource.Resource)3 N4JSGlobals (org.eclipse.n4js.N4JSGlobals)3 JSONArray (org.eclipse.n4js.json.JSON.JSONArray)3