Search in sources :

Example 1 with IJSONPair

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

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

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

the class Validator method getProperties.

private Set<String> getProperties(IJSONNode node) {
    Set<String> properties = new HashSet<String>();
    IJSONNode child = node.getFirstChild();
    while (child != null) {
        if (child instanceof IJSONPair) {
            IJSONPair pair = (IJSONPair) child;
            if (pair.getName() != null) {
                properties.add(pair.getName());
            }
        }
        child = child.getNextSibling();
    }
    return properties;
}
Also used : IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode) HashSet(java.util.HashSet)

Example 4 with IJSONPair

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

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