Search in sources :

Example 1 with JsonObject

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

the class Validator method validate.

private void validate(IJSONNode node, IJSONSchemaProperty schemaProperty, JSONValidationInfo valinfo) {
    if (node == null || schemaProperty == null) {
        return;
    }
    JsonObject schema = schemaProperty.getJsonObject();
    validate(node, schema, valinfo);
    IJSONNode child = node.getFirstChild();
    while (child != null) {
        IJSONSchemaProperty property = schemaProperty.getSchemaDocument().getProperty(child.getPath());
        validate(child, property, valinfo);
        if (child instanceof IJSONPair) {
            IJSONValue value = ((IJSONPair) child).getValue();
            if (value instanceof IJSONObject) {
                IJSONSchemaProperty prop = schemaProperty.getSchemaDocument().getProperty(value.getPath());
                validate(value, prop, valinfo);
            }
        }
        child = child.getNextSibling();
    }
}
Also used : IJSONSchemaProperty(org.eclipse.json.schema.IJSONSchemaProperty) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) JsonObject(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 2 with JsonObject

use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject 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 JsonObject

use of org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject 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 JsonObject

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

the class JSONSchemaNode method addProperties.

private void addProperties(IJSONSchemaNode schemaNode, JsonObject properties) {
    if (properties == null) {
        return;
    }
    Iterator<Member> members = properties.iterator();
    while (members.hasNext()) {
        Member member = members.next();
        schemaNode.addProperty(new JSONSchemaProperty(member.getName(), (JsonObject) member.getValue(), schemaNode));
    }
}
Also used : JsonObject(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject) IJSONSchemaProperty(org.eclipse.json.schema.IJSONSchemaProperty)

Example 5 with JsonObject

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

the class JSONSchemaNode method addDefinitions.

private void addDefinitions() {
    // $NON-NLS-1$
    JsonValue defs = jsonObject.get("definitions");
    if (defs instanceof JsonObject) {
        Iterator<Member> members = ((JsonObject) defs).iterator();
        while (members.hasNext()) {
            Member member = members.next();
            JsonValue value = member.getValue();
            if (value instanceof JsonObject) {
                definitions.put(member.getName(), member.getValue());
            }
        }
    }
}
Also used : JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) JsonObject(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)

Aggregations

JsonObject (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)9 JsonValue (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue)7 JsonArray (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray)5 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 Image (org.eclipse.swt.graphics.Image)2 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)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 JSONSchemaType (org.eclipse.json.schema.JSONSchemaType)1 IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)1 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)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