Search in sources :

Example 26 with IJSONNode

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

the class JSONModelParser method insertStartObjectOLD.

protected void insertStartObjectOLD(IJSONObject element) {
    if (element == null)
        return;
    if (this.context == null)
        return;
    insertNode(element);
    JSONObjectImpl newElement = (JSONObjectImpl) element;
    if (newElement.isEmptyTag() || !newElement.isContainer())
        return;
    // Ignore container tags that have been closed
    String type = newElement.getStartStructuredDocumentRegion().getLastRegion().getType();
    // if (newElement.isContainer()
    // && type == JSONRegionContexts.JSON_EMPTY_TAG_CLOSE)
    // return;
    // 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 27 with IJSONNode

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

the class JSONModelParser method changeStartObject.

/**
 */
private void changeStartObject(IStructuredDocumentRegion flatNode, ITextRegionList newRegions, ITextRegionList oldRegions) {
    int offset = flatNode.getStart();
    if (offset < 0)
        // error
        return;
    JSONNodeImpl root = (JSONNodeImpl) this.context.getRootNode();
    if (root == null)
        // error
        return;
    IJSONNode node = root.getNodeAt(offset);
    if (node == null)
        // error
        return;
    if (node.getNodeType() != IJSONNode.OBJECT_NODE) {
        changeStructuredDocumentRegion(flatNode);
        return;
    }
    JSONObjectImpl element = (JSONObjectImpl) node;
    // check if changes are only for attributes and close tag
    boolean tagNameUnchanged = false;
    if (newRegions != null) {
        Iterator e = newRegions.iterator();
        while (e.hasNext()) {
            ITextRegion region = (ITextRegion) e.next();
            String regionType = region.getType();
            // if (regionType == JSONRegionContexts.JSON_TAG_ATTRIBUTE_NAME
            // || regionType == JSONRegionContexts.JSON_TAG_ATTRIBUTE_EQUALS
            // || regionType == JSONRegionContexts.JSON_TAG_ATTRIBUTE_VALUE)
            // continue;
            // if (regionType == JSONRegionContexts.JSON_TAG_CLOSE) {
            // // change from empty tag may have impact on structure
            // if (!element.isEmptyTag())
            // continue;
            // } else if (regionType == JSONRegionContexts.JSON_TAG_NAME
            // || isNestedTagName(regionType)) {
            // String oldTagName = element.getTagName();
            // String newTagName = flatNode.getText(region);
            // if (oldTagName != null && newTagName != null
            // && oldTagName.equals(newTagName)) {
            // // the tag name is unchanged
            // tagNameUnchanged = true;
            // continue;
            // }
            // }
            // other region has changed
            changeStructuredDocumentRegion(flatNode);
            return;
        }
    }
    // if (oldRegions != null) {
    // Iterator e = oldRegions.iterator();
    // while (e.hasNext()) {
    // ITextRegion region = (ITextRegion) e.next();
    // String regionType = region.getType();
    // if (regionType == JSONRegionContexts.JSON_TAG_ATTRIBUTE_NAME
    // || regionType == JSONRegionContexts.JSON_TAG_ATTRIBUTE_EQUALS
    // || regionType == JSONRegionContexts.JSON_TAG_ATTRIBUTE_VALUE)
    // continue;
    // if (regionType == JSONRegionContexts.JSON_TAG_CLOSE) {
    // // change from empty tag may have impact on structure
    // if (!element.isEmptyTag())
    // continue;
    // } else if (regionType == JSONRegionContexts.JSON_TAG_NAME
    // || isNestedTagName(regionType)) {
    // // if new tag name is unchanged, it's OK
    // if (tagNameUnchanged)
    // continue;
    // }
    // 
    // // other region has changed
    // changeStructuredDocumentRegion(flatNode);
    // return;
    // }
    // }
    // update attributes
    ITextRegionList regions = flatNode.getRegions();
    if (regions == null)
        // error
        return;
// NamedNodeMap attributes = element.getAttributes();
// if (attributes == null)
// return; // error
// 
// // first remove attributes
// int regionIndex = 0;
// int attrIndex = 0;
// AttrImpl attr = null;
// while (attrIndex < attributes.getLength()) {
// attr = (AttrImpl) attributes.item(attrIndex);
// if (attr == null) { // error
// attrIndex++;
// continue;
// }
// ITextRegion nameRegion = attr.getNameRegion();
// if (nameRegion == null) { // error
// element.removeAttributeNode(attr);
// continue;
// }
// boolean found = false;
// for (int i = regionIndex; i < regions.size(); i++) {
// ITextRegion region = regions.get(i);
// if (region == nameRegion) {
// regionIndex = i + 1; // next region
// found = true;
// break;
// }
// }
// if (found) {
// attrIndex++;
// } else {
// element.removeAttributeNode(attr);
// }
// }
// 
// // insert or update attributes
// attrIndex = 0; // reset to first
// AttrImpl newAttr = null;
// ITextRegion oldValueRegion = null;
// Iterator e = regions.iterator();
// while (e.hasNext()) {
// ITextRegion region = (ITextRegion) e.next();
// String regionType = region.getType();
// if (regionType == JSONRegionContexts.JSON_TAG_ATTRIBUTE_NAME) {
// if (newAttr != null) {
// // insert deferred new attribute
// element.insertAttributeNode(newAttr, attrIndex++);
// newAttr = null;
// } else if (attr != null && oldValueRegion != null) {
// // notify existing attribute value removal
// attr.notifyValueChanged();
// }
// 
// oldValueRegion = null;
// attr = (AttrImpl) attributes.item(attrIndex);
// if (attr != null && attr.getNameRegion() == region) {
// // existing attribute
// attrIndex++;
// // clear other regions
// oldValueRegion = attr.getValueRegion();
// attr.setEqualRegion(null);
// attr.setValueRegion(null);
// } else {
// String name = flatNode.getText(region);
// attr = (AttrImpl) this.model.getDocument().createAttribute(
// name);
// if (attr != null)
// attr.setNameRegion(region);
// // defer insertion of new attribute
// newAttr = attr;
// }
// } else if (regionType ==
// JSONRegionContexts.JSON_TAG_ATTRIBUTE_EQUALS) {
// if (attr != null) {
// attr.setEqualRegion(region);
// }
// } else if (regionType ==
// JSONRegionContexts.JSON_TAG_ATTRIBUTE_VALUE) {
// if (attr != null) {
// attr.setValueRegion(region);
// if (attr != newAttr && oldValueRegion != region) {
// // notify existing attribute value changed
// attr.notifyValueChanged();
// }
// oldValueRegion = null;
// attr = null;
// }
// }
// }
// 
// if (newAttr != null) {
// // insert deferred new attribute
// element.appendAttributeNode(newAttr);
// } else if (attr != null && oldValueRegion != null) {
// // notify existing attribute value removal
// attr.notifyValueChanged();
// }
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 28 with IJSONNode

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

the class JSONNodeImpl method getEndOffset.

/**
 * getEndOffset method
 *
 * @return int
 */
@Override
public int getEndOffset() {
    IJSONNode node = this;
    while (node != null) {
        if (node.getNodeType() == IJSONNode.OBJECT_NODE) {
            JSONObjectImpl element = (JSONObjectImpl) node;
            IStructuredDocumentRegion endStructuredDocumentRegion = element.getEndStructuredDocumentRegion();
            if (endStructuredDocumentRegion != null)
                return endStructuredDocumentRegion.getEnd();
        }
        IJSONNode last = node.getLastChild();
        if (last != null) {
            // dig into the last
            node = last;
            continue;
        }
        IStructuredDocumentRegion lastStructuredDocumentRegion = ((JSONNodeImpl) node).getStructuredDocumentRegion();
        if (lastStructuredDocumentRegion != null)
            return lastStructuredDocumentRegion.getEnd();
        IJSONNode prev = node.getPreviousSibling();
        if (prev != null) {
            // move to the previous
            node = prev;
            continue;
        }
        IJSONNode parent = node.getParentNode();
        node = null;
        while (parent != null) {
            if (parent.getNodeType() == IJSONNode.OBJECT_NODE) {
                JSONObjectImpl element = (JSONObjectImpl) parent;
                IStructuredDocumentRegion startStructuredDocumentRegion = element.getStartStructuredDocumentRegion();
                if (startStructuredDocumentRegion != null)
                    return startStructuredDocumentRegion.getEnd();
            }
            IJSONNode parentPrev = parent.getPreviousSibling();
            if (parentPrev != null) {
                // move to the previous
                node = parentPrev;
                break;
            }
            parent = parent.getParentNode();
        }
    }
    return 0;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 29 with IJSONNode

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

the class JSONStructureImpl method removeChildNodes.

/**
 * removeChildNodes method
 */
public void removeChildNodes() {
    if (!isChildEditable()) {
        // JSONMessages.NO_MODIFICATION_ALLOWED_ERR);
        throw new JSONException();
    }
    IJSONNode nextChild = null;
    for (IJSONNode child = getFirstChild(); child != null; child = nextChild) {
        nextChild = child.getNextSibling();
        removeChild(child);
    }
}
Also used : JSONException(org.eclipse.wst.json.core.document.JSONException) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 30 with IJSONNode

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

the class JSONModelParser method insertNode.

/**
 * insertNode method
 *
 * @param child
 *            org.w3c.dom.Node
 */
private void insertNode(IJSONNode node) {
    if (node == null || this.context == null) {
        return;
    }
    IJSONNode parent = this.context.getParentNode();
    if (parent == null) {
        return;
    }
    if (parent.getNodeType() == IJSONNode.PAIR_NODE) {
        IJSONPair pair = (IJSONPair) parent;
        ((JSONPairImpl) pair).setValue((IJSONValue) node);
        return;
    }
    if (parent.getLastChild() != null && parent.getLastChild().getNodeType() == IJSONNode.PAIR_NODE) {
        IJSONPair pair = (IJSONPair) parent.getLastChild();
        ((JSONPairImpl) pair).setValue((IJSONValue) node);
        return;
    }
    IJSONNode next = this.context.getNextNode();
    insertNode(parent, node, next);
    next = node.getNextSibling();
    if (next != null) {
        this.context.setCurrentNode(next);
    } else {
        this.context.setParentNode(node.getParentNode());
    }
// if (node != null && this.context != null) {
// IJSONNode aparent = this.context.getParentNode();
// if (parent != null) {
// IJSONNode next = this.context.getNextNode();
// // Reset parents which are closed container elements; should not
// // be parents
// if (parent.getNodeType() == IJSONNode.OBJECT_NODE) {
// String type = ((JSONObjectImpl) parent)
// .getStartStructuredDocumentRegion().getLastRegion()
// .getType();
// if (((JSONObjectImpl) parent).isContainer()
// /* && type == JSONRegionContexts.JSON_EMPTY_TAG_CLOSE */) {
// // next = parent.getNextSibling();
// // parent = parent.getParentNode();
// } else {
// // ModelParserAdapter adapter = getParserAdapter();
// // if (adapter != null) {
// // while (parent.getNodeType() == IJSONNode.OBJECT_NODE
// // && !adapter.canContain((Element) parent,
// // node)
// // && adapter
// // .isEndTagOmissible((Element) parent)) {
// // next = parent.getNextSibling();
// // parent = parent.getParentNode();
// // }
// // }
// }
// }
// insertNode(parent, node, next);
// next = node.getNextSibling();
// if (next != null) {
// this.context.setCurrentNode(next);
// } else {
// this.context.setParentNode(node.getParentNode());
// }
// }
// }
}
Also used : IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) 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