Search in sources :

Example 41 with IJSONNode

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

the class JSONStructureImpl method insertBefore.

/**
 * insertBefore method
 *
 * @return org.w3c.dom.Node
 * @param newChild
 *            org.w3c.dom.Node
 * @param refChild
 *            org.w3c.dom.Node
 */
public IJSONNode insertBefore(IJSONNode newChild, IJSONNode refChild) throws JSONException {
    if (newChild == null)
        // nothing to do
        return null;
    if (refChild != null && refChild.getParentNode() != this) {
    // throw new JSONException(JSONException.NOT_FOUND_ERR,
    // JSONMessages.NOT_FOUND_ERR);
    }
    if (!isChildEditable()) {
    // throw new
    // JSONException(JSONException.NO_MODIFICATION_ALLOWED_ERR,
    // JSONMessages.NO_MODIFICATION_ALLOWED_ERR);
    }
    if (newChild == refChild)
        // nothing to do
        return newChild;
    // new child can not be a parent of this, would cause cycle
    if (isParent(newChild)) {
    // throw new JSONException(JSONException.HIERARCHY_REQUEST_ERR,
    // JSONMessages.HIERARCHY_REQUEST_ERR);
    }
    // synchronized in case another thread is getting item, or length
    synchronized (lockObject) {
        // invalidate child nodes cache
        this.childNodesCache = null;
    }
    JSONNodeImpl child = (JSONNodeImpl) newChild;
    JSONNodeImpl next = (JSONNodeImpl) refChild;
    JSONNodeImpl prev = null;
    IJSONNode oldParent = child.getParentNode();
    if (oldParent != null)
        oldParent.removeChild(child);
    if (next == null) {
        prev = this.lastChild;
        this.lastChild = child;
    } else {
        prev = (JSONNodeImpl) next.getPreviousSibling();
        next.setPreviousSibling(child);
    }
    if (prev == null)
        this.firstChild = child;
    else
        prev.setNextSibling(child);
    child.setPreviousSibling(prev);
    child.setNextSibling(next);
    child.setParentNode(this);
    // make sure having the same owner document
    if (child.getOwnerDocument() == null) {
        if (getNodeType() == DOCUMENT_NODE) {
            child.setOwnerDocument((IJSONDocument) this);
        } else {
            child.setOwnerDocument(getOwnerDocument());
        }
    }
    notifyChildReplaced(child, null);
    return child;
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 42 with IJSONNode

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

the class JSONStructureImpl method cloneChildNodes.

/**
 * cloneChildNodes method
 *
 * @param container
 *            org.w3c.dom.Node
 * @param deep
 *            boolean
 */
protected void cloneChildNodes(IJSONNode newParent, boolean deep) {
    if (newParent == null || newParent == this)
        return;
    if (!(newParent instanceof JSONStructureImpl))
        return;
    JSONStructureImpl container = (JSONStructureImpl) newParent;
    container.removeChildNodes();
    for (IJSONNode child = getFirstChild(); child != null; child = child.getNextSibling()) {
        IJSONNode cloned = child.cloneNode(deep);
        if (cloned != null)
            container.appendChild(cloned);
    }
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 43 with IJSONNode

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

the class JSONArrayFormatter method formatChildren.

@Override
protected void formatChildren(IJSONNode node, IRegion region, StringBuilder source) {
    IJSONNode child = node.getFirstChild();
    int start = region.getOffset();
    int end = region.getOffset() + region.getLength();
    while (child != null) {
        int curEnd = child.getEndOffset();
        StringBuilder childSource = null;
        boolean toFinish = false;
        if (start < curEnd) {
            int curStart = child.getStartOffset();
            if (curStart < end) {
                // append child
                IJSONSourceFormatter formatter = (IJSONSourceFormatter) ((INodeNotifier) child).getAdapterFor(IJSONSourceFormatter.class);
                if (formatter == null) {
                    formatter = JSONSourceFormatterFactory.getInstance().getSourceFormatter(child);
                }
                if (includes(region, curStart, curEnd))
                    childSource = ((AbstractJSONSourceFormatter) formatter).formatProc(child);
                else
                    childSource = ((AbstractJSONSourceFormatter) formatter).formatProc(child, overlappedRegion(region, curStart, curEnd));
            } else
                toFinish = true;
        }
        if (childSource != null) {
            source.append(childSource);
        }
        if (toFinish)
            break;
        child = child.getNextSibling();
    }
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 44 with IJSONNode

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

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

the class JSONFormatUtil method collectJSONNodes.

public List collectJSONNodes(IStructuredModel model, int start, int length) {
    List nodes = new ArrayList();
    IndexedRegion startNode = model.getIndexedRegion(start);
    IndexedRegion endNode = model.getIndexedRegion(start + length - 1);
    if (startNode == null || endNode == null) {
        return nodes;
    }
    if (model instanceof IJSONModel && startNode instanceof IJSONNode && endNode instanceof IJSONNode) {
        // JSON model
        IJSONNode ca = getCommonAncestor((IJSONNode) startNode, (IJSONNode) endNode);
        if (ca != null) {
            for (IJSONNode node = ca.getFirstChild(); node != null && start + length < ((IndexedRegion) node).getStartOffset(); node = node.getNextSibling()) {
                if (start < ((IndexedRegion) node).getEndOffset()) {
                    nodes.add(node);
                }
            }
        }
    }
    return nodes;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) IJSONModel(org.eclipse.wst.json.core.document.IJSONModel) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) 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