Search in sources :

Example 1 with IJSONValue

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

the class JSONContentOutlineConfiguration method getFilteredNode.

private Object getFilteredNode(Object o) {
    IJSONNode node = null;
    if (o instanceof IJSONNode) {
        node = (IJSONNode) o;
        if (node.getOwnerPairNode() != null) {
            IJSONPair owner = node.getOwnerPairNode();
            IJSONValue value = owner.getValue();
            if (!(value instanceof IJSONArray)) {
                return node.getOwnerPairNode();
            }
        }
    /*
			 * short nodeType = node.getNodeType(); if (node instanceof
			 * IJSONValue) { while (node != null && !(node instanceof
			 * IJSONStyleDeclItem)) { node = node.getParentNode(); } } else if
			 * (nodeType == IJSONNode.STYLEDECLARATION_NODE) { node =
			 * node.getParentNode(); } else if (nodeType ==
			 * IJSONNode.MEDIALIST_NODE) { node = node.getParentNode(); }
			 */
    }
    return node;
}
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) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 2 with IJSONValue

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

the class JSONModelImpl method getIndexedRegion.

@Override
public IndexedRegion getIndexedRegion(int offset) {
    if (this.document == null)
        return null;
    // search in document children
    IJSONNode parent = null;
    int length = this.document.getEndOffset();
    if (offset * 2 < length) {
        // search from the first
        IJSONNode child = (IJSONNode) this.document.getFirstChild();
        while (child != null) {
            if (child.getEndOffset() <= offset) {
                child = (IJSONNode) child.getNextSibling();
                continue;
            }
            if (child.getStartOffset() > offset) {
                break;
            }
            IStructuredDocumentRegion startStructuredDocumentRegion = child.getStartStructuredDocumentRegion();
            if (startStructuredDocumentRegion != null) {
                if (startStructuredDocumentRegion.getEnd() > offset)
                    return child;
            }
            IStructuredDocumentRegion endStructuredDocumentRegion = child.getEndStructuredDocumentRegion();
            if (endStructuredDocumentRegion != null) {
                if (endStructuredDocumentRegion.getStart() <= offset) {
                    if (child instanceof IJSONPair) {
                        IJSONValue value = ((IJSONPair) child).getValue();
                        if (value instanceof IJSONObject || value instanceof IJSONArray) {
                            if (value.getStartOffset() < offset) {
                                child = value;
                                continue;
                            }
                        }
                    }
                    return child;
                }
            }
            // dig more
            parent = child;
            if (parent != null && parent.getNodeType() == IJSONNode.PAIR_NODE) {
                IJSONPair pair = (IJSONPair) parent;
                child = pair.getValue();
            } else {
                child = (IJSONNode) parent.getFirstChild();
            }
        }
    } else {
        // search from the last
        IJSONNode child = (IJSONNode) this.document.getLastChild();
        while (child != null) {
            if (child.getStartOffset() > offset) {
                child = (IJSONNode) child.getPreviousSibling();
                continue;
            }
            if (child.getEndOffset() <= offset) {
                break;
            }
            IStructuredDocumentRegion startStructuredDocumentRegion = child.getStartStructuredDocumentRegion();
            if (startStructuredDocumentRegion != null) {
                if (startStructuredDocumentRegion.getEnd() > offset)
                    return child;
            }
            IStructuredDocumentRegion endStructuredDocumentRegion = child.getEndStructuredDocumentRegion();
            if (endStructuredDocumentRegion != null) {
                if (endStructuredDocumentRegion.getStart() <= offset) {
                    if (child instanceof IJSONPair) {
                        IJSONValue value = ((IJSONPair) child).getValue();
                        if (value instanceof IJSONObject || value instanceof IJSONArray) {
                            if (value.getStartOffset() < offset) {
                                child = value;
                                continue;
                            }
                        }
                    }
                    return child;
                }
            }
            // dig more
            parent = child;
            if (parent != null && parent.getNodeType() == IJSONNode.PAIR_NODE) {
                IJSONPair pair = (IJSONPair) parent;
                child = pair.getValue();
            } else {
                child = (IJSONNode) parent.getLastChild();
            }
        }
    }
    return parent != null ? parent : document.getFirstChild();
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) 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) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 3 with IJSONValue

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

the class JSONModelNotifierImpl method pairReplaced.

/**
 * attrReplaced method
 *
 * @param element
 *            org.w3c.dom.Element
 * @param newAttr
 *            org.w3c.dom.IJSONNode
 * @param oldAttr
 *            org.w3c.dom.IJSONNode
 */
public void pairReplaced(IJSONObject element, IJSONPair newAttr, IJSONPair oldAttr) {
    if (element == null)
        return;
    IJSONNode attr = null;
    IJSONValue oldValue = null;
    IJSONValue newValue = null;
    if (oldAttr != null) {
        attr = oldAttr;
        oldValue = oldAttr.getValue();
    }
    if (newAttr != null) {
        attr = newAttr;
        newValue = newAttr.getValue();
    }
    IJSONNode notifier = (IJSONNode) element;
    int offset = notifier.getStartOffset();
    notify(notifier, INodeNotifier.CHANGE, attr, oldValue, newValue, offset);
    propertyChanged(notifier);
}
Also used : IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 4 with IJSONValue

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

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

Aggregations

IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)15 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)10 IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)8 IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)3 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)3 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)3 JsonArray (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray)2 JsonObject (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)2 JsonValue (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue)2 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 StyledString (org.eclipse.jface.viewers.StyledString)1 Styler (org.eclipse.jface.viewers.StyledString.Styler)1 IJSONPath (org.eclipse.json.jsonpath.IJSONPath)1 IJSONSchemaDocument (org.eclipse.json.schema.IJSONSchemaDocument)1