Search in sources :

Example 6 with IJSONPair

use of org.eclipse.wst.json.core.document.IJSONPair 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 7 with IJSONPair

use of org.eclipse.wst.json.core.document.IJSONPair in project webtools.sourceediting by eclipse.

the class SchemaProcessorRegistryReader method getSchemaDocument.

public IJSONSchemaDocument getSchemaDocument(IJSONModel model) throws IOException {
    IJSONSchemaProcessor processor = getDefaultProcessor();
    if (processor == null) {
        return null;
    }
    IJSONDocument document = model.getDocument();
    IJSONNode jsonObject = document.getFirstChild();
    if (jsonObject != null) {
        IJSONNode child = jsonObject.getFirstChild();
        while (child != null) {
            if (child instanceof IJSONPair) {
                IJSONPair pair = (IJSONPair) child;
                String name = pair.getName();
                IJSONValue valueNode = pair.getValue();
                if (valueNode != null && "$schema".equals(name)) {
                    // $NON-NLS-1$
                    String schema = JSONUtil.getString(valueNode);
                    try {
                        if (schema != null) {
                            schema = URIHelper.addImpliedFileProtocol(schema);
                            new URL(schema);
                            return processor.getSchema(schema);
                        }
                    } catch (MalformedURLException e) {
                    }
                }
            }
            child = child.getNextSibling();
        }
    }
    String base = model == null || model.getResolver() == null ? null : model.getResolver().getFileBaseLocation();
    // Assert.isNotNull(base, "Base location is expected to be non null."); //$NON-NLS-1$
    if (base != null) {
        base = URIHelper.addImpliedFileProtocol(base);
    }
    String schemaURL = resolve(base, null, null);
    if (schemaURL != null) {
        return processor.getSchema(schemaURL);
    }
    return null;
}
Also used : MalformedURLException(java.net.MalformedURLException) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONDocument(org.eclipse.wst.json.core.document.IJSONDocument) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode) IJSONSchemaProcessor(org.eclipse.json.schema.IJSONSchemaProcessor) URL(java.net.URL)

Example 8 with IJSONPair

use of org.eclipse.wst.json.core.document.IJSONPair in project webtools.sourceediting by eclipse.

the class TestUtil method serialize.

private static void serialize(IJSONNode node, StringBuilder json) {
    if (node == null) {
        return;
    }
    // .getNextSibling()) {
    switch(node.getNodeType()) {
        case IJSONNode.DOCUMENT_NODE:
            serialize(node.getFirstChild(), json);
            break;
        case IJSONNode.OBJECT_NODE:
        case IJSONNode.ARRAY_NODE:
            IJSONStructure object = (IJSONStructure) node;
            json.append(object.getFirstStructuredDocumentRegion().getText());
            serialize(node.getFirstChild(), json);
            if (object.isClosed()) {
                json.append(object.getEndStructuredDocumentRegion().getText());
            }
            if (node.getNextSibling() != null) {
                json.append(",");
                serialize(node.getNextSibling(), json);
            }
            break;
        case IJSONNode.PAIR_NODE:
            IJSONPair pair = (IJSONPair) node;
            json.append("\"");
            json.append(pair.getName());
            json.append("\"");
            if (pair.getValue() != null) {
                json.append(":");
                serialize(pair.getValue(), json);
            }
            if (node.getNextSibling() != null) {
                json.append(",");
                serialize(node.getNextSibling(), json);
            }
            break;
        case IJSONNode.VALUE_BOOLEAN_NODE:
        case IJSONNode.VALUE_NULL_NODE:
        case IJSONNode.VALUE_NUMBER_NODE:
        case IJSONNode.VALUE_STRING_NODE:
            IJSONValue value = (IJSONValue) node;
            json.append(value.getSimpleValue());
            if (node.getNextSibling() != null) {
                json.append(",");
                serialize(node.getNextSibling(), json);
            }
            break;
    }
// }
}
Also used : IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONStructure(org.eclipse.wst.json.core.document.IJSONStructure)

Example 9 with IJSONPair

use of org.eclipse.wst.json.core.document.IJSONPair in project webtools.sourceediting by eclipse.

the class JSONModelParser method insertNode.

/**
 * insertNode method
 *
 * @param child
 *            org.w3c.dom.Node
 */
private void insertNode(IJSONNode node) {
    if (node == null || this.context == null) {
        return;
    }
    IJSONNode parent = this.context.getParentNode();
    if (parent == null) {
        return;
    }
    if (parent.getNodeType() == IJSONNode.PAIR_NODE) {
        IJSONPair pair = (IJSONPair) parent;
        ((JSONPairImpl) pair).setValue((IJSONValue) node);
        return;
    }
    if (parent.getLastChild() != null && parent.getLastChild().getNodeType() == IJSONNode.PAIR_NODE) {
        IJSONPair pair = (IJSONPair) parent.getLastChild();
        ((JSONPairImpl) pair).setValue((IJSONValue) node);
        return;
    }
    IJSONNode next = this.context.getNextNode();
    insertNode(parent, node, next);
    next = node.getNextSibling();
    if (next != null) {
        this.context.setCurrentNode(next);
    } else {
        this.context.setParentNode(node.getParentNode());
    }
// if (node != null && this.context != null) {
// IJSONNode aparent = this.context.getParentNode();
// if (parent != null) {
// IJSONNode next = this.context.getNextNode();
// // Reset parents which are closed container elements; should not
// // be parents
// if (parent.getNodeType() == IJSONNode.OBJECT_NODE) {
// String type = ((JSONObjectImpl) parent)
// .getStartStructuredDocumentRegion().getLastRegion()
// .getType();
// if (((JSONObjectImpl) parent).isContainer()
// /* && type == JSONRegionContexts.JSON_EMPTY_TAG_CLOSE */) {
// // next = parent.getNextSibling();
// // parent = parent.getParentNode();
// } else {
// // ModelParserAdapter adapter = getParserAdapter();
// // if (adapter != null) {
// // while (parent.getNodeType() == IJSONNode.OBJECT_NODE
// // && !adapter.canContain((Element) parent,
// // node)
// // && adapter
// // .isEndTagOmissible((Element) parent)) {
// // next = parent.getNextSibling();
// // parent = parent.getParentNode();
// // }
// // }
// }
// }
// insertNode(parent, node, next);
// next = node.getNextSibling();
// if (next != null) {
// this.context.setCurrentNode(next);
// } else {
// this.context.setParentNode(node.getParentNode());
// }
// }
// }
}
Also used : IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 10 with IJSONPair

use of org.eclipse.wst.json.core.document.IJSONPair in project webtools.sourceediting by eclipse.

the class JSONPairFormatter method formatChildren.

@Override
protected void formatChildren(IJSONNode node, StringBuilder source) {
    if (node instanceof IJSONPair) {
        IJSONPair pair = (IJSONPair) node;
        IJSONValue value = pair.getValue();
        if (value instanceof IJSONObject || value instanceof IJSONArray) {
            formatObject(node, source, value);
        } else {
            formatValue(node, source, value);
        }
    }
}
Also used : IJSONArray(org.eclipse.wst.json.core.document.IJSONArray) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject)

Aggregations

IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)16 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)10 IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)9 HashSet (java.util.HashSet)4 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)4 JsonArray (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray)3 JsonValue (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue)3 IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)3 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)3 JsonObject (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)2 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)2 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 DocumentRewriteSession (org.eclipse.jface.text.DocumentRewriteSession)1