Search in sources :

Example 16 with IJSONNode

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

the class Validator method validate.

private void validate(IJSONModel model, IJSONSchemaProperty schemaProperty, JSONValidationInfo valinfo) {
    IJSONDocument document = model.getDocument();
    IJSONNode node = document.getFirstChild();
    while (node != null) {
        validate(node, schemaProperty, valinfo);
        node = node.getNextSibling();
    }
}
Also used : IJSONDocument(org.eclipse.wst.json.core.document.IJSONDocument) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 17 with IJSONNode

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

the class Validator method getSize.

private int getSize(IJSONArray instance) {
    if (instance == null) {
        return 0;
    }
    int instanceSize = 0;
    IJSONNode child = instance.getFirstChild();
    while (child != null) {
        instanceSize = instanceSize + 1;
        child = child.getNextSibling();
    }
    return instanceSize;
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 18 with IJSONNode

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

the class Validator method validateAdditionalProperties.

private void validateAdditionalProperties(IJSONNode node, JsonObject schema, JsonValue value, JSONValidationInfo valinfo) {
    if (value != null && value.isBoolean() && !value.asBoolean()) {
        Set<String> s = getProperties(node);
        Set<String> p = getProperties(schema.get(IJSONSchemaNode.PROPERTIES));
        Set<String> pp = getProperties(schema.get(IJSONSchemaNode.PATTERN_PROPERTIES));
        for (String string : p) {
            if (s.contains(string)) {
                s.remove(string);
            }
        }
        for (String patternStr : pp) {
            Pattern pattern = Pattern.compile(patternStr);
            Iterator<String> iter = s.iterator();
            while (iter.hasNext()) {
                String ss = iter.next();
                if (ss != null) {
                    Matcher matcher = pattern.matcher(ss);
                    if (matcher.find()) {
                        iter.remove();
                    }
                }
            }
        }
        for (String string : s) {
            if ("$schema".equals(string)) {
                // $NON-NLS-1$
                continue;
            }
            IJSONNode n = node.getFirstChild();
            while (n != null) {
                if (n instanceof IJSONPair) {
                    IJSONPair pair = (IJSONPair) n;
                    if (string.equals(pair.getName())) {
                        break;
                    }
                }
                n = n.getNextSibling();
            }
            if (n == null) {
                node = n;
            }
            int offset = n.getStartOffset();
            int line = n.getModel().getStructuredDocument().getLineOfOffset(offset);
            valinfo.addMessage("Property " + string + " is not allowed", line, 0, offset == 0 ? 1 : offset);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) Matcher(java.util.regex.Matcher) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 19 with IJSONNode

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

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

Aggregations

IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)56 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)10 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)9 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)9 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)8 IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)4 IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)4 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)3 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1