Search in sources :

Example 6 with IJSONValue

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

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

the class HTMLJSONPrinter method getAdditionalProposalInfo.

public static String getAdditionalProposalInfo(IJSONPair pair) {
    StringBuilder builder = new StringBuilder();
    ImageDescriptor descriptor = null;
    IJSONValue value = pair.getValue();
    if (value != null) {
        descriptor = JSONEditorPluginImageHelper.getInstance().getImageDescriptor(value.getNodeType());
    }
    startPage(builder, getTitleKey(value), descriptor);
    startDefinitionList(builder);
    StringBuilder build = new StringBuilder();
    build.append(pair.getName());
    try {
        IJSONSchemaDocument schemaDocument = JSONCorePlugin.getDefault().getSchemaDocument(pair);
        if (schemaDocument != null) {
            IJSONPath path = pair.getPath();
            IJSONSchemaProperty property = schemaDocument.getProperty(path);
            if (property != null) {
                String description = property.getDescription();
                if (description != null) {
                    build.append(" - ");
                    build.append(description);
                }
            }
        }
    } catch (IOException e) {
        Logger.logException(e);
    }
    addDefinitionListItem(builder, "Key", build.toString());
    addDefinitionListItem(builder, "Type", getValueType(pair.getValue()));
    endDefinitionList(builder);
    endPage(builder);
    return builder.toString();
}
Also used : IJSONSchemaProperty(org.eclipse.json.schema.IJSONSchemaProperty) IJSONPath(org.eclipse.json.jsonpath.IJSONPath) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONSchemaDocument(org.eclipse.json.schema.IJSONSchemaDocument) IOException(java.io.IOException)

Example 8 with IJSONValue

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

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

Example 10 with IJSONValue

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

the class Validator method validateType.

private void validateType(IJSONNode node, Member member, JSONValidationInfo valinfo) {
    if (IJSONSchemaNode.TYPE.equals(member.getName())) {
        Set<String> types = new HashSet<String>();
        if (member.getValue().isString()) {
            types.add(member.getValue().asString());
        } else if (member.getValue().isArray()) {
            JsonArray array = (JsonArray) member.getValue();
            for (JsonValue item : array) {
                types.add(item.asString());
            }
        }
        boolean valid = false;
        for (String type : types) {
            if (node.getNodeType() == IJSONNode.OBJECT_NODE && JSONSchemaType.Object.getName().equals(type)) {
                valid = true;
                break;
            }
            if (node.getNodeType() == IJSONNode.PAIR_NODE) {
                IJSONValue value = ((IJSONPair) node).getValue();
                if (value == null && JSONSchemaType.Null.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value == null) {
                    valid = false;
                    break;
                }
                if (value.getNodeType() == IJSONNode.OBJECT_NODE && JSONSchemaType.Object.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.VALUE_STRING_NODE && JSONSchemaType.String.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.ARRAY_NODE && JSONSchemaType.Array.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.VALUE_BOOLEAN_NODE && JSONSchemaType.Boolean.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.VALUE_NULL_NODE && JSONSchemaType.Null.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.VALUE_NUMBER_NODE && JSONSchemaType.Number.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.VALUE_NUMBER_NODE && JSONSchemaType.Integer.getName().equals(type)) {
                    valid = true;
                    break;
                }
            }
        }
        if (!valid) {
            int offset = node.getStartOffset();
            int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
            StringBuffer buffer = new StringBuffer();
            Iterator<String> iter = types.iterator();
            buffer.append(OPEN_BRACKET);
            while (iter.hasNext()) {
                buffer.append(iter.next());
                if (iter.hasNext()) {
                    buffer.append(COMMA);
                }
            }
            buffer.append(CLOSE_BRACKET);
            valinfo.addMessage("Incorrect type. Expected " + buffer.toString(), 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) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) HashSet(java.util.HashSet)

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