Search in sources :

Example 1 with JsonArray

use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.

the class JSONSchemaNode method add.

private void add(JsonObject jsonObject, IJSONSchemaNode schemaNode, String pref) {
    JsonValue values = jsonObject.get(pref);
    if (values instanceof JsonArray) {
        JsonArray array = (JsonArray) values;
        Iterator<JsonValue> iter = array.iterator();
        while (iter.hasNext()) {
            JsonValue value = iter.next();
            if (value != null) {
                walk(value.asObject(), schemaNode);
            }
        }
    }
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue)

Example 2 with JsonArray

use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.

the class Validator method validate.

private void validate(IJSONNode node, JsonObject schema, Member member, JSONValidationInfo valinfo) {
    if (IJSONSchemaNode.ALL_OF.equals(member.getName()) && member.getValue() instanceof JsonArray) {
        JsonArray jsonArray = (JsonArray) member.getValue();
        Iterator<JsonValue> iter = jsonArray.iterator();
        while (iter.hasNext()) {
            JsonValue value = iter.next();
            if (value instanceof JsonObject) {
                validate(node, (JsonObject) value, valinfo);
            }
        }
    }
    if (IJSONSchemaNode.ANY_OF.equals(member.getName()) && member.getValue() instanceof JsonArray) {
        JsonArray jsonArray = (JsonArray) member.getValue();
        Iterator<JsonValue> iter = jsonArray.iterator();
        while (iter.hasNext()) {
            JsonValue value = iter.next();
            if (value instanceof JsonObject) {
                JSONValidationInfo info = new JSONValidationInfo("");
                validate(node, (JsonObject) value, info);
                if (info.getValidationMessages() == null || info.getValidationMessages().length == 0) {
                    return;
                }
            }
        }
        int offset = node.getStartOffset();
        int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
        valinfo.addMessage("Matches a schema that is not allowed", line, 0, offset == 0 ? 1 : offset);
    }
    if (IJSONSchemaNode.ONE_OF.equals(member.getName()) && member.getValue() instanceof JsonArray) {
        JsonArray jsonArray = (JsonArray) member.getValue();
        Iterator<JsonValue> iter = jsonArray.iterator();
        int count = 0;
        while (iter.hasNext()) {
            JsonValue value = iter.next();
            if (value instanceof JsonObject) {
                JSONValidationInfo info = new JSONValidationInfo("");
                validate(node, (JsonObject) value, info);
                if (info.getValidationMessages() == null || info.getValidationMessages().length == 0) {
                    count = count + 1;
                }
            }
        }
        if (count != 1) {
            int offset = node.getStartOffset();
            int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
            valinfo.addMessage("Matches a schema that is not allowed", line, 0, offset == 0 ? 1 : offset);
        }
    }
    if (IJSONSchemaNode.NOT.equals(member.getName()) && member.getValue() instanceof JsonObject) {
        JsonObject json = (JsonObject) member.getValue();
        JSONValidationInfo info = new JSONValidationInfo("");
        validate(node, json, info);
        if (info.getValidationMessages() == null || info.getValidationMessages().length == 0) {
            int offset = node.getStartOffset();
            int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
            valinfo.addMessage("Matches a schema that is not allowed", line, 0, offset == 0 ? 1 : offset);
        }
    }
    if (IJSONSchemaNode.TYPE.equals(member.getName())) {
        validateType(node, member, valinfo);
    }
    if (IJSONSchemaNode.ENUM.equals(member.getName())) {
        validateEnum(node, schema, valinfo);
    }
    if (node.getNodeType() == IJSONNode.OBJECT_NODE) {
        if (IJSONSchemaNode.REQUIRED.equals(member.getName())) {
            validateRequired(node, schema, valinfo);
        }
        if (IJSONSchemaNode.MAX_PROPERTIES.equals(member.getName())) {
            validateMaxProperties(node, schema, valinfo);
        }
        if (IJSONSchemaNode.MIN_PROPERTIES.equals(member.getName())) {
            validateMinProperties(node, schema, valinfo);
        }
        if (IJSONSchemaNode.ADDITIONAL_PROPERTIES.equals(member.getName())) {
            validateAdditionalProperties(node, schema, member.getValue(), valinfo);
        }
    }
    if (node.getNodeType() == IJSONNode.PAIR_NODE) {
        IJSONValue value = ((IJSONPair) node).getValue();
        JSONSchemaType[] types = JSONSchemaNode.getType(schema.get(IJSONSchemaNode.TYPE));
        if (value != null) {
            if (value.getNodeType() == IJSONNode.VALUE_STRING_NODE && isType(types, JSONSchemaType.String)) {
                validateString(node, schema, member, valinfo, value);
            }
            if (value.getNodeType() == IJSONNode.VALUE_NUMBER_NODE && (isType(types, JSONSchemaType.Integer) || isType(types, JSONSchemaType.Number))) {
                validateNumber(node, schema, member, valinfo, value);
            }
            if (value.getNodeType() == IJSONNode.ARRAY_NODE && isType(types, JSONSchemaType.Array)) {
                validateArray(node, schema, member, valinfo, value);
            }
        }
    }
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) JsonObject(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) JSONValidationInfo(org.eclipse.wst.json.core.internal.validation.JSONValidationInfo) JSONSchemaType(org.eclipse.json.schema.JSONSchemaType)

Example 3 with JsonArray

use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.

the class CatalogSchemastoreReader method readSchemastore.

protected void readSchemastore() {
    File f = getUrl();
    if (f != null) {
        int type = ICatalogEntry.ENTRY_TYPE_SCHEMA;
        JsonValue schemas;
        try {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(f));
            JsonObject json = JsonObject.readFrom(reader);
            schemas = json.get(SCHEMAS);
        } catch (IOException e) {
            Logger.logException(e);
            return;
        }
        if (schemas != null && schemas instanceof JsonArray) {
            JsonArray elements = (JsonArray) schemas;
            Iterator<JsonValue> iter = elements.iterator();
            while (iter.hasNext()) {
                JsonValue value = iter.next();
                if (value instanceof JsonObject) {
                    JsonObject jsonObject = (JsonObject) value;
                    // $NON-NLS-1$
                    JsonValue urlJson = jsonObject.get("url");
                    // $NON-NLS-1$
                    JsonValue fileMatchJson = jsonObject.get("fileMatch");
                    if (urlJson != null && fileMatchJson != null && urlJson.isString() && fileMatchJson.isArray()) {
                        String url = urlJson.asString();
                        JsonArray fileMatchArray = fileMatchJson.asArray();
                        Iterator<JsonValue> fileIter = fileMatchArray.iterator();
                        while (fileIter.hasNext()) {
                            JsonValue fileMatchValue = fileIter.next();
                            if (fileMatchValue.isString()) {
                                String fileMatch = fileMatchValue.asString();
                                ICatalogElement catalogElement = catalog.createCatalogElement(type);
                                if (catalogElement instanceof ICatalogEntry) {
                                    ICatalogEntry entry = (ICatalogEntry) catalogElement;
                                    entry.setKey(fileMatch);
                                    entry.setURI(url);
                                    entry.setId(fileMatch);
                                }
                                catalog.addCatalogElement(catalogElement);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) InputStreamReader(java.io.InputStreamReader) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) JsonObject(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject) ICatalogElement(org.eclipse.wst.json.core.schema.catalog.ICatalogElement) IOException(java.io.IOException) ICatalogEntry(org.eclipse.wst.json.core.schema.catalog.ICatalogEntry) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 4 with JsonArray

use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.

the class Validator method validateEnum.

private void validateEnum(IJSONNode node, JsonObject schema, JSONValidationInfo valinfo) {
    JsonValue value = schema.get(IJSONSchemaNode.ENUM);
    if (value instanceof JsonArray) {
        JsonArray array = value.asArray();
        Iterator<JsonValue> iter = array.iterator();
        Set<String> values = new HashSet<String>();
        while (iter.hasNext()) {
            String v = iter.next().toString();
            values.add(JSONUtil.removeQuote(v));
        }
        if (node instanceof IJSONPair) {
            IJSONPair pair = (IJSONPair) node;
            String v = JSONUtil.getString(pair.getValue());
            if (!values.contains(v)) {
                int offset = node.getStartOffset();
                int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
                valinfo.addMessage("Value is not an accepted value. Valid values " + values + "'", line, 0, offset == 0 ? 1 : offset);
            }
        }
    }
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) HashSet(java.util.HashSet)

Example 5 with JsonArray

use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray in project webtools.sourceediting by eclipse.

the class Validator method validateRequired.

private void validateRequired(IJSONNode node, JsonObject schema, JSONValidationInfo valinfo) {
    JsonValue required = schema.get(IJSONSchemaNode.REQUIRED);
    if (required instanceof JsonArray) {
        JsonArray array = required.asArray();
        Iterator<JsonValue> iter = array.iterator();
        Set<String> values = new HashSet<String>();
        while (iter.hasNext()) {
            JsonValue v = iter.next();
            if (v.isString()) {
                values.add(v.asString());
            }
        }
        Set<String> properties = getProperties(node);
        for (String property : properties) {
            if (property != null && values.contains(property)) {
                values.remove(property);
            }
        }
        for (String value : values) {
            int offset = node.getStartOffset();
            int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
            valinfo.addMessage("Missing property '" + value + "'", line, 0, offset == 0 ? 1 : offset);
        }
    }
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) HashSet(java.util.HashSet)

Aggregations

JsonArray (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray)10 JsonValue (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue)10 JsonObject (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)5 HashSet (java.util.HashSet)3 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)3 JSONSchemaType (org.eclipse.json.schema.JSONSchemaType)2 Image (org.eclipse.swt.graphics.Image)2 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)2 JSONKeyCompletionProposal (org.eclipse.wst.json.ui.contentassist.JSONKeyCompletionProposal)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 JSONValidationInfo (org.eclipse.wst.json.core.internal.validation.JSONValidationInfo)1 ICatalogElement (org.eclipse.wst.json.core.schema.catalog.ICatalogElement)1 ICatalogEntry (org.eclipse.wst.json.core.schema.catalog.ICatalogEntry)1