Search in sources :

Example 1 with IJSONArray

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

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

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

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

the class Validator method validateItems.

private void validateItems(IJSONNode node, JsonObject schema, IJSONValue value, JsonValue memberValue, JSONValidationInfo valinfo) {
    JsonValue additionalItems = schema.get(IJSONSchemaNode.ADDITIONAL_ITEMS);
    if (additionalItems != null && additionalItems.isBoolean() && !additionalItems.asBoolean()) {
        if (memberValue != null && memberValue.isArray()) {
            if (value instanceof IJSONArray) {
                int instanceSize = getSize((IJSONArray) value);
                int size = memberValue.asArray().size();
                if (instanceSize > size) {
                    int offset = node.getStartOffset();
                    int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
                    valinfo.addMessage("Array has too many items. Expected " + size + " or fewer", line, 0, offset == 0 ? 1 : offset);
                }
            }
        }
    }
}
Also used : IJSONArray(org.eclipse.wst.json.core.document.IJSONArray) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue)

Example 5 with IJSONArray

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

the class Validator method validateUniqueItems.

private void validateUniqueItems(IJSONNode node, JsonObject schema, IJSONValue value, JsonValue memberValue, JSONValidationInfo valinfo) {
    if (memberValue != null && memberValue.isBoolean() && memberValue.asBoolean()) {
        if (value instanceof IJSONArray) {
            Set<String> instanceValues = new HashSet<String>();
            IJSONNode child = value.getFirstChild();
            int instanceSize = 0;
            while (child != null) {
                instanceSize = instanceSize + 1;
                instanceValues.add(JSONUtil.getString(child));
                child = child.getNextSibling();
            }
            ;
            if (instanceSize != instanceValues.size()) {
                int offset = node.getStartOffset();
                int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
                valinfo.addMessage("Array has duplicate items", line, 0, offset == 0 ? 1 : offset);
            }
        }
    }
}
Also used : IJSONArray(org.eclipse.wst.json.core.document.IJSONArray) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode) HashSet(java.util.HashSet)

Aggregations

IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)7 IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)4 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)3 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)3 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)3 IJSONCleanupStrategy (org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy)2 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)2 HashSet (java.util.HashSet)1 JsonValue (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue)1 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)1