Search in sources :

Example 6 with JSONObject

use of org.eclipse.n4js.json.JSON.JSONObject 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;
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject)

Example 7 with JSONObject

use of org.eclipse.n4js.json.JSON.JSONObject 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));
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) URI(org.eclipse.emf.common.util.URI) SafeURI(org.eclipse.n4js.workspace.locations.SafeURI)

Example 8 with JSONObject

use of org.eclipse.n4js.json.JSON.JSONObject 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;
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject)

Example 9 with JSONObject

use of org.eclipse.n4js.json.JSON.JSONObject 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);
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject)

Example 10 with JSONObject

use of org.eclipse.n4js.json.JSON.JSONObject 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);
        }
    }
}
Also used : JSONValue(org.eclipse.n4js.json.JSON.JSONValue) NameValuePair(org.eclipse.n4js.json.JSON.NameValuePair) JSONObject(org.eclipse.n4js.json.JSON.JSONObject) JSONStringLiteral(org.eclipse.n4js.json.JSON.JSONStringLiteral)

Aggregations

JSONObject (org.eclipse.n4js.json.JSON.JSONObject)18 NameValuePair (org.eclipse.n4js.json.JSON.NameValuePair)16 JSONValue (org.eclipse.n4js.json.JSON.JSONValue)14 JSONStringLiteral (org.eclipse.n4js.json.JSON.JSONStringLiteral)6 JSONArray (org.eclipse.n4js.json.JSON.JSONArray)3 ArrayList (java.util.ArrayList)2 EObject (org.eclipse.emf.ecore.EObject)2 JSONBooleanLiteral (org.eclipse.n4js.json.JSON.JSONBooleanLiteral)2 JSONDocument (org.eclipse.n4js.json.JSON.JSONDocument)2 JSONNumericLiteral (org.eclipse.n4js.json.JSON.JSONNumericLiteral)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 URI (org.eclipse.emf.common.util.URI)1 EPackage (org.eclipse.emf.ecore.EPackage)1 JSONNullLiteral (org.eclipse.n4js.json.JSON.JSONNullLiteral)1