Search in sources :

Example 6 with IJSONObject

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

the class JSONModelParser method insertValue.

private void insertValue(IStructuredDocumentRegion flatNode, String regionType) {
    ITextRegionList regions = flatNode.getRegions();
    if (regions == null)
        return;
    ITextRegion nameRegion = StructuredDocumentRegionUtil.getFirstRegion(flatNode);
    // Create JSON Value
    JSONValueImpl value = (JSONValueImpl) createJSONValue(regionType);
    value.setStructuredDocumentRegion(flatNode);
    // inserted as the value of an existing pair node
    if (this.context.getCurrentNode() != null && this.context.getCurrentNode() instanceof JSONPairImpl) {
        JSONPairImpl pair = (JSONPairImpl) this.context.getCurrentNode();
        pair.setValue(value);
        JSONNodeImpl parent = (JSONNodeImpl) this.context.getParentNode();
        parent.insertBefore(pair, this.context.getNextNode());
        // parent node
        if (parent instanceof IJSONObject) {
            JSONObjectImpl parentObject = (JSONObjectImpl) parent;
            parentObject.add(pair);
        }
    } else {
        // There is no context, it means the model is reloaded or loaded for
        // the first time
        JSONStructureImpl structure = (JSONStructureImpl) this.context.findParentStructure();
        if (structure != null) {
            if (structure.getNodeType() == IJSONNode.OBJECT_NODE) {
                // children
                if (structure.getLastChild() != null && structure.getLastChild().getNodeType() == IJSONNode.PAIR_NODE) {
                    ((JSONPairImpl) structure.getLastChild()).setValue(value);
                }
                if (structure.getParentOrPairNode() instanceof JSONPairImpl) {
                    JSONPairImpl parentPair = (JSONPairImpl) structure.getParentOrPairNode();
                    if (structure.getParentNode() != parentPair.getValue()) {
                        parentPair.setValue(structure);
                    }
                }
            } else if (structure.getNodeType() == IJSONNode.ARRAY_NODE) {
                // If the parent is a JSONArray insert the new JSONValue at
                // the end of the structure
                JSONArrayImpl array = (JSONArrayImpl) structure;
                structure.insertBefore(value, null);
                array.add(value);
                JSONPairImpl ownerPair = (JSONPairImpl) array.getParentOrPairNode();
                if (ownerPair.getValue() == null)
                    ownerPair.setValue(array);
                else
                    ownerPair.updateValue(array);
            } else {
                insertNode(structure, value, null);
            }
        }
    }
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject)

Example 7 with IJSONObject

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

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

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

the class JSONArrayFormatter method formatChildren.

@Override
protected void formatChildren(IJSONNode node, StringBuilder source) {
    if (node instanceof IJSONArray) {
        IJSONArray array = (IJSONArray) node;
        IJSONNode child = array.getFirstChild();
        while (child != null) {
            if (child instanceof IJSONObject || child instanceof IJSONArray) {
                formatObject(node, source, child);
                if (child.getNextSibling() != null) {
                    int start = child.getEndOffset();
                    int end = child.getNextSibling().getStartOffset();
                    if (end > start) {
                        IJSONCleanupStrategy stgy = getCleanupStrategy(node);
                        IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
                        CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
                        for (int i = 0; i < regions.length; i++) {
                            source.append(decoratedRegion(regions[i], 0, stgy));
                        }
                    }
                }
                String delim = getLineDelimiter(node);
                source.append(delim);
                source.append(getIndent(node));
            } else {
                formatValue(node, source, child);
            }
            child = child.getNextSibling();
            if (child != null) {
                source.append(getIndentString());
            }
        }
    }
}
Also used : IJSONArray(org.eclipse.wst.json.core.document.IJSONArray) IJSONCleanupStrategy(org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 10 with IJSONObject

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

the class JSONModelParser method insertObject.

private void insertObject(IStructuredDocumentRegion flatNode) {
    ITextRegionList regions = flatNode.getRegions();
    if (regions == null)
        return;
    JSONObjectImpl element = null;
    if (this.context.getCurrentNode() != null && this.context.getCurrentNode() instanceof IJSONObject) {
        element = (JSONObjectImpl) this.context.getCurrentNode();
    } else {
        element = (JSONObjectImpl) this.model.getDocument().createJSONObject();
    }
    if (element == null) {
        return;
    }
    element.setStartStructuredDocumentRegion(flatNode);
    insertStartObjectOLD(element);
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject)

Aggregations

IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)12 IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)8 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)4 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)4 IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)3 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)3 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)2 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 IJSONPath (org.eclipse.json.jsonpath.IJSONPath)1 JSONPath (org.eclipse.json.jsonpath.JSONPath)1 JsonObject (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)1 IJSONSchemaDocument (org.eclipse.json.schema.IJSONSchemaDocument)1 Image (org.eclipse.swt.graphics.Image)1 IJSONCleanupStrategy (org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy)1