Search in sources :

Example 21 with IJSONNode

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

the class JSONModelParser method demoteNodes.

/**
 */
private void demoteNodes(IJSONNode root, IJSONNode newParent, IJSONNode oldParent, IJSONNode next) {
    if (newParent.getNodeType() != IJSONNode.OBJECT_NODE)
        return;
    JSONObjectImpl newElement = (JSONObjectImpl) newParent;
    // find next
    while (next == null) {
        if (oldParent.getNodeType() != IJSONNode.OBJECT_NODE)
            return;
        JSONObjectImpl oldElement = (JSONObjectImpl) oldParent;
        if (oldElement.hasEndTag())
            return;
        oldParent = oldElement.getParentNode();
        if (oldParent == null)
            // error
            return;
        next = oldElement.getNextSibling();
    }
    while (next != null) {
        boolean done = false;
        if (next.getNodeType() == IJSONNode.OBJECT_NODE) {
            JSONObjectImpl nextElement = (JSONObjectImpl) next;
            if (!nextElement.hasStartTag()) {
                IJSONNode nextChild = nextElement.getFirstChild();
                if (nextChild != null) {
                    // demote children
                    next = nextChild;
                    oldParent = nextElement;
                    continue;
                }
                // if (nextElement.hasEndTag()) {
                // if (nextElement.matchEndTag(newElement)) {
                // // stop at the matched invalid end tag
                // next = nextElement.getNextSibling();
                // oldParent.removeChild(nextElement);
                // newElement.addEndTag(nextElement);
                // 
                // if (newElement == root)
                // return;
                // IJSONNode p = newElement.getParentNode();
                // // check if reached to top
                // if (p == null || p == oldParent
                // || p.getNodeType() != IJSONNode.OBJECT_NODE)
                // return;
                // newElement = (JSONObjectImpl) p;
                // done = true;
                // }
                // } else {
                // remove implicit element
                next = nextElement.getNextSibling();
                oldParent.removeChild(nextElement);
                done = true;
            // }
            }
        }
        if (!done) {
            // if (!canContain(newElement, next)) {
            // if (newElement == root)
            // return;
            // Node p = newElement.getParentNode();
            // // check if reached to top
            // if (p == null || p == oldParent
            // || p.getNodeType() != IJSONNode.OBJECT_NODE)
            // return;
            // newElement = (JSONObjectImpl) p;
            // continue;
            // }
            IJSONNode child = next;
            next = next.getNextSibling();
            oldParent.removeChild(child);
            insertNode(newElement, child, null);
            IJSONNode childParent = child.getParentNode();
            if (childParent != newElement) {
                newElement = (JSONObjectImpl) childParent;
            }
        }
        // find next parent and sibling
        while (next == null) {
            if (oldParent.getNodeType() != IJSONNode.OBJECT_NODE)
                return;
            JSONObjectImpl oldElement = (JSONObjectImpl) oldParent;
            // dug parent must not have children at this point
            if (!oldElement.hasChildNodes() && !oldElement.hasStartTag()) {
                oldParent = oldElement.getParentNode();
                if (oldParent == null)
                    // error
                    return;
                next = oldElement;
                break;
            }
            if (oldElement.hasEndTag())
                return;
            oldParent = oldElement.getParentNode();
            if (oldParent == null)
                // error
                return;
            next = oldElement.getNextSibling();
        }
    }
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 22 with IJSONNode

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

the class JSONModelParser method removeEndTag.

/**
 * removeEndTag method
 *
 * @param element
 *            org.w3c.dom.Element
 */
private void removeEndTag(IJSONNode element) {
    if (element == null)
        return;
    if (this.context == null)
        return;
    IJSONNode parent = element.getParentNode();
    if (parent == null)
        // error
        return;
    if (!((JSONObjectImpl) element).isContainer()) {
        // just update context
        IJSONNode elementNext = element.getNextSibling();
        if (elementNext != null)
            this.context.setCurrentNode(elementNext);
        else
            this.context.setParentNode(parent);
        return;
    }
    // demote siblings
    IJSONNode next = element.getNextSibling();
    JSONObjectImpl newElement = (JSONObjectImpl) element;
    // 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(element, 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 : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 23 with IJSONNode

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

the class JSONModelParser method promoteNodes.

/**
 */
private void promoteNodes(IJSONNode root, IJSONNode newParent, IJSONNode newNext, IJSONNode oldParent, IJSONNode next) {
    JSONObjectImpl newElement = null;
    if (newParent.getNodeType() == IJSONNode.OBJECT_NODE) {
        newElement = (JSONObjectImpl) newParent;
    }
    IJSONNode rootParent = root.getParentNode();
    while (oldParent != rootParent) {
        while (next != null) {
            boolean done = false;
            boolean endTag = false;
            if (next.getNodeType() == IJSONNode.OBJECT_NODE) {
                JSONObjectImpl nextElement = (JSONObjectImpl) next;
                if (!nextElement.hasStartTag()) {
                    IJSONNode nextChild = nextElement.getFirstChild();
                    if (nextChild != null) {
                        // promote children
                        next = nextChild;
                        oldParent = nextElement;
                        continue;
                    }
                    // if (nextElement.hasEndTag()) {
                    // if (nextElement.matchEndTag(newElement)) {
                    // endTag = true;
                    // }
                    // } else {
                    // remove implicit element
                    next = nextElement.getNextSibling();
                    oldParent.removeChild(nextElement);
                    done = true;
                // }
                }
            }
            if (!done) {
                if (!endTag && newElement != null) /* && !canContain(newElement, next) */
                {
                    newParent = newElement.getParentNode();
                    if (newParent == null)
                        // error
                        return;
                    IJSONNode elementNext = newElement.getNextSibling();
                    // promote siblings
                    promoteNodes(newElement, newParent, elementNext, newElement, newNext);
                    newNext = newElement.getNextSibling();
                    if (newParent.getNodeType() == IJSONNode.OBJECT_NODE) {
                        newElement = (JSONObjectImpl) newParent;
                    } else {
                        newElement = null;
                    }
                    continue;
                }
                IJSONNode child = next;
                next = next.getNextSibling();
                oldParent.removeChild(child);
                insertNode(newParent, child, newNext);
                IJSONNode childParent = child.getParentNode();
                if (childParent != newParent) {
                    newParent = childParent;
                    newElement = (JSONObjectImpl) newParent;
                    newNext = child.getNextSibling();
                }
            }
        }
        if (oldParent.getNodeType() != IJSONNode.OBJECT_NODE)
            return;
        JSONObjectImpl oldElement = (JSONObjectImpl) oldParent;
        oldParent = oldElement.getParentNode();
        if (oldParent == null)
            // error
            return;
        next = oldElement.getNextSibling();
        if (oldElement.hasEndTag()) {
            IJSONObject end = null;
            if (!oldElement.hasChildNodes() && !oldElement.hasStartTag()) {
                oldParent.removeChild(oldElement);
                end = oldElement;
            } else {
            // end = oldElement.removeEndTag();
            }
            if (end != null) {
                insertNode(newParent, end, newNext);
                IJSONNode endParent = end.getParentNode();
                if (endParent != newParent) {
                    newParent = endParent;
                    newElement = (JSONObjectImpl) newParent;
                    newNext = end.getNextSibling();
                }
            }
        }
    }
}
Also used : IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 24 with IJSONNode

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

the class JSONModelParser method changeAttrValue.

private void changeAttrValue(IStructuredDocumentRegion flatNode, ITextRegion region) {
    int offset = flatNode.getStart();
    if (offset < 0)
        return;
    JSONNodeImpl root = (JSONNodeImpl) this.context.getRootNode();
    if (root == null)
        return;
    IJSONNode node = root.getNodeAt(offset);
    if (node == null)
        return;
    JSONValueImpl value = (JSONValueImpl) createJSONValue(region.getType());
    value.setStructuredDocumentRegion(flatNode);
    if (node.getNodeType() == IJSONNode.PAIR_NODE) {
        JSONPairImpl pair = (JSONPairImpl) node;
        pair.updateValue(value);
    } else if (node.getFirstStructuredDocumentRegion() != null && isJSONValue(node.getFirstStructuredDocumentRegion().getType())) {
        JSONValueImpl oldValue = (JSONValueImpl) node;
        oldValue.updateValue(value);
    } else if (node instanceof JSONArrayImpl) {
        if (value.getValueRegionType().equals(JSONRegionContexts.JSON_VALUE_BOOLEAN) || value.getValueRegionType().equals(JSONRegionContexts.JSON_VALUE_NULL)) {
            // If the parent is a JSONArray insert the new JSONValue at
            // the end of the structure
            JSONArrayImpl array = (JSONArrayImpl) node;
            node.insertBefore(value, null);
            array.add(value);
            JSONPairImpl ownerPair = (JSONPairImpl) array.getParentOrPairNode();
            if (ownerPair.getValue() == null)
                ownerPair.setValue(array);
            else
                ownerPair.updateValue(array);
        }
    }
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 25 with IJSONNode

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

the class JSONModelParser method cleanupEndTag.

/**
 * cleanupContext method
 */
private void cleanupEndTag() {
    IJSONNode parent = this.context.getParentNode();
    IJSONNode next = this.context.getNextNode();
    while (parent != null) {
        while (next != null) {
            if (next.getNodeType() == IJSONNode.OBJECT_NODE) {
                JSONObjectImpl element = (JSONObjectImpl) next;
            // if (element.isEndTag()) {
            // // floating end tag
            // String tagName = element.getTagName();
            // String rootName = getFindRootName(tagName);
            // JSONObjectImpl start = (JSONObjectImpl) this.context
            // .findStartTag(tagName, rootName);
            // if (start != null) {
            // insertEndTag(start);
            // // move the end tag from 'element' to 'start'
            // start.addEndTag(element);
            // removeNode(element);
            // parent = this.context.getParentNode();
            // next = this.context.getNextNode();
            // continue;
            // }
            // }
            }
            IJSONNode first = next.getFirstChild();
            if (first != null) {
                parent = next;
                next = first;
                this.context.setCurrentNode(next);
            } else {
                next = next.getNextSibling();
                this.context.setCurrentNode(next);
            }
        }
        // if (parent.getNodeType() == IJSONNode.OBJECT_NODE) {
        // JSONObjectImpl element = (JSONObjectImpl) parent;
        // if (!element.hasEndTag() && element.hasStartTag()
        // && element.getNextSibling() == null) {
        // String tagName = element.getTagName();
        // JSONObjectImpl end = (JSONObjectImpl) this.context
        // .findEndTag(tagName);
        // if (end != null) {
        // // move the end tag from 'end' to 'element'
        // element.addEndTag(end);
        // removeEndTag(end);
        // this.context.setParentNode(parent); // reset context
        // continue;
        // }
        // }
        // }
        next = parent.getNextSibling();
        parent = parent.getParentNode();
        if (next != null) {
            this.context.setCurrentNode(next);
        } else {
            this.context.setParentNode(parent);
        }
    }
}
Also used : 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