Search in sources :

Example 36 with IJSONNode

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

the class JSONModelParser method insertStartArray.

// ---------------------------- JSON Array
protected void insertStartArray(IJSONArray element) {
    if (element == null)
        return;
    if (this.context == null)
        return;
    insertNode(element);
    JSONArrayImpl newElement = (JSONArrayImpl) element;
    String type = newElement.getStartStructuredDocumentRegion().getLastRegion().getType();
    // demote siblings
    IJSONNode parent = this.context.getParentNode();
    if (parent == null)
        // error
        return;
    IJSONNode next = this.context.getNextNode();
    demoteNodes(element, element, parent, next);
    // update context
    IJSONNode firstChild = element.getFirstChild();
    if (firstChild != null)
        this.context.setCurrentNode(firstChild);
    else
        this.context.setParentNode(element);
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 37 with IJSONNode

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

the class JSONModelParser method removeNode.

/**
 * removeNode method
 *
 * @param node
 *            org.w3c.dom.Node
 */
private void removeNode(IJSONNode node) {
    if (node == null)
        return;
    if (this.context == null)
        return;
    IJSONNode parent = node.getParentNode();
    if (parent == null) {
        if (node instanceof IJSONValue) {
            parent = node.getParentOrPairNode();
            if (parent == null) {
                return;
            }
        }
    }
    IJSONNode next = node.getNextSibling();
    IJSONNode prev = node.getPreviousSibling();
    // update context
    IJSONNode oldParent = this.context.getParentNode();
    if (node == oldParent) {
        if (next != null)
            this.context.setCurrentNode(next);
        else
            this.context.setParentNode(parent);
    } else {
        IJSONNode oldNext = this.context.getNextNode();
        if (node == oldNext) {
            this.context.setCurrentNode(next);
        }
    }
    parent.removeChild(node);
    // demote sibling
    if (prev != null && prev.getNodeType() == IJSONNode.OBJECT_NODE) {
        JSONObjectImpl newElement = (JSONObjectImpl) prev;
        if (!newElement.hasEndTag() && !newElement.isEmptyTag() && newElement.isContainer()) {
            // find new parent
            for (IJSONNode last = newElement.getLastChild(); last != null; last = last.getLastChild()) {
                if (last.getNodeType() != IJSONNode.OBJECT_NODE)
                    break;
                JSONObjectImpl lastElement = (JSONObjectImpl) last;
                if (lastElement.hasEndTag() || lastElement.isEmptyTag() || !lastElement.isContainer())
                    break;
                newElement = lastElement;
            }
            IJSONNode lastChild = newElement.getLastChild();
            demoteNodes(prev, newElement, parent, next);
            // update context
            IJSONNode newNext = null;
            if (lastChild != null)
                newNext = lastChild.getNextSibling();
            else
                newNext = newElement.getFirstChild();
            if (newNext != null)
                this.context.setCurrentNode(newNext);
            else
                this.context.setParentNode(newElement);
        }
    }
}
Also used : IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 38 with IJSONNode

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

the class JSONNodeImpl method getNodeAt.

/**
 * getNodeAt method
 *
 * @return org.w3c.dom.Node
 * @param offset
 *            int
 */
IJSONNode getNodeAt(int offset) {
    IJSONNode parent = this;
    IJSONNode child = getFirstChild();
    while (child != null) {
        if (child.getEndOffset() == offset) {
            return child;
        }
        if (child.getEndOffset() <= offset) {
            child = child.getNextSibling();
            continue;
        }
        if (child.getStartOffset() > offset) {
            break;
        }
        IStructuredDocumentRegion startStructuredDocumentRegion = child.getStartStructuredDocumentRegion();
        if (startStructuredDocumentRegion != null) {
            if (startStructuredDocumentRegion.getEnd() > offset)
                return child;
        }
        // dig more
        parent = child;
        // JSONNode like JSONObject or JSONArray
        if (parent instanceof JSONPairImpl) {
            IJSONValue value = ((JSONPairImpl) parent).getValue();
            if (value instanceof JSONObjectImpl || value instanceof JSONArrayImpl) {
                parent = value;
            }
        }
        child = parent.getFirstChild();
    }
    return parent;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 39 with IJSONNode

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

the class JSONNodeImpl method getIndex.

/**
 */
public int getIndex() {
    IJSONNode parent = getParentNode();
    if (parent == null)
        // error
        return -1;
    int index = 0;
    for (IJSONNode child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child == this)
            return index;
        index++;
    }
    // error
    return -1;
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 40 with IJSONNode

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

the class JSONNodeImpl method getStartOffset.

/**
 * getStartOffset method
 *
 * @return int
 */
@Override
public int getStartOffset() {
    if (this.flatNode != null)
        return this.flatNode.getStart();
    JSONNodeImpl prev = (JSONNodeImpl) getPreviousSibling();
    if (prev != null)
        return prev.getEndOffset();
    IJSONNode parent = getParentNode();
    if (parent != null && parent.getNodeType() == IJSONNode.OBJECT_NODE) {
        JSONObjectImpl element = (JSONObjectImpl) parent;
        if (element.hasStartTag())
            return element.getStartEndOffset();
        return element.getStartOffset();
    }
    // final fallback to look into first child
    JSONNodeImpl child = (JSONNodeImpl) getFirstChild();
    while (child != null) {
        IStructuredDocumentRegion childStructuredDocumentRegion = child.getStructuredDocumentRegion();
        if (childStructuredDocumentRegion != null)
            return childStructuredDocumentRegion.getStart();
        child = (JSONNodeImpl) child.getFirstChild();
    }
    return 0;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

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