use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.
the class PackageJsonHelper method convertN4jsPairs.
private void convertN4jsPairs(ProjectDescriptionBuilder target, List<NameValuePair> n4jsPairs) {
for (NameValuePair pair : n4jsPairs) {
PackageJsonProperties property = PackageJsonProperties.valueOfNameValuePairOrNull(pair);
if (property == null) {
continue;
}
JSONValue value = pair.getValue();
switch(property) {
case PROJECT_TYPE:
ProjectType projectType = parseProjectType(asNonEmptyStringOrNull(value));
if (projectType != null) {
target.setType(projectType);
}
break;
case VENDOR_ID:
target.setVendorId(asNonEmptyStringOrNull(value));
break;
case VENDOR_NAME:
target.setVendorName(asNonEmptyStringOrNull(value));
break;
case OUTPUT:
target.setOutputPath(asNonEmptyStringOrNull(value));
break;
case SOURCES:
target.getSourceContainers().addAll(asSourceContainerDescriptionsOrEmpty(value));
break;
case MODULE_FILTERS:
target.getModuleFilters().addAll(asModuleFiltersInObjectOrEmpty(value));
break;
case MAIN_MODULE:
target.setMainModule(asNonEmptyStringOrNull(value));
break;
case TESTED_PROJECTS:
target.getTestedProjects().addAll(asProjectReferencesInArrayOrEmpty(value));
break;
case IMPLEMENTATION_ID:
target.setImplementationId(asNonEmptyStringOrNull(value));
break;
case IMPLEMENTED_PROJECTS:
target.getImplementedProjects().addAll(asProjectReferencesInArrayOrEmpty(value));
break;
case EXTENDED_RUNTIME_ENVIRONMENT:
target.setExtendedRuntimeEnvironment(asProjectReferenceOrNull(value));
break;
case PROVIDED_RUNTIME_LIBRARIES:
target.getProvidedRuntimeLibraries().addAll(asProjectReferencesInArrayOrEmpty(value));
break;
case REQUIRED_RUNTIME_LIBRARIES:
target.getRequiredRuntimeLibraries().addAll(asProjectReferencesInArrayOrEmpty(value));
break;
case DEFINES_PACKAGE:
target.setDefinesPackage(asStringOrNull(value));
break;
case GENERATOR:
convertN4jsPairs(target, asNameValuePairsOrEmpty(value));
break;
case GENERATOR_SOURCE_MAPS:
target.setGeneratorEnabledSourceMaps(asBooleanOrDefault(value, (Boolean) PackageJsonProperties.GENERATOR_SOURCE_MAPS.defaultValue));
break;
case GENERATOR_DTS:
target.setGeneratorEnabledDts(asBooleanOrDefault(value, (Boolean) PackageJsonProperties.GENERATOR_DTS.defaultValue));
break;
case GENERATOR_REWRITE_MODULE_SPECIFIERS:
for (NameValuePair nvp : asNameValuePairsOrEmpty(value)) {
String n = nvp.getName();
String v = asStringOrNull(nvp.getValue());
if (n != null && v != null) {
// note: we allow empty strings
target.getGeneratorRewriteModuleSpecifiers().put(n, v);
}
}
break;
case GENERATOR_REWRITE_CJS_IMPORTS:
target.setGeneratorEnabledRewriteCjsImports(asBooleanOrDefault(value, (Boolean) PackageJsonProperties.GENERATOR_REWRITE_CJS_IMPORTS.defaultValue));
break;
default:
break;
}
}
}
use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.
the class ProjectDescriptionLoader method adjustMainPath.
/**
* Adjust the path value of the "main" property of the given package.json document as follows (in-place change of
* the given JSON document):
* <ol>
* <li>if the path points to a folder, then "/index.js" will be appended,
* <li>if neither a folder nor a file exist at the location the path points to and the path does not end in ".js",
* then ".js" will be appended.
* </ol>
*/
private void adjustMainPath(URI location, JSONDocument packageJSON) {
JSONValue content = packageJSON.getContent();
if (!(content instanceof JSONObject))
return;
JSONObject contentCasted = (JSONObject) content;
NameValuePair mainProperty = JSONModelUtils.getNameValuePair(contentCasted, MAIN.name).orElse(null);
if (mainProperty == null) {
return;
}
String main = asNonEmptyStringOrNull(mainProperty.getValue());
if (main == null) {
return;
}
String[] mainSegments;
if (File.separatorChar == '/') {
List<String> splitted = Strings.split(main, '/');
mainSegments = splitted.toArray(String[]::new);
} else {
mainSegments = windowsPattern.split(main);
}
URI locationWithMain = location.appendSegments(mainSegments);
if (!main.endsWith(".js") && isFile(URIConverter.INSTANCE, locationWithMain.appendFileExtension("js"))) {
main += ".js";
mainProperty.setValue(JSONModelUtils.createStringLiteral(main));
} else if (isDirectory(URIConverter.INSTANCE, locationWithMain)) {
if (!(main.endsWith("/") || main.endsWith(File.separator))) {
main += "/";
}
main += "index.js";
mainProperty.setValue(JSONModelUtils.createStringLiteral(main));
}
}
use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.
the class AbstractPackageJSONValidatorExtension method collectDocumentValues.
/**
* Collects all key-path-value pairs that can be extracted from the given {@code object} and stores them in
* {@code documentValues}.
*
* Key-paths are represented in terms of concatenated strings, separated by a {@code .} character (e.g.
* "nested.name.value" => JSONValue).
*
* @param object
* The {@link JSONObject} to collect the values form.
* @param documentValues
* The map to store the values to.
* @param prefix
* The prefix to use for key-paths.
* @param depth
* The depth up to which the given {@link JSONObject} should be traversed. If {@code -1} no depth limit
* is assumed.
*/
private Multimap<String, JSONValue> collectDocumentValues(JSONObject object, Multimap<String, JSONValue> documentValues, String prefix, int depth) {
// For negative depths this will always evaluate to false -> no depth limit
if (depth == 0) {
return documentValues;
}
for (NameValuePair pair : object.getNameValuePairs()) {
final String pairName = pair.getName();
final String name = prefix.isEmpty() ? pairName : prefix + "." + pairName;
final JSONValue value = pair.getValue();
documentValues.put(name, value);
if (value instanceof JSONObject) {
// recursively collect all values from pair value
collectDocumentValues((JSONObject) value, documentValues, name, depth - 1);
}
}
return documentValues;
}
use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.
the class PackageJsonValidatorExtension method checkN4JSSection.
/**
* Checks basic structural properties of the 'n4js' section (e.g. mandatory properties).
*/
@CheckProperty(property = N4JS)
public void checkN4JSSection(JSONValue n4jsSection) {
// make sure n4js section is an object
if (!checkIsType(n4jsSection, JSONPackage.Literals.JSON_OBJECT, "as package.json n4js section.")) {
return;
}
JSONObject n4jsSectionJO = (JSONObject) n4jsSection;
final Multimap<String, JSONValue> n4jsValues = collectObjectValues(n4jsSectionJO);
// Check for correct types (null-values (non-existent) will not lead to issues)
// Properties that are not checked here, have their own check-method which also validates their types.
checkIsType(n4jsValues.get(VENDOR_ID.name), JSONPackage.Literals.JSON_STRING_LITERAL, "as vendor ID");
checkIsType(n4jsValues.get(VENDOR_NAME.name), JSONPackage.Literals.JSON_STRING_LITERAL, "as vendor name");
checkIsType(n4jsValues.get(OUTPUT.name), JSONPackage.Literals.JSON_STRING_LITERAL, "as output folder path");
checkIsType(n4jsValues.get(EXTENDED_RUNTIME_ENVIRONMENT.name), JSONPackage.Literals.JSON_STRING_LITERAL, "as reference to extended runtime environment");
checkIsArrayOfType(n4jsValues.get(PROVIDED_RUNTIME_LIBRARIES.name), JSONPackage.Literals.JSON_STRING_LITERAL, "as provided runtime libraries", "as library reference");
checkIsArrayOfType(n4jsValues.get(REQUIRED_RUNTIME_LIBRARIES.name), JSONPackage.Literals.JSON_STRING_LITERAL, "as required runtime libraries", "as library reference");
// Check for empty strings
checkIsNonEmptyString(n4jsValues.get(VENDOR_ID.name), VENDOR_ID);
checkIsNonEmptyString(n4jsValues.get(VENDOR_NAME.name), VENDOR_NAME);
Set<String> allN4JSPropertyNames = PackageJsonProperties.getAllN4JSPropertyNames();
for (NameValuePair nameValuePair : n4jsSectionJO.getNameValuePairs()) {
String nvpName = nameValuePair.getName();
if (!allN4JSPropertyNames.contains(nvpName)) {
String msg = IssueCodes.getMessageForPKGJ_PROPERTY_UNKNOWN(nvpName);
addIssue(msg, nameValuePair, JSONPackage.Literals.NAME_VALUE_PAIR__NAME, IssueCodes.PKGJ_PROPERTY_UNKNOWN);
}
}
}
use of org.eclipse.n4js.json.JSON.NameValuePair in project n4js by eclipse.
the class PackageJsonValidatorExtension method checkRewriteModuleSpecifiers.
/**
* Checks 'rewriteModuleSpecifiers'.
*/
@CheckProperty(property = GENERATOR_REWRITE_MODULE_SPECIFIERS)
public void checkRewriteModuleSpecifiers(JSONValue value) {
if (!checkIsType(value, JSONPackage.Literals.JSON_OBJECT, "(map from module specifier in N4JS source code to specifier used in output code)")) {
return;
}
for (NameValuePair nvp : ((JSONObject) value).getNameValuePairs()) {
String n = nvp.getName();
JSONValue v = nvp.getValue();
if (n == null || v == null) {
// syntax error
continue;
}
if (n.isEmpty()) {
addIssue(IssueCodes.getMessageForPKGJ_REWRITE_MODULE_SPECIFIERS__EMPTY_SPECIFIER("Source"), nvp, JSONPackage.eINSTANCE.getNameValuePair_Name(), IssueCodes.PKGJ_REWRITE_MODULE_SPECIFIERS__EMPTY_SPECIFIER);
} else if (!(v instanceof JSONStringLiteral)) {
addIssue(IssueCodes.getMessageForPKGJ_REWRITE_MODULE_SPECIFIERS__INVALID_VALUE(), v, IssueCodes.PKGJ_REWRITE_MODULE_SPECIFIERS__INVALID_VALUE);
} else if (((JSONStringLiteral) v).getValue().isEmpty()) {
addIssue(IssueCodes.getMessageForPKGJ_REWRITE_MODULE_SPECIFIERS__EMPTY_SPECIFIER("Output code"), v, IssueCodes.PKGJ_REWRITE_MODULE_SPECIFIERS__EMPTY_SPECIFIER);
}
}
}
Aggregations