use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.
the class JSONModelUtils method getPathToNameValuePairOrNull.
/**
* Returns the path to the given {@link NameValuePair} in its containing JSON object tree or <code>null</code> in
* case of syntax error.
*/
public static List<String> getPathToNameValuePairOrNull(NameValuePair nvp) {
List<String> path = new ArrayList<>();
EObject currNVP = nvp;
do {
String currName = ((NameValuePair) currNVP).getName();
if (currName == null) {
// syntax error in JSON file
return null;
}
path.add(currName);
currNVP = currNVP.eContainer();
currNVP = currNVP instanceof JSONObject ? currNVP.eContainer() : null;
} while (currNVP instanceof NameValuePair);
Collections.reverse(path);
return path;
}
use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.
the class PackageJsonUtils method searchNameValuePair.
@SuppressWarnings("unchecked")
private static <T extends JSONValue> void searchNameValuePair(NameValuePair valuePair, String[] pathElems, int i, Class<T> clazz, List<T> result) {
String searchName = pathElems[i];
String jsonName = valuePair.getName();
JSONValue jsonValue = valuePair.getValue();
if (i >= pathElems.length || !searchName.equals(jsonName)) {
return;
}
if (i == pathElems.length - 1) {
if (clazz == null || clazz.isAssignableFrom(jsonValue.getClass())) {
result.add((T) jsonValue);
return;
}
}
if (jsonValue instanceof JSONObject) {
JSONObject jObj = (JSONObject) jsonValue;
for (NameValuePair child : jObj.getNameValuePairs()) {
searchNameValuePair(child, pathElems, i + 1, clazz, result);
}
}
}
use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.
the class PackageJsonUtils method asModuleFilterSpecifierOrNull.
/**
* Converts given JSON value to a {@link ModuleFilterSpecifier}; returns <code>null</code> if not possible.<br>
* The following variants are supported:
*
* <pre>
* "abc*"
*
* // or:
*
* {
* "sourceContainer": "src"
* "module": "abc*",
* }
* </pre>
*/
public static ModuleFilterSpecifier asModuleFilterSpecifierOrNull(JSONValue jsonValue) {
// 1st variant:
String singleString = asNonEmptyStringOrNull(jsonValue);
if (singleString != null) {
return new ModuleFilterSpecifier(singleString, null);
}
// 2nd variant:
List<NameValuePair> pairs = asNameValuePairsOrEmpty(jsonValue);
NameValuePair pathNVP = pairs.stream().filter(p -> NV_SOURCE_CONTAINER.name.equals(p.getName())).findFirst().orElse(null);
NameValuePair moduleNVP = pairs.stream().filter(p -> NV_MODULE.name.equals(p.getName())).findFirst().orElse(null);
String pathStr = pathNVP != null ? asNonEmptyStringOrNull(pathNVP.getValue()) : null;
String moduleStr = moduleNVP != null ? asNonEmptyStringOrNull(moduleNVP.getValue()) : null;
if (moduleStr != null) {
// pathStr may be null, i.e. "sourceContainer" is optional
return new ModuleFilterSpecifier(moduleStr, pathStr);
}
return null;
}
use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.
the class PackageJsonHelper method convertToProjectDescription.
/**
* Transform the given {@code packageJSON} into an equivalent {@link ProjectDescriptionBuilder} instance. If no
* further adjustments are required, the client can immediately invoke the {@code #build()} method to obtain the
* corresponding {@link ProjectDescription}.
* <p>
* Note: this method does not implement the package.json feature that a "main" path may point to a folder and then a
* file "index.js" in that folder will be used as main module (for details see
* {@link ProjectDescriptionUtils#convertMainPathToModuleSpecifier(String, List)}).
*
* @param packageJSON
* the JSON document to convert (should be the representation of a valid {@code package.json} file).
* @param applyDefaultValues
* whether default values should be applied to the project description after conversion.
* @param defaultProjectName
* the default project ID (will be ignored if {@code applyDefaultValues} is set to <code>false</code>.
* @return the project description converted from the given JSON document or <code>null</code> if the root value of
* the given JSON document is not a {@link JSONObject}.
*/
public ProjectDescriptionBuilder convertToProjectDescription(JSONDocument packageJSON, boolean applyDefaultValues, String defaultProjectName) {
JSONValue rootValue = packageJSON.getContent();
if (!(rootValue instanceof JSONObject)) {
return null;
}
ProjectDescriptionBuilder target = new ProjectDescriptionBuilder();
List<NameValuePair> rootPairs = ((JSONObject) rootValue).getNameValuePairs();
convertRootPairs(target, rootPairs);
String valueOfPropMain = asNonEmptyStringOrNull(getProperty((JSONObject) rootValue, MAIN.name).orElse(null));
adjustProjectDescriptionAfterConversion(target, applyDefaultValues, defaultProjectName, valueOfPropMain);
return target;
}
use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.
the class PackageJsonHelper method convertRootPairs.
private void convertRootPairs(ProjectDescriptionBuilder target, List<NameValuePair> rootPairs) {
for (NameValuePair pair : rootPairs) {
PackageJsonProperties property = PackageJsonProperties.valueOfNameValuePairOrNull(pair);
if (property == null) {
continue;
}
JSONValue value = pair.getValue();
switch(property) {
case NAME:
target.setPackageName(asNonEmptyStringOrNull(value));
break;
case VERSION:
target.setVersion(asVersionNumberOrNull(value));
break;
case TYPE:
// legal values are 'commonjs' and 'module'
target.setESM("module".equals(asNonEmptyStringOrNull(value)));
break;
case DEPENDENCIES:
convertDependencies(target, asNameValuePairsOrEmpty(value), true, DependencyType.RUNTIME);
break;
case DEV_DEPENDENCIES:
// for the moment, we do not separate devDependencies from ordinary dependencies in ProjectDescription
convertDependencies(target, asNameValuePairsOrEmpty(value), true, DependencyType.DEVELOPMENT);
break;
case MAIN:
// (see method #adjustProjectDescriptionAfterConversion())
break;
case MODULE:
// we don't care about the actual value, just about the fact that property "module" is present
target.setModuleProperty(true);
break;
case N4JS:
// mark project with N4JS nature
target.setN4JSNature(true);
convertN4jsPairs(target, asNameValuePairsOrEmpty(value));
break;
case WORKSPACES_ARRAY:
target.setYarnWorkspaceRoot(true);
target.getWorkspaces().addAll(asStringsInArrayOrEmpty(value));
break;
case WORKSPACES_OBJECT:
target.setYarnWorkspaceRoot(true);
JSONObject workspaces = (JSONObject) value;
for (NameValuePair pairOfWorkspaces : workspaces.getNameValuePairs()) {
PackageJsonProperties wProp = PackageJsonProperties.valueOfNameValuePairOrNull(pairOfWorkspaces);
if (wProp == PACKAGES) {
JSONValue packagesValue = pairOfWorkspaces.getValue();
target.getWorkspaces().addAll(asStringsInArrayOrEmpty(packagesValue));
break;
}
}
break;
default:
break;
}
}
}
Aggregations