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